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        restOperationContext = new RestOperationContext();
048        this.input = input;
049        params = new HashMap<String, Object>();
050    }
051
052    public void setInput(Object input) {
053        this.input = input;
054    }
055
056    public Object getInput() {
057        return input;
058    }
059
060    public void setContextParam(String key, Object value) {
061        restOperationContext.put(key, value);
062    }
063
064    public void setContextParam(String key, String value) {
065        restOperationContext.put(key, value);
066    }
067
068    public void setParam(String key, Object jsonObject) {
069        params.put(key, jsonObject);
070    }
071
072    public void setParam(String key, String value) {
073        if (value.startsWith("expr:")) {
074            value = value.substring(5).trim();
075            if (value.contains("@{")) {
076                params.put(key, Scripting.newTemplate(value));
077            } else {
078                params.put(key, Scripting.newExpression(value));
079            }
080        } else {
081            params.put(key, value);
082        }
083    }
084
085    public Map<String, Object> getParams() {
086        return params;
087    }
088
089    public OperationContext createContext(HttpServletRequest request, HttpServletResponse response, CoreSession session) {
090        restOperationContext.addRequestCleanupHandler(request);
091        restOperationContext.setCoreSession(session);
092        restOperationContext.setInput(input);
093        restOperationContext.put("request", request);
094        return restOperationContext;
095    }
096
097    /**
098     * @since 7.1
099     */
100    public RestOperationContext getRestOperationContext() {
101        return restOperationContext;
102    }
103}