Commit dfa3aa0b by Adi Amir

support binary content in response

parent 668d3a7b
...@@ -5,3 +5,5 @@ ...@@ -5,3 +5,5 @@
- add "mavenLocal", for local repository. enables to check this dependency locally - add "mavenLocal", for local repository. enables to check this dependency locally
- Handle correctly Exceptions in http requsts/responses - Handle correctly Exceptions in http requsts/responses
- support Encoding type (other than utf8) in http responses - support Encoding type (other than utf8) in http responses
## 1.2.4
- support binary content in response
\ No newline at end of file
group 'com.ipgallery.common' group 'com.ipgallery.common'
version '1.2.0' version '1.2.4'
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'maven-publish' apply plugin: 'maven-publish'
......
package http.simpleHttpClient; package http.simpleHttpClient;
import com.google.gdata.util.common.util.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.CookieStore; import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.PlainSocketFactory;
...@@ -12,12 +15,10 @@ import org.apache.http.cookie.Cookie; ...@@ -12,12 +15,10 @@ import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import java.io.BufferedReader; import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
//import javax.ws.rs.core.HttpHeaders; //import javax.ws.rs.core.HttpHeaders;
//import javax.ws.rs.core.MediaType; //import javax.ws.rs.core.MediaType;
...@@ -28,8 +29,12 @@ public class SimpleHttpClient { ...@@ -28,8 +29,12 @@ public class SimpleHttpClient {
private DefaultHttpClient defaulthttpClient = null; private DefaultHttpClient defaulthttpClient = null;
private ThreadSafeClientConnManager connManager = null; private ThreadSafeClientConnManager connManager = null;
private CookieStore cookieStore = null; private CookieStore cookieStore = null;
private Map<String, Boolean> binaryMimeTypes = new HashMap<String, Boolean>();
public SimpleHttpClient() public SimpleHttpClient()
{ {
setDefaultBinaryMimeTypes();
} }
public void Initialize(int maxConnection) public void Initialize(int maxConnection)
...@@ -83,6 +88,7 @@ public class SimpleHttpClient { ...@@ -83,6 +88,7 @@ public class SimpleHttpClient {
public SimpleHttpResponse ParseResponseAndCookies(org.apache.http.HttpResponse response, CookieStore cookieStore) { public SimpleHttpResponse ParseResponseAndCookies(org.apache.http.HttpResponse response, CookieStore cookieStore) {
SimpleHttpResponse resp = new SimpleHttpResponse(); SimpleHttpResponse resp = new SimpleHttpResponse();
String content = null;
// get status code // get status code
resp.setStatusCode(response.getStatusLine().getStatusCode()); resp.setStatusCode(response.getStatusLine().getStatusCode());
...@@ -102,31 +108,79 @@ public class SimpleHttpClient { ...@@ -102,31 +108,79 @@ public class SimpleHttpClient {
try { try {
HttpEntity httpEntity = response.getEntity(); HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) { if (httpEntity != null) {
BufferedReader rd; if (isContentBinary(response)) {
Header contentEncoding = response.getEntity().getContentEncoding(); // content is binary
String contentEncodingValue = null; InputStream is = httpEntity.getContent();
if (contentEncoding != null) byte[] baContent = IOUtils.toByteArray(is);
contentEncodingValue = contentEncoding.getValue(); content = Base64.encode(baContent);
if (contentEncodingValue!= null) }
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), else {
contentEncodingValue)); // content is textual
else BufferedReader rd;
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); Header contentEncoding = response.getEntity().getContentEncoding();
String contentEncodingValue = null;
StringBuffer result = new StringBuffer(); if (contentEncoding != null)
String line = ""; contentEncodingValue = contentEncoding.getValue();
while ((line = rd.readLine()) != null) { if (contentEncodingValue!= null)
result.append(line); rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
} contentEncodingValue));
String content = result.toString(); else
resp.setContent(content); rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
content = result.toString();
}
resp.setContent(content);
} }
else {
SimpleHttpResponse errResp = new SimpleHttpResponse(500, "no entity in response");
return errResp;
}
} catch (IOException e) { } catch (IOException e) {
return resp; SimpleHttpResponse errResp = new SimpleHttpResponse(500, e.toString());
return errResp;
} }
return resp; return resp; // success
}
private Boolean isContentBinary(org.apache.http.HttpResponse response) {
Header contentTypeHeader = response.getFirstHeader("Content-Type");
String hdrValue = contentTypeHeader.getValue();
String[] hdrTokens = hdrValue.split(";");
if (hdrTokens.length > 0) {
String mimeType = hdrTokens[0];
if (binaryMimeTypes.containsKey(mimeType))
return true;
}
else {
if (binaryMimeTypes.containsKey(hdrValue))
return true;
}
return false;
}
public void setDefaultBinaryMimeTypes() {
binaryMimeTypes.put("image/gif", true);
binaryMimeTypes.put("image/jpeg", true);
binaryMimeTypes.put("video/mpeg", true);
binaryMimeTypes.put("video/x-msvideo", true);
binaryMimeTypes.put("image/x-icon", true);
binaryMimeTypes.put("image/svg+xml", true);
binaryMimeTypes.put("image/tiff", true);
}
public void resetBinaryMimeTypes() {
binaryMimeTypes.clear();
}
public void addMimeType(String mimeType) {
binaryMimeTypes.put(mimeType, true);
} }
} }
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