KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > serverimpl > PublisherHttpConnector


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.publisher.serverimpl;
17
18 import org.apache.avalon.framework.service.Serviceable;
19 import org.apache.avalon.framework.service.ServiceManager;
20 import org.apache.avalon.framework.service.ServiceException;
21 import org.apache.avalon.framework.logger.LogEnabled;
22 import org.apache.avalon.framework.logger.Logger;
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.activity.Initializable;
25 import org.apache.xmlbeans.XmlOptions;
26 import org.outerj.daisy.publisher.Publisher;
27 import org.outerj.daisy.publisher.BlobInfo;
28 import org.outerj.daisy.httpconnector.*;
29 import org.outerj.daisy.repository.Repository;
30 import org.outerj.daisy.repository.VariantKey;
31 import org.outerj.daisy.xmlutil.XmlSerializer;
32 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
33 import org.mortbay.http.handler.AbstractHttpHandler;
34 import org.mortbay.http.HttpRequest;
35 import org.mortbay.http.HttpResponse;
36 import org.mortbay.http.HttpException;
37 import org.xml.sax.ContentHandler JavaDoc;
38 import org.outerx.daisy.x10Publisher.*;
39
40 import java.io.IOException JavaDoc;
41 import java.io.OutputStream JavaDoc;
42 import java.io.InputStream JavaDoc;
43
44 /**
45  * @avalon.component version="1.0" name="publisher-httpconnector" lifestyle="singleton"
46  */

