001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
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: package org.outerj.daisy.doctaskrunner.serverimpl.httphandlers;
017:
018: import org.outerj.daisy.repository.Repository;
019: import org.outerj.daisy.repository.VariantKey;
020: import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
021: import org.outerj.daisy.doctaskrunner.TaskSpecification;
022: import org.outerj.daisy.doctaskrunner.DocumentSelection;
023: import org.outerj.daisy.doctaskrunner.TaskException;
024: import org.outerj.daisy.doctaskrunner.DocumentTaskManager;
025: import org.outerj.daisy.doctaskrunner.commonimpl.SimpleActionsTaskSpecificationImpl;
026: import org.outerj.daisy.util.HttpConstants;
027: import org.outerj.daisy.httpconnector.spi.RequestHandlerSupport;
028: import org.apache.xmlbeans.XmlOptions;
029: import org.outerx.daisy.x10Doctaskrunner.TaskDescriptionDocument;
030: import org.outerx.daisy.x10Doctaskrunner.TaskCreatedDocument;
031:
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034: import java.util.Map;
035: import java.util.List;
036:
037: public class TasksHandler extends AbstractDocTaskRequestHandler {
038: public void handleRequest(Map matchMap, HttpServletRequest request,
039: HttpServletResponse response, Repository repository,
040: RequestHandlerSupport support) throws Exception {
041: DocumentTaskManager taskManager = (DocumentTaskManager) repository
042: .getExtension("DocumentTaskManager");
043: if (request.getMethod().equals(HttpConstants.GET)) {
044: taskManager.getTasks().getXml().save(
045: response.getOutputStream());
046: } else if (request.getMethod().equals(HttpConstants.POST)) {
047: XmlOptions xmlOptions = new XmlOptions()
048: .setLoadUseXMLReader(LocalSAXParserFactory
049: .newXmlReader());
050: TaskDescriptionDocument taskDescriptionDocument = TaskDescriptionDocument.Factory
051: .parse(request.getInputStream(), xmlOptions);
052: TaskDescriptionDocument.TaskDescription taskDescription = taskDescriptionDocument
053: .getTaskDescription();
054:
055: TaskSpecification taskSpecification;
056: TaskDescriptionDocument.TaskDescription.Specification specificationXml = taskDescription
057: .getSpecification();
058: boolean stopOnFirstError = specificationXml
059: .getStopOnFirstError();
060: String description = specificationXml.getDescription();
061: if (specificationXml.isSetSimpleActions()) {
062: taskSpecification = taskManager
063: .createSimpleActionsTaskSpecification(
064: description, stopOnFirstError);
065: ((SimpleActionsTaskSpecificationImpl) taskSpecification)
066: .fromXml(specificationXml.getSimpleActions());
067: } else {
068: String script = specificationXml.getScript()
069: .getStringValue();
070: String scriptLanguage = specificationXml.getScript()
071: .getLanguage();
072: taskSpecification = taskManager
073: .createTaskSpecification(description, script,
074: scriptLanguage, stopOnFirstError);
075: }
076:
077: DocumentSelection documentSelection;
078: if (taskDescription.getDocumentSelection().isSetQuery()) {
079: String query = taskDescription.getDocumentSelection()
080: .getQuery();
081: documentSelection = taskManager
082: .createQueryDocumentSelection(query);
083: } else if (taskDescription.getDocumentSelection()
084: .isSetEnumeration()) {
085: List<TaskDescriptionDocument.TaskDescription.DocumentSelection.Enumeration.Docvariant> docVariantsXml = taskDescription
086: .getDocumentSelection().getEnumeration()
087: .getDocvariantList();
088: VariantKey[] variantKeys = new VariantKey[docVariantsXml
089: .size()];
090: for (int i = 0; i < docVariantsXml.size(); i++) {
091: TaskDescriptionDocument.TaskDescription.DocumentSelection.Enumeration.Docvariant docvariant = docVariantsXml
092: .get(i);
093: variantKeys[i] = new VariantKey(docvariant
094: .getDocumentId(), docvariant.getBranchId(),
095: docvariant.getLanguageId());
096: }
097: documentSelection = taskManager
098: .createEnumerationDocumentSelection(variantKeys);
099: } else {
100: throw new TaskException(
101: "Missing document selection in posted XML.");
102: }
103:
104: long taskId = taskManager.runTask(documentSelection,
105: taskSpecification);
106:
107: TaskCreatedDocument taskCreatedDocument = TaskCreatedDocument.Factory
108: .newInstance();
109: taskCreatedDocument.addNewTaskCreated().setTaskId(taskId);
110: taskCreatedDocument.save(response.getOutputStream());
111: } else {
112: response.sendError(HttpConstants._405_Method_Not_Allowed);
113: }
114: }
115:
116: public String getPathPattern() {
117: return "/task";
118: }
119: }
|