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
9b81b2a1
authored
Dec 26, 2016
by
Adi Amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
support PATCH method in http request
parent
745930e3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
12 deletions
build.gradle
src/main/java/http/simpleHttpClient/SimpleHttpRequest.java
build.gradle
View file @
9b81b2a1
group
'com.ipgallery.common'
version
'1.
1.5
'
version
'1.
2.0
'
apply
plugin:
'java'
apply
plugin:
'maven-publish'
...
...
@@ -13,7 +13,7 @@ repositories {
//mavenCentral()
//use mavenLocal in cases you want to create this jar on your local machine
//or to be able to use one
//
mavenLocal()
mavenLocal
()
maven
{
url
"http://172.16.1.132:8081/repository/internal"
}
maven
{
url
"http://mandubian-mvn.googlecode.com/svn/trunk/mandubian-mvn/repository"
}
}
...
...
@@ -25,7 +25,7 @@ dependencies {
compile
'com.fasterxml.jackson.core:jackson-databind:2.2.3'
compile
'com.googlecode.libphonenumber:libphonenumber:5.8'
compile
group:
'com.google.gdata'
,
name:
'core'
,
version:
'1.47.1'
compile
'org.apache.httpcomponents:httpclient:4.
1
'
compile
'org.apache.httpcomponents:httpclient:4.
5.2
'
compile
'org.apache.httpcomponents:httpmime:4.1'
compile
'javax.ws.rs:javax.ws.rs-api:2.0'
compile
'com.ipgallery.common:itc:1.0.0'
...
...
src/main/java/http/simpleHttpClient/SimpleHttpRequest.java
View file @
9b81b2a1
...
...
@@ -7,18 +7,14 @@ import java.util.List;
//import java.util.Iterator;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.concurrent.ExecutionException
;
//import javax.ws.rs.core.HttpHeaders;
//import javax.ws.rs.core.MediaType;
import
org.apache.http.client.methods.HttpDelete
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.client.methods.HttpPut
;
import
org.apache.http.client.methods.HttpUriRequest
;
import
org.apache.http.client.methods.*
;
import
org.apache.http.cookie.Cookie
;
import
org.apache.http.entity.StringEntity
;
...
...
@@ -44,11 +40,14 @@ public class SimpleHttpRequest {
return
"PUT"
;
}
},
DELETE
{
DELETE
{
public
String
toString
()
{
return
"DELETE"
;
}
},
PATCH
{
public
String
toString
()
{
return
"PATCH"
;
}
}
}
protected
String
protocol
=
"http"
;
// default is http
protected
Method
method
=
null
;
...
...
@@ -144,6 +143,8 @@ public class SimpleHttpRequest {
return
buildPutRequest
();
case
DELETE:
return
buildDeleteRequest
();
case
PATCH:
return
buildPatchRequest
();
}
return
null
;
}
...
...
@@ -178,7 +179,7 @@ public class SimpleHttpRequest {
return
getRequest
;
}
protected
HttpUriRequest
buildPostRequest
()
throws
UnsupportedEncodingException
{
protected
HttpUriRequest
buildPostRequest
()
{
// build URI
StringBuilder
sb
=
new
StringBuilder
();
...
...
@@ -206,15 +207,20 @@ public class SimpleHttpRequest {
// add content
if
(
content
!=
null
)
{
try
{
StringEntity
contentEntity
=
new
StringEntity
(
content
,
"UTF-8"
);
contentEntity
.
setContentType
(
contentType
);
postRequest
.
setEntity
(
contentEntity
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
return
postRequest
;
}
protected
HttpUriRequest
buildPutRequest
()
throws
UnsupportedEncodingException
{
protected
HttpUriRequest
buildPutRequest
()
{
// build URI
StringBuilder
sb
=
new
StringBuilder
();
...
...
@@ -242,10 +248,15 @@ public class SimpleHttpRequest {
// add content
if
(
content
!=
null
)
{
try
{
StringEntity
contentEntity
=
new
StringEntity
(
content
);
//contentEntity.setContentType("application/json");
contentEntity
.
setContentType
(
contentType
);
putRequest
.
setEntity
(
contentEntity
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
return
putRequest
;
...
...
@@ -279,4 +290,45 @@ public class SimpleHttpRequest {
return
deleteRequest
;
}
protected
HttpUriRequest
buildPatchRequest
()
{
// build URI
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
protocol
+
"://"
);
sb
.
append
(
domain
);
if
(
port
>
0
)
{
sb
.
append
(
":"
);
sb
.
append
(
port
);
}
sb
.
append
(
"/"
);
if
(
path
!=
null
)
sb
.
append
(
path
);
if
(
queryString
!=
null
)
{
sb
.
append
(
"?"
);
sb
.
append
(
queryString
);
}
String
uri
=
sb
.
toString
();
HttpPatch
PatchRequest
=
new
HttpPatch
(
uri
);
// add headers
for
(
Entry
<
String
,
String
>
hdr
:
headers
.
entrySet
())
{
PatchRequest
.
addHeader
(
hdr
.
getKey
(),
hdr
.
getValue
());
}
// add content
if
(
content
!=
null
)
{
try
{
StringEntity
contentEntity
=
new
StringEntity
(
content
,
"UTF-8"
);
contentEntity
.
setContentType
(
contentType
);
PatchRequest
.
setEntity
(
contentEntity
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
return
PatchRequest
;
}
}
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