Commit 9b81b2a1 by Adi Amir

support PATCH method in http request

parent 745930e3
group 'com.ipgallery.common'
version '1.1.5'
version '1.2.0'
apply plugin: 'java'
apply plugin: 'maven-publish'
......@@ -13,7 +13,7 @@ repositories {
//mavenCentral()
//use mavenLocal in cases you want to create this jar on your local machine
//or to be able to use one
//mavenLocal()
mavenLocal()
maven { url "http://172.16.1.132:8081/repository/internal" }
maven { url "http://mandubian-mvn.googlecode.com/svn/trunk/mandubian-mvn/repository" }
}
......@@ -25,7 +25,7 @@ dependencies {
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3'
compile 'com.googlecode.libphonenumber:libphonenumber:5.8'
compile group: 'com.google.gdata', name: 'core', version: '1.47.1'
compile 'org.apache.httpcomponents:httpclient:4.1'
compile 'org.apache.httpcomponents:httpclient:4.5.2'
compile 'org.apache.httpcomponents:httpmime:4.1'
compile 'javax.ws.rs:javax.ws.rs-api:2.0'
compile 'com.ipgallery.common:itc:1.0.0'
......
......@@ -7,18 +7,14 @@ import java.util.List;
//import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
//import javax.ws.rs.core.HttpHeaders;
//import javax.ws.rs.core.MediaType;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.*;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
......@@ -44,11 +40,14 @@ public class SimpleHttpRequest {
return "PUT";
}
},
DELETE{
DELETE {
public String toString() {
return "DELETE";
}
},
PATCH {
public String toString() { return "PATCH"; }
}
}
protected String protocol = "http"; // default is http
protected Method method = null;
......@@ -144,6 +143,8 @@ public class SimpleHttpRequest {
return buildPutRequest();
case DELETE:
return buildDeleteRequest();
case PATCH:
return buildPatchRequest();
}
return null;
}
......@@ -178,7 +179,7 @@ public class SimpleHttpRequest {
return getRequest;
}
protected HttpUriRequest buildPostRequest() throws UnsupportedEncodingException {
protected HttpUriRequest buildPostRequest() {
// build URI
StringBuilder sb = new StringBuilder();
......@@ -206,15 +207,20 @@ public class SimpleHttpRequest {
// add content
if (content != null) {
try {
StringEntity contentEntity = new StringEntity(content,"UTF-8");
contentEntity.setContentType(contentType);
postRequest.setEntity(contentEntity);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return postRequest;
}
protected HttpUriRequest buildPutRequest() throws UnsupportedEncodingException {
protected HttpUriRequest buildPutRequest() {
// build URI
StringBuilder sb = new StringBuilder();
......@@ -242,10 +248,15 @@ 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 null;
}
}
return putRequest;
......@@ -279,4 +290,45 @@ public class SimpleHttpRequest {
return deleteRequest;
}
protected HttpUriRequest buildPatchRequest() {
// build URI
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
sb.append(domain);
if (port > 0) {
sb.append(":");
sb.append(port);
}
sb.append("/");
if (path != null)
sb.append(path);
if (queryString != null) {
sb.append("?");
sb.append(queryString);
}
String uri = sb.toString();
HttpPatch PatchRequest = new HttpPatch(uri);
// add headers
for(Entry<String, String> hdr : headers.entrySet()) {
PatchRequest.addHeader(hdr.getKey(), hdr.getValue());
}
// add content
if (content != null) {
try {
StringEntity contentEntity = new StringEntity(content,"UTF-8");
contentEntity.setContentType(contentType);
PatchRequest.setEntity(contentEntity);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return PatchRequest;
}
}
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