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.OperationContext; 031import org.nuxeo.ecm.automation.OperationException; 032import org.nuxeo.ecm.automation.OperationNotFoundException; 033import org.nuxeo.ecm.automation.jaxrs.io.operations.ExecutionRequest; 034import org.nuxeo.ecm.automation.server.AutomationServer; 035import org.nuxeo.ecm.core.api.CoreSession; 036import org.nuxeo.ecm.core.api.NuxeoException; 037import org.nuxeo.ecm.platform.web.common.exceptionhandling.ExceptionHelper; 038import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException; 039import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 040import org.nuxeo.runtime.api.Framework; 041 042/** 043 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 044 */ 045public abstract class ExecutableResource extends DefaultObject { 046 047 @Context 048 protected AutomationService service; 049 050 @Context 051 protected HttpServletRequest request; 052 053 @Context 054 protected HttpServletResponse response; 055 056 @Context 057 protected CoreSession session; 058 059 protected OperationContext createContext(ExecutionRequest xreq) { 060 return xreq.createContext(request, response, session); 061 } 062 063 @POST 064 public Object doPost(ExecutionRequest xreq) { 065 try { 066 AutomationServer srv = Framework.getService(AutomationServer.class); 067 if (!srv.accept(getId(), isChain(), request)) { 068 return ResponseHelper.notFound(); 069 } 070 Object result = execute(xreq); 071 int customHttpStatus = xreq.getRestOperationContext().getHttpStatus(); 072 return ResponseHelper.getResponse(result, request, customHttpStatus); 073 } catch (OperationException | NuxeoException | MessagingException | IOException cause) { 074 String exceptionMessage = "Failed to invoke operation: " + getId(); 075 if (cause instanceof OperationNotFoundException) { 076 throw new WebResourceNotFoundException(exceptionMessage, cause); 077 } else if (cause instanceof NuxeoException) { 078 NuxeoException nuxeoException = (NuxeoException) cause; 079 nuxeoException.addInfo(exceptionMessage); 080 throw nuxeoException; 081 } else { 082 Throwable unWrapException = ExceptionHelper.unwrapException(cause); 083 if (unWrapException instanceof RestOperationException) { 084 int customHttpStatus = ((RestOperationException) unWrapException).getStatus(); 085 throw new NuxeoException(exceptionMessage, cause, customHttpStatus); 086 } 087 throw new NuxeoException(exceptionMessage, 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}