You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by ro...@apache.org on 2021/04/07 19:16:29 UTC
[axis-axis2-java-core] branch master updated: AXIS2-5959 replace
org.apache.commons.httpclient with org.apache.http in the json samples and
unit tests
This is an automated email from the ASF dual-hosted git repository.
robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-java-core.git
The following commit(s) were added to refs/heads/master by this push:
new 91be03f AXIS2-5959 replace org.apache.commons.httpclient with org.apache.http in the json samples and unit tests
91be03f is described below
commit 91be03fe100c63ea7e611be08aa8510c3b29abfa
Author: robert lazarski <ro...@apache.org>
AuthorDate: Wed Apr 7 09:15:48 2021 -1000
AXIS2-5959 replace org.apache.commons.httpclient with org.apache.http in the json samples and unit tests
---
.../axis2/json/gson/JSONXMLStreamAPITest.java | 6 +-
.../test/org/apache/axis2/json/gson/UtilTest.java | 36 ++++++++----
.../json/gson/rpc/JSONRPCIntegrationTest.java | 7 +--
.../json/src/sample/json/client/JsonClient.java | 64 +++++++++++++---------
4 files changed, 66 insertions(+), 47 deletions(-)
diff --git a/modules/json/test/org/apache/axis2/json/gson/JSONXMLStreamAPITest.java b/modules/json/test/org/apache/axis2/json/gson/JSONXMLStreamAPITest.java
index bb11e97..348ab23 100644
--- a/modules/json/test/org/apache/axis2/json/gson/JSONXMLStreamAPITest.java
+++ b/modules/json/test/org/apache/axis2/json/gson/JSONXMLStreamAPITest.java
@@ -32,8 +32,6 @@ public class JSONXMLStreamAPITest {
public void xmlStreamAPITest()throws Exception{
String getLibURL = server.getEndpoint("LibraryService") + "getLibrary";
String echoLibURL = server.getEndpoint("LibraryService") + "echoLibrary";
- String contentType = "application/json";
- String charSet = "UTF-8";
String echoLibrary = "{\"echoLibrary\":{\"args0\":{\"admin\":{\"address\":{\"city\":\"My City\",\"country\":" +
"\"My Country\",\"street\":\"My Street\",\"zipCode\":\"00000\"},\"age\":24,\"name\":\"micheal\"," +
@@ -67,11 +65,11 @@ public class JSONXMLStreamAPITest {
"{\"author\":\"Jhon_4\",\"numOfPages\":175,\"publisher\":\"Foxier\",\"reviewers\":[\"rev1\",\"rev2\"," +
"\"rev3\"]}],\"staff\":50}}}";
- String actualResponse = UtilTest.post(echoLibrary, echoLibURL, contentType, charSet);
+ String actualResponse = UtilTest.post(echoLibrary, echoLibURL);
Assert.assertNotNull(actualResponse);
Assert.assertEquals(echoLibraryResponse , actualResponse);
- String actualRespose_2 = UtilTest.post(getLibrary, getLibURL, contentType, charSet);
+ String actualRespose_2 = UtilTest.post(getLibrary, getLibURL);
Assert.assertNotNull(actualRespose_2);
Assert.assertEquals(getLibraryResponse, actualRespose_2);
diff --git a/modules/json/test/org/apache/axis2/json/gson/UtilTest.java b/modules/json/test/org/apache/axis2/json/gson/UtilTest.java
index ac1df45..c70937b 100644
--- a/modules/json/test/org/apache/axis2/json/gson/UtilTest.java
+++ b/modules/json/test/org/apache/axis2/json/gson/UtilTest.java
@@ -19,26 +19,38 @@
package org.apache.axis2.json.gson;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.RequestEntity;
-import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.HttpEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class UtilTest {
- public static String post(String jsonString, String strURL, String contentType, String charSet)
+ public static String post(String jsonString, String strURL)
throws IOException {
- PostMethod post = new PostMethod(strURL);
- RequestEntity entity = new StringRequestEntity(jsonString, contentType, charSet);
- post.setRequestEntity(entity);
- HttpClient httpclient = new HttpClient();
+ HttpEntity stringEntity = new StringEntity(jsonString,ContentType.APPLICATION_JSON);
+ HttpPost httpPost = new HttpPost(strURL);
+ httpPost.setEntity(stringEntity);
+ CloseableHttpClient httpclient = HttpClients.createDefault();
+
try {
- int result = httpclient.executeMethod(post);
- return post.getResponseBodyAsString();
+ CloseableHttpResponse response = httpclient.execute(httpPost);
+ int status = response.getStatusLine().getStatusCode();
+ if (status >= 200 && status < 300) {
+ HttpEntity entity = response.getEntity();
+ return entity != null ? EntityUtils.toString(entity,"UTF-8") : null;
+ } else {
+ throw new ClientProtocolException("Unexpected response status: " + status);
+ }
}finally {
- post.releaseConnection();
+ httpclient.close();
}
}
}
diff --git a/modules/json/test/org/apache/axis2/json/gson/rpc/JSONRPCIntegrationTest.java b/modules/json/test/org/apache/axis2/json/gson/rpc/JSONRPCIntegrationTest.java
index f5566c3..1b364ea 100644
--- a/modules/json/test/org/apache/axis2/json/gson/rpc/JSONRPCIntegrationTest.java
+++ b/modules/json/test/org/apache/axis2/json/gson/rpc/JSONRPCIntegrationTest.java
@@ -29,15 +29,12 @@ public class JSONRPCIntegrationTest {
@ClassRule
public static Axis2Server server = new Axis2Server("target/repo/gson");
- String contentType = "application/json";
- String charSet = "UTF-8";
-
@Test
public void testJsonRpcMessageReceiver() throws Exception {
String jsonRequest = "{\"echoPerson\":[{\"arg0\":{\"name\":\"Simon\",\"age\":\"35\",\"gender\":\"male\"}}]}";
String echoPersonUrl = server.getEndpoint("JSONPOJOService") + "echoPerson";
String expectedResponse = "{\"response\":{\"name\":\"Simon\",\"age\":\"35\",\"gender\":\"male\"}}";
- String response = UtilTest.post(jsonRequest, echoPersonUrl, contentType, charSet);
+ String response = UtilTest.post(jsonRequest, echoPersonUrl);
Assert.assertNotNull(response);
Assert.assertEquals(expectedResponse , response);
}
@@ -46,7 +43,7 @@ public class JSONRPCIntegrationTest {
public void testJsonInOnlyRPCMessageReceiver() throws Exception {
String jsonRequest = "{\"ping\":[{\"arg0\":{\"name\":\"Simon\",\"age\":\"35\",\"gender\":\"male\"}}]}";
String echoPersonUrl = server.getEndpoint("JSONPOJOService") + "ping";
- String response = UtilTest.post(jsonRequest, echoPersonUrl, contentType, charSet);
+ String response = UtilTest.post(jsonRequest, echoPersonUrl);
Assert.assertEquals("", response);
}
}
diff --git a/modules/samples/json/src/sample/json/client/JsonClient.java b/modules/samples/json/src/sample/json/client/JsonClient.java
index ebd3681..c5be2a8 100644
--- a/modules/samples/json/src/sample/json/client/JsonClient.java
+++ b/modules/samples/json/src/sample/json/client/JsonClient.java
@@ -19,45 +19,57 @@
package sample.json.client;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpException;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.RequestEntity;
-import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.HttpEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
public class JsonClient{
private String url = "http://localhost:8080/axis2/services/JsonService/echoUser";
- private String contentType = "application/json";
- private String charSet = "UTF-8";
public static void main(String[] args)throws IOException {
- String echoUser = "{\"echoUser\":[{\"arg0\":{\"name\":\"My_Name\",\"surname\":\"MY_Surname\",\"middleName\":" + "\"My_MiddleName\",\"age\":123,\"address\":{\"country\":\"My_Country\",\"city\":\"My_City\",\"street\":" + "\"My_Street\",\"building\":\"My_Building\",\"flat\":\"My_Flat\",\"zipCode\":\"My_ZipCode\"}}}]}";
+
+ String echoUser = "{\"echoUser\":[{\"arg0\":{\"name\":\"My_Name\",\"surname\":\"MY_Surname\",\"middleName\":" +
+ "\"My_MiddleName\",\"age\":123,\"address\":{\"country\":\"My_Country\",\"city\":\"My_City\",\"street\":" +
+ "\"My_Street\",\"building\":\"My_Building\",\"flat\":\"My_Flat\",\"zipCode\":\"My_ZipCode\"}}}]}";
JsonClient jsonClient = new JsonClient();
- jsonClient.post(echoUser);
+ String echo = jsonClient.post(echoUser);
+ System.out.println (echo);
+
}
- public boolean post(String message) throws UnsupportedEncodingException {
- PostMethod post = new PostMethod(url);
- RequestEntity entity = new StringRequestEntity(message , contentType, charSet);
- post.setRequestEntity(entity);
- HttpClient httpclient = new HttpClient();
+ public String post(String message) throws IOException {
+
+ HttpEntity stringEntity = new StringEntity(message,ContentType.APPLICATION_JSON);
+ HttpPost httpPost = new HttpPost(url);
+ httpPost.setEntity(stringEntity);
+ CloseableHttpClient httpclient = HttpClients.createDefault();
+
try {
- int result = httpclient.executeMethod(post);
- System.out.println("Response status code: " + result);
- System.out.println("Response body: ");
- System.out.println(post.getResponseBodyAsString());
- } catch (HttpException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- post.releaseConnection();
+ CloseableHttpResponse response = httpclient.execute(httpPost);
+ HttpEntity entity = null;
+ int status = response.getStatusLine().getStatusCode();
+ if (status >= 200 && status < 300) {
+ entity = response.getEntity();
+ } else {
+ throw new ClientProtocolException("Unexpected HTTP response status: " + status);
+ }
+ if (entity == null || EntityUtils.toString(entity,"UTF-8") == null) {
+ throw new ClientProtocolException("Error connecting to url: "+url+" , unexpected response: " + EntityUtils.toString(entity,"UTF-8"));
+ }
+ return EntityUtils.toString(entity,"UTF-8");
+ }finally {
+ httpclient.close();
}
- return false;
}
+
}