Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery.common.java
/
utils
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
06c5b9ca
authored
May 23, 2019
by
Adi Amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
bugfix: null while analyzing checking for binary content
parent
21877950
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
166 additions
and
45 deletions
src/main/java/http/simpleHttpClient/SimpleHttpClient.java
src/main/java/http/simpleHttpClient/SimpleHttpRequest.java
src/main/java/http/simpleHttpClient/SimpleHttpClient.java
View file @
06c5b9ca
...
...
@@ -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.ThreadSafeClientCon
nManager
;
import
org.apache.http.impl.client.
*
;
import
org.apache.http.impl.conn.
PoolingHttpClientConnectio
nManager
;
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
{
default
httpClient
.
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
;
}
...
...
src/main/java/http/simpleHttpClient/SimpleHttpRequest.java
View file @
06c5b9ca
...
...
@@ -133,7 +133,7 @@ public class SimpleHttpRequest {
return
pes
.
escape
(
queryString
);
}
public
Http
UriRequest
build
()
throws
UnsupportedEncodingException
{
public
Http
RequestBase
build
()
throws
UnsupportedEncodingException
{
switch
(
method
)
{
case
GET:
return
buildGetRequest
();
...
...
@@ -151,7 +151,7 @@ public class SimpleHttpRequest {
// http://domain:port/path?query_string
protected
Http
UriRequest
buildGetRequest
()
{
protected
Http
RequestBase
buildGetRequest
()
{
// build URI
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
protocol
+
"://"
);
...
...
@@ -179,7 +179,7 @@ public class SimpleHttpRequest {
return
getRequest
;
}
protected
Http
UriRequest
buildPostRequest
()
{
protected
Http
RequestBase
buildPostRequest
()
{
// build URI
StringBuilder
sb
=
new
StringBuilder
();
...
...
@@ -220,7 +220,7 @@ public class SimpleHttpRequest {
return
postRequest
;
}
protected
Http
UriRequest
buildPutRequest
()
{
protected
Http
RequestBase
buildPutRequest
()
{
// build URI
StringBuilder
sb
=
new
StringBuilder
();
...
...
@@ -262,7 +262,7 @@ public class SimpleHttpRequest {
return
putRequest
;
}
protected
Http
UriRequest
buildDeleteRequest
()
{
protected
Http
RequestBase
buildDeleteRequest
()
{
// build URI
StringBuilder
sb
=
new
StringBuilder
();
...
...
@@ -290,7 +290,7 @@ public class SimpleHttpRequest {
return
deleteRequest
;
}
protected
Http
UriRequest
buildPatchRequest
()
{
protected
Http
RequestBase
buildPatchRequest
()
{
// build URI
StringBuilder
sb
=
new
StringBuilder
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment