From d2ca90920c86e637a5b28b6143f7d1a7cad2aecb Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Fri, 13 Oct 2017 13:57:45 -0700 Subject: [PATCH 1/3] Add RestResponse classes --- client-runtime/pom.xml | 4 + .../java/com/microsoft/rest/RestProxy.java | 86 ++++++++++------- .../java/com/microsoft/rest/RestResponse.java | 36 +++++++ .../microsoft/rest/RestResponseWithBody.java | 24 +++++ .../com/microsoft/rest/http/HttpHeaders.java | 14 +++ .../com/microsoft/rest/HttpBinHeaders.java | 31 +++++++ .../com/microsoft/rest/RestProxyTests.java | 93 ++++++++++++++++++- .../microsoft/rest/http/MockHttpClient.java | 11 ++- .../microsoft/rest/http/MockHttpResponse.java | 12 ++- pom.xml | 5 + 10 files changed, 279 insertions(+), 37 deletions(-) create mode 100644 client-runtime/src/main/java/com/microsoft/rest/RestResponse.java create mode 100644 client-runtime/src/main/java/com/microsoft/rest/RestResponseWithBody.java create mode 100644 client-runtime/src/test/java/com/microsoft/rest/HttpBinHeaders.java diff --git a/client-runtime/pom.xml b/client-runtime/pom.xml index b3e18b2cc9982..72f96e34afa23 100644 --- a/client-runtime/pom.xml +++ b/client-runtime/pom.xml @@ -99,6 +99,10 @@ com.microsoft.azure azure-annotations + + org.json + json + junit junit diff --git a/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java b/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java index c533efdf0d84a..cf7a646186eb1 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java @@ -8,19 +8,20 @@ import com.google.common.reflect.TypeToken; import com.microsoft.rest.http.ContentType; +import com.microsoft.rest.http.HttpHeaders; import com.microsoft.rest.protocol.SerializerAdapter; import com.microsoft.rest.http.HttpClient; import com.microsoft.rest.http.HttpHeader; import com.microsoft.rest.http.HttpRequest; import com.microsoft.rest.http.HttpResponse; import com.microsoft.rest.http.UrlBuilder; +import org.json.JSONObject; import rx.Completable; import rx.Observable; import rx.Single; import rx.exceptions.Exceptions; import rx.functions.Func1; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Constructor; @@ -79,10 +80,13 @@ protected SerializerAdapter serializer() { * @param resultType The Type of the object to return. * @param The type of the object to return. This should be the same as the resultType parameter. * @return The deserialized version of the provided String value. - * @throws IOException if there is an error deserializing. */ - public T deserialize(String value, Type resultType) throws IOException { - return serializer.deserialize(value, resultType); + public T deserialize(String value, Type resultType) { + try { + return serializer.deserialize(value, resultType); + } catch (IOException e) { + throw Exceptions.propagate(e); + } } /** @@ -229,7 +233,7 @@ public HttpResponse call(String responseBody) { return asyncResult; } - private Single toProxyReturnValueAsync(HttpResponse response, final String httpMethod, final Type entityType) { + private Single toProxyReturnValueAsync(final HttpResponse response, final String httpMethod, final Type entityType) { final TypeToken entityTypeToken = TypeToken.of(entityType); final int responseStatusCode = response.statusCode(); final Single asyncResult; @@ -247,41 +251,59 @@ private Single toProxyReturnValueAsync(HttpResponse response, final String ht asyncResult = response.bodyAsInputStreamAsync(); } else if (entityTypeToken.isSubtypeOf(byte[].class)) { asyncResult = response.bodyAsByteArrayAsync(); + } else if (entityTypeToken.isSubtypeOf(RestResponse.class)) { + if (!entityTypeToken.isSubtypeOf(RestResponseWithBody.class)) { + final Type deserializedHeadersType = ((ParameterizedType) entityType).getActualTypeArguments()[0]; + final HttpHeaders responseHeaders = response.headers(); + final Object deserializedHeaders = deserializeHeaders(responseHeaders, deserializedHeadersType); + asyncResult = Single.just(new RestResponse<>(responseStatusCode, deserializedHeaders)); + } + else { + final Type[] deserializedTypes = ((ParameterizedType) entityType).getActualTypeArguments(); + + final Type deserializedHeadersType = deserializedTypes[0]; + final HttpHeaders responseHeaders = response.headers(); + final Object deserializedHeaders = deserializeHeaders(responseHeaders, deserializedHeadersType); + + final Type deserializedBodyType = deserializedTypes[1]; + final TypeToken deserializedBodyTypeToken = TypeToken.of(deserializedBodyType); + if (deserializedBodyTypeToken.isSubtypeOf(byte[].class)) { + asyncResult = response.bodyAsByteArrayAsync() + .map(new Func1>() { + @Override + public RestResponseWithBody call(byte[] responseBodyBytes) { + return new RestResponseWithBody<>(responseStatusCode, deserializedHeaders, responseBodyBytes); + } + }); + } + else { + asyncResult = response.bodyAsStringAsync() + .map(new Func1>() { + @Override + public RestResponseWithBody call(String responseBodyString) { + final Object deserializedBody = deserialize(responseBodyString, deserializedBodyType); + return new RestResponseWithBody<>(responseStatusCode, deserializedHeaders, deserializedBody); + } + }); + } + } } else { asyncResult = response .bodyAsStringAsync() - .flatMap(new Func1>() { + .map(new Func1() { @Override - public Single call(String responseBodyString) { - try { - return Single.just(toProxyReturnValue(responseStatusCode, responseBodyString, httpMethod, entityType)); - } catch (Throwable e) { - return Single.error(e); - } - } - }); + public Object call(String responseBodyString) { + return deserialize(responseBodyString, entityType); + } + }); } return asyncResult; } - private Object toProxyReturnValue(int httpResponseStatusCode, String httpResponseBody, String httpMethod, Type entityType) throws IOException { - final TypeToken entityTypeToken = TypeToken.of(entityType); - Object result = null; - if (entityTypeToken.isSubtypeOf(void.class) || entityTypeToken.isSubtypeOf(Void.class)) { - result = null; - } else if (httpMethod.equalsIgnoreCase("HEAD") - && (entityTypeToken.isSubtypeOf(boolean.class) || entityTypeToken.isSubtypeOf(Boolean.class))) { - result = httpResponseStatusCode / 100 == 2; - } else if (entityTypeToken.isSubtypeOf(InputStream.class)) { - result = new ByteArrayInputStream(httpResponseBody.getBytes()); - } else if (entityTypeToken.isSubtypeOf(byte[].class)) { - result = httpResponseBody.getBytes(); - } else { - if (httpResponseBody != null && !httpResponseBody.isEmpty()) { - result = serializer.deserialize(httpResponseBody, entityType); - } - } - return result; + private Object deserializeHeaders(HttpHeaders headers, Type deserializedHeadersType) { + final JSONObject headersJson = headers.toJSON(); + final String headersJsonString = headersJson.toString(); + return deserialize(headersJsonString, deserializedHeadersType); } protected Object handleAsyncHttpResponse(HttpRequest httpRequest, Single asyncHttpResponse, SwaggerMethodParser methodParser, Type returnType) { diff --git a/client-runtime/src/main/java/com/microsoft/rest/RestResponse.java b/client-runtime/src/main/java/com/microsoft/rest/RestResponse.java new file mode 100644 index 0000000000000..74b6c30d89b70 --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/RestResponse.java @@ -0,0 +1,36 @@ +package com.microsoft.rest; + +/** + * The response object that is a result of making a REST request. + * @param The deserialized type of the response headers. + */ +public class RestResponse { + private final int statusCode; + private final THeaders headers; + + /** + * Create a new RestResponse object. + * @param statusCode The status code of the HTTP response. + * @param headers The deserialized headers of the HTTP response. + */ + public RestResponse(int statusCode, THeaders headers) { + this.statusCode = statusCode; + this.headers = headers; + } + + /** + * The status code of the HTTP response. + * @return The status code of the HTTP response. + */ + public int statusCode() { + return statusCode; + } + + /** + * The deserialized headers of the HTTP response. + * @return The deserialized headers of the HTTP response. + */ + public THeaders headers() { + return headers; + } +} diff --git a/client-runtime/src/main/java/com/microsoft/rest/RestResponseWithBody.java b/client-runtime/src/main/java/com/microsoft/rest/RestResponseWithBody.java new file mode 100644 index 0000000000000..90dc60185ffb9 --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/RestResponseWithBody.java @@ -0,0 +1,24 @@ +package com.microsoft.rest; + +/** + * The REST response object that is a result of making a REST request. + * @param The deserialized type of the response body. + * @param The deserialized type of the response headers. + */ +public class RestResponseWithBody extends RestResponse { + private final TBody body; + + public RestResponseWithBody(int statusCode, THeaders headers, TBody body) { + super(statusCode, headers); + + this.body = body; + } + + /** + * The deserialized body of the HTTP response. + * @return The deserialized body of the HTTP response. + */ + public TBody body() { + return body; + } +} diff --git a/client-runtime/src/main/java/com/microsoft/rest/http/HttpHeaders.java b/client-runtime/src/main/java/com/microsoft/rest/http/HttpHeaders.java index 83d3733027119..ce8d0c193609e 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/http/HttpHeaders.java +++ b/client-runtime/src/main/java/com/microsoft/rest/http/HttpHeaders.java @@ -6,6 +6,8 @@ package com.microsoft.rest.http; +import org.json.JSONObject; + import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -107,6 +109,18 @@ private HttpHeader getHeader(String headerName) { return headers.get(headerKey); } + /** + * Convert this HttpHeaders collection to JSON. + * @return The JSON object that contains this HttpHeaders collection's values. + */ + public JSONObject toJSON() { + final JSONObject result = new JSONObject(); + for (final HttpHeader header : headers.values()) { + result.put(header.name(), header.value()); + } + return result; + } + @Override public Iterator iterator() { return headers.values().iterator(); diff --git a/client-runtime/src/test/java/com/microsoft/rest/HttpBinHeaders.java b/client-runtime/src/test/java/com/microsoft/rest/HttpBinHeaders.java new file mode 100644 index 0000000000000..4d9718d766ac5 --- /dev/null +++ b/client-runtime/src/test/java/com/microsoft/rest/HttpBinHeaders.java @@ -0,0 +1,31 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + * + * Code generated by Microsoft (R) AutoRest Code Generator. + */ + +package com.microsoft.rest; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Defines headers for httpbin.org operations. + */ +public class HttpBinHeaders { + @JsonProperty(value = "Date") + public DateTimeRfc1123 date; + + @JsonProperty(value = "Via") + public String via; + + @JsonProperty(value = "Connection") + public String connection; + + @JsonProperty(value = "X-Processed-Time") + public double xProcessedTime; + + @JsonProperty(value = "Access-Control-Allow-Credentials") + public boolean accessControlAllowCredentials; +} diff --git a/client-runtime/src/test/java/com/microsoft/rest/RestProxyTests.java b/client-runtime/src/test/java/com/microsoft/rest/RestProxyTests.java index 1b68962cd07ac..0e377823c68db 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/RestProxyTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/RestProxyTests.java @@ -25,7 +25,6 @@ import rx.Observable; import rx.Single; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.LinkedHashMap; @@ -1144,6 +1143,98 @@ public void service19PutWithBodyParamApplicationOctetStreamContentTypeAndByteArr assertEquals(new String(new byte[] { 0, 1, 2, 3, 4 }), result.data); } + @Host("http://httpbin.org") + private interface Service20 { + @GET("bytes/100") + RestResponse getBytes100OnlyHeaders(); + + @GET("bytes/100") + RestResponseWithBody getBytes100BodyAndHeaders(); + + @PUT("put") + RestResponse putOnlyHeaders(@BodyParam String body); + + @PUT("put") + RestResponseWithBody putBodyAndHeaders(@BodyParam String body); + } + + @Test + public void service20GetBytes100OnlyHeaders() { + final RestResponse response = createService(Service20.class) + .getBytes100OnlyHeaders(); + assertNotNull(response); + + assertEquals(200, response.statusCode()); + + final HttpBinHeaders headers = response.headers(); + assertNotNull(headers); + assertEquals(true, headers.accessControlAllowCredentials); + assertEquals("keep-alive", headers.connection); + assertNotNull(headers.date); + assertEquals("1.1 vegur", headers.via); + assertNotEquals(0, headers.xProcessedTime); + } + + @Test + public void service20GetBytes100BodyAndHeaders() { + final RestResponseWithBody response = createService(Service20.class) + .getBytes100BodyAndHeaders(); + assertNotNull(response); + + assertEquals(200, response.statusCode()); + + final byte[] body = response.body(); + assertNotNull(body); + assertEquals(100, body.length); + + final HttpBinHeaders headers = response.headers(); + assertNotNull(headers); + assertEquals(true, headers.accessControlAllowCredentials); + assertEquals("keep-alive", headers.connection); + assertNotNull(headers.date); + assertEquals("1.1 vegur", headers.via); + assertNotEquals(0, headers.xProcessedTime); + } + + @Test + public void service20PutOnlyHeaders() { + final RestResponse response = createService(Service20.class) + .putOnlyHeaders("body string"); + assertNotNull(response); + + assertEquals(200, response.statusCode()); + + final HttpBinHeaders headers = response.headers(); + assertNotNull(headers); + assertEquals(true, headers.accessControlAllowCredentials); + assertEquals("keep-alive", headers.connection); + assertNotNull(headers.date); + assertEquals("1.1 vegur", headers.via); + assertNotEquals(0, headers.xProcessedTime); + } + + @Test + public void service20PutBodyAndHeaders() { + final RestResponseWithBody response = createService(Service20.class) + .putBodyAndHeaders("body string"); + assertNotNull(response); + + assertEquals(200, response.statusCode()); + + final HttpBinJSON body = response.body(); + assertNotNull(body); + assertEquals("http://httpbin.org/put", body.url); + assertEquals("body string", body.data); + + final HttpBinHeaders headers = response.headers(); + assertNotNull(headers); + assertEquals(true, headers.accessControlAllowCredentials); + assertEquals("keep-alive", headers.connection); + assertNotNull(headers.date); + assertEquals("1.1 vegur", headers.via); + assertNotEquals(0, headers.xProcessedTime); + } + // Helpers private T createService(Class serviceClass) { final HttpClient httpClient = createHttpClient(); diff --git a/client-runtime/src/test/java/com/microsoft/rest/http/MockHttpClient.java b/client-runtime/src/test/java/com/microsoft/rest/http/MockHttpClient.java index 19f6b5a7a7842..7afc53d06de83 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/http/MockHttpClient.java +++ b/client-runtime/src/test/java/com/microsoft/rest/http/MockHttpClient.java @@ -23,6 +23,13 @@ * This HttpClient attempts to mimic the behavior of http://httpbin.org without ever making a network call. */ public class MockHttpClient extends HttpClient { + private static final HttpHeaders responseHeaders = new HttpHeaders() + .add("Date", "Fri, 13 Oct 2017 20:33:09 GMT") + .add("Via", "1.1 vegur") + .add("Connection", "keep-alive") + .add("X-Processed-Time", "1.0") + .add("Access-Control-Allow-Credentials", "true"); + public MockHttpClient() {} public MockHttpClient(List policyFactories) { @@ -54,7 +61,7 @@ protected Single sendRequestInternalAsync(HttpRequest request) { else if (requestPathLower.startsWith("/bytes/")) { final String byteCountString = requestPath.substring("/bytes/".length()); final int byteCount = Integer.parseInt(byteCountString); - response = new MockHttpResponse(200, new byte[byteCount]); + response = new MockHttpResponse(200, new byte[byteCount], responseHeaders); } else if (requestPathLower.equals("/delete")) { final HttpBinJSON json = new HttpBinJSON(); @@ -84,7 +91,7 @@ else if (requestPathLower.equals("/put")) { final HttpBinJSON json = new HttpBinJSON(); json.url = request.url(); json.data = bodyToString(request); - response = new MockHttpResponse(200, json); + response = new MockHttpResponse(200, json, responseHeaders); } else if (requestPathLower.startsWith("/status/")) { final String statusCodeString = requestPathLower.substring("/status/".length()); diff --git a/client-runtime/src/test/java/com/microsoft/rest/http/MockHttpResponse.java b/client-runtime/src/test/java/com/microsoft/rest/http/MockHttpResponse.java index 0b6c446afb3f6..4271ae921556e 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/http/MockHttpResponse.java +++ b/client-runtime/src/test/java/com/microsoft/rest/http/MockHttpResponse.java @@ -23,13 +23,17 @@ public class MockHttpResponse extends HttpResponse { private final byte[] bodyBytes; - public MockHttpResponse(int statusCode, byte[] bodyBytes) { - this.headers = new HttpHeaders(); + public MockHttpResponse(int statusCode, byte[] bodyBytes, HttpHeaders headers) { + this.headers = headers; this.statusCode = statusCode; this.bodyBytes = bodyBytes; } + public MockHttpResponse(int statusCode, byte[] bodyBytes) { + this(statusCode, bodyBytes, new HttpHeaders()); + } + public MockHttpResponse(int statusCode) { this(statusCode, (byte[])null); } @@ -38,6 +42,10 @@ public MockHttpResponse(int statusCode, String string) { this(statusCode, string == null ? null : string.getBytes()); } + public MockHttpResponse(int statusCode, Object serializable, HttpHeaders headers) { + this(statusCode, serialize(serializable), headers); + } + public MockHttpResponse(int statusCode, Object serializable) { this(statusCode, serialize(serializable)); } diff --git a/pom.xml b/pom.xml index 264b341c21a5f..72fd7fe8800a9 100644 --- a/pom.xml +++ b/pom.xml @@ -135,6 +135,11 @@ netty-codec-http 4.1.3.Final + + org.json + json + 20170516 + From 7dee987965d292711a5834ef2bddbc240360d521 Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Fri, 13 Oct 2017 14:05:43 -0700 Subject: [PATCH 2/3] Fix checkstyle --- .../main/java/com/microsoft/rest/RestProxy.java | 6 +++--- .../main/java/com/microsoft/rest/RestResponse.java | 6 ++++++ .../com/microsoft/rest/RestResponseWithBody.java | 14 +++++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java b/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java index cf7a646186eb1..ce2739fe0326f 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java @@ -269,16 +269,16 @@ private Single toProxyReturnValueAsync(final HttpResponse response, final Str final TypeToken deserializedBodyTypeToken = TypeToken.of(deserializedBodyType); if (deserializedBodyTypeToken.isSubtypeOf(byte[].class)) { asyncResult = response.bodyAsByteArrayAsync() - .map(new Func1>() { + .map(new Func1>() { @Override - public RestResponseWithBody call(byte[] responseBodyBytes) { + public RestResponseWithBody call(byte[] responseBodyBytes) { return new RestResponseWithBody<>(responseStatusCode, deserializedHeaders, responseBodyBytes); } }); } else { asyncResult = response.bodyAsStringAsync() - .map(new Func1>() { + .map(new Func1>() { @Override public RestResponseWithBody call(String responseBodyString) { final Object deserializedBody = deserialize(responseBodyString, deserializedBodyType); diff --git a/client-runtime/src/main/java/com/microsoft/rest/RestResponse.java b/client-runtime/src/main/java/com/microsoft/rest/RestResponse.java index 74b6c30d89b70..be1490f4d19da 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestResponse.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestResponse.java @@ -1,3 +1,9 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + package com.microsoft.rest; /** diff --git a/client-runtime/src/main/java/com/microsoft/rest/RestResponseWithBody.java b/client-runtime/src/main/java/com/microsoft/rest/RestResponseWithBody.java index 90dc60185ffb9..7c27680421a15 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestResponseWithBody.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestResponseWithBody.java @@ -1,3 +1,9 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + package com.microsoft.rest; /** @@ -5,9 +11,15 @@ * @param The deserialized type of the response body. * @param The deserialized type of the response headers. */ -public class RestResponseWithBody extends RestResponse { +public class RestResponseWithBody extends RestResponse { private final TBody body; + /** + * Create a new RestResponseWithBody object. + * @param statusCode The status code for the REST response. + * @param headers The deserialized headers. + * @param body The deserialized body. + */ public RestResponseWithBody(int statusCode, THeaders headers, TBody body) { super(statusCode, headers); From e488375cd8d154f56f57f7abd1a7228cef81672f Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Fri, 13 Oct 2017 14:58:22 -0700 Subject: [PATCH 3/3] Remove org.json dependency, make HttpHeaders implement JsonSerializable --- client-runtime/pom.xml | 4 --- .../java/com/microsoft/rest/RestProxy.java | 10 ++++--- .../com/microsoft/rest/http/HttpHeaders.java | 26 ++++++++++++++----- pom.xml | 5 ---- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/client-runtime/pom.xml b/client-runtime/pom.xml index 72f96e34afa23..b3e18b2cc9982 100644 --- a/client-runtime/pom.xml +++ b/client-runtime/pom.xml @@ -99,10 +99,6 @@ com.microsoft.azure azure-annotations - - org.json - json - junit junit diff --git a/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java b/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java index ce2739fe0326f..2b47bb628c6fd 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java @@ -15,7 +15,6 @@ import com.microsoft.rest.http.HttpRequest; import com.microsoft.rest.http.HttpResponse; import com.microsoft.rest.http.UrlBuilder; -import org.json.JSONObject; import rx.Completable; import rx.Observable; import rx.Single; @@ -301,9 +300,12 @@ public Object call(String responseBodyString) { } private Object deserializeHeaders(HttpHeaders headers, Type deserializedHeadersType) { - final JSONObject headersJson = headers.toJSON(); - final String headersJsonString = headersJson.toString(); - return deserialize(headersJsonString, deserializedHeadersType); + try { + final String headersJsonString = serializer.serialize(headers); + return deserialize(headersJsonString, deserializedHeadersType); + } catch (IOException e) { + throw Exceptions.propagate(e); + } } protected Object handleAsyncHttpResponse(HttpRequest httpRequest, Single asyncHttpResponse, SwaggerMethodParser methodParser, Type returnType) { diff --git a/client-runtime/src/main/java/com/microsoft/rest/http/HttpHeaders.java b/client-runtime/src/main/java/com/microsoft/rest/http/HttpHeaders.java index ce8d0c193609e..caee669e48974 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/http/HttpHeaders.java +++ b/client-runtime/src/main/java/com/microsoft/rest/http/HttpHeaders.java @@ -6,8 +6,12 @@ package com.microsoft.rest.http; -import org.json.JSONObject; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializable; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.jsontype.TypeSerializer; +import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -15,7 +19,7 @@ /** * A collection of headers that will be applied to a HTTP request. */ -public class HttpHeaders implements Iterable { +public class HttpHeaders implements Iterable, JsonSerializable { private final Map headers = new HashMap<>(); /** @@ -110,11 +114,11 @@ private HttpHeader getHeader(String headerName) { } /** - * Convert this HttpHeaders collection to JSON. - * @return The JSON object that contains this HttpHeaders collection's values. + * Convert this HttpHeaders collection to a Map. + * @return The Map representation of this HttpHeaders collection. */ - public JSONObject toJSON() { - final JSONObject result = new JSONObject(); + public Map toMap() { + final Map result = new HashMap<>(); for (final HttpHeader header : headers.values()) { result.put(header.name(), header.value()); } @@ -125,4 +129,14 @@ public JSONObject toJSON() { public Iterator iterator() { return headers.values().iterator(); } + + @Override + public void serialize(JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { + jsonGenerator.writeObject(toMap()); + } + + @Override + public void serializeWithType(JsonGenerator jsonGenerator, SerializerProvider serializerProvider, TypeSerializer typeSerializer) throws IOException { + serialize(jsonGenerator, serializerProvider); + } } diff --git a/pom.xml b/pom.xml index 72fd7fe8800a9..264b341c21a5f 100644 --- a/pom.xml +++ b/pom.xml @@ -135,11 +135,6 @@ netty-codec-http 4.1.3.Final - - org.json - json - 20170516 -