Skip to content

Commit d6abcf1

Browse files
author
Majid Mallis
committed
Moved OAuthJerseyClient class to its own file
1 parent dfd8dc0 commit d6abcf1

File tree

6 files changed

+130
-122
lines changed

6 files changed

+130
-122
lines changed
Lines changed: 1 addition & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
package com.docusign.esign.client.auth;
22

3-
import com.migcomponents.migbase64.Base64;
43
import java.util.List;
54
import java.util.Map;
65

7-
import javax.ws.rs.core.Response.Status.Family;
8-
96
import org.apache.oltu.oauth2.client.HttpClient;
107
import org.apache.oltu.oauth2.client.OAuthClient;
118
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
9+
import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
1210
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder;
1311
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder;
14-
import org.apache.oltu.oauth2.client.response.OAuthClientResponse;
15-
import org.apache.oltu.oauth2.client.response.OAuthClientResponseFactory;
16-
import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
17-
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
18-
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
1912
import org.apache.oltu.oauth2.common.message.types.GrantType;
2013
import org.apache.oltu.oauth2.common.message.types.ResponseType;
2114
import org.apache.oltu.oauth2.common.token.BasicOAuthToken;
2215

2316
import com.docusign.esign.client.Pair;
2417
import com.sun.jersey.api.client.Client;
2518
import com.sun.jersey.api.client.ClientHandlerException;
26-
import com.sun.jersey.api.client.ClientResponse;
27-
import com.sun.jersey.api.client.WebResource.Builder;
2819

