1 16 package org.outerj.daisy.doctaskrunner.serverimpl; 17 18 import org.outerj.daisy.httpconnector.HttpConnector; 19 import org.outerj.daisy.httpconnector.DaisyUserPrincipal; 20 import org.outerj.daisy.httpconnector.BadRequestException; 21 import org.outerj.daisy.httpconnector.HttpUtil; 22 import org.outerj.daisy.repository.Repository; 23 import org.outerj.daisy.repository.VariantKey; 24 import org.outerj.daisy.doctaskrunner.*; 25 import org.outerj.daisy.doctaskrunner.commonimpl.SimpleActionsTaskSpecificationImpl; 26 import org.outerj.daisy.xmlutil.LocalSAXParserFactory; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.ServiceException; 29 import org.apache.avalon.framework.service.Serviceable; 30 import org.apache.avalon.framework.logger.AbstractLogEnabled; 31 import org.apache.avalon.framework.activity.Initializable; 32 import org.apache.avalon.framework.activity.Disposable; 33 import org.apache.cocoon.matching.helpers.WildcardHelper; 34 import org.apache.xmlbeans.XmlOptions; 35 import org.mortbay.http.handler.AbstractHttpHandler; 36 import org.mortbay.http.HttpRequest; 37 import org.mortbay.http.HttpResponse; 38 import org.mortbay.http.HttpException; 39 import org.outerx.daisy.x10Doctaskrunner.TaskDescriptionDocument; 40 import org.outerx.daisy.x10Doctaskrunner.TaskCreatedDocument; 41 42 import java.io.IOException ; 43 import java.util.HashMap ; 44 45 48 public class DocumentTaskManagerHttpConnector extends AbstractLogEnabled implements Serviceable, Initializable, Disposable { 49 private HttpConnector httpConnector; 50 private DocumentTaskManagerHttpHandler httpHandler = new DocumentTaskManagerHttpHandler(); 51 private static final int[] taskPattern = WildcardHelper.compilePattern("/doctaskrunner/task/*"); 52 private static final int[] taskDocDetailsPattern = WildcardHelper.compilePattern("/doctaskrunner/task/*/docdetails"); 53 54 57 public void service(ServiceManager serviceManager) throws ServiceException { 58 httpConnector = (HttpConnector)serviceManager.lookup("httpconnector"); 59 } 60 61 public void initialize() throws Exception { 62 httpConnector.addHandler(httpHandler); 63 } 64 65 public void dispose() { 66 httpConnector.removeHandler(httpHandler); 67 } 68 69 private class DocumentTaskManagerHttpHandler extends AbstractHttpHandler { 70 public void handle(String pathInContext, String pathParams, HttpRequest request, HttpResponse response) 71 throws HttpException, IOException { 72 String path = request.getPath(); 73 if (!path.startsWith("/doctaskrunner")) 74 return; 75 76 try { 77 Repository repository = ((DaisyUserPrincipal)request.getUserPrincipal()).getRepository(); 78 DocumentTaskManager taskManager = (DocumentTaskManager)repository.getExtension("DocumentTaskManager"); 79 80 HashMap matchMap = new HashMap (); 81 if (path.equals("/doctaskrunner/task")) { 82 if (request.getMethod().equals(HttpRequest.__GET)) { 83 taskManager.getTasks().getXml().save(response.getOutputStream()); 84 response.commit(); 85 } else if (request.getMethod().equals(HttpRequest.__POST)) { 86 XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader()); 87 TaskDescriptionDocument taskDescriptionDocument = TaskDescriptionDocument.Factory.parse(request.getInputStream(), xmlOptions); 88 TaskDescriptionDocument.TaskDescription taskDescription = taskDescriptionDocument.getTaskDescription(); 89 90 TaskSpecification taskSpecification; 91 TaskDescriptionDocument.TaskDescription.Specification specificationXml = taskDescription.getSpecification(); 92 boolean stopOnFirstError = specificationXml.getStopOnFirstError(); 93 String description = specificationXml.getDescription(); 94 if (specificationXml.isSetSimpleActions()) { 95 taskSpecification = taskManager.createSimpleActionsTaskSpecification(description, stopOnFirstError); 96 ((SimpleActionsTaskSpecificationImpl)taskSpecification).fromXml(specificationXml.getSimpleActions()); 97 } else { 98 String script = specificationXml.getScript().getStringValue(); 99 String scriptLanguage = specificationXml.getScript().getLanguage(); 100 taskSpecification = taskManager.createTaskSpecification(description, script, scriptLanguage, stopOnFirstError); 101 } 102 103 DocumentSelection documentSelection; 104 if (taskDescription.getDocumentSelection().isSetQuery()) { 105 String query = taskDescription.getDocumentSelection().getQuery(); 106 documentSelection = taskManager.createQueryDocumentSelection(query); 107 } else if (taskDescription.getDocumentSelection().isSetEnumeration()) { 108 TaskDescriptionDocument.TaskDescription.DocumentSelection.Enumeration.Docvariant[] docVariantsXml = taskDescription.getDocumentSelection().getEnumeration().getDocvariantArray(); 109 VariantKey[] variantKeys = new VariantKey[docVariantsXml.length]; 110 for (int i = 0; i < docVariantsXml.length; i++) { 111 TaskDescriptionDocument.TaskDescription.DocumentSelection.Enumeration.Docvariant docvariant = docVariantsXml[i]; 112 variantKeys[i] = new VariantKey(docvariant.getDocumentId(), docvariant.getBranchId(), docvariant.getLanguageId()); 113 } 114 documentSelection = taskManager.createEnumerationDocumentSelection(variantKeys); 115 } else { 116 throw new TaskException("Missing document selection in posted XML."); 117 } 118 119 long taskId = taskManager.runTask(documentSelection, taskSpecification); 120 121 TaskCreatedDocument taskCreatedDocument = TaskCreatedDocument.Factory.newInstance(); 122 taskCreatedDocument.addNewTaskCreated().setTaskId(taskId); 123 taskCreatedDocument.save(response.getOutputStream()); 124 response.commit(); 125 } else { 126 response.sendError(HttpResponse.__405_Method_Not_Allowed); 127 } 128 } else if (WildcardHelper.match(matchMap, path, taskPattern)) { 129 long taskId = HttpUtil.parseId("task", (String )matchMap.get("1")); 130 if (request.getMethod().equals(HttpRequest.__GET)) { 131 Task task = taskManager.getTask(taskId); 132 task.getXml().save(response.getOutputStream()); 133 response.commit(); 134 } else if (request.getMethod().equals(HttpRequest.__POST)) { 135 String action = HttpUtil.getStringParam(request, "action"); 136 if (action.equals("interrupt")) { 137 taskManager.interruptTask(taskId); 138 response.commit(); 139 } else { 140 throw new TaskException("Unsupported action parameter: " + action); 141 } 142 } else if (request.getMethod().equals(HttpRequest.__DELETE)) { 143 taskManager.deleteTask(taskId); 144 response.commit(); 145 } else { 146 response.sendError(HttpResponse.__405_Method_Not_Allowed); 147 } 148 } else if (WildcardHelper.match(matchMap, path, taskDocDetailsPattern)) { 149 long taskId = HttpUtil.parseId("task", (String )matchMap.get("1")); 150 if (request.getMethod().equals(HttpRequest.__GET)) { 151 taskManager.getTaskDocDetails(taskId).getXml().save(response.getOutputStream()); 152 response.commit(); 153 } else { 154 response.sendError(HttpResponse.__405_Method_Not_Allowed); 155 } 156 } 157 } catch (BadRequestException e) { 158 HttpUtil.sendCustomError(e.getMessage(), HttpResponse.__400_Bad_Request, response); 160 } catch (Throwable e) { 161 getLogger().error("Error processing request " + path, e); 162 HttpUtil.sendCustomError(e, HttpResponse.__202_Accepted, response); 163 } 164 } 165 } 166 } 167 | Popular Tags |