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

com.codereligion.bugsnag.logback.resource.GsonMessageBodyWriter Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2014 www.codereligion.com
 *
 * 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.codereligion.bugsnag.logback.resource;

import com.google.common.base.Charsets;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

/**
 * Glues together the json serialization provided by {@link Gson} with the JAX-RS implementation.
 *
 * @author Sebastian Gröbler
 */
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class GsonMessageBodyWriter implements MessageBodyWriter {

    private final Gson gson;

    /**
     * Creates a new instance using the given {@code gson} for serialization.
     * @param gson the {@link Gson} object to use
     */
    public GsonMessageBodyWriter(final Gson gson) {
        this.gson = gson;
    }

    @Override
    public boolean isWriteable(
            final Class type,
            final Type genericType,
            final Annotation[] annotations,
            final MediaType mediaType) {
        return true;
    }

    @Override
    public long getSize(
            final Object object,
            final Class type,
            final Type genericType,
            final Annotation[] annotations,
            final MediaType mediaType) {
        return -1;
    }

    @Override
    public void writeTo(
            final Object object,
            final Class type,
            final Type genericType,
            final Annotation[] annotations,
            final MediaType mediaType,
            final MultivaluedMap httpHeaders,
            final OutputStream entityStream) throws IOException {

        final OutputStreamWriter writer = new OutputStreamWriter(entityStream, Charsets.UTF_8);

        try {
            gson.toJson(object, type, writer);
        } finally {
            writer.close();
        }
    }
}