Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit 042559e

Browse files
committed
fixes #44 Replace Client with Http2Client and remove dependency of apache httpclient
1 parent 0775944 commit 042559e

File tree

36 files changed

+3043
-1292
lines changed

36 files changed

+3043
-1292
lines changed

authorize/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,6 @@
163163
<groupId>org.postgresql</groupId>
164164
<artifactId>postgresql</artifactId>
165165
</dependency>
166-
<dependency>
167-
<groupId>org.apache.httpcomponents</groupId>
168-
<artifactId>httpclient</artifactId>
169-
</dependency>
170166

171167

172168
<!-- Test Dependencies -->

authorize/src/test/java/com/networknt/oauth/authorize/handler/Oauth2AuthorizeGetHandlerTest.java

Lines changed: 234 additions & 109 deletions
Large diffs are not rendered by default.
Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
package com.networknt.oauth.authorize.handler;
22

3-
import org.apache.commons.io.IOUtils;
4-
import org.apache.http.NameValuePair;
5-
import org.apache.http.client.entity.UrlEncodedFormEntity;
6-
import org.apache.http.client.methods.CloseableHttpResponse;
7-
import org.apache.http.client.methods.HttpPost;
8-
import org.apache.http.impl.client.CloseableHttpClient;
9-
import org.apache.http.impl.client.HttpClients;
10-
import org.apache.http.message.BasicNameValuePair;
3+
import com.networknt.client.Http2Client;
4+
import com.networknt.exception.ClientException;
5+
import io.undertow.client.ClientConnection;
6+
import io.undertow.client.ClientRequest;
7+
import io.undertow.client.ClientResponse;
8+
import io.undertow.util.Headers;
9+
import io.undertow.util.Methods;
1110
import org.junit.ClassRule;
1211
import org.junit.Test;
1312
import org.slf4j.Logger;
1413
import org.slf4j.LoggerFactory;
14+
import org.xnio.IoUtils;
15+
import org.xnio.OptionMap;
1516

16-
import java.util.ArrayList;
17-
import java.util.Arrays;
18-
import java.util.List;
17+
import java.net.URI;
18+
import java.util.*;
19+
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.TimeUnit;
21+
import java.util.concurrent.atomic.AtomicReference;
1922