47 public class PublisherHttpConnector implements Serviceable, Initializable, Disposable, LogEnabled {
48     private HttpConnector httpConnector;
49     private PublisherHttpHandler httpHandler = new PublisherHttpHandler();
50     private Logger requestErrorsLogger;
51     private static final int BUFFER_SIZE = 32768;
52
53     /**
54      * @avalon.dependency key="httpconnector" type="org.outerj.daisy.httpconnector.HttpConnector"
55      */

56     public void service(ServiceManager serviceManager) throws ServiceException {
57         httpConnector = (HttpConnector)serviceManager.lookup("httpconnector");
58     }
59
60     public void enableLogging(Logger logger) {
61         this.requestErrorsLogger = logger.getChildLogger("request-errors");
62     }
63
64     public void initialize() throws Exception JavaDoc {
65         httpConnector.addHandler(httpHandler);
66     }
67
68     public void dispose() {
69         httpConnector.removeHandler(httpHandler);
70     }
71
72     private class PublisherHttpHandler extends AbstractHttpHandler {
73         public void handle(String JavaDoc pathInContext, String JavaDoc pathParams, HttpRequest request, HttpResponse response)
74                 throws HttpException, IOException JavaDoc {
75             String JavaDoc path = request.getPath();
76             try {
77                 Repository repository = ((DaisyUserPrincipal)request.getUserPrincipal()).getRepository();
78
79                 if (path.equals("/publisher/blob")) {
80                     long documentId = HttpUtil.getLongParam(request, "documentId");
81                     long branchId = HttpUtil.getBranchId(request, repository);
82                     long languageId = HttpUtil.getLanguageId(request, repository);
83                     VariantKey variantKey = new VariantKey(documentId, branchId, languageId);
84                     String JavaDoc version = HttpUtil.getStringParam(request, "version");
85                     String JavaDoc partType = HttpUtil.getStringParam(request, "partType");
86
87                     Publisher publisher = (Publisher)repository.getExtension("Publisher");
88                     BlobInfo blobInfo = publisher.getBlobInfo(variantKey, version, partType);
89
90                     response.setDateField("Last-Modified", blobInfo.getLastModified());
91                     response.setContentType(blobInfo.getMimeType());
92                     response.setField("Content-Length", String.valueOf(blobInfo.getSize()));
93
94                     OutputStream JavaDoc os = response.getOutputStream();
95                     byte[] buffer = new byte[BUFFER_SIZE];
96                     InputStream JavaDoc is = blobInfo.getInputStream();
97                     try {
98                         int read;
99                         while ((read = is.read(buffer)) != -1) {
100                             os.write(buffer, 0, read);
101                         }
102                     } finally {
103                         is.close();
104                     }
105
106                     response.commit();
107                 } else if (path.equals("/publisher/request")) {
108                     ContentHandler JavaDoc serializer = new XmlSerializer(response.getOutputStream());
109
110                     XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader());
111                     PublisherRequestDocument publisherRequestDocument = PublisherRequestDocument.Factory.parse(request.getInputStream(), xmlOptions);
112                     Publisher publisher = (Publisher)repository.getExtension("Publisher");
113                     publisher.processRequest(publisherRequestDocument, serializer);
114
115                     response.commit();
116                 } else if (path.equals("/publisher/document")) {
117                     // Upon special request:
118
// This URL offers a built-in publisher request that can be performed using GET, and is
119
// equivalent to the /publisher/documentPage request available in Daisy 1.2
120
long documentId = HttpUtil.getLongParam(request, "documentId");
121                     String JavaDoc locale = HttpUtil.getStringParam(request, "locale");
122                     String JavaDoc version = HttpUtil.getStringParam(request, "version");
123                     boolean includeNavigation = HttpUtil.getBooleanParam(request, "includeNavigation");
124                     long navigationDocId = -1;
125                     String JavaDoc activePath = null;
126                     boolean contextualized = true;
127                     if (includeNavigation) {
128                         navigationDocId = HttpUtil.getLongParam(request, "navigationDocId");
129                         activePath = request.getParameter("activePath"); // this is allowed to be null
130
contextualized = HttpUtil.getBooleanParam(request, "contextualized");
131                     }
132
133                     String JavaDoc branch = String.valueOf(HttpUtil.getBranchId(request, repository));
134                     String JavaDoc language = String.valueOf(HttpUtil.getLanguageId(request, repository));
135
136                     PublisherRequestDocument publisherRequestDocument = PublisherRequestDocument.Factory.newInstance();
137                     PublisherRequestDocument.PublisherRequest publisherRequest = publisherRequestDocument.addNewPublisherRequest();
138                     publisherRequest.setLocale(locale);
139                     DocumentDocument.Document pubDocReq = publisherRequest.addNewDocument();
140                     pubDocReq.setId(documentId);
141                     pubDocReq.setBranch(branch);
142                     pubDocReq.setLanguage(language);
143                     pubDocReq.setVersion(version);
144                     pubDocReq.addNewAclInfo();
145                     pubDocReq.addNewSubscriptionInfo();
146                     pubDocReq.addNewShallowAnnotatedVersion();
147                     pubDocReq.addNewAnnotatedDocument();
148                     PreparedDocumentsDocument.PreparedDocuments prepDocReq = pubDocReq.addNewPreparedDocuments();
149                     PreparedDocumentsDocument.PreparedDocuments.Context prepDocReqContext = prepDocReq.addNewContext();
150                     prepDocReqContext.setBranch(branch);
151                     prepDocReqContext.setLanguage(language);
152                     if (includeNavigation) {
153                         VariantKeyType prepDocNav = prepDocReq.addNewNavigationDocument();
154                         prepDocNav.setId(navigationDocId);
155                         prepDocNav.setBranch(branch);
156                         prepDocNav.setLanguage(language);
157                     }
158                     pubDocReq.addNewComments();
159                     if (includeNavigation) {
160                         NavigationTreeDocument.NavigationTree navigationTreeReq = publisherRequest.addNewNavigationTree();
161                         VariantKeyType navDoc = navigationTreeReq.addNewNavigationDocument();
162                         navDoc.setId(navigationDocId);
163                         navDoc.setBranch(branch);
164                         navDoc.setLanguage(language);
165                         VariantKeyType activeDoc = navigationTreeReq.addNewActiveDocument();
166                         activeDoc.setId(documentId);
167                         activeDoc.setBranch(branch);
168                         activeDoc.setLanguage(language);
169                         if (activePath != null) {
170                             navigationTreeReq.setActivePath(activePath);
171                         }
172                         navigationTreeReq.setContextualized(contextualized);
173                     }
174
175                     ContentHandler JavaDoc serializer = new XmlSerializer(response.getOutputStream());
176
177                     Publisher publisher = (Publisher)repository.getExtension("Publisher");
178                     publisher.processRequest(publisherRequestDocument, serializer);
179
180                     response.commit();
181                 }
182             } catch (BadRequestException e) {
183                 // doesn't need to be logged
184
HttpUtil.sendCustomError(e.getMessage(), HttpResponse.__400_Bad_Request, response);
185             } catch (Throwable JavaDoc e) {
186                 requestErrorsLogger.error("Error processing request " + path, e);
187                 HttpUtil.sendCustomError(e, HttpResponse.__202_Accepted, response);
188             }
189         }
190     }
191 }
192
Popular Tags