KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > clientimpl > RemotePublisher


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.clientimpl;
17
18 import org.outerj.daisy.publisher.Publisher;
19 import org.outerj.daisy.publisher.BlobInfo;
20 import org.outerj.daisy.repository.VariantKey;
21 import org.outerj.daisy.repository.RepositoryException;
22 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryImpl;
23 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
24 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
25 import org.xml.sax.ContentHandler JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.outerx.daisy.x10Publisher.PublisherRequestDocument;
29 import org.apache.commons.httpclient.methods.PostMethod;
30 import org.apache.commons.httpclient.methods.GetMethod;
31
32 import javax.xml.parsers.SAXParser JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.HashMap JavaDoc;
35
36 public class RemotePublisher implements Publisher {
37     private RemoteRepositoryImpl repository;
38
39     public RemotePublisher(RemoteRepositoryImpl repository) {
40         this.repository = repository;
41     }
42
43     public BlobInfo getBlobInfo(VariantKey variantKey, String JavaDoc versionSpec, String JavaDoc partType) throws RepositoryException {
44         Map JavaDoc requestParams = new HashMap JavaDoc();
45         requestParams.put("documentId", String.valueOf(variantKey.getDocumentId()));
46         requestParams.put("branch", String.valueOf(variantKey.getBranchId()));
47         requestParams.put("language", String.valueOf(variantKey.getLanguageId()));
48         requestParams.put("version", versionSpec);
49         requestParams.put("partType", partType);
50
51         GetMethod method = repository.getResource("/publisher/blob", requestParams);
52         return new BlobInfoImpl(method);
53     }
54
55     public void processRequest(PublisherRequestDocument publisherRequestDocument, ContentHandler JavaDoc contentHandler) throws RepositoryException, SAXException JavaDoc {
56         try {
57             DaisyHttpClient httpClient = repository.getHttpClient();
58             PostMethod method = new PostMethod("/publisher/request");
59             method.setRequestBody(publisherRequestDocument.newInputStream());
60
61             try {
62                 httpClient.executeMethod(method, null, false);
63                 SAXParser JavaDoc parser = LocalSAXParserFactory.getSAXParserFactory().newSAXParser();
64                 parser.getXMLReader().setContentHandler(contentHandler);
65                 InputSource JavaDoc is = new InputSource JavaDoc(method.getResponseBodyAsStream());
66                 parser.getXMLReader().parse(is);
67             } finally {
68                 method.releaseConnection();
69             }
70         } catch (Exception JavaDoc e) {
71             if (e instanceof RepositoryException)
72                 throw (RepositoryException)e;
73             if (e instanceof SAXException JavaDoc)
74                 throw (SAXException JavaDoc)e;
75             throw new RepositoryException("Error handling publisher request.", e);
76         }
77     }
78 }
79
Popular Tags