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.Response; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034import org.nuxeo.common.xmap.XMap; 035import org.nuxeo.ecm.automation.AutomationService; 036import org.nuxeo.ecm.automation.OperationChain; 037import org.nuxeo.ecm.automation.OperationContext; 038import org.nuxeo.ecm.automation.OperationException; 039import org.nuxeo.ecm.automation.core.OperationChainContribution; 040import org.nuxeo.ecm.automation.jaxrs.io.JsonWriter; 041import org.nuxeo.ecm.core.api.CoreSession; 042import org.nuxeo.ecm.core.api.DocumentRef; 043import org.nuxeo.ecm.core.api.IdRef; 044import org.nuxeo.ecm.core.api.NuxeoException; 045import org.nuxeo.ecm.core.api.NuxeoPrincipal; 046import org.nuxeo.ecm.core.api.PathRef; 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 @Inject 067 AutomationService service; 068 069 @Inject 070 OperationContext ctx; 071 072 @Inject 073 CoreSession session; 074 075 public AutomationService getOperationService() { 076 return service; 077 } 078 079 public String getOperationsListAsJson() throws OperationException, IOException { 080 return JsonWriter.exportOperations(); 081 } 082 083 @GET 084 @Produces("text/html") 085 public Object doGet() { 086 return getView("index"); 087 } 088 089 @GET 090 @Produces("text/plain") 091 @Path("/doc") 092 public Object doGetText() throws OperationException, IOException { 093 return getOperationsListAsJson(); 094 } 095 096 /** 097 * @since 5.9.1 098 */ 099 @GET 100 @Produces("text/plain") 101 @Path("/studioDoc") 102 public Object doGetStudioDoc() throws OperationException, IOException { 103 return JsonWriter.exportOperations(true); 104 } 105 106 @GET 107 @Produces("application/json") 108 public Object doGetJSON() throws OperationException, IOException { 109 return getOperationsListAsJson(); 110 } 111 112 @POST 113 public Response doPost(@FormParam("input") String input, @FormParam("chain") String chainXml) { 114 if (!((NuxeoPrincipal) session.getPrincipal()).isAdministrator()) { 115 return Response.status(403).build(); 116 } 117 try { 118 ByteArrayInputStream in = new ByteArrayInputStream(chainXml.getBytes()); 119 OperationChainContribution contrib = (OperationChainContribution) xmap.load(in); 120 OperationChain chain = contrib.toOperationChain(Framework.getRuntime().getContext().getBundle()); 121 ctx.setInput(getDocumentRef(input)); 122 getOperationService().run(ctx, chain); 123 return Response.ok("Operation Done.").build(); 124 } catch (NuxeoException | OperationException | IOException e) { 125 log.error(e, e); 126 return Response.status(500).build(); 127 } 128 } 129 130 @POST 131 @Path("{chainId}") 132 public Response doChainIdPost(@FormParam("input") String input, @FormParam("chainId") String chainId) { 133 try { 134 ctx.setInput(getDocumentRef(input)); 135 getOperationService().run(ctx, chainId); 136 return Response.ok("Operation Done.").build(); 137 } catch (OperationException e) { 138 log.error(e, e); 139 return Response.status(500).build(); 140 } 141 } 142 143 protected DocumentRef getDocumentRef(String ref) { 144 if (ref == null || ref.length() == 0) { 145 return null; 146 } 147 if (ref.startsWith("/")) { 148 return new PathRef(ref); 149 } else { 150 return new IdRef(ref); 151 } 152 } 153 154}