forked from Azure/autorest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93de90c
commit b348fdd
Showing
18 changed files
with
369 additions
and
82 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
src/client/Java/client-runtime/src/main/java/com/microsoft/rest/Base64Url.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../Java/client-runtime/src/main/java/com/microsoft/rest/serializer/Base64UrlSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Base64Url> { | ||
/** | ||
* 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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.