Skip to content

Commit

Permalink
Merge pull request awsdocs#166 from walkerk1980/Java_GuardDuty_ListDe…
Browse files Browse the repository at this point in the history
…tectors

Added GuardDuty ListDetectors() example for Java
  • Loading branch information
soo-aws authored Jul 28, 2018
2 parents dd6a3ce + 89a32b6 commit 4f87517
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
8 changes: 8 additions & 0 deletions java/example_code/guardduty/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all: target/aws-guardduty-examples-1.0.jar

target/aws-guardduty-examples-1.0.jar: src/main/java/aws/example/guardduty/*.java
mvn package

clean:
mvn clean

38 changes: 38 additions & 0 deletions java/example_code/guardduty/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aws.example.guardduty</groupId>
<artifactId>aws-guardduty-examples</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>Amazon GuardDuty Examples</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.375</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-guardduty</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

12 changes: 12 additions & 0 deletions java/example_code/guardduty/run_example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
if [[ -z $* ]] ; then
echo 'Supply the name of one of the example classes as an argument.'
echo 'If there are arguments to the class, put them in quotes after the class name.'
exit 1
fi
export CLASSPATH=target/aws-guardduty-examples-1.0.jar
export className=$1
echo "## Running $className..."
shift
echo "## arguments $@..."
mvn exec:java -Dexec.mainClass="aws.example.guardduty.$className" -Dexec.args="$@" -Dexec.cleanupDaemonThreads=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package aws.example.guardduty;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.guardduty.AmazonGuardDuty;
import com.amazonaws.services.guardduty.AmazonGuardDutyClientBuilder;
import com.amazonaws.services.guardduty.model.*;
/**
* List GuardDuty Detectors in the current AWS Region
*/
public class ListDetectors {
public static void main(String[] args) {

AmazonGuardDuty guardduty =
AmazonGuardDutyClientBuilder.defaultClient();

try {
ListDetectorsRequest request = new ListDetectorsRequest();

ListDetectorsResult response = guardduty.listDetectors(request);

for (String detectorId : response.getDetectorIds())
{
System.out.println("DetectorId: " + detectorId );
}
} catch (AmazonServiceException ase) {
System.out.println("Caught Exception: " + ase.getMessage());
System.out.println("Reponse Status Code: " + ase.getStatusCode());
System.out.println("Error Code: " + ase.getErrorCode());
System.out.println("Request ID: " + ase.getRequestId());
}

}
}

0 comments on commit 4f87517

Please sign in to comment.