Commit 9b81b2a1 by Adi Amir

support PATCH method in http request

parent 745930e3
group 'com.ipgallery.common' group 'com.ipgallery.common'
version '1.1.5' version '1.2.0'
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'maven-publish' apply plugin: 'maven-publish'
...@@ -13,7 +13,7 @@ repositories { ...@@ -13,7 +13,7 @@ repositories {
//mavenCentral() //mavenCentral()
//use mavenLocal in cases you want to create this jar on your local machine //use mavenLocal in cases you want to create this jar on your local machine
//or to be able to use one //or to be able to use one
//mavenLocal() mavenLocal()
maven { url "http://172.16.1.132:8081/repository/internal" } maven { url "http://172.16.1.132:8081/repository/internal" }
maven { url "http://mandubian-mvn.googlecode.com/svn/trunk/mandubian-mvn/repository" } maven { url "http://mandubian-mvn.googlecode.com/svn/trunk/mandubian-mvn/repository" }
} }
...@@ -25,7 +25,7 @@ dependencies { ...@@ -25,7 +25,7 @@ dependencies {
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3' compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3'
compile 'com.googlecode.libphonenumber:libphonenumber:5.8' compile 'com.googlecode.libphonenumber:libphonenumber:5.8'
compile group: 'com.google.gdata', name: 'core', version: '1.47.1' 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 'org.apache.httpcomponents:httpmime:4.1'
compile 'javax.ws.rs:javax.ws.rs-api:2.0' compile 'javax.ws.rs:javax.ws.rs-api:2.0'
compile 'com.ipgallery.common:itc:1.0.0' compile 'com.ipgallery.common:itc:1.0.0'
......
...@@ -7,51 +7,50 @@ import java.util.List; ...@@ -7,51 +7,50 @@ import java.util.List;
//import java.util.Iterator; //import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
//import javax.ws.rs.core.HttpHeaders; //import javax.ws.rs.core.HttpHeaders;
//import javax.ws.rs.core.MediaType; //import javax.ws.rs.core.MediaType;
import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.*;
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.cookie.Cookie; import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import com.google.gdata.util.common.base.PercentEscaper; import com.google.gdata.util.common.base.PercentEscaper;
public class SimpleHttpRequest { public class SimpleHttpRequest {
private static PercentEscaper pes = new PercentEscaper(PercentEscaper.SAFEQUERYSTRINGCHARS_URLENCODER/*SAFECHARS_URLENCODER*/, false); private static PercentEscaper pes = new PercentEscaper(PercentEscaper.SAFEQUERYSTRINGCHARS_URLENCODER/*SAFECHARS_URLENCODER*/, false);
public enum Method { public enum Method {
GET{ GET{
public String toString() { public String toString() {
return "GET"; return "GET";
} }
}, },
POST{ POST{
public String toString() { public String toString() {
return "POST"; return "POST";
} }
}, },
PUT{ PUT{
public String toString() { public String toString() {
return "PUT"; return "PUT";
} }
}, },
DELETE{ DELETE {
public String toString() { public String toString() {
return "DELETE"; return "DELETE";
} }
}, },
} PATCH {
protected String protocol = "http"; // default is http public String toString() { return "PATCH"; }
protected Method method = null; }
}
protected String protocol = "http"; // default is http
protected Method method = null;
protected String domain = null; protected String domain = null;
protected int port = 0; protected int port = 0;
protected String path = null; protected String path = null;
...@@ -61,26 +60,26 @@ public class SimpleHttpRequest { ...@@ -61,26 +60,26 @@ public class SimpleHttpRequest {
protected String content = null; protected String content = null;
protected List<Cookie> cookies = null; protected List<Cookie> cookies = null;
public SimpleHttpRequest() { public SimpleHttpRequest() {
headers = new HashMap<String, String>(); headers = new HashMap<String, String>();
cookies = new ArrayList<Cookie>(); cookies = new ArrayList<Cookie>();
} }
// get/set methods // get/set methods
public String getProtocol() { return protocol; } public String getProtocol() { return protocol; }
public void setProtocol(String protocol) { this.protocol = protocol; } public void setProtocol(String protocol) { this.protocol = protocol; }
public Method getMethod() { return method; } public Method getMethod() { return method; }
public void setMethod(Method method) { this.method = method; } public void setMethod(Method method) { this.method = method; }
public String getDomain() { return domain; } public String getDomain() { return domain; }
public void setDomain(String domain) { this.domain = domain; } public void setDomain(String domain) { this.domain = domain; }
public int getPort() { return port; } public int getPort() { return port; }
public void setPort(int port) { this.port = port; } public void setPort(int port) { this.port = port; }
public String getQueryString() { return queryString; } public String getQueryString() { return queryString; }
public void setQueryString(String queryString) { this.queryString = EscapeQueryString(queryString); /*this.queryString = pes.escape(queryString);*/ } public void setQueryString(String queryString) { this.queryString = EscapeQueryString(queryString); /*this.queryString = pes.escape(queryString);*/ }
public String getContentType() { return contentType;} public String getContentType() { return contentType;}
public void setContentType(String contentType) {this.contentType = contentType; } public void setContentType(String contentType) {this.contentType = contentType; }
public String getContent() { return content; } public String getContent() { return content; }
public void setContent(String content) { this.content = content; } public void setContent(String content) { this.content = content; }
public String getPath() { return path; } public String getPath() { return path; }
public void setPath(String path) { this.path = pes.escape(path); } public void setPath(String path) { this.path = pes.escape(path); }
public void addHeader(String hdrName, String hdrValue) { public void addHeader(String hdrName, String hdrValue) {
...@@ -101,12 +100,12 @@ public class SimpleHttpRequest { ...@@ -101,12 +100,12 @@ public class SimpleHttpRequest {
} }
public void addQueryStringParam(String name, String value) { public void addQueryStringParam(String name, String value) {
String escName = pes.escape(name); String escName = pes.escape(name);
String escValue = pes.escape(value); String escValue = pes.escape(value);
if (!queryString.equals("")) if (!queryString.equals(""))
queryString += "&"; queryString += "&";
queryString += escName + "=" + escValue; queryString += escName + "=" + escValue;
} }
private String EscapeQueryString(String queryString) { private String EscapeQueryString(String queryString) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String[] paramTokens = queryString.split("&"); String[] paramTokens = queryString.split("&");
...@@ -132,34 +131,36 @@ public class SimpleHttpRequest { ...@@ -132,34 +131,36 @@ public class SimpleHttpRequest {
} }
else else
return pes.escape(queryString); return pes.escape(queryString);
} }
public HttpUriRequest build() throws UnsupportedEncodingException { public HttpUriRequest build() throws UnsupportedEncodingException {
switch (method) { switch (method) {
case GET: case GET:
return buildGetRequest(); return buildGetRequest();
case POST: case POST:
return buildPostRequest(); return buildPostRequest();
case PUT: case PUT:
return buildPutRequest(); return buildPutRequest();
case DELETE: case DELETE:
return buildDeleteRequest(); return buildDeleteRequest();
case PATCH:
return buildPatchRequest();
} }
return null; return null;
} }
// http://domain:port/path?query_string // http://domain:port/path?query_string
protected HttpUriRequest buildGetRequest() { protected HttpUriRequest buildGetRequest() {
// build URI // build URI
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(protocol+"://"); sb.append(protocol+"://");
sb.append(domain); sb.append(domain);
if (port > 0) { if (port > 0) {
sb.append(":"); sb.append(":");
sb.append(port); sb.append(port);
} }
if (path != null) { if (path != null) {
sb.append("/"); sb.append("/");
sb.append(path); sb.append(path);
} }
...@@ -167,22 +168,22 @@ public class SimpleHttpRequest { ...@@ -167,22 +168,22 @@ public class SimpleHttpRequest {
sb.append("?"); sb.append("?");
sb.append(queryString); sb.append(queryString);
} }
String uri = sb.toString(); String uri = sb.toString();
HttpGet getRequest = new HttpGet(uri); HttpGet getRequest = new HttpGet(uri);
// add headers // add headers
for(Entry<String, String> hdr : headers.entrySet()) { for(Entry<String, String> hdr : headers.entrySet()) {
getRequest.addHeader(hdr.getKey(), hdr.getValue()); getRequest.addHeader(hdr.getKey(), hdr.getValue());
} }
return getRequest; return getRequest;
} }
protected HttpUriRequest buildPostRequest() throws UnsupportedEncodingException { protected HttpUriRequest buildPostRequest() {
// build URI // build URI
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(protocol+"://"); sb.append(protocol+"://");
sb.append(domain); sb.append(domain);
if (port > 0) { if (port > 0) {
sb.append(":"); sb.append(":");
...@@ -195,30 +196,35 @@ public class SimpleHttpRequest { ...@@ -195,30 +196,35 @@ public class SimpleHttpRequest {
sb.append("?"); sb.append("?");
sb.append(queryString); sb.append(queryString);
} }
String uri = sb.toString(); String uri = sb.toString();
HttpPost postRequest = new HttpPost(uri); HttpPost postRequest = new HttpPost(uri);
// add headers // add headers
for(Entry<String, String> hdr : headers.entrySet()) { for(Entry<String, String> hdr : headers.entrySet()) {
postRequest.addHeader(hdr.getKey(), hdr.getValue()); postRequest.addHeader(hdr.getKey(), hdr.getValue());
} }
// add content // add content
if (content != null) { if (content != null) {
StringEntity contentEntity = new StringEntity(content,"UTF-8"); try {
contentEntity.setContentType(contentType); StringEntity contentEntity = new StringEntity(content,"UTF-8");
postRequest.setEntity(contentEntity); contentEntity.setContentType(contentType);
postRequest.setEntity(contentEntity);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} }
return postRequest; return postRequest;
} }
protected HttpUriRequest buildPutRequest() throws UnsupportedEncodingException { protected HttpUriRequest buildPutRequest() {
// build URI // build URI
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(protocol+"://"); sb.append(protocol+"://");
sb.append(domain); sb.append(domain);
if (port > 0) { if (port > 0) {
sb.append(":"); sb.append(":");
...@@ -231,31 +237,36 @@ public class SimpleHttpRequest { ...@@ -231,31 +237,36 @@ public class SimpleHttpRequest {
sb.append("?"); sb.append("?");
sb.append(queryString); sb.append(queryString);
} }
String uri = sb.toString(); String uri = sb.toString();
HttpPut putRequest = new HttpPut(uri); HttpPut putRequest = new HttpPut(uri);
// add headers // add headers
for(Entry<String, String> hdr : headers.entrySet()) { for(Entry<String, String> hdr : headers.entrySet()) {
putRequest.addHeader(hdr.getKey(), hdr.getValue()); putRequest.addHeader(hdr.getKey(), hdr.getValue());
} }
// add content // add content
if (content != null) { if (content != null) {
try {
StringEntity contentEntity = new StringEntity(content); StringEntity contentEntity = new StringEntity(content);
//contentEntity.setContentType("application/json"); //contentEntity.setContentType("application/json");
contentEntity.setContentType(contentType); contentEntity.setContentType(contentType);
putRequest.setEntity(contentEntity); putRequest.setEntity(contentEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
} }
return putRequest; return putRequest;
} }
protected HttpUriRequest buildDeleteRequest() { protected HttpUriRequest buildDeleteRequest() {
// build URI // build URI
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(protocol+"://"); sb.append(protocol+"://");
sb.append(domain); sb.append(domain);
if (port > 0) { if (port > 0) {
sb.append(":"); sb.append(":");
...@@ -268,15 +279,56 @@ public class SimpleHttpRequest { ...@@ -268,15 +279,56 @@ public class SimpleHttpRequest {
sb.append("?"); sb.append("?");
sb.append(queryString); sb.append(queryString);
} }
String uri = sb.toString(); String uri = sb.toString();
HttpDelete deleteRequest = new HttpDelete(uri); HttpDelete deleteRequest = new HttpDelete(uri);
// add headers // add headers
for(Entry<String, String> hdr : headers.entrySet()) { for(Entry<String, String> hdr : headers.entrySet()) {
deleteRequest.addHeader(hdr.getKey(), hdr.getValue()); deleteRequest.addHeader(hdr.getKey(), hdr.getValue());
} }
return deleteRequest; 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