Skip to content

Commit

Permalink
Client Libraries( Java +Python) for REST API Web Services
Browse files Browse the repository at this point in the history
  • Loading branch information
mahmoodm2 committed Aug 30, 2016
1 parent 142928c commit 83e2056
Show file tree
Hide file tree
Showing 77 changed files with 46,340 additions and 5 deletions.
Binary file not shown.
216 changes: 216 additions & 0 deletions client-libs/Java/demo/RestApiJavaDemo.java
@@ -0,0 +1,216 @@
package org.appSensor;

import org.owasp.appsensor.clientLibs.java.api.*;
import org.owasp.appsensor.clientLibs.java.model.*;
import org.owasp.appsensor.clientLibs.java.handler.*;

import java.math.BigDecimal;
import java.util.*;

import org.joda.time.DateTimeZone;
import org.joda.time.DateTime;

/**
* AppSensor's REST Web Services demo.
* These sample code uses the AppSensor RestApi Java client libraries.
* The "clientLibs.java-1.0-jar-with-dependencies.jar" file should be accessible through the Java build path.
* More information can be found at: client-libs/readme.md
*
* @author Mahmoud Mohammadi (mahmood.mohamadi@gmail.com)
*
*/


public class RestApiJavaDemo {

public static void main(String[] args) {

RestReportingEngineApi apiInstance = new RestReportingEngineApi();

RestRequestHandlerApi apiHandler = new RestRequestHandlerApi();

// The Appsensor's custom request header
apiInstance.getApiClient().addDefaultHeader("X-Appsensor-Client-Application-Name2","myclientapp");

//Setting the address of the server listening to the rest api requests.
apiInstance.getApiClient().setBasePath("http://localhost:8085");


try {

// Getting the current web services configuration settings
getServerConfiguration(apiInstance) ;

//Calling the web service to add a new event
addEvent(apiHandler);

// Calling the web service to get the events
getEvents(apiInstance);

} catch (ApiException e) {

e.printStackTrace();
}
}

/**
* @param apiInstance
*
* @throws ApiException
*/
private static void getServerConfiguration(RestReportingEngineApi apiInstance) {

String result;
try {

result = apiInstance.resourceRestReportingEngineGetServerConfigurationAsJsonGET();

System.out.println(result + "\n");

} catch (ApiException e) {

e.printStackTrace();
}
}

/**
* @param apiHandler
*
* @throws ApiException
*/
private static void getEvents(RestReportingEngineApi apiInstance) throws ApiException {


String earliest = new DateTime(DateTimeZone.UTC).toString();
//earliest = "2016-08-02T14:00:00.05Z";
//Getting all the events after the time set by the "earliest" parameter.
List<JsonEvent> events= apiInstance.resourceRestReportingEngineFindEventsGET(earliest);

for (JsonEvent e : events) {
System.out.print(e.toString() + "\n");
}
}

/**
* @param apiHandler
*
* @throws ApiException
*/
private static void getResponses(RestRequestHandlerApi apiHandler) throws ApiException {


String earliest = new DateTime(DateTimeZone.UTC).toString();

//Getting all the responses before the time set by the "earliest" parameter.
List<JsonResponse> responses =apiHandler.resourceRestRequestHandlerGetResponsesGET(earliest);

for (JsonResponse resp : responses) {
System.out.print(resp.toString() + "\n");
}
}

/**
* @param apiHandler
* @throws ApiException
*/
private static void addEvent(RestRequestHandlerApi apiHandler)
throws ApiException {

JsonEvent event = new JsonEvent();

JsonUser user = new JsonUser();

//Setting the user name related to the event
user.setUsername("username666");

JsonIpaddress ip = new JsonIpaddress();

//IP address of the source of the event
ip.setAddress("8.8.8.8");

JsonGeolocation location = new JsonGeolocation();
location.setLatitude(new BigDecimal(37.596758));
location.setLongitude(new BigDecimal(-121.647992));

ip.setGeoLocation(location);
user.setIpAddress(ip);

JsonDetectionsystem detectionSys = new JsonDetectionsystem();

JsonDetectionpoint detectionPoint = new JsonDetectionpoint();
detectionPoint.setCategory("Input Validation");
detectionPoint.setLabel("IE1");

event.setDetectionPoint(detectionPoint);
event.setDetectionSystem(detectionSys);

//Setting the current time as the time of the Event
event.setTimestamp(new DateTime(DateTimeZone.UTC).toString());
event.setUser(user);

//Calling the corresponding REST API web service to add the event
apiHandler.resourceRestRequestHandlerAddEventPOST(event);
}

/**
* @param apiHandler
* @throws ApiException
*/
private static void addAttack(RestRequestHandlerApi apiHandler)
throws ApiException {

JsonAttack attack = new JsonAttack();

// Setting the user name related to the Attack
JsonUser user = new JsonUser();
user.setUsername("username");

// Setting the IP address of the attacked system
JsonIpaddress ip = new JsonIpaddress();
ip.setAddress("8.8.8.8");

JsonGeolocation location = new JsonGeolocation();
location.setLatitude(new BigDecimal(37.596758));
location.setLongitude(new BigDecimal(-121.647992));

ip.setGeoLocation(location);

user.setIpAddress(ip);

JsonDetectionsystem detectionSys = new JsonDetectionsystem();
detectionSys.setDetectionSystemId("Sample System");

JsonDetectionpoint detectionPoint = new JsonDetectionpoint();
detectionPoint.setCategory("Input Validation");
detectionPoint.setLabel("IE1");

//Defining a Threshold : 5 occurrences having 20 seconds between each.
JsonThreshold thr = new JsonThreshold();
thr.setCount(new BigDecimal(5));

JsonInterval interval = new JsonInterval();
interval.setDuration(new BigDecimal(20));
interval.setUnit("seconds");

thr.setInterval(interval);

detectionPoint.setThreshold(thr);

attack.setDetectionPoint(detectionPoint);
attack.setDetectionSystem(detectionSys);

//Setting the current time as the time of the Event
attack.setTimestamp(new DateTime(DateTimeZone.UTC).toString());
attack.setUser(user);

//Calling the corresponding REST API web service to add the Attack
apiHandler.resourceRestRequestHandlerAddAttackPOST(attack);

}


}




