001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.jaxrs.io.operations;
013
014import java.io.IOException;
015import java.io.InputStream;
016import java.lang.annotation.Annotation;
017import java.lang.reflect.Type;
018import java.net.URLDecoder;
019
020import javax.servlet.http.HttpServletRequest;
021import javax.ws.rs.Consumes;
022import javax.ws.rs.WebApplicationException;
023import javax.ws.rs.core.Context;
024import javax.ws.rs.core.MediaType;
025import javax.ws.rs.core.MultivaluedMap;
026import javax.ws.rs.core.Response;
027import javax.ws.rs.ext.MessageBodyReader;
028import javax.ws.rs.ext.Provider;
029
030import org.apache.commons.io.IOUtils;
031import org.codehaus.jackson.JsonFactory;
032import org.codehaus.jackson.JsonParser;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext;
035import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
036
037/**
038 * Reads {@link ExecutionRequest} from a urlencoded POST (Needed for OAuth calls)
039 *
040 * @author Tiry (tdelprat@nuxeo.com)
041 */
042@Provider
043@Consumes("application/x-www-form-urlencoded")
044public class UrlEncodedFormRequestReader implements MessageBodyReader<ExecutionRequest> {
045
046    @Context
047    protected HttpServletRequest request;
048
049    @Context
050    JsonFactory factory;
051
052    public CoreSession getCoreSession() {
053        return SessionFactory.getSession(request);
054    }
055
056    @Override
057    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
058        return (MediaType.APPLICATION_FORM_URLENCODED_TYPE.equals(mediaType) && ExecutionRequest.class.isAssignableFrom(type));
059    }
060
061    @Override
062    public ExecutionRequest readFrom(Class<ExecutionRequest> type, Type genericType, Annotation[] annotations,
063            MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
064            throws IOException, WebApplicationException {
065
066        String content = IOUtils.toString(entityStream, "UTF-8");
067        String jsonString = null;
068        if (content == null || content.isEmpty()) {
069            // body was consumed by OAuth Filter and but Request parameters must
070            // have been cached
071            // => need to get access to the request params
072            jsonString = RequestContext.getActiveContext().getRequest().getParameter("jsondata");
073        } else {
074            if (content.startsWith("jsondata=")) {
075                jsonString = content.substring(9);
076                jsonString = URLDecoder.decode(jsonString, "UTF-8");
077            } else {
078                return null;
079            }
080        }
081
082        if (jsonString == null) {
083            return null;
084        }
085        JsonParser jp = factory.createJsonParser(jsonString);
086        try {
087            return JsonRequestReader.readRequest(jp, httpHeaders, getCoreSession());
088        } catch (IOException e) {
089            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
090        }
091    }
092
093}