Skip to content

Commit

Permalink
DateTimeDeserializer now moved to azure-core; add a HttpResponse obje…
Browse files Browse the repository at this point in the history
…ct in LRO ManagementException (Azure#14291)
  • Loading branch information
weidongxu-microsoft authored Aug 20, 2020
1 parent 07c8c2a commit e281616
Showing 1 changed file with 52 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

package com.azure.resourcemanager.resources.fluentcore;

import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.rest.Response;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.exception.ManagementError;
import com.azure.core.management.exception.ManagementException;
import com.azure.core.management.polling.PollerFactory;
import com.azure.core.management.polling.PollResult;
import com.azure.core.management.serializer.AzureJacksonAdapter;
import com.azure.core.util.Context;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.logging.ClientLogger;
Expand All @@ -20,23 +21,14 @@
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.core.util.serializer.SerializerEncoding;
import com.azure.resourcemanager.resources.fluentcore.utils.SdkContext;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQueries;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
Expand All @@ -58,21 +50,11 @@ public abstract class AzureServiceClient {

private final String sdkName;

protected AzureServiceClient(HttpPipeline httpPipeline, AzureEnvironment environment) {
sdkName = this.getClass().getPackage().getName();

this.serializerAdapter = new AzureJacksonAdapter();
((AzureJacksonAdapter) serializerAdapter).serializer().registerModule(DateTimeDeserializer.getModule());
}

protected AzureServiceClient(HttpPipeline httpPipeline, SerializerAdapter serializerAdapter,
AzureEnvironment environment) {
sdkName = this.getClass().getPackage().getName();

this.serializerAdapter = serializerAdapter;
if (serializerAdapter instanceof AzureJacksonAdapter) {
((AzureJacksonAdapter) serializerAdapter).serializer().registerModule(DateTimeDeserializer.getModule());
}
}

/**
Expand Down Expand Up @@ -146,7 +128,12 @@ public <T, U> Mono<U> getLroFinalResultOrError(AsyncPollResponse<PollResult<T>,
if (response.getStatus() != LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) {
String errorMessage;
ManagementError managementError = null;
if (response.getValue().getError() != null) {
HttpResponse errorResponse = null;
PollResult.Error lroError = response.getValue().getError();
if (lroError != null) {
errorResponse = new HttpResponseImpl(lroError.getResponseStatusCode(),
lroError.getResponseHeaders(), lroError.getResponseBody());

errorMessage = response.getValue().getError().getMessage();
String errorBody = response.getValue().getError().getResponseBody();
if (errorBody != null) {
Expand All @@ -171,32 +158,57 @@ public <T, U> Mono<U> getLroFinalResultOrError(AsyncPollResponse<PollResult<T>,
// fallback to default ManagementError
managementError = new ManagementError(response.getStatus().toString(), errorMessage);
}
return Mono.error(new ManagementException(errorMessage, null, managementError));
return Mono.error(new ManagementException(errorMessage, errorResponse, managementError));
} else {
return response.getFinalResult();
}
}

// this should be moved to core-mgmt when stable.
private static class DateTimeDeserializer extends JsonDeserializer<OffsetDateTime> {
private static class HttpResponseImpl extends HttpResponse {
private final int statusCode;
private final byte[] responseBody;
private final HttpHeaders httpHeaders;

public static SimpleModule getModule() {
SimpleModule module = new SimpleModule();
module.addDeserializer(OffsetDateTime.class, new DateTimeDeserializer());
return module;
HttpResponseImpl(int statusCode, HttpHeaders httpHeaders, String responseBody) {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody.getBytes(StandardCharsets.UTF_8);
}

@Override
public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
String string = jsonParser.getText();
TemporalAccessor temporal =
DateTimeFormatter.ISO_DATE_TIME.parseBest(string, OffsetDateTime::from, LocalDateTime::from);
if (temporal.query(TemporalQueries.offset()) == null) {
return LocalDateTime.from(temporal).atOffset(ZoneOffset.UTC);
} else {
return OffsetDateTime.from(temporal);
}
public int getStatusCode() {
return statusCode;
}

@Override
public String getHeaderValue(String s) {
return httpHeaders.getValue(s);
}

@Override
public HttpHeaders getHeaders() {
return httpHeaders;
}

@Override
public Flux<ByteBuffer> getBody() {
return Flux.just(ByteBuffer.wrap(responseBody));
}

@Override
public Mono<byte[]> getBodyAsByteArray() {
return Mono.just(responseBody);
}

@Override
public Mono<String> getBodyAsString() {
return Mono.just(new String(responseBody, StandardCharsets.UTF_8));
}

@Override
public Mono<String> getBodyAsString(Charset charset) {
return Mono.just(new String(responseBody, charset));
}
}
}

0 comments on commit e281616

Please sign in to comment.