001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.webengine.jaxrs.coreiodelegate;
021
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.OutputStream;
025import java.lang.annotation.Annotation;
026import java.lang.reflect.Type;
027
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030import javax.ws.rs.WebApplicationException;
031import javax.ws.rs.core.Context;
032import javax.ws.rs.core.HttpHeaders;
033import javax.ws.rs.core.MediaType;
034import javax.ws.rs.core.MultivaluedMap;
035import javax.ws.rs.ext.MessageBodyReader;
036import javax.ws.rs.ext.MessageBodyWriter;
037
038import org.nuxeo.ecm.core.io.registry.MarshallerRegistry;
039import org.nuxeo.ecm.core.io.registry.Reader;
040import org.nuxeo.ecm.core.io.registry.Writer;
041import org.nuxeo.ecm.core.io.registry.context.RenderingContext;
042import org.nuxeo.runtime.api.Framework;
043
044/**
045 * An abstract JAX-RS {@link MessageBodyWriter} that delegate marshalling to all nuxeo-core-io {@link Writer} and
046 * {@link Reader} with conditions.
047 *
048 * @since 7.2
049 */
050public abstract class PartialCoreIODelegate implements MessageBodyWriter<Object>, MessageBodyReader<Object> {
051
052    public static final String CONTENT_TYPE = "Content-Type";
053
054    public static final String NUXEO_ENTITY = "; nuxeo-entity=";
055
056    public static MarshallerRegistry registry;
057
058    public static MarshallerRegistry getRegistry() {
059        if (registry == null) {
060            registry = Framework.getService(MarshallerRegistry.class);
061        }
062        return registry;
063    }
064
065    private Writer<?> writer = null;
066
067    private Reader<?> reader = null;
068
069    @Context
070    private HttpServletRequest request;
071
072    @Context
073    private HttpServletResponse response;
074
075    @Context
076    private HttpHeaders headers;
077
078    /**
079     * If it returns true, it delegates marshalling to {@link MarshallerRegistry}.
080     *
081     * @since 7.2
082     */
083    protected abstract boolean accept(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType);
084
085    @Override
086    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
087        if (accept(type, genericType, annotations, mediaType)) {
088            RenderingContext ctx = RenderingContextWebUtils.getContext(request);
089            writer = getRegistry().getWriter(ctx, type, genericType, mediaType);
090            return writer != null;
091        }
092        return false;
093    }
094
095    @Override
096    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
097        if (accept(type, genericType, annotations, mediaType)) {
098            RenderingContext ctx = RenderingContextWebUtils.getContext(request);
099            reader = getRegistry().getReader(ctx, type, genericType, mediaType);
100            if (reader != null) {
101                // backward compatibility for json document model marshalling
102                DocumentModelJsonReaderLegacy.pushInstanceIfNeeded(ctx, request, headers.getRequestHeaders());
103                return true;
104            }
105            return false;
106        }
107        return false;
108    }
109
110    @Override
111    public long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
112        return -1;
113    }
114
115    @Override
116    @SuppressWarnings("unchecked")
117    public final void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
118            MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException,
119            WebApplicationException {
120        if (writer != null) {
121            ((Writer<Object>) writer).write(t, type, genericType, mediaType, entityStream);
122        }
123        RenderingContext ctx = RenderingContextWebUtils.getContext(request);
124        response.setHeader(CONTENT_TYPE,
125                mediaType + NUXEO_ENTITY + ctx.getParameter(RenderingContext.RESPONSE_HEADER_ENTITY_TYPE_KEY));
126    }
127
128    @Override
129    @SuppressWarnings("unchecked")
130    public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
131            MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException,
132            WebApplicationException {
133        if (reader != null) {
134            return reader.read(type, genericType, mediaType, entityStream);
135        }
136        return null;
137    }
138
139}