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