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

com.ovea.tadjin.util.jersey.GroovyJSONProvider Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2011 Ovea 
 *
 * 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.ovea.tadjin.util.jersey;

import com.ovea.json.JSONType;
import com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider;
import groovy.json.JsonBuilder;
import groovy.json.JsonSlurper;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * @author japod
 */
public abstract class GroovyJSONProvider extends AbstractMessageReaderWriterProvider {

    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public static final class App extends GroovyJSONProvider {
        @Override
        protected boolean isSupported(MediaType m) {
            return true;
        }
    }

    @Produces("*/*")
    @Consumes("*/*")
    public static final class General extends GroovyJSONProvider {
        @Override
        protected boolean isSupported(MediaType m) {
            return m.getSubtype().endsWith("+json");
        }
    }

    @Override
    public final boolean isReadable(Class type, Type genericType, Annotation annotations[], MediaType mediaType) {
        return isSupported(mediaType) && (Object.class == type || Map.class == type || List.class == type);
    }

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

    protected abstract boolean isSupported(MediaType m);

    @Override
    public Object readFrom(
        Class type,
        Type genericType,
        Annotation annotations[],
        MediaType mediaType,
        MultivaluedMap httpHeaders,
        InputStream entityStream) throws IOException {
        try {
            return new JsonSlurper().parseText(readFromAsString(entityStream, mediaType));
        } catch (Exception e) {
            throw new WebApplicationException(new Exception("Error creating JSON Type: " + type.getName(), e), 400);
        }
    }

    @Override
    public void writeTo(
        Object t,
        Class type,
        Type genericType,
        Annotation annotations[],
        MediaType mediaType,
        MultivaluedMap httpHeaders,
        OutputStream entityStream) throws IOException {
        try {
            OutputStreamWriter writer = new OutputStreamWriter(entityStream, getCharset(mediaType));
            if (t instanceof JsonBuilder) {
                ((JsonBuilder) t).writeTo(writer);
            } else if (t instanceof JSONType) {
                ((JSONType) t).write(writer);
            } else {
                new JsonBuilder(t).writeTo(writer);
            }
            writer.flush();
        } catch (Exception je) {
            throw new WebApplicationException(new Exception("Error writing JSON Type: " + type.getName(), je), 500);
        }
    }
}