From 7ad505c0e9127eaad8c549792b19b73f6ae90698 Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Mon, 18 Jul 2016 09:52:42 -0700 Subject: [PATCH] Revert "Revert "Changes from AutoRest & SDK"" --- azure-client-authentication/build.gradle | 8 +- azure-client-runtime/build.gradle | 8 +- build-tools/src/main/resources/checkstyle.xml | 1 - client-runtime/build.gradle | 8 +- .../java/com/microsoft/rest/Base64Url.java | 94 +++++++++++++++++++ .../com/microsoft/rest/ServiceClient.java | 3 + .../rest/serializer/Base64UrlSerializer.java | 38 ++++++++ .../rest/serializer/JacksonMapperAdapter.java | 1 + 8 files changed, 148 insertions(+), 13 deletions(-) create mode 100644 client-runtime/src/main/java/com/microsoft/rest/Base64Url.java create mode 100644 client-runtime/src/main/java/com/microsoft/rest/serializer/Base64UrlSerializer.java diff --git a/azure-client-authentication/build.gradle b/azure-client-authentication/build.gradle index 2b40ada004e0e..65c1c86a20a36 100644 --- a/azure-client-authentication/build.gradle +++ b/azure-client-authentication/build.gradle @@ -14,9 +14,9 @@ version = '1.0.0-SNAPSHOT' checkstyle { toolVersion = "6.18" - configFile = new File("$rootDir/ClientRuntimes/Java/build-tools/src/main/resources/checkstyle.xml") - configProperties = [samedir: "$rootDir/ClientRuntimes/Java/build-tools/src/main/resources"] - reportsDir = new File("$rootDir/ClientRuntimes/Java/build-tools/reports") + configFile = new File("$rootDir/src/client/Java/build-tools/src/main/resources/checkstyle.xml") + configProperties = [samedir: "$rootDir/src/client/Java/build-tools/src/main/resources"] + reportsDir = new File("$rootDir/src/client/Java/build-tools/reports") } dependencies { @@ -92,7 +92,7 @@ artifacts { test { reports.getHtml() - reports.html.destination = file("${projectDir}/../../../TestResults/JavaAzureRuntime") + reports.html.destination = file("$rootDir/TestResults/JavaAzureRuntime") } tasks.compileJava.dependsOn 'clean' diff --git a/azure-client-runtime/build.gradle b/azure-client-runtime/build.gradle index 5c29359536abc..db64fd0b46abd 100644 --- a/azure-client-runtime/build.gradle +++ b/azure-client-runtime/build.gradle @@ -14,9 +14,9 @@ version = '1.0.0-SNAPSHOT' checkstyle { toolVersion = "6.18" - configFile = new File("$rootDir/ClientRuntimes/Java/build-tools/src/main/resources/checkstyle.xml") - configProperties = [samedir: "$rootDir/ClientRuntimes/Java/build-tools/src/main/resources"] - reportsDir = new File("$rootDir/ClientRuntimes/Java/build-tools/reports") + configFile = new File("$rootDir/src/client/Java/build-tools/src/main/resources/checkstyle.xml") + configProperties = [samedir: "$rootDir/src/client/Java/build-tools/src/main/resources"] + reportsDir = new File("$rootDir/src/client/Java/build-tools/reports") } dependencies { @@ -90,7 +90,7 @@ artifacts { test { reports.getHtml() - reports.html.destination = file("${projectDir}/../../../TestResults/JavaAzureRuntime") + reports.html.destination = file("$rootDir/TestResults/JavaAzureRuntime") } tasks.compileJava.dependsOn 'clean' diff --git a/build-tools/src/main/resources/checkstyle.xml b/build-tools/src/main/resources/checkstyle.xml index 1875d6f100cab..b7f934898253c 100644 --- a/build-tools/src/main/resources/checkstyle.xml +++ b/build-tools/src/main/resources/checkstyle.xml @@ -231,7 +231,6 @@ --> - diff --git a/client-runtime/build.gradle b/client-runtime/build.gradle index 73750c0d3d935..e530072694672 100644 --- a/client-runtime/build.gradle +++ b/client-runtime/build.gradle @@ -16,9 +16,9 @@ version = '1.0.0-SNAPSHOT' checkstyle { toolVersion = "6.18" - configFile = new File("$rootDir/ClientRuntimes/Java/build-tools/src/main/resources/checkstyle.xml") - configProperties = [samedir: "$rootDir/ClientRuntimes/Java/build-tools/src/main/resources"] - reportsDir = new File("$rootDir/ClientRuntimes/Java/build-tools/reports") + configFile = new File("$rootDir/src/client/Java/build-tools/src/main/resources/checkstyle.xml") + configProperties = [samedir: "$rootDir/src/client/Java/build-tools/src/main/resources"] + reportsDir = new File("$rootDir/src/client/Java/build-tools/reports") } dependencies { @@ -99,7 +99,7 @@ artifacts { test { reports.getHtml() - reports.html.destination = file("${projectDir}/../../../TestResults/JavaRuntime") + reports.html.destination = file("$rootDir/TestResults/JavaRuntime") } tasks.compileJava.dependsOn 'clean' diff --git a/client-runtime/src/main/java/com/microsoft/rest/Base64Url.java b/client-runtime/src/main/java/com/microsoft/rest/Base64Url.java new file mode 100644 index 0000000000000..61b02879feae1 --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/Base64Url.java @@ -0,0 +1,94 @@ +/** + * + * 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; + +import com.google.common.io.BaseEncoding; + +import java.util.Arrays; + +/** + * Simple wrapper over Base64Url encoded byte array used during serialization/deserialization. + */ +public final class Base64Url { + /** + * The Base64Url encoded bytes. + */ + private final byte[] bytes; + + /** + * Creates a new Base64Url object with the specified encoded string. + * + * @param string The encoded string. + */ + private Base64Url(String string) { + if (string == null) { + this.bytes = null; + } else { + this.bytes = string.getBytes(); + } + } + + /** + * Encode a byte array into Base64Url encoded bytes. + * + * @param bytes The byte array to encode. + * @return a Base64Url instance + */ + public static Base64Url encode(byte[] bytes) { + if (bytes == null) { + return new Base64Url(null); + } else { + return new Base64Url(BaseEncoding.base64Url().omitPadding().encode(bytes)); + } + } + + /** + * Returns the underlying encoded byte array. + * + * @return The underlying encoded byte array. + */ + public byte[] getEncodedBytes() { + return bytes; + } + + /** + * Decode the bytes and return. + * + * @return The decoded byte array. + */ + public byte[] getDecodedBytes() { + if (this.bytes == null) { + return null; + } + return BaseEncoding.base64Url().decode(new String(bytes)); + } + + @Override + public String toString() { + return new String(bytes); + } + + @Override + public int hashCode() { + return Arrays.hashCode(bytes); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + + if (!(obj instanceof Base64Url)) { + return false; + } + + Base64Url rhs = (Base64Url) obj; + return Arrays.equals(this.bytes, rhs.getEncodedBytes()); + } +} \ No newline at end of file diff --git a/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java b/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java index 8311a243de139..5d6f0f850336c 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/ServiceClient.java @@ -40,6 +40,9 @@ protected ServiceClient(String baseUrl) { /** * Initializes a new instance of the ServiceClient class. * + * @param baseUrl the service base uri + * @param clientBuilder the http client builder + * @param restBuilder the retrofit rest client builder */ protected ServiceClient(String baseUrl, OkHttpClient.Builder clientBuilder, Retrofit.Builder restBuilder) { if (clientBuilder == null) { diff --git a/client-runtime/src/main/java/com/microsoft/rest/serializer/Base64UrlSerializer.java b/client-runtime/src/main/java/com/microsoft/rest/serializer/Base64UrlSerializer.java new file mode 100644 index 0000000000000..757ab735e2c0d --- /dev/null +++ b/client-runtime/src/main/java/com/microsoft/rest/serializer/Base64UrlSerializer.java @@ -0,0 +1,38 @@ +/** + * + * 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.serializer; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.microsoft.rest.Base64Url; + +import java.io.IOException; + +/** + * Custom serializer for serializing {@link Byte[]} objects into Base64 strings. + */ +public class Base64UrlSerializer extends JsonSerializer { + /** + * Gets a module wrapping this serializer as an adapter for the Jackson + * ObjectMapper. + * + * @return a simple module to be plugged onto Jackson ObjectMapper. + */ + public static SimpleModule getModule() { + SimpleModule module = new SimpleModule(); + module.addSerializer(Base64Url.class, new Base64UrlSerializer()); + return module; + } + + @Override + public void serialize(Base64Url value, JsonGenerator jgen, SerializerProvider provider) throws IOException { + jgen.writeString(value.toString()); + } +} diff --git a/client-runtime/src/main/java/com/microsoft/rest/serializer/JacksonMapperAdapter.java b/client-runtime/src/main/java/com/microsoft/rest/serializer/JacksonMapperAdapter.java index a59247207502b..ea938fe50c9c5 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/serializer/JacksonMapperAdapter.java +++ b/client-runtime/src/main/java/com/microsoft/rest/serializer/JacksonMapperAdapter.java @@ -56,6 +56,7 @@ protected void initializeObjectMapper(ObjectMapper mapper) { .setSerializationInclusion(JsonInclude.Include.NON_NULL) .registerModule(new JodaModule()) .registerModule(ByteArraySerializer.getModule()) + .registerModule(Base64UrlSerializer.getModule()) .registerModule(DateTimeSerializer.getModule()) .registerModule(DateTimeRfc1123Serializer.getModule()) .registerModule(HeadersSerializer.getModule());