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 {
......
......@@ -49,13 +49,13 @@ public class SimpleHttpClient {
public void shutdown() throws Throwable
{
defaulthttpClient.getConnectionManager().shutdown();
}
}
public SimpleHttpResponse processRequest(SimpleHttpRequest request) throws UnsupportedEncodingException {
HttpUriRequest uriRequest = request.build();
org.apache.http.HttpResponse resultResp = null;
SimpleHttpResponse response = null;
SimpleHttpResponse response = null;
try {
// add cookies
if (request.getCookies().size() > 0) {
......@@ -63,19 +63,22 @@ public class SimpleHttpClient {
cookieStore.addCookie(cookie);
}
}
defaulthttpClient.setCookieStore(cookieStore);
defaulthttpClient.setCookieStore(cookieStore);
// send the request
resultResp = defaulthttpClient.execute(uriRequest);
if (resultResp != null) {
response = ParseResponseAndCookies(resultResp, defaulthttpClient.getCookieStore());
return response;
}
} catch (Exception e) {
}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();
}
}
StringEntity contentEntity = new StringEntity(content,"UTF-8");
contentEntity.setContentType(contentType);
postRequest.setEntity(contentEntity);
}
return postRequest;
}
protected HttpUriRequest buildPutRequest() {
protected HttpUriRequest buildPutRequest() throws UnsupportedEncodingException {
// build URI
StringBuilder sb = new StringBuilder();
......@@ -246,15 +242,11 @@ 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;
}
......
......@@ -26,5 +26,24 @@ public class SimpleHttpResponse {
public Map<String, String> getHeaders() { return headers; }
public void setHeaders(Map<String, String> headers) { this.headers = headers; }
public String getContent() { return content; }
public void setContent(String content) { this.content = 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();
}
}
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