Response map()

in sdk/core/azure-core-rest/src/main/java/com/azure/android/core/rest/HttpResponseMapper.java [91:192]


    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);
                }
            }
        }
    }