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