001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.jaxrs.io.operations;
020
021import static org.apache.http.HttpStatus.SC_BAD_REQUEST;
022
023import java.io.IOException;
024import java.io.InputStream;
025import java.lang.annotation.Annotation;
026import java.lang.reflect.Type;
027import java.net.URLDecoder;
028
029import javax.servlet.http.HttpServletRequest;
030import javax.ws.rs.Consumes;
031import javax.ws.rs.WebApplicationException;
032import javax.ws.rs.core.Context;
033import javax.ws.rs.core.MediaType;
034import javax.ws.rs.core.MultivaluedMap;
035import javax.ws.rs.ext.MessageBodyReader;
036import javax.ws.rs.ext.Provider;
037
038import org.apache.commons.io.IOUtils;
039import org.nuxeo.ecm.core.api.CoreSession;
040import org.nuxeo.ecm.platform.web.common.RequestContext;
041import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
042
043import com.fasterxml.jackson.core.JsonFactory;
044import com.fasterxml.jackson.core.JsonParser;
045import com.fasterxml.jackson.core.JsonProcessingException;
046
047/**
048 * Reads {@link ExecutionRequest} from a urlencoded POST (Needed for OAuth calls)
049 *
050 * @author Tiry (tdelprat@nuxeo.com)
051 */
052@Provider
053@Consumes("application/x-www-form-urlencoded")
054public class UrlEncodedFormRequestReader implements MessageBodyReader<ExecutionRequest> {
055
056    @Context
057    protected HttpServletRequest request;
058
059    @Context
060    JsonFactory factory;
061
062    public 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 (MediaType.APPLICATION_FORM_URLENCODED_TYPE.equals(mediaType) && ExecutionRequest.class.isAssignableFrom(type));
069    }
070
071    @Override
072    public ExecutionRequest readFrom(Class<ExecutionRequest> type, Type genericType, Annotation[] annotations,
073            MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
074            throws IOException, WebApplicationException {
075
076        String content = IOUtils.toString(entityStream, "UTF-8");
077        String jsonString = null;
078        if (content == null || content.isEmpty()) {
079            // body was consumed by OAuth Filter and but Request parameters must
080            // have been cached
081            // => need to get access to the request params
082            jsonString = RequestContext.getActiveContext().getRequest().getParameter("jsondata");
083        } else {
084            if (content.startsWith("jsondata=")) {
085                jsonString = content.substring(9);
086                jsonString = URLDecoder.decode(jsonString, "UTF-8");
087            } else {
088                return null;
089            }
090        }
091
092        if (jsonString == null) {
093            return null;
094        }
095        try (JsonParser jp = factory.createParser(jsonString)) {
096            return JsonRequestReader.readRequest(jp, httpHeaders, getCoreSession());
097        } catch (JsonProcessingException e) {
098            throw new WebApplicationException(e, SC_BAD_REQUEST);
099        }
100    }
101
102}