001/*
002 * (C) Copyright 2013 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 *     dmetzler
018 */
019package org.nuxeo.ecm.automation.jaxrs.io.documents;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.lang.annotation.Annotation;
024import java.lang.reflect.Type;
025
026import javax.servlet.http.HttpServletRequest;
027import javax.ws.rs.Consumes;
028import javax.ws.rs.WebApplicationException;
029import javax.ws.rs.core.Context;
030import javax.ws.rs.core.MediaType;
031import javax.ws.rs.core.MultivaluedMap;
032import javax.ws.rs.core.Response;
033import javax.ws.rs.ext.MessageBodyReader;
034import javax.ws.rs.ext.Provider;
035
036import org.apache.commons.io.IOUtils;
037import org.nuxeo.ecm.automation.core.operations.business.adapter.BusinessAdapter;
038import org.nuxeo.ecm.automation.io.services.codec.ObjectCodecService;
039import org.nuxeo.ecm.core.api.CoreSession;
040import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
041import org.nuxeo.runtime.api.Framework;
042
043import com.fasterxml.jackson.core.JsonFactory;
044import com.fasterxml.jackson.core.JsonParser;
045import com.fasterxml.jackson.databind.JsonNode;
046
047/**
048 * JAX-RS Message body reeader to decode BusinessAdapter
049 *
050 * @since 5.7.2
051 */
052@Provider
053@Consumes(MediaType.APPLICATION_JSON)
054public class BusinessAdapterReader implements MessageBodyReader<BusinessAdapter> {
055
056    @Context
057    protected HttpServletRequest request;
058
059    @Context
060    JsonFactory factory;
061
062    private CoreSession getCoreSession() {
063        return SessionFactory.getSession(request);
064    }
065
066    @Override
067    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
068        return BusinessAdapter.class.isAssignableFrom(type);
069    }
070
071    @Override
072    public BusinessAdapter readFrom(Class<BusinessAdapter> arg0, Type arg1, Annotation[] arg2, MediaType arg3,
073            MultivaluedMap<String, String> headers, InputStream in) throws IOException, WebApplicationException {
074        return readRequest(in, headers);
075    }
076
077    public BusinessAdapter readRequest(InputStream in, MultivaluedMap<String, String> headers) throws IOException,
078            WebApplicationException {
079        // As stated in http://tools.ietf.org/html/rfc4627.html UTF-8 is the
080        // default encoding for JSON content
081        // TODO: add introspection on the first bytes to detect other admissible
082        // json encodings, namely: UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE)
083        String content = IOUtils.toString(in, "UTF-8");
084        if (content.isEmpty()) {
085            throw new WebApplicationException(Response.Status.BAD_REQUEST);
086        }
087        return readRequest(content, headers);
088    }
089
090    public BusinessAdapter readRequest(String content, MultivaluedMap<String, String> headers)
091            throws WebApplicationException {
092        try {
093            return readRequest0(content, headers);
094        } catch (WebApplicationException e) {
095            throw e;
096        } catch (IOException e) {
097            throw new WebApplicationException(e);
098        }
099    }
100
101    public BusinessAdapter readRequest0(String content, MultivaluedMap<String, String> headers) throws IOException {
102        ObjectCodecService codecService = Framework.getService(ObjectCodecService.class);
103
104        try (JsonParser jp = factory.createParser(content)) {
105            JsonNode inputNode = jp.readValueAsTree();
106
107            return (BusinessAdapter) codecService.readNode(inputNode, getCoreSession());
108        }
109
110    }
111
112}