001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.server.jaxrs;
013
014import java.io.IOException;
015
016import org.nuxeo.ecm.automation.AutomationService;
017import org.nuxeo.ecm.automation.ConflictOperationException;
018import org.nuxeo.ecm.automation.OperationException;
019import org.nuxeo.ecm.automation.OperationNotFoundException;
020import org.nuxeo.ecm.automation.jaxrs.io.operations.ExecutionRequest;
021import org.nuxeo.ecm.automation.server.AutomationServer;
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.NuxeoException;
024import org.nuxeo.ecm.platform.web.common.exceptionhandling.ExceptionHelper;
025import org.nuxeo.ecm.webengine.WebException;
026import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
027import org.nuxeo.runtime.api.Framework;
028
029import javax.mail.MessagingException;
030import javax.servlet.http.HttpServletRequest;
031import javax.servlet.http.HttpServletResponse;
032import javax.ws.rs.POST;
033import javax.ws.rs.core.Context;
034
035/**
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038public abstract class ExecutableResource {
039
040    @Context
041    protected HttpServletRequest request;
042
043    protected AutomationService service;
044
045    protected ExecutableResource(AutomationService service) {
046        this.service = service;
047    }
048
049    public CoreSession getCoreSession() {
050        return SessionFactory.getSession(request);
051    }
052
053    @POST
054    public Object doPost(@Context HttpServletRequest request, ExecutionRequest xreq) {
055        this.request = request;
056        try {
057            AutomationServer srv = Framework.getLocalService(AutomationServer.class);
058            if (!srv.accept(getId(), isChain(), request)) {
059                return ResponseHelper.notFound();
060            }
061            Object result = execute(xreq);
062            int customHttpStatus = xreq.getRestOperationContext().getHttpStatus();
063            if (customHttpStatus >= 100) {
064                return ResponseHelper.getResponse(result, request, customHttpStatus);
065            }
066            return ResponseHelper.getResponse(result, request);
067        } catch (OperationException | NuxeoException | SecurityException | MessagingException | IOException cause) {
068            if (cause instanceof ConflictOperationException) {
069                throw WebException.newException("Failed to invoke operation: " + getId(), cause,
070                        HttpServletResponse.SC_CONFLICT);
071            } else if (cause instanceof OperationNotFoundException) {
072                throw WebException.newException("Failed to invoke operation: " + getId(), cause,
073                        HttpServletResponse.SC_NOT_FOUND);
074            } else {
075                Throwable unWrapException = ExceptionHelper.unwrapException(cause);
076                if (unWrapException instanceof RestOperationException) {
077                    int customHttpStatus = ((RestOperationException) unWrapException).getStatus();
078                    throw WebException.newException("Failed to invoke operation: " + getId(), cause, customHttpStatus);
079                }
080                throw WebException.newException("Failed to invoke operation: " + getId(), cause);
081            }
082        }
083    }
084
085    public abstract String getId();
086
087    public abstract Object execute(ExecutionRequest req) throws OperationException;
088
089    public abstract boolean isChain();
090
091}