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.server.jaxrs; 020 021import java.io.IOException; 022 023import javax.mail.MessagingException; 024import javax.servlet.http.HttpServletRequest; 025import javax.servlet.http.HttpServletResponse; 026import javax.ws.rs.POST; 027import javax.ws.rs.core.Context; 028 029import org.nuxeo.ecm.automation.AutomationService; 030import org.nuxeo.ecm.automation.ConflictOperationException; 031import org.nuxeo.ecm.automation.OperationContext; 032import org.nuxeo.ecm.automation.OperationException; 033import org.nuxeo.ecm.automation.OperationNotFoundException; 034import org.nuxeo.ecm.automation.jaxrs.io.operations.ExecutionRequest; 035import org.nuxeo.ecm.automation.server.AutomationServer; 036import org.nuxeo.ecm.core.api.CoreSession; 037import org.nuxeo.ecm.core.api.NuxeoException; 038import org.nuxeo.ecm.platform.web.common.exceptionhandling.ExceptionHelper; 039import org.nuxeo.ecm.webengine.WebException; 040import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 045 */ 046public abstract class ExecutableResource extends DefaultObject { 047 048 @Context 049 protected AutomationService service; 050 051 @Context 052 protected HttpServletRequest request; 053 054 @Context 055 protected HttpServletResponse response; 056 057 @Context 058 protected CoreSession session; 059 060 protected OperationContext createContext(ExecutionRequest xreq) { 061 return xreq.createContext(request, response, session); 062 } 063 064 @POST 065 public Object doPost(ExecutionRequest xreq) { 066 try { 067 AutomationServer srv = Framework.getLocalService(AutomationServer.class); 068 if (!srv.accept(getId(), isChain(), request)) { 069 return ResponseHelper.notFound(); 070 } 071 Object result = execute(xreq); 072 int customHttpStatus = xreq.getRestOperationContext().getHttpStatus(); 073 return ResponseHelper.getResponse(result, request, customHttpStatus); 074 } catch (OperationException | NuxeoException | SecurityException | MessagingException | IOException cause) { 075 if (cause instanceof ConflictOperationException) { 076 throw WebException.newException("Failed to invoke operation: " + getId(), cause, 077 HttpServletResponse.SC_CONFLICT); 078 } else if (cause instanceof OperationNotFoundException) { 079 throw WebException.newException("Failed to invoke operation: " + getId(), cause, 080 HttpServletResponse.SC_NOT_FOUND); 081 } else { 082 Throwable unWrapException = ExceptionHelper.unwrapException(cause); 083 if (unWrapException instanceof RestOperationException) { 084 int customHttpStatus = ((RestOperationException) unWrapException).getStatus(); 085 throw WebException.newException("Failed to invoke operation: " + getId(), cause, customHttpStatus); 086 } 087 throw WebException.newException("Failed to invoke operation: " + getId(), cause); 088 } 089 } 090 } 091 092 public abstract String getId(); 093 094 public abstract Object execute(ExecutionRequest req) throws OperationException; 095 096 public abstract boolean isChain(); 097 098}