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;
...
@@ -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.ThreadSafeClientCon
nManager
;
import
org.apache.http.impl.conn.
PoolingHttpClientConnectio
nManager
;
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
{
{
default
httpClient
.
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
;
}
}
...
...
src/main/java/http/simpleHttpClient/SimpleHttpRequest.java
View file @
06c5b9ca
...
@@ -133,7 +133,7 @@ public class SimpleHttpRequest {
...
@@ -133,7 +133,7 @@ public class SimpleHttpRequest {
return
pes
.
escape
(
queryString
);
return
pes
.
escape
(
queryString
);
}
}
public
Http
UriRequest
build
()
throws
UnsupportedEncodingException
{
public
Http
RequestBase
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
Http
UriRequest
buildGetRequest
()
{
protected
Http
RequestBase
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
Http
UriRequest
buildPostRequest
()
{
protected
Http
RequestBase
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
Http
UriRequest
buildPutRequest
()
{
protected
Http
RequestBase
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
Http
UriRequest
buildDeleteRequest
()
{
protected
Http
RequestBase
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
Http
UriRequest
buildPatchRequest
()
{
protected
Http
RequestBase
buildPatchRequest
()
{
// build URI
// build URI
StringBuilder
sb
=
new
StringBuilder
();
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