2920
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2017-03-06T16:42:36.211-08:00")
3021
public class OAuth implements Authentication {
@@ -141,103 +132,4 @@ public void setOauthClient(OAuthClient oauthClient) {
141132
public void setOauthClient(Client client) {
142133
this.oauthClient = new OAuthClient(new OAuthJerseyClient(client));
143134
}
144-
145-
public static class OAuthJerseyClient implements HttpClient {
146-
147-
private Client client;
148-
149-
public OAuthJerseyClient() {
150-
this.client = new Client(null, null);
151-
}
152-
153-
public OAuthJerseyClient(Client client) {
154-
this.client = client;
155-
}
156-
157-
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
158-
String requestMethod, Class<T> responseClass) throws OAuthSystemException, OAuthProblemException {
159-
String contentType = headers.get("Content-Type");
160-
String url = request.getLocationUri();
161-
String body = request.getBody();
162-
Builder builder = this.client.resource(url).getRequestBuilder();
163-
164-
for (String key : headers.keySet()) {
165-
builder = builder.header(key, headers.get(key));
166-
}
167-
168-
String grantType = null, code = null, clientId = null, clientSecret = null;
169-
for (String entry : body.split("&")) {
170-
String key = entry.split("=")[0];
171-
String value = entry.split("=")[1];
172-
if ("grant_type".equals(key)) {
173-
grantType = value;
174-
} else if ("code".equals(key)) {
175-
code = value;
176-
} else if ("client_id".equals(key)) {
177-
clientId = value;
178-
} else if ("client_secret".equals(key)) {
179-
clientSecret = value;
180-
}
181-
}
182-
183-
if (grantType == null || code == null) {
184-
throw new OAuthSystemException("Missing grant_type/code");
185-
} else {
186-
body = "grant_type=" + grantType + "&code=" + code;
187-
}
188-
189-
if (clientId == null || clientSecret == null) {
190-
throw new OAuthSystemException("Missing clientId/secret");
191-
} else {
192-
byte[] bytes = (clientId + ":" + clientSecret).getBytes();
193-
builder.header("Authorization", "Basic " + Base64.encodeToString(bytes, false));
194-
}
195-
196-
ClientResponse response = null;
197-
198-
if ("GET".equals(requestMethod)) {
199-
response = (ClientResponse) builder.get(ClientResponse.class);
200-
} else if ("POST".equals(requestMethod)) {
201-
response = builder.type(contentType).post(ClientResponse.class, body);
202-
} else if ("PUT".equals(requestMethod)) {
203-
response = builder.type(contentType).put(ClientResponse.class, body);
204-
} else if ("DELETE".equals(requestMethod)) {
205-
response = builder.type(contentType).delete(ClientResponse.class, body);
206-
}
207-
208-
if(response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
209-
return null;
210-
} else if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
211-
if (responseClass == null)
212-
return null;
213-
else {
214-
String respBody = response.getEntity(String.class);
215-
return OAuthClientResponseFactory.createCustomResponse(
216-
respBody,
217-
contentType,
218-
response.getStatus(),
219-
response.getHeaders(),
220-
responseClass
221-
);
222-
}
223-
} else {
224-
String message = "error";
225-
String respBody = null;
226-
if (response.hasEntity()) {
227-
try {
228-
respBody = response.getEntity(String.class);
229-
message = respBody;
230-
System.err.println(message);
231-
} catch (RuntimeException e) {
232-
e.printStackTrace();
233-
}
234-
}
235-
}
236-
return null;
237-
}
238-
239-
public void shutdown() {
240-
// Nothing to do here
241-
}
242-
}
243135
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.docusign.esign.client.auth;
2+
3+
import com.migcomponents.migbase64.Base64;
4+
import java.util.Map;
5+
6+
import javax.ws.rs.core.Response.Status.Family;
7+
import org.apache.oltu.oauth2.client.HttpClient;
8+
import org.apache.oltu.oauth2.client.response.OAuthClientResponse;
9+
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
10+
import org.apache.oltu.oauth2.client.response.OAuthClientResponseFactory;
11+
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.AuthenticationRequestBuilder;
12+
import org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder;
13+
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
14+
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
15+
import com.sun.jersey.api.client.WebResource.Builder;
16+
import com.sun.jersey.api.client.Client;
17+
import com.sun.jersey.api.client.ClientResponse;
18+
19+
public class OAuthJerseyClient implements HttpClient {
20+
21+
private Client client;
22+
23+
public OAuthJerseyClient() {
24+
this.client = new Client(null, null);
25+
}
26+
27+
public OAuthJerseyClient(Client client) {
28+
this.client = client;
29+
}
30+
31+
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
32+
String requestMethod, Class<T> responseClass) throws OAuthSystemException, OAuthProblemException {
33+
String contentType = headers.get("Content-Type");
34+
String url = request.getLocationUri();
35+
String body = request.getBody();
36+
Builder builder = this.client.resource(url).getRequestBuilder();
37+
38+
for (String key : headers.keySet()) {
39+
builder = builder.header(key, headers.get(key));
40+
}
41+
42+
String grantType = null, code = null, clientId = null, clientSecret = null;
43+
for (String entry : body.split("&")) {
44+
String key = entry.split("=")[0];
45+
String value = entry.split("=")[1];
46+
if ("grant_type".equals(key)) {
47+
grantType = value;
48+
} else if ("code".equals(key)) {
49+
code = value;
50+
} else if ("client_id".equals(key)) {
51+
clientId = value;
52+
} else if ("client_secret".equals(key)) {
53+
clientSecret = value;
54+
}
55+
}
56+
57+
if (grantType == null || code == null) {
58+
throw new OAuthSystemException("Missing grant_type/code");
59+
} else {
60+
body = "grant_type=" + grantType + "&code=" + code;
61+
}
62+
63+
if (clientId == null || clientSecret == null) {
64+
throw new OAuthSystemException("Missing clientId/secret");
65+
} else {
66+
byte[] bytes = (clientId + ":" + clientSecret).getBytes();
67+
builder.header("Authorization", "Basic " + Base64.encodeToString(bytes, false));
68+
}
69+
70+
ClientResponse response = null;
71+
72+
if ("GET".equals(requestMethod)) {
73+
response = (ClientResponse) builder.get(ClientResponse.class);
74+
} else if ("POST".equals(requestMethod)) {
75+
response = builder.type(contentType).post(ClientResponse.class, body);
76+
} else if ("PUT".equals(requestMethod)) {
77+
response = builder.type(contentType).put(ClientResponse.class, body);
78+
} else if ("DELETE".equals(requestMethod)) {
79+
response = builder.type(contentType).delete(ClientResponse.class, body);
80+
}
81+
82+
if(response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
83+
return null;
84+
} else if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
85+
if (responseClass == null)
86+
return null;
87+
else {
88+
String respBody = response.getEntity(String.class);
89+
return OAuthClientResponseFactory.createCustomResponse(
90+
respBody,
91+
contentType,
92+
response.getStatus(),
93+
response.getHeaders(),
94+
responseClass
95+
);
96+
}
97+
} else {
98+
String message = "error";
99+
String respBody = null;
100+
if (response.hasEntity()) {
101+
try {
102+
respBody = response.getEntity(String.class);
103+
message = respBody;
104+
System.err.println(message);
105+
} catch (RuntimeException e) {
106+
e.printStackTrace();
107+
}
108+
}
109+
}
110+
return null;
111+
}
112+
113+
public void shutdown() {
114+
// Nothing to do here
115+
}
116+
}
-34 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Generated by Maven
2-
#Thu Jul 13 12:09:30 PDT 2017
2+
#Thu Jul 13 22:13:05 PDT 2017
33
version=2.2.1
44
groupId=com.docusign
55
artifactId=docusign-esign-java
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-------------------------------------------------------------------------------
22
Test set: SdkUnitTests
33
-------------------------------------------------------------------------------
4-
Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 39.097 sec
4+
Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 34.763 sec

