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

com.nesscomputing.httpclient.response.JsonContentConverter Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
/**
 * Copyright (C) 2012 Ness Computing, Inc.
 *
 * 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.nesscomputing.httpclient.response;

import com.nesscomputing.httpclient.HttpClientResponse;
import com.nesscomputing.logging.Log;

import java.io.IOException;
import java.io.InputStream;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JsonContentConverter extends AbstractErrorHandlingContentConverter
{
    private static final Log LOG = Log.findLog();

    private final TypeReference typeReference;
    private final ObjectMapper objectMapper;
    private final boolean ignore404;

    public static  ContentResponseHandler getResponseHandler(final TypeReference typeReference, final ObjectMapper objectMapper)
    {
        return ContentResponseHandler.forConverter(getConverter(typeReference, objectMapper));
    }

    public static  ContentConverter getConverter(final TypeReference typeReference, final ObjectMapper objectMapper)
    {
        return new JsonContentConverter(typeReference, objectMapper);
    }

    public static  ContentResponseHandler getResponseHandler(final TypeReference typeReference, final ObjectMapper objectMapper, final boolean ignore404)
    {
        return ContentResponseHandler.forConverter(getConverter(typeReference, objectMapper, ignore404));
    }

    public static  ContentConverter getConverter(final TypeReference typeReference, final ObjectMapper objectMapper, final boolean ignore404)
    {
        return new JsonContentConverter(typeReference, objectMapper, ignore404);
    }

    public JsonContentConverter(final TypeReference typeReference,
                                final ObjectMapper objectMapper)
    {
        this(typeReference, objectMapper, false);
    }

    public JsonContentConverter(final TypeReference typeReference,
                                final ObjectMapper objectMapper,
                                final boolean ignore404)
    {
        this.typeReference = typeReference;
        this.objectMapper = objectMapper;
        this.ignore404 = ignore404;
    }

    @Override
    public T convert(final HttpClientResponse httpClientResponse,
                     final InputStream inputStream)
    throws IOException
    {
        final int responseCode = httpClientResponse.getStatusCode();
        switch (responseCode) {
            case 200:
                return objectMapper.readValue(inputStream, typeReference);

            case 204:
                return null; // Return null for "CREATED" response code.

                // 201 may or may not contain a result object
            case 201:
                try {
                    return objectMapper.readValue(inputStream, typeReference);
                }
                catch (JsonParseException jpe) {
                    LOG.trace(jpe, "while reading response");
                    return null;
                }

            case 404:
                if (ignore404) {
                    return null;
                }
            // FALL THROUGH
            default:
                LOG.warn("Remote service responsed with %d code (cause: %s)", responseCode, httpClientResponse.getStatusText());
                throw new HttpResponseException(httpClientResponse);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy