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