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; 022 023import org.nuxeo.ecm.automation.OperationContext; 024import org.nuxeo.ecm.automation.OperationException; 025import org.nuxeo.ecm.webengine.jaxrs.context.RequestCleanupHandler; 026import org.nuxeo.ecm.webengine.jaxrs.context.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; 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 try { 054 deferredDispose(); 055 } catch (OperationException e) { 056 throw new RuntimeException(e); 057 } 058 } 059 }); 060 } 061 062 /** 063 * @since 7.1 064 */ 065 public int getHttpStatus() { 066 return httpStatus; 067 } 068 069 /** 070 * @since 7.1 071 */ 072 public void setHttpStatus(int httpStatus) { 073 this.httpStatus = httpStatus; 074 } 075 076 protected void deferredDispose() throws OperationException { 077 super.dispose(); 078 } 079 080 @Override 081 public void dispose() { 082 // do nothing 083 } 084 085}