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,51 +7,50 @@ 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;
import com.google.gdata.util.common.base.PercentEscaper;
public class SimpleHttpRequest {
private static PercentEscaper pes = new PercentEscaper(PercentEscaper.SAFEQUERYSTRINGCHARS_URLENCODER/*SAFECHARS_URLENCODER*/, false);
public enum Method {
GET{
public String toString() {
return "GET";
}
public String toString() {
return "GET";
}
},
POST{
public String toString() {
return "POST";
}
public String toString() {
return "POST";
}
},
PUT{
public String toString() {
return "PUT";
}
public String toString() {
return "PUT";
}
},
DELETE{
public String toString() {
return "DELETE";
}
DELETE {
public String toString() {
return "DELETE";
}
},
}
protected String protocol = "http"; // default is http
protected Method method = null;
PATCH {
public String toString() { return "PATCH"; }
}
}
protected String protocol = "http"; // default is http
protected Method method = null;
protected String domain = null;
protected int port = 0;
protected String path = null;
......@@ -61,26 +60,26 @@ public class SimpleHttpRequest {
protected String content = null;
protected List<Cookie> cookies = null;
public SimpleHttpRequest() {
public SimpleHttpRequest() {
headers = new HashMap<String, String>();
cookies = new ArrayList<Cookie>();
}
// get/set methods
public String getProtocol() { return protocol; }
public void setProtocol(String protocol) { this.protocol = protocol; }
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 void setDomain(String domain) { this.domain = domain; }
public void setDomain(String domain) { this.domain = domain; }
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 void setQueryString(String queryString) { this.queryString = EscapeQueryString(queryString); /*this.queryString = pes.escape(queryString);*/ }
public String getContentType() { return contentType;}
public void setContentType(String contentType) {this.contentType = contentType; }
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 void setPath(String path) { this.path = pes.escape(path); }
public void addHeader(String hdrName, String hdrValue) {
......@@ -101,12 +100,12 @@ public class SimpleHttpRequest {
}
public void addQueryStringParam(String name, String value) {
String escName = pes.escape(name);
String escValue = pes.escape(value);
if (!queryString.equals(""))
String escValue = pes.escape(value);
if (!queryString.equals(""))
queryString += "&";
queryString += escName + "=" + escValue;
}
private String EscapeQueryString(String queryString) {
StringBuilder sb = new StringBuilder();
String[] paramTokens = queryString.split("&");
......@@ -132,34 +131,36 @@ public class SimpleHttpRequest {
}
else
return pes.escape(queryString);
}
public HttpUriRequest build() throws UnsupportedEncodingException {
}
public HttpUriRequest build() throws UnsupportedEncodingException {
switch (method) {
case GET:
return buildGetRequest();
case POST:
return buildPostRequest();
case PUT:
return buildPutRequest();
return buildPutRequest();
case DELETE:
return buildDeleteRequest();
return buildDeleteRequest();
case PATCH:
return buildPatchRequest();
}
return null;
}
// http://domain:port/path?query_string
protected HttpUriRequest buildGetRequest() {
protected HttpUriRequest buildGetRequest() {
// build URI
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
sb.append(domain);
if (port > 0) {
sb.append(":");
sb.append(port);
}
if (path != null) {
}
if (path != null) {
sb.append("/");
sb.append(path);
}
......@@ -167,22 +168,22 @@ public class SimpleHttpRequest {
sb.append("?");
sb.append(queryString);
}
String uri = sb.toString();
HttpGet getRequest = new HttpGet(uri);
// add headers
for(Entry<String, String> hdr : headers.entrySet()) {
getRequest.addHeader(hdr.getKey(), hdr.getValue());
}
}
return getRequest;
}
protected HttpUriRequest buildPostRequest() throws UnsupportedEncodingException {
protected HttpUriRequest buildPostRequest() {
// build URI
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
sb.append(domain);
if (port > 0) {
sb.append(":");
......@@ -195,30 +196,35 @@ public class SimpleHttpRequest {
sb.append("?");
sb.append(queryString);
}
String uri = sb.toString();
HttpPost postRequest = new HttpPost(uri);
// add headers
for(Entry<String, String> hdr : headers.entrySet()) {
postRequest.addHeader(hdr.getKey(), hdr.getValue());
}
}
// add content
if (content != null) {
StringEntity contentEntity = new StringEntity(content,"UTF-8");
contentEntity.setContentType(contentType);
postRequest.setEntity(contentEntity);
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();
sb.append(protocol+"://");
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
sb.append(domain);
if (port > 0) {
sb.append(":");
......@@ -231,31 +237,36 @@ public class SimpleHttpRequest {
sb.append("?");
sb.append(queryString);
}
String uri = sb.toString();
HttpPut putRequest = new HttpPut(uri);
// add headers
for(Entry<String, String> hdr : headers.entrySet()) {
putRequest.addHeader(hdr.getKey(), hdr.getValue());
}
}
// 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;
}
protected HttpUriRequest buildDeleteRequest() {
// build URI
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
sb.append(domain);
if (port > 0) {
sb.append(":");
......@@ -268,15 +279,56 @@ public class SimpleHttpRequest {
sb.append("?");
sb.append(queryString);
}
String uri = sb.toString();
HttpDelete deleteRequest = new HttpDelete(uri);
// add headers
for(Entry<String, String> hdr : headers.entrySet()) {
deleteRequest.addHeader(hdr.getKey(), hdr.getValue());
}
}
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