зеркало из https://github.com/Azure/autorest.git
Java client runtime code check
This commit is contained in:
Родитель
394ac9a757
Коммит
ed9e17d520
|
@ -137,6 +137,7 @@ local.properties
|
|||
.project
|
||||
target
|
||||
build
|
||||
reports
|
||||
.gradle
|
||||
.idea
|
||||
*.iml
|
||||
|
|
|
@ -31,7 +31,7 @@ public abstract class AzureServiceClient extends ServiceClient {
|
|||
this.client.setCookieHandler(cookieManager);
|
||||
|
||||
Executor executor = Executors.newCachedThreadPool();
|
||||
this.retrofitBuilder = this.retrofitBuilder
|
||||
this.retrofitBuilder
|
||||
.addConverterFactory(new AzureJacksonUtils().getConverterFactory())
|
||||
.callbackExecutor(executor);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ buildscript {
|
|||
apply plugin: 'java'
|
||||
apply plugin: 'maven'
|
||||
apply plugin: 'com.github.johnrengelman.shadow'
|
||||
apply plugin: 'checkstyle'
|
||||
|
||||
group = 'com.microsoft.rest'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
|
@ -18,11 +19,14 @@ configurations {
|
|||
deployerJars
|
||||
}
|
||||
|
||||
checkstyle {
|
||||
configFile = new File("config/checkstyle/checkstyle.xml")
|
||||
reportsDir = new File("config/checkstyle/reports")
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven {
|
||||
url "https://oss.sonatype.org/content/repositories/snapshots/"
|
||||
}
|
||||
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
|
|
@ -0,0 +1,265 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE module PUBLIC
|
||||
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
|
||||
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
|
||||
|
||||
<!--
|
||||
|
||||
Checkstyle configuration that checks the sun coding conventions from:
|
||||
|
||||
- the Java Language Specification at
|
||||
http://java.sun.com/docs/books/jls/second_edition/html/index.html
|
||||
|
||||
- the Sun Code Conventions at http://java.sun.com/docs/codeconv/
|
||||
|
||||
- the Javadoc guidelines at
|
||||
http://java.sun.com/j2se/javadoc/writingdoccomments/index.html
|
||||
|
||||
- the JDK Api documentation http://java.sun.com/j2se/docs/api/index.html
|
||||
|
||||
- some best practices
|
||||
|
||||
Checkstyle is very configurable. Be sure to read the documentation at
|
||||
http://checkstyle.sf.net (or in your downloaded distribution).
|
||||
|
||||
Most Checks are configurable, be sure to consult the documentation.
|
||||
|
||||
To completely disable a check, just comment it out or delete it from the file.
|
||||
|
||||
Finally, it is worth reading the documentation.
|
||||
|
||||
-->
|
||||
|
||||
<module name="Checker">
|
||||
<module name="SuppressWithNearbyCommentFilter">
|
||||
<property name="commentFormat" value="CHECKSTYLE IGNORE (\w+) FOR NEXT (\d+) LINE"/>
|
||||
<property name="checkFormat" value="$1"/>
|
||||
<property name="influenceFormat" value="$2"/>
|
||||
</module>
|
||||
|
||||
<!--
|
||||
If you set the basedir property below, then all reported file
|
||||
names will be relative to the specified directory. See
|
||||
http://checkstyle.sourceforge.net/5.x/config.html#Checker
|
||||
|
||||
<property name="basedir" value="${basedir}"/>
|
||||
-->
|
||||
|
||||
<!-- Checks that each Java package has a Javadoc file used for commenting. -->
|
||||
<!-- See http://checkstyle.sf.net/config_javadoc.html#JavadocPackage -->
|
||||
<module name="JavadocPackage">
|
||||
<property name="allowLegacy" value="true"/>
|
||||
</module>
|
||||
|
||||
<!-- Checks whether files end with a new line. -->
|
||||
<!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile -->
|
||||
|
||||
<!-- Removed on 11/02/2012
|
||||
Justification: The rule incorrectly flags all files as violating
|
||||
this rule, regardless of what they end with.
|
||||
<module name="NewlineAtEndOfFile"/>
|
||||
-->
|
||||
|
||||
<!-- Checks that property files contain the same keys. -->
|
||||
<!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
|
||||
<module name="Translation"/>
|
||||
|
||||
<!-- Removed on 20/01/2014
|
||||
Justification: generated code.
|
||||
<module name="FileLength"/> -->
|
||||
|
||||
<!-- Following interprets the header file as regular expressions. -->
|
||||
<!-- <module name="RegexpHeader"/> -->
|
||||
|
||||
<module name="FileTabCharacter">
|
||||
<property name="eachLine" value="true"/>
|
||||
</module>
|
||||
|
||||
<!-- Removed on 11/02/2012:
|
||||
Justification: Over 5000 hits in generated boilerplate comments.
|
||||
Use Eclipse preference file to auto-remove EOL whitespace in code.
|
||||
|
||||
<module name="RegexpSingleline">
|
||||
<property name="format" value="\s+$"/>
|
||||
<property name="message" value="Line has trailing spaces."/>
|
||||
</module>
|
||||
-->
|
||||
|
||||
<module name="TreeWalker">
|
||||
<!-- Checks for Javadoc comments. -->
|
||||
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
|
||||
|
||||
<!-- Removed on 11/02/2012
|
||||
Justification: Many methods that could be documented are
|
||||
intentionally undocumented because they are not part of
|
||||
the public surface SDK area.
|
||||
|
||||
<module name="JavadocMethod"/>
|
||||
<module name="JavadocType"/>
|
||||
<module name="JavadocVariable"/>
|
||||
<module name="JavadocStyle"/>
|
||||
-->
|
||||
|
||||
|
||||
<!-- Checks for Naming Conventions. -->
|
||||
<!-- See http://checkstyle.sf.net/config_naming.html -->
|
||||
<module name="ConstantName"/>
|
||||
<module name="LocalFinalVariableName"/>
|
||||
<module name="LocalVariableName"/>
|
||||
<module name="MemberName"/>
|
||||
<module name="MethodName"/>
|
||||
<module name="PackageName"/>
|
||||
<module name="ParameterName"/>
|
||||
<module name="StaticVariableName"/>
|
||||
<module name="TypeName"/>
|
||||
|
||||
|
||||
<!-- Checks for Headers -->
|
||||
<!-- See http://checkstyle.sf.net/config_header.html -->
|
||||
<!-- <module name="Header"> -->
|
||||
<!-- The follow property value demonstrates the ability -->
|
||||
<!-- to have access to ANT properties. In this case it uses -->
|
||||
<!-- the ${basedir} property to allow Checkstyle to be run -->
|
||||
<!-- from any directory within a project. See property -->
|
||||
<!-- expansion, -->
|
||||
<!-- http://checkstyle.sf.net/config.html#properties -->
|
||||
<!-- <property -->
|
||||
<!-- name="headerFile" -->
|
||||
<!-- value="${basedir}/java.header"/> -->
|
||||
<!-- </module> -->
|
||||
|
||||
|
||||
<!-- Checks for imports -->
|
||||
<!-- See http://checkstyle.sf.net/config_import.html -->
|
||||
<module name="AvoidStarImport"/>
|
||||
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
|
||||
<module name="RedundantImport"/>
|
||||
<module name="UnusedImports"/>
|
||||
|
||||
|
||||
<!-- Checks for Size Violations. -->
|
||||
<!-- See http://checkstyle.sf.net/config_sizes.html -->
|
||||
|
||||
<!-- Removed on 11/02/2012:
|
||||
Justification: The project properties automatically breaks
|
||||
lines longer than 120 characters. Any lines that are longer
|
||||
cannot be broken.
|
||||
<module name="LineLength">
|
||||
<property name="max" value="160" />
|
||||
</module>
|
||||
-->
|
||||
<!-- Removed on 20/01/2014
|
||||
Justification: a lot of the code is generated.
|
||||
As such, method length is not a true representation
|
||||
of complexity / etc issues.
|
||||
<module name="MethodLength"/> -->
|
||||
<module name="ParameterNumber"/>
|
||||
|
||||
|
||||
<!-- Checks for whitespace -->
|
||||
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
|
||||
<module name="EmptyForIteratorPad"/>
|
||||
<module name="MethodParamPad"/>
|
||||
<module name="NoWhitespaceAfter"/>
|
||||
<module name="NoWhitespaceBefore"/>
|
||||
<module name="OperatorWrap"/>
|
||||
<module name="ParenPad"/>
|
||||
<module name="TypecastParenPad"/>
|
||||
<module name="WhitespaceAfter"/>
|
||||
<module name="WhitespaceAround"/>
|
||||
|
||||
|
||||
<!-- Modifier Checks -->
|
||||
<!-- See http://checkstyle.sf.net/config_modifiers.html -->
|
||||
<module name="ModifierOrder"/>
|
||||
<module name="RedundantModifier"/>
|
||||
<module name="FileContentsHolder"/>
|
||||
|
||||
|
||||
<!-- Checks for blocks. You know, those {}'s -->
|
||||
<!-- See http://checkstyle.sf.net/config_blocks.html -->
|
||||
<module name="AvoidNestedBlocks"/>
|
||||
<!-- Removed on 12/04/2014:
|
||||
Justification: 1.x.x candidate. Not necessary for now.
|
||||
<module name="EmptyBlock"/>
|
||||
-->
|
||||
<module name="LeftCurly" />
|
||||
<module name="NeedBraces"/>
|
||||
|
||||
<!-- Removed on 11/02/2012:
|
||||
Justification: The Azure SDK coding guidelines
|
||||
are to have the right curly on a separate line.
|
||||
<module name="RightCurly"/>
|
||||
-->
|
||||
|
||||
|
||||
<!-- Checks for common coding problems -->
|
||||
<!-- See http://checkstyle.sf.net/config_coding.html -->
|
||||
<!-- Removed on 14/1/2014:
|
||||
Justification: The Azure SDK coding guidelines
|
||||
are that inline conditionals can actually make the
|
||||
code more readable.
|
||||
<module name="AvoidInlineConditionals"/> -->
|
||||
|
||||
<module name="EmptyStatement"/>
|
||||
<module name="EqualsHashCode"/>
|
||||
<!-- Removed on 11/02/2012:
|
||||
Justification: The Azure SDK coding guidelines
|
||||
are that the setter and constructor parameter names match
|
||||
the names of the fields that are being set.
|
||||
<module name="HiddenField"/>
|
||||
-->
|
||||
|
||||
<module name="IllegalInstantiation"/>
|
||||
<module name="InnerAssignment"/>
|
||||
|
||||
<!-- Updated on 03/17/2014: -->
|
||||
<!-- Added ignore. Code is generated so magic numbers are not a largish issue. -->
|
||||
<!-- <module name="MagicNumber" /> -->
|
||||
<module name="MissingSwitchDefault"/>
|
||||
<module name="RedundantThrows">
|
||||
<property name="allowUnchecked" value="true"/>
|
||||
<property name="allowSubclasses" value="true"/>
|
||||
<property name="suppressLoadErrors" value="true"/>
|
||||
</module>
|
||||
<!-- <module name="SimplifyBooleanExpression"/> -->
|
||||
<!-- <module name="SimplifyBooleanReturn"/> -->
|
||||
|
||||
<!-- Checks for class design -->
|
||||
<!-- See http://checkstyle.sf.net/config_design.html -->
|
||||
|
||||
<!-- Removed on 11/02/2012:
|
||||
Justification: None of the SDK classes are intended
|
||||
to be subclassed by end-users, but at the same time we don't
|
||||
want to mark them final and prevent derivation for scenarios
|
||||
like mocking, etc.
|
||||
<module name="DesignForExtension"/>
|
||||
-->
|
||||
|
||||
<module name="FinalClass"/>
|
||||
<module name="HideUtilityClassConstructor"/>
|
||||
<module name="InterfaceIsType"/>
|
||||
<module name="VisibilityModifier">
|
||||
<property name="protectedAllowed" value="true"/>
|
||||
</module>
|
||||
|
||||
|
||||
<!-- Miscellaneous other checks. -->
|
||||
<!-- See http://checkstyle.sf.net/config_misc.html -->
|
||||
<module name="ArrayTypeStyle"/>
|
||||
|
||||
<!-- Removed on 11/02/2012:
|
||||
Justification: The cost to implement and verify the changes
|
||||
outweighs the benefit here.
|
||||
<module name="FinalParameters"/>
|
||||
-->
|
||||
|
||||
<!-- Removed on 12/04/2014:
|
||||
Justification: TODO comments are fine in a 0.x.x version
|
||||
<module name="TodoComment"/>
|
||||
-->
|
||||
<module name="UpperEll"/>
|
||||
|
||||
</module>
|
||||
|
||||
</module>
|
|
@ -53,15 +53,15 @@ public class DateTimeRfc1123 {
|
|||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj == null) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!(obj instanceof DateTimeRfc1123)) {
|
||||
if (!(obj instanceof DateTimeRfc1123)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DateTimeRfc1123 rhs = (DateTimeRfc1123)obj;
|
||||
DateTimeRfc1123 rhs = (DateTimeRfc1123) obj;
|
||||
return this.dateTime.equals(rhs.getDateTime());
|
||||
}
|
||||
}
|
|
@ -25,8 +25,9 @@ import java.util.concurrent.Executors;
|
|||
* ServiceClient is the abstraction for accessing REST operations and their payload data types.
|
||||
*/
|
||||
public abstract class ServiceClient {
|
||||
protected OkHttpClient client;
|
||||
protected Retrofit.Builder retrofitBuilder;
|
||||
protected final OkHttpClient client;
|
||||
protected final Retrofit.Builder retrofitBuilder;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the ServiceClient class.
|
||||
*/
|
||||
|
@ -38,7 +39,7 @@ public abstract class ServiceClient {
|
|||
this.client.setCookieHandler(cookieManager);
|
||||
|
||||
Executor executor = Executors.newCachedThreadPool();
|
||||
this.retrofitBuilder = this.retrofitBuilder
|
||||
this.retrofitBuilder
|
||||
.addConverterFactory(new JacksonUtils().getConverterFactory())
|
||||
.callbackExecutor(executor);
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ public class ServiceException extends Exception {
|
|||
/**
|
||||
* Initializes a new instance of the ServiceException class.
|
||||
*/
|
||||
public ServiceException() {}
|
||||
public ServiceException() { }
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the ServiceException class.
|
||||
|
|
|
@ -121,8 +121,8 @@ public class ServiceResponseBuilder<T> {
|
|||
|
||||
if (responseTypes.containsKey(statusCode)) {
|
||||
return new ServiceResponse<T>(buildBody(statusCode, responseBody), response);
|
||||
} else if (response.isSuccess() &&
|
||||
(responseTypes.isEmpty() || (responseTypes.size() == 1 && responseTypes.containsKey(0)))) {
|
||||
} else if (response.isSuccess()
|
||||
&& (responseTypes.isEmpty() || (responseTypes.size() == 1 && responseTypes.containsKey(0)))) {
|
||||
return new ServiceResponse<T>(buildBody(statusCode, responseBody), response);
|
||||
} else {
|
||||
ServiceException exception = new ServiceException("Invalid status code " + statusCode);
|
||||
|
@ -136,8 +136,8 @@ public class ServiceResponseBuilder<T> {
|
|||
int statusCode = response.code();
|
||||
if (responseTypes.containsKey(statusCode)) {
|
||||
return new ServiceResponse<T>(null, response);
|
||||
} else if (response.isSuccess() &&
|
||||
(responseTypes.isEmpty() || (responseTypes.size() == 1 && responseTypes.containsKey(0)))) {
|
||||
} else if (response.isSuccess()
|
||||
&& (responseTypes.isEmpty() || (responseTypes.size() == 1 && responseTypes.containsKey(0)))) {
|
||||
return new ServiceResponse<T>(null, response);
|
||||
} else {
|
||||
ServiceException exception = new ServiceException();
|
||||
|
@ -158,7 +158,7 @@ public class ServiceResponseBuilder<T> {
|
|||
} else if (responseTypes.containsKey(0)) {
|
||||
type = responseTypes.get(0);
|
||||
} else {
|
||||
type = new TypeReference<T>() {}.getType();
|
||||
type = new TypeReference<T>() { }.getType();
|
||||
}
|
||||
|
||||
// Void response
|
||||
|
@ -167,7 +167,7 @@ public class ServiceResponseBuilder<T> {
|
|||
}
|
||||
// Return raw response if InputStream is the target type
|
||||
else if (type == InputStream.class) {
|
||||
return (T)responseBody.byteStream();
|
||||
return (T) responseBody.byteStream();
|
||||
}
|
||||
// Deserialize
|
||||
else {
|
||||
|
|
|
@ -13,7 +13,9 @@ import java.net.URLEncoder;
|
|||
/**
|
||||
* An instance of this class holds a response object and a raw REST response.
|
||||
*/
|
||||
public class URLEncoderAdapter {
|
||||
public final class URLEncoderAdapter {
|
||||
private URLEncoderAdapter() { }
|
||||
|
||||
public static String encode(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
|
|
|
@ -22,7 +22,9 @@ import java.util.Map;
|
|||
/**
|
||||
* Validates user provided parameters are not null if they are required.
|
||||
*/
|
||||
public class Validator {
|
||||
public final class Validator {
|
||||
private Validator() { }
|
||||
|
||||
/**
|
||||
* Validates a user provided required parameter to be not null.
|
||||
* An {@link IllegalArgumentException} is thrown if a property fails the validation.
|
||||
|
@ -41,18 +43,17 @@ public class Validator {
|
|||
if (Primitives.isWrapperType(parameterType)) {
|
||||
parameterToken = parameterToken.unwrap();
|
||||
}
|
||||
if (parameterToken.isPrimitive() ||
|
||||
parameterType.isEnum() ||
|
||||
parameterToken.isAssignableFrom(LocalDate.class) ||
|
||||
parameterToken.isAssignableFrom(DateTime.class) ||
|
||||
parameterToken.isAssignableFrom(String.class) ||
|
||||
parameterToken.isAssignableFrom(DateTimeRfc1123.class) ||
|
||||
parameterToken.isAssignableFrom(Period.class)) {
|
||||
if (parameterToken.isPrimitive()
|
||||
|| parameterType.isEnum()
|
||||
|| parameterToken.isAssignableFrom(LocalDate.class)
|
||||
|| parameterToken.isAssignableFrom(DateTime.class)
|
||||
|| parameterToken.isAssignableFrom(String.class)
|
||||
|| parameterToken.isAssignableFrom(DateTimeRfc1123.class)
|
||||
|| parameterToken.isAssignableFrom(Period.class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
for (Class<?> c : parameterToken.getTypes().classes().rawTypes()){
|
||||
for (Class<?> c : parameterToken.getTypes().classes().rawTypes()) {
|
||||
for (Field field : c.getDeclaredFields()) {
|
||||
field.setAccessible(true);
|
||||
JsonProperty annotation = field.getAnnotation(JsonProperty.class);
|
||||
|
@ -69,14 +70,14 @@ public class Validator {
|
|||
} else {
|
||||
try {
|
||||
Class<?> propertyType = property.getClass();
|
||||
if (TypeToken.of(List.class).isAssignableFrom(propertyType)){
|
||||
List<?> items = (List<?>)property;
|
||||
if (TypeToken.of(List.class).isAssignableFrom(propertyType)) {
|
||||
List<?> items = (List<?>) property;
|
||||
for (Object item : items) {
|
||||
Validator.validate(item);
|
||||
}
|
||||
}
|
||||
else if (TypeToken.of(Map.class).isAssignableFrom(propertyType)) {
|
||||
Map<?, ?> entries = (Map<?, ?>)property;
|
||||
Map<?, ?> entries = (Map<?, ?>) property;
|
||||
for (Map.Entry<?, ?> entry : entries.entrySet()) {
|
||||
Validator.validate(entry.getKey());
|
||||
Validator.validate(entry.getValue());
|
||||
|
|
|
@ -27,8 +27,7 @@ public class TokenCredentials implements ServiceClientCredentials {
|
|||
* @param token valid token
|
||||
*/
|
||||
public TokenCredentials(String scheme, String token) {
|
||||
if (scheme == null)
|
||||
{
|
||||
if (scheme == null) {
|
||||
scheme = "Bearer";
|
||||
}
|
||||
this.scheme = scheme;
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
/**
|
||||
* The package provides 2 basic credential classes that work with AutoRest
|
||||
* generated clients for authentication purposes.
|
||||
*/
|
||||
package com.microsoft.rest.credentials;
|
|
@ -1,5 +0,0 @@
|
|||
<html>
|
||||
<head />
|
||||
<body>This package contains the core classes across difference services.
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* The package contains the runtime classes required for AutoRest generated
|
||||
* clients to compile and function. To learn more about AutoRest generator,
|
||||
* see <a href="https://github.com/azure/autorest">https://github.com/azure/autorest</a>.
|
||||
*/
|
||||
package com.microsoft.rest;
|
|
@ -1,5 +0,0 @@
|
|||
<html>
|
||||
<head />
|
||||
<body>This package contains the runtime for compiling AutoRest generated libraries.
|
||||
</body>
|
||||
</html>
|
|
@ -9,8 +9,6 @@ package com.microsoft.rest.retry;
|
|||
|
||||
import com.squareup.okhttp.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A retry strategy with backoff parameters for calculating the exponential delay between retries.
|
||||
*/
|
||||
|
@ -88,7 +86,7 @@ public class ExponentialBackoffRetryStrategy extends RetryStrategy {
|
|||
@Override
|
||||
public boolean shouldRetry(int retryCount, Response response) {
|
||||
int code = response.code();
|
||||
return retryCount < this.retryCount &&
|
||||
(code == 408 || (code >= 500 && code != 501 && code != 505));
|
||||
return retryCount < this.retryCount
|
||||
&& (code == 408 || (code >= 500 && code != 501 && code != 505));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,13 +32,13 @@ public abstract class RetryStrategy {
|
|||
/**
|
||||
* The name of the retry strategy.
|
||||
*/
|
||||
public String name;
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* The value indicating whether the first retry attempt will be made immediately,
|
||||
* whereas subsequent retries will remain subject to the retry interval.
|
||||
*/
|
||||
public boolean fastFirstRetry;
|
||||
private boolean fastFirstRetry;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the {@link RetryStrategy} class.
|
||||
|
@ -60,4 +60,12 @@ public abstract class RetryStrategy {
|
|||
* @return true if the request should be retried; false otherwise.
|
||||
*/
|
||||
public abstract boolean shouldRetry(int retryCount, Response response);
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean isFastFirstRetry() {
|
||||
return fastFirstRetry;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
/**
|
||||
* The package contains classes that define the retry behaviors when an error
|
||||
* occurs during a REST call.
|
||||
*/
|
||||
package com.microsoft.rest.retry;
|
|
@ -27,7 +27,7 @@ public class ByteArraySerializer extends JsonSerializer<Byte[]> {
|
|||
@Override
|
||||
public void serialize(Byte[] value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
|
||||
byte[] bytes = new byte[value.length];
|
||||
for(int i = 0; i < value.length; i++){
|
||||
for (int i = 0; i < value.length; i++) {
|
||||
bytes[i] = value[i];
|
||||
}
|
||||
jgen.writeBinary(bytes);
|
||||
|
|
|
@ -12,9 +12,8 @@ import com.fasterxml.jackson.databind.JsonSerializer;
|
|||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import com.microsoft.rest.DateTimeRfc1123;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
|
|
|
@ -15,6 +15,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
|||
import com.fasterxml.jackson.datatype.joda.JodaModule;
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.base.Joiner;
|
||||
import retrofit.JacksonConverterFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
@ -22,9 +23,6 @@ import java.lang.reflect.Type;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import retrofit.Converter;
|
||||
import retrofit.JacksonConverterFactory;
|
||||
|
||||
/**
|
||||
* A serialization helper class wrapped around {@link JacksonConverterFactory} and {@link ObjectMapper}.
|
||||
*/
|
||||
|
@ -57,9 +55,9 @@ public class JacksonUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets a static instance of {@link Converter.Factory}.
|
||||
* Gets a static instance of JacksonConverter factory.
|
||||
*
|
||||
* @return an instance of {@link Converter.Factory}.
|
||||
* @return an instance of JacksonConverter factory.
|
||||
*/
|
||||
public JacksonConverterFactory getConverterFactory() {
|
||||
if (converterFactory == null) {
|
||||
|
@ -75,7 +73,9 @@ public class JacksonUtils {
|
|||
* @return the serialized string. Null if the object to serialize is null.
|
||||
*/
|
||||
public static String serialize(Object object) {
|
||||
if (object == null) return null;
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
StringWriter writer = new StringWriter();
|
||||
new JacksonUtils().getObjectMapper().writeValue(writer, object);
|
||||
|
@ -93,7 +93,9 @@ public class JacksonUtils {
|
|||
* @return the serialized string. Null if the object to serialize is null.
|
||||
*/
|
||||
public static String serializeRaw(Object object) {
|
||||
if (object == null) return null;
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
return CharMatcher.is('"').trimFrom(serialize(object));
|
||||
}
|
||||
|
||||
|
@ -107,7 +109,9 @@ public class JacksonUtils {
|
|||
* @return the serialized string
|
||||
*/
|
||||
public static String serializeList(List<?> list, CollectionFormat format) {
|
||||
if (list == null) return null;
|
||||
if (list == null) {
|
||||
return null;
|
||||
}
|
||||
List<String> serialized = new ArrayList<String>();
|
||||
for (Object element : list) {
|
||||
String raw = serializeRaw(element);
|
||||
|
@ -127,8 +131,10 @@ public class JacksonUtils {
|
|||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T deserialize(String value, final Type type) throws IOException {
|
||||
if (value == null || value.isEmpty()) return null;
|
||||
return (T)getObjectMapper().readValue(value, new TypeReference<T>() {
|
||||
if (value == null || value.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return (T) getObjectMapper().readValue(value, new TypeReference<T>() {
|
||||
@Override
|
||||
public Type getType() {
|
||||
return type;
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
/**
|
||||
* The package contains classes that handle serialization and deserialization
|
||||
* for the REST call payloads.
|
||||
*/
|
||||
package com.microsoft.rest.serializer;
|
|
@ -19,8 +19,8 @@ import java.io.IOException;
|
|||
|
||||
public class CredentialsTests {
|
||||
@Test
|
||||
public void BasicCredentialsTest() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() {};
|
||||
public void basicCredentialsTest() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() { };
|
||||
BasicAuthenticationCredentials credentials = new BasicAuthenticationCredentials("user", "pass");
|
||||
credentials.applyCredentialsFilter(serviceClient.client);
|
||||
serviceClient.getClientInterceptors().add(new Interceptor() {
|
||||
|
@ -38,8 +38,8 @@ public class CredentialsTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void TokenCredentialsTest() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() {};
|
||||
public void tokenCredentialsTest() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() { };
|
||||
TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token");
|
||||
credentials.applyCredentialsFilter(serviceClient.client);
|
||||
serviceClient.getClientInterceptors().add(new Interceptor() {
|
||||
|
|
|
@ -18,12 +18,12 @@ import java.io.IOException;
|
|||
|
||||
public class RetryHandlerTests {
|
||||
@Test
|
||||
public void ExponentialRetryEndOn501() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() {};
|
||||
public void exponentialRetryEndOn501() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() { };
|
||||
serviceClient.getClientInterceptors().add(new Interceptor() {
|
||||
// Send 408, 500, 502, all retried, with a 501 ending
|
||||
int[] codes = new int[] {408, 500, 502, 501};
|
||||
int count = 0;
|
||||
private int[] codes = new int[] {408, 500, 502, 501};
|
||||
private int count = 0;
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
return new Response.Builder()
|
||||
|
@ -39,14 +39,14 @@ public class RetryHandlerTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void ExponentialRetryMax() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() {};
|
||||
public void exponentialRetryMax() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() { };
|
||||
serviceClient.getClientInterceptors().add(new Interceptor() {
|
||||
// Send 500 until max retry is hit
|
||||
int count = 0;
|
||||
private int count = 0;
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Assert.assertTrue(count ++ < 5);
|
||||
Assert.assertTrue(count++ < 5);
|
||||
return new Response.Builder()
|
||||
.request(chain.request())
|
||||
.code(500)
|
||||
|
|
|
@ -8,8 +8,6 @@
|
|||
package com.microsoft.rest;
|
||||
|
||||
import com.squareup.okhttp.Interceptor;
|
||||
import com.squareup.okhttp.Protocol;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
@ -18,8 +16,8 @@ import java.io.IOException;
|
|||
|
||||
public class ServiceClientTests {
|
||||
@Test
|
||||
public void FilterTests() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() {};
|
||||
public void filterTests() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() { };
|
||||
serviceClient.getClientInterceptors().add(0, new FirstFilter());
|
||||
serviceClient.getClientInterceptors().add(1, new SecondFilter());
|
||||
serviceClient.getClientInterceptors().add(new Interceptor() {
|
||||
|
|
|
@ -9,7 +9,6 @@ package com.microsoft.rest;
|
|||
|
||||
import com.squareup.okhttp.Interceptor;
|
||||
import com.squareup.okhttp.Protocol;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
@ -19,7 +18,7 @@ import java.io.IOException;
|
|||
public class UserAgentTests {
|
||||
@Test
|
||||
public void defaultUserAgentTests() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() {};
|
||||
ServiceClient serviceClient = new ServiceClient() { };
|
||||
serviceClient.getClientInterceptors().add(new Interceptor() {
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
|
@ -36,7 +35,7 @@ public class UserAgentTests {
|
|||
|
||||
@Test
|
||||
public void customUserAgentTests() throws Exception {
|
||||
ServiceClient serviceClient = new ServiceClient() {};
|
||||
ServiceClient serviceClient = new ServiceClient() { };
|
||||
serviceClient.getClientInterceptors().add(new UserAgentInterceptor("Awesome"));
|
||||
serviceClient.getClientInterceptors().add(new Interceptor() {
|
||||
@Override
|
||||
|
|
|
@ -124,34 +124,40 @@ public class ValidatorTests {
|
|||
}
|
||||
}
|
||||
|
||||
public class IntWrapper {
|
||||
public final class IntWrapper {
|
||||
@JsonProperty(required = true)
|
||||
// CHECKSTYLE IGNORE VisibilityModifier FOR NEXT 2 LINES
|
||||
public int value;
|
||||
public Object nullable;
|
||||
}
|
||||
|
||||
public class IntegerWrapper {
|
||||
public final class IntegerWrapper {
|
||||
@JsonProperty(required = true)
|
||||
// CHECKSTYLE IGNORE VisibilityModifier FOR NEXT 1 LINE
|
||||
public Integer value;
|
||||
}
|
||||
|
||||
public class StringWrapper {
|
||||
public final class StringWrapper {
|
||||
@JsonProperty(required = true)
|
||||
// CHECKSTYLE IGNORE VisibilityModifier FOR NEXT 1 LINE
|
||||
public String value;
|
||||
}
|
||||
|
||||
public class LocalDateWrapper {
|
||||
public final class LocalDateWrapper {
|
||||
@JsonProperty(required = true)
|
||||
// CHECKSTYLE IGNORE VisibilityModifier FOR NEXT 1 LINE
|
||||
public LocalDate value;
|
||||
}
|
||||
|
||||
public class ListWrapper {
|
||||
public final class ListWrapper {
|
||||
@JsonProperty(required = true)
|
||||
// CHECKSTYLE IGNORE VisibilityModifier FOR NEXT 1 LINE
|
||||
public List<StringWrapper> list;
|
||||
}
|
||||
|
||||
public class MapWrapper {
|
||||
public final class MapWrapper {
|
||||
@JsonProperty(required = true)
|
||||
// CHECKSTYLE IGNORE VisibilityModifier FOR NEXT 1 LINE
|
||||
public Map<LocalDate, StringWrapper> map;
|
||||
}
|
||||
|
||||
|
@ -161,8 +167,9 @@ public class ValidatorTests {
|
|||
Blue
|
||||
}
|
||||
|
||||
public class EnumWrapper {
|
||||
public final class EnumWrapper {
|
||||
@JsonProperty(required = true)
|
||||
Color color;
|
||||
// CHECKSTYLE IGNORE VisibilityModifier FOR NEXT 1 LINE
|
||||
public Color color;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
/**
|
||||
* The package contains the tests for the client runtime.
|
||||
*/
|
||||
package com.microsoft.rest;
|
Загрузка…
Ссылка в новой задаче