target/surefire-reports/TEST-SdkUnitTests.xml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<testsuite tests="10" failures="0" name="SdkUnitTests" time="39.097" errors="0" skipped="0">
2+
<testsuite tests="10" failures="0" name="SdkUnitTests" time="34.763" errors="0" skipped="0">
33
<properties>
44
<property name="java.runtime.name" value="Java(TM) SE Runtime Environment"/>
55
<property name="sun.boot.library.path" value="/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre/lib"/>
@@ -63,14 +63,14 @@
6363
<property name="ftp.nonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
6464
<property name="sun.cpu.isalist" value=""/>
6565
</properties>
66-
<testcase classname="SdkUnitTests" name="LoginTest" time="2.431"/>
67-
<testcase classname="SdkUnitTests" name="ResendEnvelopeTest" time="5.508"/>
68-
<testcase classname="SdkUnitTests" name="ListDocumentsTest" time="2.801"/>
69-
<testcase classname="SdkUnitTests" name="EmbeddedSigningTest" time="5.008"/>
70-
<testcase classname="SdkUnitTests" name="GetDiagnosticLogsTest" time="5.033"/>
71-
<testcase classname="SdkUnitTests" name="CreateTemplateTest" time="3.905"/>
72-
<testcase classname="SdkUnitTests" name="OAuthLoginTest" time="1.072"/>
73-
<testcase classname="SdkUnitTests" name="RequestSignatureFromTemplate" time="3.746"/>
74-
<testcase classname="SdkUnitTests" name="RequestASignatureTest" time="4.447"/>
75-
<testcase classname="SdkUnitTests" name="DownLoadEnvelopeDocumentsTest" time="5.146"/>
66+
<testcase classname="SdkUnitTests" name="OAuthLoginTest" time="0.896"/>
67+
<testcase classname="SdkUnitTests" name="RequestASignatureTest" time="3.967"/>
68+
<testcase classname="SdkUnitTests" name="ListDocumentsTest" time="2.064"/>
69+
<testcase classname="SdkUnitTests" name="RequestSignatureFromTemplate" time="3.001"/>
70+
<testcase classname="SdkUnitTests" name="CreateTemplateTest" time="3.971"/>
71+
<testcase classname="SdkUnitTests" name="EmbeddedSigningTest" time="4.096"/>
72+
<testcase classname="SdkUnitTests" name="LoginTest" time="1.702"/>
73+
<testcase classname="SdkUnitTests" name="DownLoadEnvelopeDocumentsTest" time="4.335"/>
74+
<testcase classname="SdkUnitTests" name="GetDiagnosticLogsTest" time="5.79"/>
75+
<testcase classname="SdkUnitTests" name="ResendEnvelopeTest" time="4.941"/>
7676
</testsuite>

0 commit comments

Comments
 (0)