001/* 002 * (C) Copyright 2014 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 * <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a> 018 * 019 */ 020 021package org.nuxeo.ecm.restapi.server.jaxrs.routing.io; 022 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.Serializable; 026import java.lang.annotation.Annotation; 027import java.lang.reflect.Type; 028import java.util.Map; 029import javax.servlet.http.HttpServletRequest; 030import javax.ws.rs.WebApplicationException; 031import javax.ws.rs.core.Context; 032import javax.ws.rs.core.MediaType; 033import javax.ws.rs.core.MultivaluedMap; 034import javax.ws.rs.core.Response; 035import javax.ws.rs.ext.MessageBodyReader; 036import javax.ws.rs.ext.Provider; 037 038import org.apache.commons.io.IOUtils; 039import org.apache.commons.logging.Log; 040import org.apache.commons.logging.LogFactory; 041import org.codehaus.jackson.JsonFactory; 042import org.codehaus.jackson.JsonNode; 043import org.codehaus.jackson.JsonParseException; 044import org.codehaus.jackson.JsonParser; 045import org.codehaus.jackson.JsonToken; 046import org.nuxeo.ecm.core.api.CoreSession; 047import org.nuxeo.ecm.core.api.IdRef; 048import org.nuxeo.ecm.core.api.NuxeoException; 049import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants; 050import org.nuxeo.ecm.platform.task.Task; 051import org.nuxeo.ecm.restapi.server.jaxrs.routing.io.util.JsonEncodeDecodeUtils; 052import org.nuxeo.ecm.restapi.server.jaxrs.routing.model.TaskCompletionRequest; 053import org.nuxeo.ecm.webengine.WebException; 054import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory; 055 056/** 057 * @since 7.2 058 */ 059@Provider 060public class TaskCompletionRequestReader implements MessageBodyReader<TaskCompletionRequest> { 061 062 protected static final Log log = LogFactory.getLog(TaskCompletionRequestReader.class); 063 064 @Context 065 private JsonFactory factory; 066 067 @Context 068 HttpServletRequest request; 069 070 @Override 071 public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { 072 return TaskCompletionRequest.class.isAssignableFrom(type); 073 } 074 075 @Override 076 public TaskCompletionRequest readFrom(Class<TaskCompletionRequest> type, Type genericType, 077 Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, 078 InputStream entityStream) throws IOException, WebApplicationException { 079 String content = IOUtils.toString(entityStream); 080 if (content.isEmpty()) { 081 if (content.isEmpty()) { 082 throw new WebException("No content in request body", Response.Status.BAD_REQUEST.getStatusCode()); 083 } 084 085 } 086 087 try { 088 return readRequest(content, httpHeaders); 089 } catch (IOException | NuxeoException | ClassNotFoundException e) { 090 throw WebException.wrap(e); 091 } 092 } 093 094 private TaskCompletionRequest readRequest(String content, MultivaluedMap<String, String> httpHeaders) 095 throws IOException, ClassNotFoundException { 096 JsonParser jp = factory.createJsonParser(content); 097 return readJson(jp, httpHeaders); 098 } 099 100 private TaskCompletionRequest readJson(JsonParser jp, MultivaluedMap<String, String> httpHeaders) 101 throws JsonParseException, IOException, ClassNotFoundException { 102 CoreSession session = SessionFactory.getSession(request); 103 JsonToken tok = jp.nextToken(); 104 105 // skip { 106 if (jp.getCurrentToken() == JsonToken.START_OBJECT) { 107 tok = jp.nextToken(); 108 } 109 String id = null; 110 String comment = null; 111 JsonNode variableNode = null; 112 Map<String, Serializable> variables = null; 113 while (tok != JsonToken.END_OBJECT) { 114 String key = jp.getCurrentName(); 115 jp.nextToken(); 116 if ("id".equals(key)) { 117 id = jp.readValueAs(String.class); 118 } else if ("comment".equals(key)) { 119 comment = jp.readValueAs(String.class); 120 } else if ("variables".equals(key)) { 121 variableNode = jp.readValueAsTree(); 122 } else if ("entity-type".equals(key)) { 123 String entityType = jp.readValueAs(String.class); 124 if (!TaskWriter.ENTITY_TYPE.equals(entityType)) { 125 throw new WebApplicationException(Response.Status.BAD_REQUEST); 126 } 127 } else { 128 log.debug("Unknown key: " + key); 129 jp.skipChildren(); 130 } 131 tok = jp.nextToken(); 132 133 } 134 135 if (id == null) { 136 throw new WebException("No id found in request body", Response.Status.BAD_REQUEST.getStatusCode()); 137 } 138 139 TaskCompletionRequest result = new TaskCompletionRequest(); 140 Task originalTask = session.getDocument(new IdRef(id)).getAdapter(Task.class); 141 final String nodeId = originalTask.getVariable(DocumentRoutingConstants.TASK_NODE_ID_KEY); 142 String workflowInstanceId = originalTask.getProcessId(); 143 NodeAccessRunner nodeAccessRunner = new NodeAccessRunner(session, workflowInstanceId, nodeId); 144 nodeAccessRunner.runUnrestricted(); 145 if (variableNode != null) { 146 variables = JsonEncodeDecodeUtils.decodeVariables(variableNode, nodeAccessRunner.node.getVariables(), session); 147 } 148 result.setVariables(variables); 149 result.setComment(comment); 150 151 return result; 152 } 153 154}