128 changes: 128 additions & 0 deletions client-libs/Java/pom.xml
@@ -0,0 +1,128 @@
<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>org.owasp.appsensor</groupId>
<artifactId>clientLibs.java</artifactId>

<version>1.0</version>
<name>AppSensor REST WebServices: Java Client</name>
<url>http://www.appsensor.org</url>
<build>
<plugins>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.1.5</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>

<inputSpec>swagger.json</inputSpec>
<language>java</language>
<output>${project.build.directory}/generated-sources </output>
<modelPackage>${project.groupId}.${project.artifactId}.model</modelPackage>
<apiPackage>${project.groupId}.${project.artifactId}.api</apiPackage>
<invokerPackage>${project.groupId}.${project.artifactId}.handler</invokerPackage>
<configOptions>
<dateLibrary>java8</dateLibrary>
</configOptions>
<library>jersey2</library>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- dependencies are needed for the client being generated -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
</dependency>

<!-- HTTP client: jersey-client -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22.1</version>
</dependency>

<!-- JSON processing: jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime-version}</version>
</dependency>

<!-- Base64 encoding that works in both JVM and Android -->
<dependency>
<groupId>com.brsanthu</groupId>
<artifactId>migbase64</artifactId>
<version>2.2</version>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<swagger-annotations-version>1.5.0</swagger-annotations-version>
<jersey-version>2.12</jersey-version>
<jackson-version>2.4.2</jackson-version>
<jodatime-version>2.3</jodatime-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.8.1</junit-version>
</properties>
</project>

0 comments on commit 83e2056

Please sign in to comment.