KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > doctaskrunner > serverimpl > DocumentTaskManagerHttpConnector


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
43 import java.util.HashMap JavaDoc;
44
45 /**
46  * @avalon.component version="1.0" name="doctaskrunner-httpconnector" lifestyle="singleton"
47  */

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     /**
55      * @avalon.dependency key="httpconnector" type="org.outerj.daisy.httpconnector.HttpConnector"
56      */

57     public void service(ServiceManager serviceManager) throws ServiceException {
58         httpConnector = (HttpConnector)serviceManager.lookup("httpconnector");
59     }
60
61     public void initialize() throws Exception JavaDoc {
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 JavaDoc pathInContext, String JavaDoc pathParams, HttpRequest request, HttpResponse response)
71                 throws HttpException, IOException JavaDoc {
72             String JavaDoc 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 JavaDoc matchMap = new HashMap JavaDoc();
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 JavaDoc description = specificationXml.getDescription();
94                         if (specificationXml.isSetSimpleActions()) {
95                             taskSpecification = taskManager.createSimpleActionsTaskSpecification(description, stopOnFirstError);
96                             ((SimpleActionsTaskSpecificationImpl)taskSpecification).fromXml(specificationXml.getSimpleActions());
97                         } else {
98                             String JavaDoc script = specificationXml.getScript().getStringValue();
99                             String JavaDoc scriptLanguage = specificationXml.getScript().getLanguage();
100                             taskSpecification = taskManager.createTaskSpecification(description, script, scriptLanguage, stopOnFirstError);
101                         }
102
103                         DocumentSelection documentSelection;
104                         if (taskDescription.getDocumentSelection().isSetQuery()) {
105                             String JavaDoc 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 JavaDoc)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 JavaDoc 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 JavaDoc)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                 // doesn't need to be logged
159
HttpUtil.sendCustomError(e.getMessage(), HttpResponse.__400_Bad_Request, response);
160             } catch (Throwable JavaDoc e) {
161                 getLogger().error("Error processing request " + path, e);
162                 HttpUtil.sendCustomError(e, HttpResponse.__202_Accepted, response);
163             }
164         }
165     }
166 }
167
Popular Tags