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