KickJava   Java API By Example, From Geeks To Geeks.

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


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.repository.RepositoryException;
26 import org.outerj.daisy.httpconnector.HttpUtil;
27 import org.outerj.daisy.httpconnector.BadRequestException;
28 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
29 import org.outerx.daisy.x10.DocumentDocument;
30
31 import java.util.Map JavaDoc;
32 import java.util.List JavaDoc;
33
34 public class DocumentHandler extends AbstractDocumentHandler {
35
36     public DocumentHandler(Logger requestErrorLogger, int uploadThreshold, int uploadMaxSize, String JavaDoc uploadTempdir) {
37         super(requestErrorLogger, uploadThreshold, uploadMaxSize, uploadTempdir);
38     }
39
40     public String JavaDoc getPathPattern() {
41         return "/document/*";
42     }
43
44     public void handleRequest(Map JavaDoc matchMap, HttpRequest request, HttpResponse response, Repository repository) throws Exception JavaDoc {
45         long id = HttpUtil.parseId("document", (String JavaDoc)matchMap.get("1"));
46
47         if (request.getMethod().equals(HttpRequest.__GET)) {
48             long branchId = HttpUtil.getBranchId(request, repository);
49             long languageId = HttpUtil.getLanguageId(request, repository);
50             // retrieve the document (note: don't retrieve from cache so that the document contains fresh lock info)
51
Document document = repository.getDocument(id, branchId, languageId, true);
52             if (document.canReadLiveOnly()) {
53                 document.getXmlWithoutVersionedData().save(response.getOutputStream());
54             } else {
55                 document.getXml().save(response.getOutputStream());
56             }
57             response.commit();
58         } else if (request.getMethod().equals(HttpRequest.__POST)) {
59             String JavaDoc actionParam = request.getParameter("action");
60             if (actionParam != null && actionParam.equals("createVariant")) {
61                 long startBranchId = HttpUtil.getBranchId(request, repository, "startBranch");
62                 long startLanguageId = HttpUtil.getLanguageId(request, repository, "startLanguage");
63                 long startVersionId = HttpUtil.getLongParam(request, "startVersion");
64                 long newBranchId = HttpUtil.getBranchId(request, repository, "newBranch");
65                 long newLanguageId = HttpUtil.getLanguageId(request, repository, "newLanguage");
66                 Document document = repository.createVariant(id, startBranchId, startLanguageId, startVersionId, newBranchId, newLanguageId, true);
67                 document.getXml().save(response.getOutputStream());
68                 response.commit();
69             } else if (actionParam == null) {
70                 List JavaDoc uploadedItems = parseMultipartRequest(request, response);
71                 FileItem xmlItem = getItemByName(uploadedItems, "xml");
72                 if (xmlItem == null) {
73                     HttpUtil.sendCustomError("The required field named \"xml\" is missing.", HttpResponse.__400_Bad_Request, response);
74                     return;
75                 }
76                 XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader());
77                 DocumentDocument documentDocument = DocumentDocument.Factory.parse(xmlItem.getInputStream(), xmlOptions);
78                 DocumentDocument.Document documentXml = documentDocument.getDocument();
79
80                 // retrieve the document
81
Document document;
82                 String JavaDoc createVariant =request.getParameter("createVariant");
83                 if (createVariant != null && createVariant.equals("yes")) {
84                     long startBranchId = HttpUtil.getBranchId(request, repository, "startBranch");
85                     long startLanguageId = HttpUtil.getLanguageId(request, repository, "startLanguage");
86                     document = repository.createVariant(id, startBranchId, startLanguageId, -1, documentXml.getBranchId(), documentXml.getLanguageId(), false);
87                 } else {
88                     document = repository.getDocument(id, documentXml.getBranchId(), documentXml.getLanguageId(), true);
89                     if (document.getVariantUpdateCount() != documentXml.getVariantUpdateCount())
90                         throw new RepositoryException("The document variant was updated concurrently.");
91                 }
92                 if (document.getUpdateCount() != documentXml.getUpdateCount())
93                     throw new RepositoryException("The document was updated concurrently.");
94
95                 updateDocument(document, documentXml, uploadedItems, response, repository);
96
97                 boolean validate = documentXml.isSetValidateOnSave() ? documentXml.getValidateOnSave() : true;
98                 document.save(validate);
99
100                 // Send document XML back as response (to consider: maybe this is a bit heavy and/or unneeded?)
101
document.getXml().save(response.getOutputStream());
102                 response.commit();
103             } else {
104                 throw new BadRequestException("Invalid value for action parameter: " + actionParam);
105             }
106         } else if (request.getMethod().equals(HttpRequest.__DELETE)) {
107             // The presence of the branch and language parameters will determine if we delete only a variant
108
// or the complete document.
109
String JavaDoc branchParam = request.getParameter("branch");
110             String JavaDoc languageParam = request.getParameter("language");
111
112             if (branchParam != null && languageParam != null) {
113                 long branchId = HttpUtil.getBranchId(request, repository);
114                 long languageId = HttpUtil.getLanguageId(request, repository);
115                 repository.deleteVariant(id, branchId, languageId);
116             } else if (branchParam == null && languageParam == null) {
117                 repository.deleteDocument(id);
118             } else {
119                 throw new BadRequestException("branch and language request parameters must both be specified, or not at all.");
120             }
121             response.commit();
122         } else {
123             response.sendError(HttpResponse.__405_Method_Not_Allowed);
124         }
125     }
126 }
127
Popular Tags