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.debug;
013
014import java.io.ByteArrayInputStream;
015import java.io.IOException;
016
017import javax.ws.rs.FormParam;
018import javax.ws.rs.GET;
019import javax.ws.rs.POST;
020import javax.ws.rs.Path;
021import javax.ws.rs.Produces;
022import javax.ws.rs.core.Response;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.common.xmap.XMap;
027import org.nuxeo.ecm.automation.AutomationService;
028import org.nuxeo.ecm.automation.OperationChain;
029import org.nuxeo.ecm.automation.OperationContext;
030import org.nuxeo.ecm.automation.OperationException;
031import org.nuxeo.ecm.automation.core.OperationChainContribution;
032import org.nuxeo.ecm.automation.jaxrs.io.JsonWriter;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentRef;
035import org.nuxeo.ecm.core.api.IdRef;
036import org.nuxeo.ecm.core.api.NuxeoException;
037import org.nuxeo.ecm.core.api.NuxeoPrincipal;
038import org.nuxeo.ecm.core.api.PathRef;
039import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
040import org.nuxeo.ecm.webengine.model.Access;
041import org.nuxeo.ecm.webengine.model.WebObject;
042import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
043import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
044import org.nuxeo.runtime.api.Framework;
045
046/**
047 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
048 */
049@WebObject(type = "debug", administrator = Access.GRANT)
050public class DebugResource extends AbstractResource<ResourceTypeImpl> {
051
052    private static final Log log = LogFactory.getLog(DebugResource.class);
053
054    static final XMap xmap = new XMap();
055    static {
056        xmap.register(OperationChainContribution.class);
057    }
058
059    public AutomationService getOperationService() {
060        return Framework.getLocalService(AutomationService.class);
061    }
062
063    public String getOperationsListAsJson() throws OperationException, IOException {
064        return JsonWriter.exportOperations();
065    }
066
067    @GET
068    @Produces("text/html")
069    public Object doGet() {
070        return getView("index");
071    }
072
073    @GET
074    @Produces("text/plain")
075    @Path("/doc")
076    public Object doGetText() throws OperationException, IOException {
077        return getOperationsListAsJson();
078    }
079
080    /**
081     * @since 5.9.1
082     */
083    @GET
084    @Produces("text/plain")
085    @Path("/studioDoc")
086    public Object doGetStudioDoc() throws OperationException, IOException {
087        return JsonWriter.exportOperations(true);
088    }
089
090    @GET
091    @Produces("application/json")
092    public Object doGetJSON() throws OperationException, IOException {
093        return getOperationsListAsJson();
094    }
095
096    @POST
097    public Response doPost(@FormParam("input") String input, @FormParam("chain") String chainXml) {
098        CoreSession session = SessionFactory.getSession();
099        if (!((NuxeoPrincipal) session.getPrincipal()).isAdministrator()) {
100            return Response.status(403).build();
101        }
102        try {
103            ByteArrayInputStream in = new ByteArrayInputStream(chainXml.getBytes());
104            OperationChainContribution contrib = (OperationChainContribution) xmap.load(in);
105            OperationChain chain = contrib.toOperationChain(Framework.getRuntime().getContext().getBundle());
106            OperationContext ctx = new OperationContext(session);
107            ctx.setInput(getDocumentRef(input));
108            getOperationService().run(ctx, chain);
109            return Response.ok("Operation Done.").build();
110        } catch (NuxeoException | OperationException | IOException e) {
111            log.error(e, e);
112            return Response.status(500).build();
113        }
114    }
115
116    @POST
117    @Path("{chainId}")
118    public Response doChainIdPost(@FormParam("input") String input, @FormParam("chainId") String chainId) {
119        try {
120            OperationContext ctx = new OperationContext(SessionFactory.getSession());
121            ctx.setInput(getDocumentRef(input));
122            getOperationService().run(ctx, chainId);
123            return Response.ok("Operation Done.").build();
124        } catch (OperationException e) {
125            log.error(e, e);
126            return Response.status(500).build();
127        }
128    }
129
130    protected DocumentRef getDocumentRef(String ref) {
131        if (ref == null || ref.length() == 0) {
132            return null;
133        }
134        if (ref.startsWith("/")) {
135            return new PathRef(ref);
136        } else {
137            return new IdRef(ref);
138        }
139    }
140
141}