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