All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.palantir.conjure.java.okhttp.UnknownRemoteExceptionResponseHandler Maven / Gradle / Ivy

The newest version!
/*
 * (c) Copyright 2017 Palantir Technologies Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.palantir.conjure.java.okhttp;

import com.google.common.io.CharStreams;
import com.palantir.conjure.java.api.errors.UnknownRemoteException;
import com.palantir.logsafe.logger.SafeLogger;
import com.palantir.logsafe.logger.SafeLoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import okhttp3.Response;

enum UnknownRemoteExceptionResponseHandler implements ResponseHandler {
    INSTANCE;

    private static final SafeLogger log = SafeLoggerFactory.get(UnknownRemoteExceptionResponseHandler.class);

    @Override
    public Optional handle(Response response) {
        if (response.isSuccessful() || response.code() == MoreHttpCodes.SWITCHING_PROTOCOLS) {
            return Optional.empty();
        }

        String body;
        try {
            body = response.body() != null ? toString(response.body().byteStream()) : "";
        } catch (IOException e) {
            log.warn("Failed to read response body", e);
            body = "Failed to read response body";
        }

        return Optional.of(new UnknownRemoteException(response.code(), body));
    }

    private static String toString(InputStream body) throws IOException {
        try (Reader reader = new InputStreamReader(body, StandardCharsets.UTF_8)) {
            return CharStreams.toString(reader);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy