-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Moesif/moesif-java-request
WIP: Support outgoing tracking of requests with Java
- Loading branch information
Showing
16 changed files
with
740 additions
and
938 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ overridedb.* | |
*.ipr | ||
*.iws | ||
.idea | ||
.vscode | ||
*.jar | ||
.DS_Store | ||
.factorypath | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<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> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.19.RELEASE</version> | ||
</parent> | ||
<groupId>com.moesif.javarequest</groupId> | ||
<artifactId>java-request-example</artifactId> | ||
<version>1.3.0</version> | ||
<name>java-request-example</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<maven.deploy.skip>true</maven.deploy.skip> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<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> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.jayway.jsonpath</groupId> | ||
<artifactId>json-path</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.moesif.servlet</groupId> | ||
<artifactId>moesif-servlet</artifactId> | ||
<version>1.5.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.moesif.moesifjavarequest</groupId> | ||
<artifactId>moesif-moesifjavarequest</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>${commons-lang3.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-io</groupId> | ||
<artifactId>commons-io</artifactId> | ||
<version>${commons-io.version}</version> | ||
</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>jcenter</id> | ||
<url>https://jcenter.bintray.com/</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> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
<finalName>moesif-java-request-example</finalName> | ||
</build> | ||
</project> |
12 changes: 12 additions & 0 deletions
12
java-request-example/src/main/java/com/moesif/javarequest/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.moesif.javarequest; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
java-request-example/src/main/java/com/moesif/javarequest/MoesifHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.moesif.javarequest; | ||
|
||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.moesif.moesifjavarequest.MoesifSpringRequestInterceptor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class MoesifHttpClient { | ||
private RestTemplate template; | ||
|
||
public MoesifHttpClient() { | ||
template = new RestTemplate(); | ||
|
||
final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(); | ||
|
||
interceptors.add(new MoesifSpringRequestInterceptor( | ||
"Enter your Moesif AppId here", | ||
new RequestConfig() | ||
)); | ||
|
||
template.setInterceptors( interceptors ); | ||
} | ||
|
||
public RestTemplate getRestTemplate() { | ||
return template; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
java-request-example/src/main/java/com/moesif/javarequest/Post.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.moesif.javarequest; | ||
|
||
public class Post { | ||
|
||
private final long id; | ||
private final String name; | ||
|
||
public Post(long id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
java-request-example/src/main/java/com/moesif/javarequest/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.moesif.javarequest; | ||
|
||
import org.json.JSONObject; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
public class PostController { | ||
|
||
private MoesifHttpClient httpClient = new MoesifHttpClient(); | ||
|
||
@RequestMapping("/create_post") | ||
public String createPost(@RequestParam(value="name", defaultValue="New Post") String name) { | ||
JSONObject json = new JSONObject(); | ||
|
||
json.put("id", Long.toString(Math.round(Math.random() * 1000)).toString()); | ||
json.put("name", name); | ||
|
||
HttpEntity<String> request = new HttpEntity<String>(json.toString()); | ||
|
||
ResponseEntity<String> response = httpClient.getRestTemplate().exchange( | ||
"https://jsonplaceholder.typicode.com/posts", | ||
HttpMethod.POST, | ||
request, | ||
String.class | ||
); | ||
|
||
return response.getBody(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
java-request-example/src/main/java/com/moesif/javarequest/RequestConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.moesif.javarequest; | ||
|
||
import com.moesif.moesifjavarequest.MoesifRequestConfiguration; | ||
|
||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
public class RequestConfig extends MoesifRequestConfiguration { | ||
@Override | ||
public String identifyUser(HttpRequest request, ClientHttpResponse response) { | ||
return "test_user"; | ||
} | ||
} |
Oops, something went wrong.