Search in sources :

Example 1 with HttpResponseExceptionInfo

use of com.azure.android.core.rest.implementation.HttpResponseExceptionInfo in project azure-sdk-for-android by Azure.

the class HttpResponseMapper method map.

Response<?> map(HttpResponse httpResponse, JacksonSerder jacksonSerder) throws Throwable {
    if (!isExpectedStatusCode(httpResponse.getStatusCode())) {
        final HttpResponseExceptionInfo exceptionInfo = getExceptionInfo(httpResponse.getStatusCode());
        throw logger.logThrowableAsError((exceptionInfo.instantiateException(jacksonSerder, httpResponse, logger)));
    } else {
        Object headerObject = null;
        if (this.headerDecodeType != null) {
            try {
                headerObject = jacksonSerder.deserialize(httpResponse.getHeaders().toMap(), headerDecodeType);
            } catch (IOException ioe) {
                throw logger.logExceptionAsError(new HttpResponseException("HTTP response has malformed headers", httpResponse, ioe));
            }
        }
        if (isBooleanResponseForHead(httpResponse)) {
            final boolean isSuccess = (httpResponse.getStatusCode() / 100) == 2;
            httpResponse.close();
            return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, isSuccess);
        } else if (TypeUtil.isTypeOrSubTypeOf(this.contentDecodeType, Void.class)) {
            httpResponse.close();
            return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, null);
        } else if (TypeUtil.isTypeOrSubTypeOf(this.contentDecodeType, InputStream.class)) {
            return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, httpResponse.getBody());
        } else if (TypeUtil.isTypeOrSubTypeOf(this.contentDecodeType, byte[].class)) {
            if (this.contentEncodedType == Base64Url.class) {
                final byte[] encodedContent = httpResponse.getBodyAsByteArray();
                final byte[] decodedContent = new Base64Url(encodedContent).decodedBytes();
                return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, decodedContent);
            } else {
                return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, httpResponse.getBodyAsByteArray());
            }
        } else if (this.contentEncodedType == null) {
            final Object decodedContent = deserializeHttpBody(jacksonSerder, httpResponse, this.contentDecodeType);
            return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, decodedContent);
        } else {
            Objects.requireNonNull(this.contentEncodedType);
            if (TypeUtil.isTypeOrSubTypeOf(this.contentEncodedType, Page.class)) {
                final Type pageType = (this.contentEncodedType == Page.class) ? TypeUtil.createParameterizedType(ItemPage.class, this.contentDecodeType) : this.contentEncodedType;
                final Object decodedContent = deserializeHttpBody(jacksonSerder, httpResponse, pageType);
                return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, decodedContent);
            } else {
                Objects.requireNonNull(this.expandedContentEncodedType);
                final Object encodedContent = deserializeHttpBody(jacksonSerder, httpResponse, this.expandedContentEncodedType);
                final Object decodedContent = decodeContent(encodedContent, this.contentEncodedType, this.contentDecodeType);
                return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, decodedContent);
            }
        }
    }
}
Also used : UnexpectedResponseExceptionType(com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType) ReturnValueWireType(com.azure.android.core.rest.annotation.ReturnValueWireType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ItemPage(com.azure.android.core.rest.implementation.ItemPage) HttpResponseExceptionInfo(com.azure.android.core.rest.implementation.HttpResponseExceptionInfo) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) IOException(java.io.IOException) Base64Url(com.azure.android.core.util.Base64Url)

Example 2 with HttpResponseExceptionInfo

use of com.azure.android.core.rest.implementation.HttpResponseExceptionInfo in project azure-sdk-for-android by Azure.

the class HttpResponseMapper method extractDefaultAndKnownExceptions.

private Pair<HttpResponseExceptionInfo, Map<Integer, HttpResponseExceptionInfo>> extractDefaultAndKnownExceptions(Method swaggerMethod) {
    final UnexpectedResponseExceptionTypes unexpectedResponseExceptionTypesHolder = swaggerMethod.getAnnotation(UnexpectedResponseExceptionTypes.class);
    if (unexpectedResponseExceptionTypesHolder == null || unexpectedResponseExceptionTypesHolder.value() == null || unexpectedResponseExceptionTypesHolder.value().length == 0) {
        return Pair.create(new HttpResponseExceptionInfo(HttpResponseException.class), null);
    }
    final UnexpectedResponseExceptionType[] unexpectedResponseExceptionTypes = unexpectedResponseExceptionTypesHolder.value();
    Map<Integer, HttpResponseExceptionInfo> statusCodeToKnownExceptionInfo = new HashMap<>(unexpectedResponseExceptionTypes.length);
    HttpResponseExceptionInfo defaultExceptionInfo = null;
    for (UnexpectedResponseExceptionType exceptionAnnotation : unexpectedResponseExceptionTypes) {
        if (exceptionAnnotation.code().length == 0) {
            defaultExceptionInfo = new HttpResponseExceptionInfo(exceptionAnnotation.value());
        } else {
            final HttpResponseExceptionInfo knownExceptionInfo = new HttpResponseExceptionInfo(exceptionAnnotation.value());
            for (int statusCode : exceptionAnnotation.code()) {
                statusCodeToKnownExceptionInfo.put(statusCode, knownExceptionInfo);
            }
        }
    }
    if (defaultExceptionInfo == null) {
        defaultExceptionInfo = new HttpResponseExceptionInfo(HttpResponseException.class);
    }
    return Pair.create(defaultExceptionInfo, statusCodeToKnownExceptionInfo);
}
Also used : UnexpectedResponseExceptionType(com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType) HashMap(java.util.HashMap) UnexpectedResponseExceptionTypes(com.azure.android.core.rest.annotation.UnexpectedResponseExceptionTypes) HttpResponseExceptionInfo(com.azure.android.core.rest.implementation.HttpResponseExceptionInfo) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException)

Aggregations

HttpResponseException (com.azure.android.core.http.exception.HttpResponseException)2 UnexpectedResponseExceptionType (com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType)2 HttpResponseExceptionInfo (com.azure.android.core.rest.implementation.HttpResponseExceptionInfo)2 ReturnValueWireType (com.azure.android.core.rest.annotation.ReturnValueWireType)1 UnexpectedResponseExceptionTypes (com.azure.android.core.rest.annotation.UnexpectedResponseExceptionTypes)1 ItemPage (com.azure.android.core.rest.implementation.ItemPage)1 Base64Url (com.azure.android.core.util.Base64Url)1 IOException (java.io.IOException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1