KickJava   Java API By Example, From Geeks To Geeks.

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


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.outerj.daisy.publisher.Publisher;
19 import org.outerj.daisy.publisher.BlobInfo;
20 import org.outerj.daisy.publisher.PublisherException;
21 import org.outerj.daisy.publisher.serverimpl.requestmodel.PublisherRequest;
22 import org.outerj.daisy.publisher.serverimpl.requestmodel.RootPublisherContext;
23 import org.outerj.daisy.publisher.serverimpl.requestmodel.PublisherContext;
24 import org.outerj.daisy.repository.*;
25 import org.outerj.daisy.xmlutil.SaxBuffer;
26 import org.outerj.daisy.httpconnector.HttpUtil;
27 import org.xml.sax.ContentHandler JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.helpers.AttributesImpl JavaDoc;
30 import org.apache.avalon.framework.logger.Logger;
31 import org.outerx.daisy.x10Publisher.PublisherRequestDocument;
32 import org.outerx.daisy.x10.ErrorDocument;
33
34 public class PublisherImpl implements Publisher {
35     public static final String JavaDoc NAMESPACE = "http://outerx.org/daisy/1.0#publisher";
36     private Repository repository;
37     private CommonPublisher commonPublisher;
38     private Logger logger;
39
40     public PublisherImpl(Repository repository, CommonPublisher commonPublisher, Logger logger) {
41         this.repository = repository;
42         this.commonPublisher = commonPublisher;
43         this.logger = logger;
44     }
45
46     public BlobInfo getBlobInfo(VariantKey variantKey, String JavaDoc versionSpec, String JavaDoc partType) throws RepositoryException {
47         Document document = repository.getDocument(variantKey, false);
48
49         Version version;
50         if (versionSpec.equalsIgnoreCase("last")) {
51             version = document.getLastVersion();
52         } else if (versionSpec.equals("live")) {
53             version = document.getLiveVersion();
54         } else {
55             version = document.getVersion(Long.parseLong(versionSpec));
56         }
57
58         Part part;
59         if (!(partType.charAt(0) >= '0' && partType.charAt(0) <= '9')) {
60             part = version.getPart(partType);
61         } else {
62             part = version.getPart(Long.parseLong(partType));
63         }
64
65         return new BlobInfoImpl(part, version.getCreated());
66     }
67
68     public void processRequest(PublisherRequestDocument publisherRequestDocument, ContentHandler JavaDoc contentHandler)
69             throws RepositoryException, SAXException JavaDoc, PublisherException {
70         PublisherRequest publisherRequest = commonPublisher.buildPublisherRequest(publisherRequestDocument);
71         executePublisherRequest(publisherRequest, new RootPublisherContext(repository, this, logger), contentHandler);
72     }
73
74     public void performRequest(String JavaDoc pubReqSetName, Document document, Version version,
75             PublisherContext publisherContext, ContentHandler JavaDoc contentHandler) throws Exception JavaDoc {
76         PublisherRequest publisherRequest = commonPublisher.lookupPublisherRequest(pubReqSetName, document, version);
77         executePublisherRequest(publisherRequest, publisherContext, contentHandler);
78     }
79
80     private void executePublisherRequest(PublisherRequest publisherRequest, PublisherContext publisherContext,
81             ContentHandler JavaDoc contentHandler) throws PublisherException, RepositoryException, SAXException JavaDoc {
82         boolean inlineExceptions = publisherRequest.getInlineExceptions();
83         SaxBuffer buffer = new SaxBuffer();
84         try {
85             publisherRequest.process(buffer, publisherContext);
86         } catch (Throwable JavaDoc e) {
87             if (inlineExceptions) {
88                 streamThrowable(e, contentHandler);
89                 return;
90             } else if (e instanceof PublisherException)
91                 throw (PublisherException)e;
92             else if (e instanceof RepositoryException)
93                 throw (RepositoryException)e;
94             else if (e instanceof SAXException JavaDoc)
95                 throw (SAXException JavaDoc)e;
96             else
97                 throw new PublisherException("Error executing publisher request.", e);
98         }
99         buffer.toSAX(contentHandler);
100     }
101
102     private void streamThrowable(Throwable JavaDoc e, ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
103         ErrorDocument errorDocument = HttpUtil.buildErrorXml(e);
104         contentHandler.startDocument();
105         contentHandler.startPrefixMapping("p", NAMESPACE);
106         contentHandler.startElement(PublisherImpl.NAMESPACE, "publisherResponse", "p:publisherResponse", new AttributesImpl JavaDoc());
107         errorDocument.save(new StripDocumentHandler(contentHandler), new DummyLexicalHandler());
108         contentHandler.endElement(PublisherImpl.NAMESPACE, "publisherResponse", "p:publisherResponse");
109         contentHandler.endPrefixMapping("p");
110         contentHandler.endDocument();
111     }
112 }
113
Popular Tags