Commit 06c5b9ca by Adi Amir

bugfix: null while analyzing checking for binary content

parent 21877950
...@@ -4,17 +4,22 @@ import com.google.gdata.util.common.util.Base64; ...@@ -4,17 +4,22 @@ import com.google.gdata.util.common.util.Base64;
import org.apache.commons.io.IOUtils; 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.config.RequestConfig;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
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.client.protocol.HttpClientContext;
import org.apache.http.conn.scheme.Scheme; import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.cookie.Cookie; import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import javax.net.ssl.SSLContext;
import java.io.*; import java.io.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -24,70 +29,155 @@ import java.util.Map; ...@@ -24,70 +29,155 @@ import java.util.Map;
//import javax.ws.rs.core.MediaType; //import javax.ws.rs.core.MediaType;
//import org.apache.http.client.methods.HttpGet; //import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContextBuilder;
public class SimpleHttpClient { public class SimpleHttpClient {
private DefaultHttpClient defaulthttpClient = null; static public final String DEF_MAX_CONNECTIONS = "100";
private ThreadSafeClientConnManager connManager = null; static public final String DEF_MAX_CONNECTIONS_PER_ROUTE = "100";
static public final String DEF_SOCKET_TIMEOUT = "30000";
static private final char unexpectedChars[] = {'\uFEFF', '\ufffd'};
private String instanceName = "default";
private CloseableHttpClient httpClient = null;
private CookieStore cookieStore = null; private CookieStore cookieStore = null;
private Integer requestTimeout = null;
private Map<String, Boolean> binaryMimeTypes = new HashMap<String, Boolean>(); private Map<String, Boolean> binaryMimeTypes = new HashMap<String, Boolean>();
private Boolean isAssumeBinaryContent = false; private Boolean isAssumeBinaryContent = false;
public SimpleHttpClient() public SimpleHttpClient()
{ {
setDefaultBinaryMimeTypes(); setDefaultBinaryMimeTypes();
} }
public void Initialize(int maxConnection) public void Initialize(int maxConnections)
{
InitializeEx(null, maxConnections, null);
}
public Boolean InitializeEx(String instName, Integer maxConnections, Integer requestTimeout)
{ {
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger"); System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
registry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
connManager = new ThreadSafeClientConnManager(registry); try {
connManager.setDefaultMaxPerRoute(maxConnection); // get connection properties
connManager.setMaxTotal(maxConnection); if (instName != null)
this.defaulthttpClient = new DefaultHttpClient(connManager); this.instanceName = instName;
this.cookieStore = defaulthttpClient.getCookieStore();
// maxConnection
if (maxConnections == null)
maxConnections = Integer.valueOf(System.getProperty("SimpleHttpClient." + this.instanceName + ".maxConnections", DEF_MAX_CONNECTIONS));
// socketTimeout
if (requestTimeout == null)
requestTimeout = Integer.valueOf(System.getProperty("SimpleHttpClient." + this.instanceName + ".maxRequestTimeout", DEF_SOCKET_TIMEOUT));
this.requestTimeout = requestTimeout;
// create an SSL context which trusts any certificate !
org.apache.http.ssl.SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
sslContextBuilder.loadTrustMaterial(new org.apache.http.conn.ssl.TrustSelfSignedStrategy());
SSLContext sslContext = sslContextBuilder.build();
org.apache.http.conn.ssl.SSLConnectionSocketFactory sslSocketFactory =
new SSLConnectionSocketFactory(sslContext, new org.apache.http.conn.ssl.DefaultHostnameVerifier());
// create scheme for http & https
RegistryBuilder<ConnectionSocketFactory> schemeRegistry = RegistryBuilder.create();
schemeRegistry.register("http", PlainConnectionSocketFactory.getSocketFactory());
SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslContext);
schemeRegistry.register("https", sf);
// create connection pool manager
PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager(schemeRegistry.build());
pool.setMaxTotal(maxConnections);
pool.setDefaultMaxPerRoute(maxConnections);
pool.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(this.requestTimeout).build());
// create the http client
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setSSLSocketFactory(sslSocketFactory)
.setConnectionManager(pool);
httpClient = httpClientBuilder.build();
return true;
} catch (Exception e) {
System.out.println("error: SimpleHttpClient failed to initialize. e=" + e.toString());
return false;
}
} }
public void assumeBinaryContent() {
isAssumeBinaryContent = true; public void assumeBinaryContent(Boolean bBinContent) {
isAssumeBinaryContent = bBinContent;
} }
public Boolean isAssumeBinaryContent() { return isAssumeBinaryContent; } public Boolean isAssumeBinaryContent() { return isAssumeBinaryContent; }
public void shutdown() throws Throwable public void shutdown() throws Throwable
{ {
defaulthttpClient.getConnectionManager().shutdown(); httpClient.getConnectionManager().shutdown();
} }
public SimpleHttpResponse processRequest(SimpleHttpRequest request) throws UnsupportedEncodingException { public SimpleHttpResponse processRequest(SimpleHttpRequest request) throws UnsupportedEncodingException {
HttpUriRequest uriRequest = request.build(); HttpRequestBase httpRequest = request.build();
org.apache.http.HttpResponse resultResp = null; //org.apache.http.HttpResponse resultResp = null;
SimpleHttpResponse response = null; CloseableHttpResponse httpResp = null;
SimpleHttpResponse simpleResp = null;
try { try {
// add cookies // set cookies if requested to add
HttpContext localContext = null;
if (request.getCookies().size() > 0) {
cookieStore = new BasicCookieStore();
if (request.getCookies().size() > 0) { if (request.getCookies().size() > 0) {
for (Cookie cookie: request.getCookies()) { for (Cookie cookie : request.getCookies()) {
cookieStore.addCookie(cookie); cookieStore.addCookie(cookie);
} }
} }
defaulthttpClient.setCookieStore(cookieStore); localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
}
// set request timeout
RequestConfig.Builder requestConfig = RequestConfig.custom();
requestConfig.setConnectTimeout(this.requestTimeout);
requestConfig.setConnectionRequestTimeout(this.requestTimeout);
requestConfig.setSocketTimeout(this.requestTimeout);
httpRequest.setConfig(requestConfig.build());
// send the request // send the request
resultResp = defaulthttpClient.execute(uriRequest); if (localContext == null)
if (resultResp != null) { httpResp = httpClient.execute(httpRequest);
response = ParseResponseAndCookies(resultResp, defaulthttpClient.getCookieStore()); else
return response; httpResp = httpClient.execute(httpRequest, localContext);
}else // handle response ...
if (httpResp != null) {
simpleResp = ParseResponseAndCookies(httpResp, null);
httpResp.close();
httpResp = null;
}
else
{ {
return new SimpleHttpResponse(500, "request execution failed"); String err = "SimpleHttpClient.processRequest() failed - uri=" + request.getPath() + " error=" + "null http response";
simpleResp = new SimpleHttpResponse(500, err);
} }
return simpleResp;
} catch (Exception e) { } catch (Exception e) {
return new SimpleHttpResponse(500, e.toString()); String err = "SimpleHttpClient.processRequest() failed - uri=" + request.getPath() + " error=" + e.toString();
return new SimpleHttpResponse(500, err);
} finally {
try {
if (httpResp != null)
httpResp.close();
} catch (IOException e) {
String err = "SimpleHttpClient.processRequest() failed - uri=" + request.getPath() + " error=" + "execption on finally httpResp.close() e=" + e.toString();
return new SimpleHttpResponse(500, err);
}
} }
} }
...@@ -107,8 +197,10 @@ public class SimpleHttpClient { ...@@ -107,8 +197,10 @@ public class SimpleHttpClient {
} }
// get cookies // get cookies
if (cookieStore != null) {
List<Cookie> cookies = cookieStore.getCookies(); List<Cookie> cookies = cookieStore.getCookies();
resp.setCookies(cookies); resp.setCookies(cookies);
}
// get content // get content
try { try {
...@@ -132,14 +224,23 @@ public class SimpleHttpClient { ...@@ -132,14 +224,23 @@ public class SimpleHttpClient {
if (contentEncoding != null) if (contentEncoding != null)
contentEncodingValue = contentEncoding.getValue(); contentEncodingValue = contentEncoding.getValue();
if (contentEncodingValue!= null) if (contentEncodingValue!= null)
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), if (contentEncodingValue.equals("gzip")) {
contentEncodingValue)); // gunzip the content
response.setEntity(new GzipDecompressingEntity(httpEntity));
httpEntity = response.getEntity();
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
}
else {
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), contentEncodingValue));
}
else else
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// build content ...
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
String line = ""; String line = "";
while ((line = rd.readLine()) != null) { while ((line = rd.readLine()) != null) {
line = CleanUnexpectedCharacters(line);
result.append(line); result.append(line);
} }
content = result.toString(); content = result.toString();
...@@ -158,20 +259,40 @@ public class SimpleHttpClient { ...@@ -158,20 +259,40 @@ public class SimpleHttpClient {
return resp; // success return resp; // success
} }
protected String CleanUnexpectedCharacters(String content) {
StringBuilder sb = new StringBuilder();
for (final char ch : content.toCharArray()) {
if (isUnexpectedChar(ch) == false) {
sb.append(ch);
}
}
String sanitzedString = sb.toString();
return sanitzedString;
}
protected Boolean isUnexpectedChar(char ch) {
for (char uch: unexpectedChars) {
if (ch == uch)
return true;
}
return false;
}
private Boolean isContentBinary(org.apache.http.HttpResponse response) { private Boolean isContentBinary(org.apache.http.HttpResponse response) {
Header contentTypeHeader = response.getFirstHeader("Content-Type"); Header contentTypeHeader = response.getFirstHeader("Content-Type");
if (contentTypeHeader != null) {
String hdrValue = contentTypeHeader.getValue(); String hdrValue = contentTypeHeader.getValue();
String[] hdrTokens = hdrValue.split(";"); String[] hdrTokens = hdrValue.split(";");
if (hdrTokens.length > 0) { if (hdrTokens.length > 0) {
String mimeType = hdrTokens[0]; String mimeType = hdrTokens[0];
if (binaryMimeTypes.containsKey(mimeType)) if (binaryMimeTypes.containsKey(mimeType))
return true; return true;
} } else {
else {
if (binaryMimeTypes.containsKey(hdrValue)) if (binaryMimeTypes.containsKey(hdrValue))
return true; return true;
} }
}
return false; return false;
} }
......
...@@ -133,7 +133,7 @@ public class SimpleHttpRequest { ...@@ -133,7 +133,7 @@ public class SimpleHttpRequest {
return pes.escape(queryString); return pes.escape(queryString);
} }
public HttpUriRequest build() throws UnsupportedEncodingException { public HttpRequestBase build() throws UnsupportedEncodingException {
switch (method) { switch (method) {
case GET: case GET:
return buildGetRequest(); return buildGetRequest();
...@@ -151,7 +151,7 @@ public class SimpleHttpRequest { ...@@ -151,7 +151,7 @@ public class SimpleHttpRequest {
// http://domain:port/path?query_string // http://domain:port/path?query_string
protected HttpUriRequest buildGetRequest() { protected HttpRequestBase buildGetRequest() {
// build URI // build URI
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(protocol+"://"); sb.append(protocol+"://");
...@@ -179,7 +179,7 @@ public class SimpleHttpRequest { ...@@ -179,7 +179,7 @@ public class SimpleHttpRequest {
return getRequest; return getRequest;
} }
protected HttpUriRequest buildPostRequest() { protected HttpRequestBase buildPostRequest() {
// build URI // build URI
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -220,7 +220,7 @@ public class SimpleHttpRequest { ...@@ -220,7 +220,7 @@ public class SimpleHttpRequest {
return postRequest; return postRequest;
} }
protected HttpUriRequest buildPutRequest() { protected HttpRequestBase buildPutRequest() {
// build URI // build URI
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -262,7 +262,7 @@ public class SimpleHttpRequest { ...@@ -262,7 +262,7 @@ public class SimpleHttpRequest {
return putRequest; return putRequest;
} }
protected HttpUriRequest buildDeleteRequest() { protected HttpRequestBase buildDeleteRequest() {
// build URI // build URI
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -290,7 +290,7 @@ public class SimpleHttpRequest { ...@@ -290,7 +290,7 @@ public class SimpleHttpRequest {
return deleteRequest; return deleteRequest;
} }
protected HttpUriRequest buildPatchRequest() { protected HttpRequestBase buildPatchRequest() {
// build URI // build URI
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
......
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