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 javax.servlet.http.HttpServletRequest;
022import javax.servlet.http.HttpServletResponse;
023
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.platform.web.common.RequestCleanupHandler;
026import org.nuxeo.ecm.platform.web.common.RequestContext;
027
028/**
029 * A custom operation context to be used in REST calls on server side. This implementation is delegating the post
030 * execution cleanup to the webengine filter through {@link RequestContext} and {@link RequestCleanupHandler}.
031 * <p>
032 * This way temporary resources like files used by operations are removed after the response is sent to the client.
033 *
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class RestOperationContext extends OperationContext {
037
038
039    /**
040     * Specify the http status when no failure occurs.
041     *
042     * @since 7.1
043     */
044    protected int httpStatus = HttpServletResponse.SC_OK;
045
046    /**
047     * Must be called before context execution.
048     */
049    public void addRequestCleanupHandler(HttpServletRequest request) {
050        RequestContext.getActiveContext(request).addRequestCleanupHandler(new RequestCleanupHandler() {
051            @Override
052            public void cleanup(HttpServletRequest req) {
053                close();
054            }
055        });
056    }
057
058    /**
059     * @since 7.1
060     */
061    public int getHttpStatus() {
062        return httpStatus;
063    }
064
065    /**
066     * @since 7.1
067     */
068    public void setHttpStatus(int httpStatus) {
069        this.httpStatus = httpStatus;
070    }
071
072}