001/*
002 * (C) Copyright 2014 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 *     vpasquier <vpasquier@nuxeo.com>
019 */
020package org.nuxeo.ecm.automation.server.jaxrs;
021
022import java.io.IOException;
023import java.security.Principal;
024import java.util.HashSet;
025import java.util.List;
026
027import javax.mail.MessagingException;
028import javax.servlet.http.HttpServletRequest;
029import javax.ws.rs.GET;
030import javax.ws.rs.POST;
031import javax.ws.rs.Path;
032import javax.ws.rs.PathParam;
033import javax.ws.rs.QueryParam;
034import javax.ws.rs.core.Context;
035import javax.ws.rs.core.Response;
036
037import org.nuxeo.ecm.automation.AutomationService;
038import org.nuxeo.ecm.automation.OperationException;
039import org.nuxeo.ecm.automation.OperationNotFoundException;
040import org.nuxeo.ecm.automation.OperationType;
041import org.nuxeo.ecm.automation.core.Constants;
042import org.nuxeo.ecm.automation.jaxrs.LoginInfo;
043import org.nuxeo.ecm.automation.jaxrs.io.operations.AutomationInfo;
044import org.nuxeo.ecm.core.api.Blob;
045import org.nuxeo.ecm.core.api.CoreSession;
046import org.nuxeo.ecm.core.api.DocumentModel;
047import org.nuxeo.ecm.core.api.IdRef;
048import org.nuxeo.ecm.core.api.NuxeoException;
049import org.nuxeo.ecm.core.api.NuxeoPrincipal;
050import org.nuxeo.ecm.core.api.PropertyException;
051import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
052import org.nuxeo.ecm.webengine.model.WebObject;
053import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
054import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
055import org.nuxeo.runtime.api.Framework;
056
057/**
058 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
059 */
060@Path("/automation")
061@WebObject(type = "automation")
062public class AutomationResource extends ModuleRoot {
063
064    protected AutomationService service;
065
066    public AutomationResource() {
067        service = Framework.getService(AutomationService.class);
068    }
069
070    @Path("/doc")
071    public Object getDocPage() {
072        return newObject("doc");
073    }
074
075    @Path("/debug")
076    public Object getDebugPage() {
077        return newObject("debug");
078    }
079
080    /**
081     * Gets the content of the blob or blobs (multipart/mixed) located by the given doc uid and property path.
082     */
083    @SuppressWarnings("unchecked")
084    @GET
085    @Path("/files/{uid}")
086    public Object getFile(@Context HttpServletRequest request, @PathParam("uid") String uid,
087            @QueryParam("path") String path) {
088        try {
089            CoreSession session = SessionFactory.getSession(request);
090            DocumentModel doc = session.getDocument(new IdRef(uid));
091            Object obj;
092            try {
093                obj = doc.getPropertyValue(path);
094            } catch (PropertyException e) {
095                return ResponseHelper.notFound();
096            }
097            if (obj == null) {
098                return ResponseHelper.notFound();
099            }
100            if (obj instanceof List<?>) {
101                List<?> list = (List<?>) obj;
102                if (list.isEmpty()) {
103                    return ResponseHelper.notFound();
104                }
105                if (list.get(0) instanceof Blob) { // a list of blobs -> use
106                    // multipart/mixed
107                    return ResponseHelper.blobs((List<Blob>) list);
108                }
109            } else if (obj instanceof Blob) {
110                return obj; // BlobWriter will do all the processing and call the DownloadService
111            }
112            return ResponseHelper.notFound();
113        } catch (MessagingException | IOException e) {
114            throw new NuxeoException(e);
115        }
116    }
117
118    @GET
119    public AutomationInfo doGet() throws OperationException {
120        return new AutomationInfo(service);
121    }
122
123    @POST
124    @Path("/login")
125    public Object login(@Context HttpServletRequest request) {
126        Principal p = request.getUserPrincipal();
127        if (p instanceof NuxeoPrincipal) {
128            NuxeoPrincipal np = (NuxeoPrincipal) p;
129            List<String> groups = np.getAllGroups();
130            HashSet<String> set = new HashSet<>(groups);
131            return new LoginInfo(np.getName(), set, np.isAdministrator());
132        } else {
133            return Response.status(401).build();
134        }
135    }
136
137    @Path("/{oid}")
138    public Object getExecutable(@PathParam("oid") String oid) {
139        if (oid.startsWith(Constants.CHAIN_ID_PREFIX)) {
140            oid = oid.substring(6);
141        }
142        try {
143            OperationType op = service.getOperation(oid);
144            return newObject("operation", op);
145        } catch (OperationNotFoundException cause) {
146            return new WebResourceNotFoundException("Failed to invoke operation: " + oid, cause);
147        }
148    }
149
150}