Skip to content

Commit 6df20ab

Browse files
author
Shashi Ranjan
committed
Merge branch 'dewos-master'
2 parents 7fd96da + 361552d commit 6df20ab

35 files changed

+1177
-841
lines changed

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,23 @@ For example, serializing Json from\to Object using the popular Jackson ObjectMap
123123
```java
124124
// Only one time
125125
Unirest.setObjectMapper(new ObjectMapper() {
126-
private com.fasterxml.jackson.databind.ObjectMapper objectMapper
127-
= new com.fasterxml.jackson.databind.ObjectMapper();
128-
129-
public Object readValue(String value) {
130-
return objectMapper.readValue(value);
131-
}
126+
private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper
127+
= new com.fasterxml.jackson.databind.ObjectMapper();
132128

129+
public <T> T readValue(String value, Class<T> valueType) {
130+
try {
131+
return jacksonObjectMapper.readValue(value, valueType);
132+
} catch (IOException e) {
133+
throw new RuntimeException(e);
134+
}
135+
}
136+
133137
public String writeValue(Object value) {
134-
return objectMapper.writeValueAsString(value);
138+
try {
139+
return jacksonObjectMapper.writeValueAsString(value);
140+
} catch (JsonProcessingException e) {
141+
throw new RuntimeException(e);
142+
}
135143
}
136144
});
137145

formatter/unirest-code-format.xml

Lines changed: 295 additions & 0 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<properties>
4444
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4545
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
46+
<jackson.version>2.6.0</jackson.version>
4647
</properties>
4748

4849
<build>
@@ -132,5 +133,11 @@
132133
<version>2.4</version>
133134
<scope>test</scope>
134135
</dependency>
136+
<dependency>
137+
<groupId>com.fasterxml.jackson.core</groupId>
138+
<artifactId>jackson-databind</artifactId>
139+
<version>${jackson.version}</version>
140+
<scope>test</scope>
141+
</dependency>
135142
</dependencies>
136143
</project>

src/main/java/com/mashape/unirest/http/Headers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class Headers extends HashMap<String, List<String>> {
88
private static final long serialVersionUID = 71310341388734766L;
99

1010
public String getFirst(Object key) {
11-
Object lowerCasedKey = key instanceof String ? ((String)key).toLowerCase() : key;
11+
Object lowerCasedKey = key instanceof String ? ((String) key).toLowerCase() : key;
1212
List<String> list = get(lowerCasedKey);
1313
if (list != null && list.size() > 0) {
1414
return list.get(0);

src/main/java/com/mashape/unirest/http/HttpClientHelper.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public class HttpClientHelper {
6868
private static final String USER_AGENT_HEADER = "user-agent";
6969
private static final String USER_AGENT = "unirest-java/1.3.11";
7070

71-
private static <T> FutureCallback<org.apache.http.HttpResponse> prepareCallback(final Class<T> responseClass,
72-
final Callback<T> callback) {
71+
private static <T> FutureCallback<org.apache.http.HttpResponse> prepareCallback(final Class<T> responseClass, final Callback<T> callback) {
7372
if (callback == null)
7473
return null;
7574

@@ -100,8 +99,7 @@ public static <T> Future<HttpResponse<T>> requestAsync(HttpRequest request, fina
10099
asyncIdleConnectionMonitorThread.start();
101100
}
102101

103-
final Future<org.apache.http.HttpResponse> future = asyncHttpClient.execute(requestObj,
104-
prepareCallback(responseClass, callback));
102+
final Future<org.apache.http.HttpResponse> future = asyncHttpClient.execute(requestObj, prepareCallback(responseClass, callback));
105103

106104
return new Future<HttpResponse<T>>() {
107105

@@ -122,8 +120,7 @@ public HttpResponse<T> get() throws InterruptedException, ExecutionException {
122120
return new HttpResponse<T>(httpResponse, responseClass);
123121
}
124122

125-
public HttpResponse<T> get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException,
126-
TimeoutException {
123+
public HttpResponse<T> get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
127124
org.apache.http.HttpResponse httpResponse = future.get(timeout, unit);
128125
return new HttpResponse<T>(httpResponse, responseClass);
129126
}
@@ -135,7 +132,7 @@ public static <T> HttpResponse<T> request(HttpRequest request, Class<T> response
135132
HttpClient client = ClientFactory.getHttpClient(); // The
136133
// DefaultHttpClient
137134
// is thread-safe
138-
135+
139136
org.apache.http.HttpResponse response;
140137
try {
141138
response = client.execute(requestObj);
@@ -185,7 +182,7 @@ private static HttpRequestBase prepareRequest(HttpRequest request, boolean async
185182
} catch (Exception e) {
186183
throw new RuntimeException(e);
187184
}
188-
185+
189186
switch (request.getHttpMethod()) {
190187
case GET:
191188
reqObj = new HttpGet(urlToRequest);
@@ -209,12 +206,12 @@ private static HttpRequestBase prepareRequest(HttpRequest request, boolean async
209206
reqObj = new HttpHead(urlToRequest);
210207
break;
211208
}
212-
209+
213210
Set<Entry<String, List<String>>> entrySet = request.getHeaders().entrySet();
214-
for(Entry<String, List<String>> entry : entrySet) {
211+
for (Entry<String, List<String>> entry : entrySet) {
215212
List<String> values = entry.getValue();
216213
if (values != null) {
217-
for(String value : values) {
214+
for (String value : values) {
218215
reqObj.addHeader(entry.getKey(), value);
219216
}
220217
}

src/main/java/com/mashape/unirest/http/HttpDeleteWithBody.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ a copy of this software and associated documentation files (the
2121
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2222
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2323
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24-
*/
24+
*/
2525

2626
package com.mashape.unirest.http;
2727

@@ -30,16 +30,23 @@ a copy of this software and associated documentation files (the
3030
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
3131

3232
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
33-
public static final String METHOD_NAME = "DELETE";
34-
public String getMethod() { return METHOD_NAME; }
35-
36-
public HttpDeleteWithBody(final String uri) {
37-
super();
38-
setURI(URI.create(uri));
39-
}
40-
public HttpDeleteWithBody(final URI uri) {
41-
super();
42-
setURI(uri);
43-
}
44-
public HttpDeleteWithBody() { super(); }
33+
public static final String METHOD_NAME = "DELETE";
34+
35+
public String getMethod() {
36+
return METHOD_NAME;
37+
}
38+
39+
public HttpDeleteWithBody(final String uri) {
40+
super();
41+
setURI(URI.create(uri));
42+
}
43+
44+
public HttpDeleteWithBody(final URI uri) {
45+
super();
46+
setURI(uri);
47+
}
48+
49+
public HttpDeleteWithBody() {
50+
super();
51+
}
4552
}

src/main/java/com/mashape/unirest/http/HttpMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ a copy of this software and associated documentation files (the
2121
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2222
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2323
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24-
*/
24+
*/
2525

2626
package com.mashape.unirest.http;
2727

src/main/java/com/mashape/unirest/http/HttpPatchWithBody.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ a copy of this software and associated documentation files (the
2121
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2222
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2323
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24-
*/
24+
*/
2525

2626
package com.mashape.unirest.http;
2727

@@ -30,16 +30,23 @@ a copy of this software and associated documentation files (the
3030
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
3131

3232
class HttpPatchWithBody extends HttpEntityEnclosingRequestBase {
33-
public static final String METHOD_NAME = "PATCH";
34-
public String getMethod() { return METHOD_NAME; }
35-
36-
public HttpPatchWithBody(final String uri) {
37-
super();
38-
setURI(URI.create(uri));
39-
}
40-
public HttpPatchWithBody(final URI uri) {
41-
super();
42-
setURI(uri);
43-
}
44-
public HttpPatchWithBody() { super(); }
33+
public static final String METHOD_NAME = "PATCH";
34+
35+
public String getMethod() {
36+
return METHOD_NAME;
37+
}
38+
39+
public HttpPatchWithBody(final String uri) {
40+
super();
41+
setURI(URI.create(uri));
42+
}
43+
44+
public HttpPatchWithBody(final URI uri) {
45+
super();
46+
setURI(uri);
47+
}
48+
49+
public HttpPatchWithBody() {
50+
super();
51+
}
4552
}

src/main/java/com/mashape/unirest/http/HttpResponse.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ a copy of this software and associated documentation files (the
2121
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2222
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2323
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24-
*/
24+
*/
2525

2626
package com.mashape.unirest.http;
2727

@@ -52,30 +52,31 @@ public class HttpResponse<T> {
5252
public HttpResponse(org.apache.http.HttpResponse response, Class<T> responseClass) {
5353
HttpEntity responseEntity = response.getEntity();
5454
ObjectMapper objectMapper = (ObjectMapper) Options.getOption(Option.OBJECT_MAPPER);
55-
55+
5656
Header[] allHeaders = response.getAllHeaders();
57-
for(Header header : allHeaders) {
57+
for (Header header : allHeaders) {
5858
String headerName = header.getName().toLowerCase();
5959
List<String> list = headers.get(headerName);
60-
if (list == null) list = new ArrayList<String>();
60+
if (list == null)
61+
list = new ArrayList<String>();
6162
list.add(header.getValue());
6263
headers.put(headerName, list);
6364
}
6465
StatusLine statusLine = response.getStatusLine();
6566
this.statusCode = statusLine.getStatusCode();
6667
this.statusText = statusLine.getReasonPhrase();
67-
68+
6869
if (responseEntity != null) {
6970
String charset = "UTF-8";
70-
71+
7172
Header contentType = responseEntity.getContentType();
7273
if (contentType != null) {
7374
String responseCharset = ResponseUtils.getCharsetFromContentType(contentType.getValue());
7475
if (responseCharset != null && !responseCharset.trim().equals("")) {
7576
charset = responseCharset;
7677
}
7778
}
78-
79+
7980
try {
8081
byte[] rawBody;
8182
try {
@@ -97,15 +98,15 @@ public HttpResponse(org.apache.http.HttpResponse response, Class<T> responseClas
9798
} else if (InputStream.class.equals(responseClass)) {
9899
this.body = (T) this.rawBody;
99100
} else if (objectMapper != null) {
100-
this.body = (T) objectMapper.readValue(new String(rawBody, charset));
101+
this.body = objectMapper.readValue(new String(rawBody, charset), responseClass);
101102
} else {
102103
throw new Exception("Only String, JsonNode and InputStream are supported, or an ObjectMapper implementation is required.");
103104
}
104105
} catch (Exception e) {
105106
throw new RuntimeException(e);
106107
}
107108
}
108-
109+
109110
try {
110111
EntityUtils.consume(responseEntity);
111112
} catch (IOException e) {
@@ -116,7 +117,7 @@ public HttpResponse(org.apache.http.HttpResponse response, Class<T> responseClas
116117
public int getStatus() {
117118
return statusCode;
118119
}
119-
120+
120121
public String getStatusText() {
121122
return statusText;
122123
}

src/main/java/com/mashape/unirest/http/JsonNode.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ a copy of this software and associated documentation files (the
2121
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2222
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2323
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24-
*/
24+
*/
2525

2626
package com.mashape.unirest.http;
2727

@@ -30,10 +30,10 @@ a copy of this software and associated documentation files (the
3030
import org.json.JSONObject;
3131

3232
public class JsonNode {
33-
33+
3434
private JSONObject jsonObject;
3535
private JSONArray jsonArray;
36-
36+
3737
private boolean array;
3838

3939
public JsonNode(String json) {
@@ -53,11 +53,11 @@ public JsonNode(String json) {
5353
}
5454
}
5555
}
56-
56+
5757
public JSONObject getObject() {
5858
return this.jsonObject;
5959
}
60-
60+
6161
public JSONArray getArray() {
6262
JSONArray result = this.jsonArray;
6363
if (array == false) {
@@ -66,18 +66,20 @@ public JSONArray getArray() {
6666
}
6767
return result;
6868
}
69-
69+
7070
public boolean isArray() {
7171
return this.array;
7272
}
73-
73+
7474
@Override
7575
public String toString() {
7676
if (isArray()) {
77-
if (jsonArray == null) return null;
77+
if (jsonArray == null)
78+
return null;
7879
return jsonArray.toString();
7980
}
80-
if (jsonObject == null) return null;
81+
if (jsonObject == null)
82+
return null;
8183
return jsonObject.toString();
8284
}
8385
}

0 commit comments

Comments
 (0)