KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > httpconnector > handlers > DocumentsHandler


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.httpconnector.handlers;
17
18 import org.apache.avalon.framework.logger.Logger;
19 import org.apache.commons.fileupload.FileItem;
20 import org.apache.xmlbeans.XmlOptions;
21 import org.mortbay.http.HttpRequest;
22 import org.mortbay.http.HttpResponse;
23 import org.outerj.daisy.repository.Repository;
24 import org.outerj.daisy.repository.Document;
25 import org.outerj.daisy.httpconnector.HttpUtil;
26 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
27 import org.outerx.daisy.x10.DocumentDocument;
28
29 import java.util.Map JavaDoc;
30 import java.util.List JavaDoc;
31
32 public class DocumentsHandler extends AbstractDocumentHandler {
33
34     public DocumentsHandler(Logger requestErrorLogger, int uploadThreshold, int uploadMaxSize, String JavaDoc uploadTempdir) {
35         super(requestErrorLogger, uploadThreshold, uploadMaxSize, uploadTempdir);
36     }
37
38     public String JavaDoc getPathPattern() {
39         return "/document";
40     }
41
42     public void handleRequest(Map JavaDoc matchMap, HttpRequest request, HttpResponse response, Repository repository) throws Exception JavaDoc {
43         if (request.getMethod().equals(HttpRequest.__POST)) {
44             // POST here is for creating a new document
45

46             List JavaDoc uploadedItems = parseMultipartRequest(request, response);
47             FileItem xmlItem = getItemByName(uploadedItems, "xml");
48             if (xmlItem == null) {
49                 HttpUtil.sendCustomError("The required field named \"xml\" is missing.", HttpResponse.__400_Bad_Request, response);
50                 return;
51             }
52             XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader());
53             DocumentDocument documentDocument = DocumentDocument.Factory.parse(xmlItem.getInputStream(), xmlOptions);
54             DocumentDocument.Document documentXml = documentDocument.getDocument();
55
56             // create the document
57
Document document = repository.createDocument(documentXml.getName(), documentXml.getTypeId(), documentXml.getBranchId(), documentXml.getLanguageId());
58             updateDocument(document, documentXml, uploadedItems, response, repository);
59
60             boolean validate = documentXml.isSetValidateOnSave() ? documentXml.getValidateOnSave() : true;
61             document.save(validate);
62
63             // Send document XML back as response
64
document.getXml().save(response.getOutputStream());
65             response.commit();
66             return;
67         } else {
68             response.sendError(HttpResponse.__405_Method_Not_Allowed);
69             return;
70         }
71     }
72 }
73
Popular Tags