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 org.nuxeo.ecm.automation.AutomationService;
024import org.nuxeo.ecm.automation.ConflictOperationException;
025import org.nuxeo.ecm.automation.OperationException;
026import org.nuxeo.ecm.automation.OperationNotFoundException;
027import org.nuxeo.ecm.automation.jaxrs.io.operations.ExecutionRequest;
028import org.nuxeo.ecm.automation.server.AutomationServer;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.platform.web.common.exceptionhandling.ExceptionHelper;
032import org.nuxeo.ecm.webengine.WebException;
033import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
034import org.nuxeo.runtime.api.Framework;
035
036import javax.mail.MessagingException;
037import javax.servlet.http.HttpServletRequest;
038import javax.servlet.http.HttpServletResponse;
039import javax.ws.rs.POST;
040import javax.ws.rs.core.Context;
041
042/**
043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
044 */
045public abstract class ExecutableResource {
046
047    @Context
048    protected HttpServletRequest request;
049
050    protected AutomationService service;
051
052    protected ExecutableResource(AutomationService service) {
053        this.service = service;
054    }
055
056    public CoreSession getCoreSession() {
057        return SessionFactory.getSession(request);
058    }
059
060    @POST
061    public Object doPost(@Context HttpServletRequest request, ExecutionRequest xreq) {
062        this.request = request;
063        try {
064            AutomationServer srv = Framework.getLocalService(AutomationServer.class);
065            if (!srv.accept(getId(), isChain(), request)) {
066                return ResponseHelper.notFound();
067            }
068            Object result = execute(xreq);
069            int customHttpStatus = xreq.getRestOperationContext().getHttpStatus();
070            if (customHttpStatus >= 100) {
071                return ResponseHelper.getResponse(result, request, customHttpStatus);
072            }
073            return ResponseHelper.getResponse(result, request);
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}