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