2023
/**
2124
* Generated by swagger-codegen
@@ -28,24 +31,43 @@ public class Oauth2AuthorizePostHandlerTest {
2831

2932
@Test
3033
public void testAuthorizationCode() throws Exception {
31-
String url = "http://localhost:6881/oauth2/authorize";
32-
CloseableHttpClient client = HttpClients.createDefault();
33-
HttpPost httpPost = new HttpPost(url);
34-
BasicNameValuePair[] pairs = new BasicNameValuePair[]{
35-
new BasicNameValuePair("j_username", "admin"),
36-
new BasicNameValuePair("j_password", "123456"),
37-
new BasicNameValuePair("response_type", "code"),
38-
new BasicNameValuePair("client_id", "59f347a0-c92d-11e6-9d9d-cec0c932ce01")
39-
};
40-
final List<NameValuePair> data = new ArrayList<>();
41-
data.addAll(Arrays.asList(pairs));
42-
httpPost.setEntity(new UrlEncodedFormEntity(data));
43-
CloseableHttpResponse response = client.execute(httpPost);
34+
Map<String, String> params = new HashMap<>();
35+
params.put("j_username", "admin");
36+
params.put("j_password", "123456");
37+
params.put("response_type", "code");
38+
params.put("client_id", "59f347a0-c92d-11e6-9d9d-cec0c932ce01");
4439

45-
int statusCode = response.getStatusLine().getStatusCode();
46-
String body = IOUtils.toString(response.getEntity().getContent(), "utf8");
47-
//Assert.assertEquals(statusCode, 302);
48-
49-
// at this moment, an exception will help as it is redirected to localhost:8080 and it is not up.
40+
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
41+
final Http2Client client = Http2Client.getInstance();
42+
final CountDownLatch latch = new CountDownLatch(1);
43+
final ClientConnection connection;
44+
try {
45+
connection = client.connect(new URI("http://localhost:6881"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
46+
} catch (Exception e) {
47+
throw new ClientException(e);
48+
}
49+
String s = Http2Client.getFormDataString(params);
50+
try {
51+
connection.getIoThread().execute(new Runnable() {
52+
@Override
53+
public void run() {
54+
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/oauth2/authorize");
55+
request.getRequestHeaders().put(Headers.HOST, "localhost");
56+
request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
57+
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
58+
connection.sendRequest(request, client.createClientCallback(reference, latch, s));
59+
}
60+
});
61+
latch.await(10, TimeUnit.SECONDS);
62+
int statusCode = reference.get().getResponseCode();
63+
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
64+
//Assert.assertEquals(statusCode, 302);
65+
// at this moment, an exception will help as it is redirected to localhost:8080 and it is not up.
66+
} catch (Exception e) {
67+
logger.error("IOException: ", e);
68+
throw new ClientException(e);
69+
} finally {
70+
IoUtils.safeClose(connection);
71+
}
5072
}
5173
}

cache/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,6 @@
129129
<groupId>io.undertow</groupId>
130130
<artifactId>undertow-core</artifactId>
131131
</dependency>
132-
<dependency>
133-
<groupId>org.apache.httpcomponents</groupId>
134-
<artifactId>httpclient</artifactId>
135-
</dependency>
136132
<dependency>
137133
<groupId>com.zaxxer</groupId>
138134
<artifactId>HikariCP</artifactId>

client/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@
144144
<groupId>io.undertow</groupId>
145145
<artifactId>undertow-core</artifactId>
146146
</dependency>
147-
<dependency>
148-
<groupId>org.apache.httpcomponents</groupId>
149-
<artifactId>httpclient</artifactId>
150-
</dependency>
151147
<dependency>
152148
<groupId>com.zaxxer</groupId>
153149
<artifactId>HikariCP</artifactId>
Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package com.networknt.oauth.client.handler;
22

3-
import com.networknt.client.Client;
3+
import com.networknt.client.Http2Client;
44
import com.networknt.config.Config;
55
import com.networknt.exception.ApiException;
66
import com.networknt.exception.ClientException;
77
import com.networknt.status.Status;
8-
import org.apache.commons.io.IOUtils;
9-
import org.apache.http.client.methods.CloseableHttpResponse;
10-
import org.apache.http.client.methods.HttpDelete;
11-
import org.apache.http.impl.client.CloseableHttpClient;
8+
import io.undertow.client.ClientConnection;
9+
import io.undertow.client.ClientRequest;
10+
import io.undertow.client.ClientResponse;
11+
import io.undertow.util.Methods;
1212
import org.junit.Assert;
1313
import org.junit.ClassRule;
1414
import org.junit.Test;
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
17+
import org.xnio.IoUtils;
18+
import org.xnio.OptionMap;
19+
20+
import java.net.URI;
21+
import java.util.concurrent.CountDownLatch;
22+
import java.util.concurrent.atomic.AtomicReference;
1723

1824
/**
1925
* Generated by swagger-codegen
@@ -26,25 +32,47 @@ public class Oauth2ClientClientIdDeleteHandlerTest {
2632

2733
@Test
2834
public void testOauth2ClientClientIdDeleteHandler() throws ClientException, ApiException {
29-
CloseableHttpClient client = Client.getInstance().getSyncClient();
30-
HttpDelete httpDelete = new HttpDelete("http://localhost:6884/oauth2/client/59f347a0-c92d-11e6-9d9d-cec0c932ce01");
35+
final Http2Client client = Http2Client.getInstance();
36+
final CountDownLatch latch = new CountDownLatch(1);
37+
final ClientConnection connection;
38+
try {
39+
connection = client.connect(new URI("http://localhost:6884"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
40+
} catch (Exception e) {
41+
throw new ClientException(e);
42+
}
43+
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
3144
try {
32-
CloseableHttpResponse response = client.execute(httpDelete);
33-
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
34-
//Assert.assertNotNull(IOUtils.toString(response.getEntity().getContent(), "utf8"));
45+
ClientRequest request = new ClientRequest().setMethod(Methods.DELETE).setPath("/oauth2/client/59f347a0-c92d-11e6-9d9d-cec0c932ce01");
46+
connection.sendRequest(request, client.createClientCallback(reference, latch));
47+
latch.await();
3548
} catch (Exception e) {
36-
e.printStackTrace();
49+
logger.error("Exception: ", e);
50+
throw new ClientException(e);
51+
} finally {
52+
IoUtils.safeClose(connection);
3753
}
54+
int statusCode = reference.get().getResponseCode();
55+
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
56+
Assert.assertEquals(200, statusCode);
3857
}
3958

4059
@Test
4160
public void testClientNotFound() throws ClientException, ApiException {
42-
CloseableHttpClient client = Client.getInstance().getSyncClient();
43-
HttpDelete httpDelete = new HttpDelete("http://localhost:6884/oauth2/client/fake");
61+
final Http2Client client = Http2Client.getInstance();
62+
final CountDownLatch latch = new CountDownLatch(1);
63+
final ClientConnection connection;
4464
try {
45-
CloseableHttpResponse response = client.execute(httpDelete);
46-
int statusCode = response.getStatusLine().getStatusCode();
47-
String body = IOUtils.toString(response.getEntity().getContent(), "utf8");
65+
connection = client.connect(new URI("http://localhost:6884"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
66+
} catch (Exception e) {
67+
throw new ClientException(e);
68+
}
69+
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
70+
try {
71+
ClientRequest request = new ClientRequest().setMethod(Methods.DELETE).setPath("/oauth2/client/fake");
72+
connection.sendRequest(request, client.createClientCallback(reference, latch));
73+
latch.await();
74+
int statusCode = reference.get().getResponseCode();
75+
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
4876
Assert.assertEquals(404, statusCode);
4977
if(statusCode == 404) {
5078
Status status = Config.getInstance().getMapper().readValue(body, Status.class);
@@ -53,8 +81,10 @@ public void testClientNotFound() throws ClientException, ApiException {
5381
Assert.assertEquals("CLIENT_NOT_FOUND", status.getMessage());
5482
}
5583
} catch (Exception e) {
56-
e.printStackTrace();
84+
logger.error("Exception: ", e);
85+
throw new ClientException(e);
86+
} finally {
87+
IoUtils.safeClose(connection);
5788
}
5889
}
59-
6090
}
Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package com.networknt.oauth.client.handler;
22

3-
import com.networknt.client.Client;
3+
import com.networknt.client.Http2Client;
44
import com.networknt.config.Config;
55
import com.networknt.exception.ApiException;
66
import com.networknt.exception.ClientException;
77
import com.networknt.status.Status;
8-
import org.apache.commons.io.IOUtils;
9-
import org.apache.http.client.methods.CloseableHttpResponse;
10-
import org.apache.http.client.methods.HttpGet;
11-
import org.apache.http.impl.client.CloseableHttpClient;
8+
import io.undertow.client.ClientConnection;
9+
import io.undertow.client.ClientRequest;
10+
import io.undertow.client.ClientResponse;
11+
import io.undertow.util.Methods;
1212
import org.junit.Assert;
1313
import org.junit.ClassRule;
1414
import org.junit.Test;
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
17+
import org.xnio.IoUtils;
18+
import org.xnio.OptionMap;
19+
20+
import java.net.URI;
21+
import java.util.concurrent.CountDownLatch;
22+
import java.util.concurrent.atomic.AtomicReference;
1723

1824
/**
1925
* Generated by swagger-codegen
@@ -26,26 +32,50 @@ public class Oauth2ClientClientIdGetHandlerTest {
2632

2733
@Test
2834
public void testOauth2ClientClientIdGetHandler() throws ClientException, ApiException {
29-
CloseableHttpClient client = Client.getInstance().getSyncClient();
30-
HttpGet httpGet = new HttpGet("http://localhost:6884/oauth2/client/f7d42348-c647-4efb-a52d-4c5787421e72");
35+
final Http2Client client = Http2Client.getInstance();
36+
final CountDownLatch latch = new CountDownLatch(1);
37+
final ClientConnection connection;
3138
try {
32-
CloseableHttpResponse response = client.execute(httpGet);
33-
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
34-
String body = IOUtils.toString(response.getEntity().getContent(), "utf8");
35-
Assert.assertNotNull(body);
39+
connection = client.connect(new URI("http://localhost:6884"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
40+
} catch (Exception e) {
41+
throw new ClientException(e);
42+
}
43+
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
44+
try {
45+
ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath("/oauth2/client/f7d42348-c647-4efb-a52d-4c5787421e72");
46+
connection.sendRequest(request, client.createClientCallback(reference, latch));
47+
latch.await();
3648
} catch (Exception e) {
37-
e.printStackTrace();
49+
logger.error("Exception: ", e);
50+
throw new ClientException(e);
51+
} finally {
52+
IoUtils.safeClose(connection);
53+
}
54+
int statusCode = reference.get().getResponseCode();
55+
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
56+
Assert.assertEquals(200, statusCode);
57+
if(statusCode == 200) {
58+
Assert.assertNotNull(body);
3859
}
3960
}
4061

4162
@Test
4263
public void testClientNotFound() throws ClientException, ApiException {
43-
CloseableHttpClient client = Client.getInstance().getSyncClient();
44-
HttpGet httpGet = new HttpGet("http://localhost:6884/oauth2/client/fake");
64+
final Http2Client client = Http2Client.getInstance();
65+
final CountDownLatch latch = new CountDownLatch(1);
66+
final ClientConnection connection;
4567
try {
46-
CloseableHttpResponse response = client.execute(httpGet);
47-
int statusCode = response.getStatusLine().getStatusCode();
48-
String body = IOUtils.toString(response.getEntity().getContent(), "utf8");
68+
connection = client.connect(new URI("http://localhost:6884"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
69+
} catch (Exception e) {
70+
throw new ClientException(e);
71+
}
72+
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
73+
try {
74+
ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath("/oauth2/client/fake");
75+
connection.sendRequest(request, client.createClientCallback(reference, latch));
76+
latch.await();
77+
int statusCode = reference.get().getResponseCode();
78+
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
4979
Assert.assertEquals(404, statusCode);
5080
if(statusCode == 404) {
5181
Status status = Config.getInstance().getMapper().readValue(body, Status.class);
@@ -54,8 +84,10 @@ public void testClientNotFound() throws ClientException, ApiException {
5484
Assert.assertEquals("CLIENT_NOT_FOUND", status.getMessage());
5585
}
5686
} catch (Exception e) {
57-
e.printStackTrace();
87+
logger.error("Exception: ", e);
88+
throw new ClientException(e);
89+
} finally {
90+
IoUtils.safeClose(connection);
5891
}
5992
}
60-
6193
}

0 commit comments

Comments
 (0)