Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jcschaff committed Dec 1, 2017
0 parents commit cf32cfa
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target/
/.settings/
/.classpath
/.project
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# c3pro-mockmobile - a mock mobile client for C3-PRO platform.

This mock client exercies the c3pro-api and c3pro-auth servers designed to be compatible with [C3-PRO/c3-pro-server](https://www.github.com/C3-PRO/c3-pro-server).
77 changes: 77 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.uconn</groupId>
<artifactId>c3pro-mockmobile</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<scribejava.version>5.0.0</scribejava.version>
<hapi.fhir.version>3.0.0</hapi.fhir.version>
</properties>


<dependencies>

<dependency>
<groupId>com.github.scribejava</groupId>
<artifactId>scribejava-core</artifactId>
<version>${scribejava.version}</version>
</dependency>

<dependency>
<groupId>com.github.scribejava</groupId>
<artifactId>scribejava-apis</artifactId>
<version>${scribejava.version}</version>
</dependency>

<dependency>
<groupId>com.github.scribejava</groupId>
<artifactId>scribejava-httpclient-ahc</artifactId>
<version>${scribejava.version}</version>
</dependency>

<!-- Binding for System.out -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>

<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-base</artifactId>
<version>${hapi.fhir.version}</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-structures-hl7org-dstu2</artifactId>
<version>${hapi.fhir.version}</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-validation-resources-dstu2</artifactId>
<version>${hapi.fhir.version}</version>
</dependency>
<dependency>
<groupId>com.phloc</groupId>
<artifactId>phloc-schematron</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.phloc</groupId>
<artifactId>phloc-commons</artifactId>
<version>4.4.11</version>
</dependency>
</dependencies>

</project>
28 changes: 28 additions & 0 deletions src/main/java/edu/uconn/c3pro/mockmobile/C3proApiDefinition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package edu.uconn.c3pro.mockmobile;
import com.github.scribejava.core.builder.api.DefaultApi20;

public class C3proApiDefinition extends DefaultApi20 {

protected C3proApiDefinition() {
}

private static class InstanceHolder {
private static final C3proApiDefinition INSTANCE = new C3proApiDefinition();
}

public static C3proApiDefinition instance() {
return InstanceHolder.INSTANCE;
}

@Override
public String getAccessTokenEndpoint() {
return "http://localhost:8081/c3pro/oauth";
}

@Override
protected String getAuthorizationBaseUrl() {
return "http://localhost:8081/oauth/authorize";
}


}
110 changes: 110 additions & 0 deletions src/main/java/edu/uconn/c3pro/mockmobile/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package edu.uconn.c3pro.mockmobile;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.hl7.fhir.instance.model.Questionnaire;

import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Token;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth20Service;
import com.github.scribejava.httpclient.ahc.AhcHttpClient;
import com.github.scribejava.httpclient.ahc.AhcHttpClientConfig;
import com.google.gson.Gson;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;

public class Client {
private static final String NETWORK_NAME = "Facebook";
private static final String PROTECTED_RESOURCE_URL = "http://localhost:8082/graph.facebook.com/me";
private static final Token EMPTY_TOKEN = null;

public static void main(String[] args) {

try {
// Replace these with your own api key and secret
String apiKey = "your_app_id";
String apiSecret = "your_api_secret";

final AhcHttpClientConfig clientConfig = new AhcHttpClientConfig(new DefaultAsyncHttpClientConfig.Builder()
.setMaxConnections(5)
.setRequestTimeout(10000)
.setPooledConnectionIdleTimeout(1000)
.setReadTimeout(1000)
.build());

//AhcHttpClient client = new AhcHttpClient(AhcHttpClientConfig.defaultConfig());
AhcHttpClient client = new AhcHttpClient(clientConfig);
String userAgent = "c3pro-java-client/1.0";
Map<String, String> headers = new HashMap<String,String>(){{
put("Antispam", "MY-ANTI-SPAM");
}};
Gson gson = new Gson();
Registration registration = new Registration(true,"your apple-supplied app purchase receipt");
String registrationJSON = gson.toJson(registration);
String registrationJSON_expected = "{\n" +
" \"sandbox\": true,\n" +
" \"receipt-data\": \"your apple-supplied app purchase receipt\"\n" +
" }";
System.out.println("registrationJSON:\n"+registrationJSON+"\n\nexpected:\n"+registrationJSON_expected+"\n");
byte[] bodyContents = registrationJSON.getBytes("UTF-8");
final String REGISTRATION_URL = "http://localhost:8081/c3pro/register";
Response response = client.execute(userAgent, headers, Verb.POST, REGISTRATION_URL, bodyContents);
String registrationResponseJSON = response.getBody();
RegistrationResponse registrationResponse = gson.fromJson(registrationResponseJSON, RegistrationResponse.class);

/* HTTP/1.1 POST /c3pro/oauth?grant_type=client_credentials
Authentication: Basic BASE64(ClientId:Secret)
NOTE: According to OAuth2 two-legged specifications both clientId and Secret should be x-www-form-urlencoded before Base64 encoding is applied.
Oauth2 authorization response
HTTP/1.1 201 Created
Content-Type: application/json
{
"access_token":"{{some token}}",
"expires_in": "{{seconds to expiration}}",
"token_type": "bearer"
}
*/
try (OAuth20Service service = new ServiceBuilder(apiKey)
.apiKey(apiKey)
.apiSecret(apiSecret)
.debug()
.callback("http://localhost:8080/oauth_callback/")
.httpClientConfig(clientConfig)
.build(C3proApiDefinition.instance());){
Scanner in = new Scanner(System.in);

OAuth2AccessToken accessToken = service.getAccessTokenClientCredentialsGrant();


FhirContext ctx = FhirContext.forDstu2();
IParser fhirParser = ctx.newJsonParser();

//
// retrieve questionnaire "q1"
//
final OAuthRequest request = new OAuthRequest(Verb.GET, "https://localhost:8082/c3pro/fhir/Questionnaire/q1");
service.signRequest(accessToken, request);
final Response questionnaireResponse = service.execute(request);
if (questionnaireResponse.getCode() != 200) {
throw new RuntimeException("unexpected return code "+questionnaireResponse.getCode()+" - "+questionnaireResponse.getMessage());
}
final String questionnaireString = response.getBody();
final Questionnaire questionnaire = fhirParser.parseResource(Questionnaire.class, questionnaireString);
System.out.println("received questionnaire: "+fhirParser.encodeResourceToString(questionnaire));
}
} catch (Exception e) {
e.printStackTrace();
}
}

}
27 changes: 27 additions & 0 deletions src/main/java/edu/uconn/c3pro/mockmobile/Registration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package edu.uconn.c3pro.mockmobile;

public class Registration {
private boolean sandbox;
private String receipt_data;

public Registration(boolean sandbox, String receipt_data) {
this.sandbox = sandbox;
}

public boolean getSandbox() {
return sandbox;
}

public void setId(boolean sandbox) {
this.sandbox = sandbox;
}

public String getContent() {
return receipt_data;
}

public void setContent(String receipt_data) {
this.receipt_data = receipt_data;
}

}
40 changes: 40 additions & 0 deletions src/main/java/edu/uconn/c3pro/mockmobile/RegistrationResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.uconn.c3pro.mockmobile;

public class RegistrationResponse {
private String client_id;
private String client_secret;
private final String[] grant_types = { "client_credentials" };
private final String token_endpoint_auth_method = "client_secret_basic";

public RegistrationResponse(String client_id, String client_secret) {
super();
this.client_id = client_id;
this.client_secret = client_secret;
}

public String getClient_id() {
return client_id;
}

public void setClient_id(String client_id) {
this.client_id = client_id;
}

public String getClient_secret() {
return client_secret;
}

public void setClient_secret(String client_secret) {
this.client_secret = client_secret;
}

public String[] getGrant_types() {
return grant_types;
}

public String getToken_endpoint_auth_method() {
return token_endpoint_auth_method;
}


}
27 changes: 27 additions & 0 deletions src/main/java/edu/uconn/c3pro/mockmobile/VCellApiDefinition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package edu.uconn.c3pro.mockmobile;
import com.github.scribejava.core.builder.api.DefaultApi20;

public class VCellApiDefinition extends DefaultApi20 {

protected VCellApiDefinition() {
}

private static class InstanceHolder {
private static final VCellApiDefinition INSTANCE = new VCellApiDefinition();
}

public static VCellApiDefinition instance() {
return InstanceHolder.INSTANCE;
}

@Override
public String getAccessTokenEndpoint() {
return "http://localhost:8081/oauth/token";
}

@Override
protected String getAuthorizationBaseUrl() {
return "http://localhost:8081/oauth/authorize";
}

}

0 comments on commit cf32cfa

Please sign in to comment.