001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.jaxrs.io.operations;
013
014import java.util.HashMap;
015import java.util.Map;
016
017import javax.servlet.http.HttpServletRequest;
018
019import org.nuxeo.ecm.automation.OperationContext;
020import org.nuxeo.ecm.automation.core.scripting.Scripting;
021import org.nuxeo.ecm.core.api.CoreSession;
022
023/**
024 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
025 */
026public class ExecutionRequest {
027
028    protected Object input;
029
030    protected RestOperationContext restOperationContext;
031
032    protected Map<String, Object> params;
033
034    public ExecutionRequest() {
035        this(null);
036    }
037
038    public ExecutionRequest(Object input) {
039        restOperationContext = new RestOperationContext();
040        this.input = input;
041        params = new HashMap<String, Object>();
042    }
043
044    public void setInput(Object input) {
045        this.input = input;
046    }
047
048    public Object getInput() {
049        return input;
050    }
051
052    public void setContextParam(String key, Object value) {
053        restOperationContext.put(key, value);
054    }
055
056    public void setContextParam(String key, String value) {
057        restOperationContext.put(key, value);
058    }
059
060    public void setParam(String key, Object jsonObject) {
061        params.put(key, jsonObject);
062    }
063
064    public void setParam(String key, String value) {
065        if (value.startsWith("expr:")) {
066            value = value.substring(5).trim();
067            if (value.contains("@{")) {
068                params.put(key, Scripting.newTemplate(value));
069            } else {
070                params.put(key, Scripting.newExpression(value));
071            }
072        } else {
073            params.put(key, value);
074        }
075    }
076
077    public Map<String, Object> getParams() {
078        return params;
079    }
080
081    public OperationContext createContext(HttpServletRequest request, CoreSession session) {
082        restOperationContext.addRequestCleanupHandler(request);
083        restOperationContext.setCoreSession(session);
084        restOperationContext.setInput(input);
085        restOperationContext.put("request", request);
086        return restOperationContext;
087    }
088
089    /**
090     * @since 7.1
091     */
092    public RestOperationContext getRestOperationContext() {
093        return restOperationContext;
094    }
095}