Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Support outgoing tracking of requests with Java #8

Merged
merged 22 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ overridedb.*
*.ipr
*.iws
.idea
.vscode
*.jar
.DS_Store
.factorypath
Expand Down
167 changes: 167 additions & 0 deletions moesif-java-request/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<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>com.moesif.moesifjavarequest</groupId>
<artifactId>moesif-moesifjavarequest</artifactId>
<version>1.5.2</version>
<packaging>jar</packaging>
<name>moesif-java-request</name>
<description>Moesif SDK for Java to log and analyze outgoing API calls</description>
<url>https://www.moesif.com/docs/server-integration/servlet/</url>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<scm>
<url>https://github.com/Moesif/moesif-servlet.git</url>
</scm>

<developers>
<developer>
<id>moesif</id>
<name>Moesif API Insights</name>
<email>support@moesif.com</email>
<url>https://www.moesif.com</url>
<organization>Moesif, Inc.</organization>
<organizationUrl>https://www.moesif.com</organizationUrl>
<roles>
<role>owner</role>
<role>developer</role>
</roles>
<timezone>America/Los_Angeles</timezone>
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<assertj-core.version>3.1.0</assertj-core.version>
<commons-io.version>2.4</commons-io.version>
<commons-lang3.version>3.4</commons-lang3.version>
<jackson.version>2.8.7</jackson.version>
<junit.version>4.12</junit.version>
<logback.version>1.1.3</logback.version>
<mockito-core.version>1.10.19</mockito-core.version>
<java.version>1.7</java.version>
</properties>

<dependencies>

<dependency>
<groupId>com.moesif.api</groupId>
<artifactId>moesifapi</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.6.RELEASE</version>
<scope>provided</scope>
</dependency>

<!-- testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<repositories>
<repository>
<id>bintray-moesif-maven</id>
<name>bintray</name>
<url>http://dl.bintray.com/moesif/maven</url>
</repository>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>

<build>
<finalName>moesif-moesifjavarequest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
<configuration>
<!-- This is necessary for gpg to not try to use the pinentry programs -->
<gpgArguments>
<arg>--pinentry-mode</arg>
<arg>loopback</arg>
</gpgArguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>bintray-moesif-maven</id>
<name>moesif-maven</name>
<url>https://api.bintray.com/maven/moesif/maven/moesif-servlet/;publish=1</url>
</repository>
</distributionManagement>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.moesif.moesifjavarequest;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;

/**
* The body of ClientHttpResponse is an InputStream which can only be read once.
*
* Since we want to report the body contents without disrupting the application
* code which depends on the response body, we must create a wrapper object.
*/
public class MoesifClientHttpResponse implements ClientHttpResponse {
private ClientHttpResponse response;
private String bodyString;
private InputStream inputStream;

MoesifClientHttpResponse(ClientHttpResponse response) throws IOException {
this.response = response;

InputStream bodyStream = response.getBody();
byte[] bodyBytes = bodyStream.readAllBytes();

bodyString = new String(bodyBytes);
inputStream = new ByteArrayInputStream(bodyBytes);
}

public String getBodyString() {
return bodyString;
}

@Override
public InputStream getBody() throws IOException {
// publicly reuse the same InputStream to keep the same behavior
// (can only read once)
return inputStream;
}

// wrap all methods required by ClientHttpResponse
@Override
public int getRawStatusCode() throws IOException {
return response.getRawStatusCode();
}

@Override
public HttpStatus getStatusCode() throws IOException {
return response.getStatusCode();
}

@Override
public void close() {
response.close();
}

@Override
public HttpHeaders getHeaders() {
return response.getHeaders();
}

@Override
public String getStatusText() throws IOException {
return response.getStatusText();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.moesif.moesifjavarequest;

import com.moesif.api.models.EventModel;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpResponse;

public class MoesifRequestConfiguration {
public boolean debug = false;

public boolean skip(HttpRequest request, ClientHttpResponse response) {
return false;
}

public EventModel maskContent(EventModel eventModel) {
return eventModel;
}

public String identifyUser(HttpRequest request, ClientHttpResponse response) {
return null;
}

public String getSessionToken(HttpRequest request, ClientHttpResponse response) {
return null;
}

public String getApiVersion(HttpRequest request, ClientHttpResponse response) {
return null;
}

public Object getMetadata(HttpRequest request, ClientHttpResponse response) {
return null;
}
}