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;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
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.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.util.HashMap;
import java.util.List;
......@@ -24,70 +29,155 @@ import java.util.Map;
//import javax.ws.rs.core.MediaType;
//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 {
private DefaultHttpClient defaulthttpClient = null;
private ThreadSafeClientConnManager connManager = null;
static public final String DEF_MAX_CONNECTIONS = "100";
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 Integer requestTimeout = null;
private Map<String, Boolean> binaryMimeTypes = new HashMap<String, Boolean>();
private Boolean isAssumeBinaryContent = false;
public SimpleHttpClient()
{
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");
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
registry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
connManager = new ThreadSafeClientConnManager(registry);
connManager.setDefaultMaxPerRoute(maxConnection);
connManager.setMaxTotal(maxConnection);
this.defaulthttpClient = new DefaultHttpClient(connManager);
this.cookieStore = defaulthttpClient.getCookieStore();
try {
// get connection properties
if (instName != null)
this.instanceName = instName;
// 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 void shutdown() throws Throwable
{
defaulthttpClient.getConnectionManager().shutdown();
httpClient.getConnectionManager().shutdown();
}
public SimpleHttpResponse processRequest(SimpleHttpRequest request) throws UnsupportedEncodingException {
HttpUriRequest uriRequest = request.build();
org.apache.http.HttpResponse resultResp = null;
SimpleHttpResponse response = null;
HttpRequestBase httpRequest = request.build();
//org.apache.http.HttpResponse resultResp = null;
CloseableHttpResponse httpResp = null;
SimpleHttpResponse simpleResp = null;
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) {
for (Cookie cookie: request.getCookies()) {
for (Cookie cookie : request.getCookies()) {
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
resultResp = defaulthttpClient.execute(uriRequest);
if (resultResp != null) {
response = ParseResponseAndCookies(resultResp, defaulthttpClient.getCookieStore());
return response;
}else
if (localContext == null)
httpResp = httpClient.execute(httpRequest);
else
httpResp = httpClient.execute(httpRequest, localContext);
// 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) {
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 {
}
// get cookies
if (cookieStore != null) {
List<Cookie> cookies = cookieStore.getCookies();
resp.setCookies(cookies);
}
// get content
try {
......@@ -132,14 +224,23 @@ public class SimpleHttpClient {
if (contentEncoding != null)
contentEncodingValue = contentEncoding.getValue();
if (contentEncodingValue!= null)
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
contentEncodingValue));
if (contentEncodingValue.equals("gzip")) {
// 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
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// build content ...
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
line = CleanUnexpectedCharacters(line);
result.append(line);
}
content = result.toString();
......@@ -158,20 +259,40 @@ public class SimpleHttpClient {
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) {
Header contentTypeHeader = response.getFirstHeader("Content-Type");
if (contentTypeHeader != null) {
String hdrValue = contentTypeHeader.getValue();
String[] hdrTokens = hdrValue.split(";");
if (hdrTokens.length > 0) {
String mimeType = hdrTokens[0];
if (binaryMimeTypes.containsKey(mimeType))
return true;
}
else {
} else {
if (binaryMimeTypes.containsKey(hdrValue))
return true;
}
}
return false;
}
......
......@@ -133,7 +133,7 @@ public class SimpleHttpRequest {
return pes.escape(queryString);
}
public HttpUriRequest build() throws UnsupportedEncodingException {
public HttpRequestBase build() throws UnsupportedEncodingException {
switch (method) {
case GET:
return buildGetRequest();
......@@ -151,7 +151,7 @@ public class SimpleHttpRequest {
// http://domain:port/path?query_string
protected HttpUriRequest buildGetRequest() {
protected HttpRequestBase buildGetRequest() {
// build URI
StringBuilder sb = new StringBuilder();
sb.append(protocol+"://");
......@@ -179,7 +179,7 @@ public class SimpleHttpRequest {
return getRequest;
}
protected HttpUriRequest buildPostRequest() {
protected HttpRequestBase buildPostRequest() {
// build URI
StringBuilder sb = new StringBuilder();
......@@ -220,7 +220,7 @@ public class SimpleHttpRequest {
return postRequest;
}
protected HttpUriRequest buildPutRequest() {
protected HttpRequestBase buildPutRequest() {
// build URI
StringBuilder sb = new StringBuilder();
......@@ -262,7 +262,7 @@ public class SimpleHttpRequest {
return putRequest;
}
protected HttpUriRequest buildDeleteRequest() {
protected HttpRequestBase buildDeleteRequest() {
// build URI
StringBuilder sb = new StringBuilder();
......@@ -290,7 +290,7 @@ public class SimpleHttpRequest {
return deleteRequest;
}
protected HttpUriRequest buildPatchRequest() {
protected HttpRequestBase buildPatchRequest() {
// build URI
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