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
dfa3aa0b
authored
Mar 07, 2017
by
Adi Amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
support binary content in response
parent
668d3a7b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
8 deletions
README.md
build.gradle
src/main/java/http/simpleHttpClient/SimpleHttpClient.java
README.md
View file @
dfa3aa0b
...
...
@@ -5,3 +5,5 @@
-
add "mavenLocal", for local repository. enables to check this dependency locally
-
Handle correctly Exceptions in http requsts/responses
-
support Encoding type (other than utf8) in http responses
## 1.2.4
-
support binary content in response
\ No newline at end of file
build.gradle
View file @
dfa3aa0b
group
'com.ipgallery.common'
version
'1.2.
0
'
version
'1.2.
4
'
apply
plugin:
'java'
apply
plugin:
'maven-publish'
...
...
src/main/java/http/simpleHttpClient/SimpleHttpClient.java
View file @
dfa3aa0b
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.HttpEntity
;
import
org.apache.http.HttpHeaders
;
import
org.apache.http.client.CookieStore
;
import
org.apache.http.client.methods.HttpUriRequest
;
import
org.apache.http.conn.scheme.PlainSocketFactory
;
...
...
@@ -12,12 +15,10 @@ import org.apache.http.cookie.Cookie;
import
org.apache.http.impl.client.DefaultHttpClient
;
import
org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.UnsupportedEncodingException
;
import
java.io.*
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
//import javax.ws.rs.core.HttpHeaders;
//import javax.ws.rs.core.MediaType;
...
...
@@ -28,8 +29,12 @@ public class SimpleHttpClient {
private
DefaultHttpClient
defaulthttpClient
=
null
;
private
ThreadSafeClientConnManager
connManager
=
null
;
private
CookieStore
cookieStore
=
null
;
private
Map
<
String
,
Boolean
>
binaryMimeTypes
=
new
HashMap
<
String
,
Boolean
>();
public
SimpleHttpClient
()
{
setDefaultBinaryMimeTypes
();
}
public
void
Initialize
(
int
maxConnection
)
...
...
@@ -83,6 +88,7 @@ public class SimpleHttpClient {
public
SimpleHttpResponse
ParseResponseAndCookies
(
org
.
apache
.
http
.
HttpResponse
response
,
CookieStore
cookieStore
)
{
SimpleHttpResponse
resp
=
new
SimpleHttpResponse
();
String
content
=
null
;
// get status code
resp
.
setStatusCode
(
response
.
getStatusLine
().
getStatusCode
());
...
...
@@ -102,6 +108,14 @@ public class SimpleHttpClient {
try
{
HttpEntity
httpEntity
=
response
.
getEntity
();
if
(
httpEntity
!=
null
)
{
if
(
isContentBinary
(
response
))
{
// content is binary
InputStream
is
=
httpEntity
.
getContent
();
byte
[]
baContent
=
IOUtils
.
toByteArray
(
is
);
content
=
Base64
.
encode
(
baContent
);
}
else
{
// content is textual
BufferedReader
rd
;
Header
contentEncoding
=
response
.
getEntity
().
getContentEncoding
();
String
contentEncodingValue
=
null
;
...
...
@@ -118,15 +132,55 @@ public class SimpleHttpClient {
while
((
line
=
rd
.
readLine
())
!=
null
)
{
result
.
append
(
line
);
}
String
content
=
result
.
toString
();
content
=
result
.
toString
();
}
resp
.
setContent
(
content
);
}
else
{
SimpleHttpResponse
errResp
=
new
SimpleHttpResponse
(
500
,
"no entity in response"
);
return
errResp
;
}
}
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
);
}
}
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