Commit ae92bfbd by Eli Ben Baruch

## 1.2.3:

 - add "mavenLocal", for local repository. enables to check this dependency locally
 - Handle correctly Exceptions in http requsts/responses
 - support Encoding type (other than utf8) in http responses
parent 6f707c20
### general utils
## 1.2.3:
- add "mavenLocal", for local repository. enables to check this dependency locally
- Handle correctly Exceptions in http requsts/responses
- support Encoding type (other than utf8) in http responses
group 'com.ipgallery.common'
version '1.1.2'
version '1.1.3'
apply plugin: 'java'
apply plugin: 'maven-publish'
//for mavenLocal
//apply plugin: 'maven'
sourceCompatibility = 1.8
repositories {
//mavenCentral()
//use mavenLocal in cases you want to create this jar on your local machine
//or to be able to use one
//mavenLocal()
maven { url "http://172.16.1.132:8081/repository/internal" }
maven { url "http://mandubian-mvn.googlecode.com/svn/trunk/mandubian-mvn/repository" }
}
......@@ -27,6 +33,18 @@ dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
//1. use this install task (under "other" section)to create this jar localy on your machine.
// version will be extended with the Postfix "-LOCAL"
//2. in order to use this jar in a project:
// in the build.gradle add "mavenLocal()" as first repository
//and the dependency with correct version number ended with "-LOCAL"
//install {
// repositories.mavenInstaller {
// pom.version = version + '-LOCAL'
// }
//}
publishing {
publications {
......
......@@ -71,11 +71,14 @@ public class SimpleHttpClient {
if (resultResp != null) {
response = ParseResponseAndCookies(resultResp, defaulthttpClient.getCookieStore());
return response;
}else
{
return new SimpleHttpResponse(500, "request execution failed");
}
} catch (Exception e) {
return new SimpleHttpResponse(500, e.toString());
}
return null;
}
public SimpleHttpResponse ParseResponseAndCookies(org.apache.http.HttpResponse response, CookieStore cookieStore) {
......@@ -99,7 +102,17 @@ public class SimpleHttpClient {
try {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
BufferedReader rd;
Header contentEncoding = response.getEntity().getContentEncoding();
String contentEncodingValue = null;
if (contentEncoding != null)
contentEncodingValue = contentEncoding.getValue();
if (contentEncodingValue!= null)
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
contentEncodingValue));
else
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
......
......@@ -178,7 +178,7 @@ public class SimpleHttpRequest {
return getRequest;
}
protected HttpUriRequest buildPostRequest() {
protected HttpUriRequest buildPostRequest() throws UnsupportedEncodingException {
// build URI
StringBuilder sb = new StringBuilder();
......@@ -206,19 +206,15 @@ public class SimpleHttpRequest {
// add content
if (content != null) {
try {
StringEntity contentEntity = new StringEntity(content,"UTF-8");
contentEntity.setContentType(contentType);
postRequest.setEntity(contentEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return postRequest;
}
protected HttpUriRequest buildPutRequest() {
protected HttpUriRequest buildPutRequest() throws UnsupportedEncodingException {
// build URI
StringBuilder sb = new StringBuilder();
......@@ -246,14 +242,10 @@ public class SimpleHttpRequest {
// add content
if (content != null) {
try {
StringEntity contentEntity = new StringEntity(content);
//contentEntity.setContentType("application/json");
contentEntity.setContentType(contentType);
putRequest.setEntity(contentEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return putRequest;
......
......@@ -27,4 +27,23 @@ public class SimpleHttpResponse {
public void setHeaders(Map<String, String> headers) { this.headers = headers; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
@Override
public String toString() {
StringBuilder respString;
try {
respString = new StringBuilder("statusCode=").append(statusCode).append(" content=");
if (content == null)
respString.append("null");
else
respString.append(content);
}
catch (Exception e){
return super.toString();
}
return respString.toString();
}
}
......@@ -18,7 +18,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonParser;
import com.google.gdata.util.common.base.PercentEscaper;
public class SimpleRestClient extends SimpleHttpClient {
public class SimpleRestClient extends SimpleHttpClient {
private static final String EMPTY_RESPONSE = "Empty response";
......@@ -90,15 +90,18 @@ import com.google.gdata.util.common.base.PercentEscaper;
SimpleHttpRequest request = buildGetRequest(entity, params, requestParams);
try {
SimpleHttpResponse httpResp = processRequest(request);
return buildResp(httpResp);
} catch (UnsupportedEncodingException e) {
return new SimpleRestResponse(false, e.toString(),null);
}
}
private SimpleRestResponse buildResp(SimpleHttpResponse httpResp) {
if(httpResp.getStatusCode() == 200)
return getResponseHandler(httpResp.getContent());
else
return new SimpleRestResponse(false, httpResp.getContent(),null);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
return new SimpleRestResponse(false, httpResp.toString(),null);
}
public StringResponse getAsString(String entity, String[] params, String requestParams) {
......@@ -122,10 +125,9 @@ import com.google.gdata.util.common.base.PercentEscaper;
if (resp != null)
return new StringResponse(resp.getStatusCode(), resp.getContent());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new StringResponse(500, "Failed to process request(GET). Exception: "+e.toString());
}
return null;
return new StringResponse(500, "Failed to process the request(GET)");
}
/******************************************************************************
......@@ -152,15 +154,10 @@ import com.google.gdata.util.common.base.PercentEscaper;
SimpleHttpRequest request = buildPostRequest(entity, params, requestParams, content);
try {
SimpleHttpResponse httpResp = processRequest(request);
if(httpResp.getStatusCode() == 200)
return getResponseHandler(httpResp.getContent());
else
return new SimpleRestResponse(false, httpResp.getContent(),null);
return buildResp(httpResp);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new SimpleRestResponse(false, e.toString(),null);
}
return null;
}
public String postAsString(String entity, String[] params, String requestParams, String content) {
......@@ -214,15 +211,10 @@ import com.google.gdata.util.common.base.PercentEscaper;
SimpleHttpRequest request = buildPutRequest(entity, params, requestParams, content);
try {
SimpleHttpResponse httpResp = processRequest(request);
if(httpResp.getStatusCode() == 200)
return getResponseHandler(httpResp.getContent());
else
return new SimpleRestResponse(false, httpResp.getContent(),null);
return buildResp(httpResp);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new SimpleRestResponse(false, e.toString(),null);
}
return null;
}
/******************************************************************************
*
......@@ -247,15 +239,10 @@ import com.google.gdata.util.common.base.PercentEscaper;
SimpleHttpRequest request = buildDeleteRequest(entity, params, requestParams);
try {
SimpleHttpResponse httpResp = processRequest(request);
if(httpResp.getStatusCode() == 200)
return getResponseHandler(httpResp.getContent());
else
return new SimpleRestResponse(false, httpResp.getContent(),null);
return buildResp(httpResp);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new SimpleRestResponse(false, e.toString(),null);
}
return null;
}
public String deleteAsString(String entity, String[] params, String requestParams) {
......
import http.simpleRestClient.SimpleRestClient;
import http.simpleRestClient.SimpleRestResponse;
import org.junit.Test;
/**
* Created by eli on 7/26/16.
*/
public class TestSimpleRestClient {
@Test
public void testSimpleRestClient(){
SimpleRestClient restClient = new SimpleRestClient("mde", "172.16.1.56:50040");
SimpleRestResponse resp;
restClient.Initialize(100);
//wrong params to get 404
resp = restClient.get("\"/api/v1/\"", "chicago/transportation/routes", "key=gT2nciTKwRv6Jy5njqm8fe7LW");
System.out.println("resp for get is: "+resp.toString());
resp = restClient.post("\"/api/v1/\"", "chicago/transportation/routes", "key=gT2nciTKwRv6Jy5njqm8fe7LW",null);
System.out.println("resp for post is: "+resp.toString());
resp = restClient.get("/api/v1/", "chicago/transportation/routes", "key=gT2nciTKwRv6Jy5njqm8fe7LW");
System.out.println("resp for get is: "+resp.toString());
//domain for http tests http://httpbin.org/post
restClient = new SimpleRestClient("post", "httpbin.org:80");
restClient.Initialize(100);
resp = restClient.post(null, (String)null, null,null);
System.out.println("resp for get is: "+resp.toString());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment