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.util.HashMap;
022import java.util.Map;
023
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.nuxeo.ecm.automation.OperationContext;
028import org.nuxeo.ecm.automation.core.scripting.Scripting;
029import org.nuxeo.ecm.core.api.CoreSession;
030
031/**
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class ExecutionRequest {
035
036    protected Object input;
037
038    protected RestOperationContext restOperationContext;
039
040    protected Map<String, Object> params;
041
042    public ExecutionRequest() {
043        this(null);
044    }
045
046    public ExecutionRequest(Object input) {
047        // this is closed by a RequestCleanupHandler
048        restOperationContext = new RestOperationContext();
049        this.input = input;
050        params = new HashMap<>();
051    }
052
053    public void setInput(Object input) {
054        this.input = input;
055    }
056
057    public Object getInput() {
058        return input;
059    }
060
061    public void setContextParam(String key, Object value) {
062        restOperationContext.put(key, value);
063    }
064
065    public void setContextParam(String key, String value) {
066        restOperationContext.put(key, value);
067    }
068
069    public void setParam(String key, Object jsonObject) {
070        params.put(key, jsonObject);
071    }
072
073    public void setParam(String key, String value) {
074        if (value.startsWith("expr:")) {
075            value = value.substring(5).trim();
076            if (value.contains("@{")) {
077                params.put(key, Scripting.newTemplate(value));
078            } else {
079                params.put(key, Scripting.newExpression(value));
080            }
081        } else {
082            params.put(key, value);
083        }
084    }
085
086    public Map<String, Object> getParams() {
087        return params;
088    }
089
090    /** Sets up and returns the current {@link OperationContext}, which MUST NOT be closed. */
091    public OperationContext createContext(HttpServletRequest request, HttpServletResponse response, CoreSession session) {
092        restOperationContext.addRequestCleanupHandler(request);
093        restOperationContext.setCoreSession(session);
094        restOperationContext.setInput(input);
095        restOperationContext.put("request", request);
096        return restOperationContext;
097    }
098
099    /**
100     * Returns the current {@link OperationContext}, which MUST NOT be closed.
101     *
102     * @since 7.1
103     */
104    public RestOperationContext getRestOperationContext() {
105        return restOperationContext;
106    }
107}