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.platform.routing.core.io;
022
023import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
024import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
025import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.DERIVATIVE;
026
027import java.io.IOException;
028import java.io.Serializable;
029import java.util.Map;
030
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader;
034import org.nuxeo.ecm.core.io.registry.MarshallingException;
035import org.nuxeo.ecm.core.io.registry.context.RenderingContext.SessionWrapper;
036import org.nuxeo.ecm.core.io.registry.reflect.Setup;
037
038import com.fasterxml.jackson.databind.JsonNode;
039
040/**
041 * @since 8.2
042 * @deprecated use @{link TaskCompletionRequestJsonReader TaskCompletionRequestJsonReader}
043 */
044@Setup(mode = SINGLETON, priority = DERIVATIVE)
045@Deprecated
046public class TaskCompletionRequestLegacyJsonReader extends EntityJsonReader<TaskCompletionRequest> {
047
048    public TaskCompletionRequestLegacyJsonReader() {
049        super(TaskCompletionRequestJsonReader.ENTITY_TYPE);
050    }
051
052    @Override
053    protected TaskCompletionRequest readEntity(JsonNode jn) throws IOException {
054        String id = jn.get("id").asText();
055        String comment = jn.get("comment").asText();
056        JsonNode variableNode = jn.get("variables");
057        Map<String, Serializable> variables = null;
058
059        if (id == null) {
060            throw new NuxeoException("No id found in request body", SC_BAD_REQUEST);
061        }
062
063        try (SessionWrapper closeable = ctx.getSession(null)) {
064            CoreSession session = closeable.getSession();
065            if (variableNode != null) {
066                try {
067                    variables = JsonEncodeDecodeUtils.decodeVariables(variableNode, null, session);
068                } catch (ClassNotFoundException e) {
069                    throw new MarshallingException(e);
070                }
071            }
072        }
073
074        return new TaskCompletionRequest(comment, variables, true);
075    }
076
077}