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