KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > navigation > clientimpl > RemoteNavigationManager


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.navigation.clientimpl;
17
18 import org.outerj.daisy.navigation.*;
19 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryImpl;
20 import org.outerj.daisy.repository.clientimpl.infrastructure.DaisyHttpClient;
21 import org.outerj.daisy.repository.RepositoryException;
22 import org.outerj.daisy.repository.VariantKey;
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.XMLReader JavaDoc;
26 import org.xml.sax.InputSource JavaDoc;
27 import org.apache.commons.httpclient.HttpMethod;
28 import org.apache.commons.httpclient.NameValuePair;
29 import org.apache.commons.httpclient.methods.GetMethod;
30 import org.apache.commons.httpclient.methods.PostMethod;
31 import org.outerx.daisy.x10Navigationspec.NavigationLookupResultDocument;
32 import org.outerx.daisy.x10Navigationspec.NavigationLookupDocument;
33
34 import javax.xml.parsers.SAXParserFactory JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.List JavaDoc;
39
40 public class RemoteNavigationManager implements NavigationManager {
41     private RemoteRepositoryImpl repository;
42
43     public RemoteNavigationManager(RemoteRepositoryImpl repository) {
44         this.repository = repository;
45     }
46
47     public void generateNavigationTree(ContentHandler JavaDoc contentHandler, NavigationParams navigationParams,
48             VariantKey activeDocument, boolean handleErrors) throws RepositoryException, SAXException JavaDoc {
49         DaisyHttpClient httpClient = repository.getHttpClient();
50         HttpMethod method = new GetMethod("/navigation");
51         List JavaDoc params = new ArrayList JavaDoc();
52         VariantKey navigationDoc = navigationParams.getNavigationDoc();
53         params.add(new NameValuePair("navigationDocId", String.valueOf(navigationDoc.getDocumentId())));
54         params.add(new NameValuePair("navigationDocBranch", String.valueOf(navigationDoc.getBranchId())));
55         params.add(new NameValuePair("navigationDocLanguage", String.valueOf(navigationDoc.getLanguageId())));
56         params.add(new NameValuePair("navigationVersionMode", navigationParams.getVersionMode().toString()));
57         if (navigationParams.getActivePath() != null)
58             params.add(new NameValuePair("activePath", navigationParams.getActivePath()));
59         if (activeDocument != null) {
60             params.add(new NameValuePair("activeDocumentId", String.valueOf(activeDocument.getDocumentId())));
61             params.add(new NameValuePair("activeDocumentBranch", String.valueOf(activeDocument.getBranchId())));
62             params.add(new NameValuePair("activeDocumentLanguage", String.valueOf(activeDocument.getLanguageId())));
63         }
64         params.add(new NameValuePair("contextualized", String.valueOf(navigationParams.getContextualized())));
65         params.add(new NameValuePair("handleErrors", String.valueOf(handleErrors)));
66
67         NameValuePair[] queryString = (NameValuePair[])params.toArray(new NameValuePair[params.size()]);
68         method.setQueryString(queryString);
69
70         httpClient.executeMethod(method, null, false);
71         try {
72             SAXParserFactory JavaDoc parserFactory = SAXParserFactory.newInstance();
73             parserFactory.setNamespaceAware(true);
74             XMLReader JavaDoc xmlReader = parserFactory.newSAXParser().getXMLReader();
75             xmlReader.setContentHandler(contentHandler);
76             xmlReader.parse(new InputSource JavaDoc(method.getResponseBodyAsStream()));
77         } catch (IOException JavaDoc e) {
78             throw new RepositoryException(e);
79         } catch (ParserConfigurationException JavaDoc e) {
80             throw new RepositoryException(e);
81         } finally {
82             method.releaseConnection();
83         }
84     }
85
86     public void generatePreviewNavigationTree(ContentHandler JavaDoc contentHandler, String JavaDoc navigationTreeXml,
87             long branchId, long languageId) throws RepositoryException, SAXException JavaDoc {
88         DaisyHttpClient httpClient = repository.getHttpClient();
89         PostMethod method = new PostMethod("/navigationPreview");
90         method.addParameter("navigationXml", navigationTreeXml);
91         method.addParameter("branch", String.valueOf(branchId));
92         method.addParameter("language", String.valueOf(languageId));
93
94         httpClient.executeMethod(method, null, false);
95         try {
96             SAXParserFactory JavaDoc parserFactory = SAXParserFactory.newInstance();
97             parserFactory.setNamespaceAware(true);
98             XMLReader JavaDoc xmlReader = parserFactory.newSAXParser().getXMLReader();
99             xmlReader.setContentHandler(contentHandler);
100             xmlReader.parse(new InputSource JavaDoc(method.getResponseBodyAsStream()));
101         } catch (IOException JavaDoc e) {
102             throw new RepositoryException(e);
103         } catch (ParserConfigurationException JavaDoc e) {
104             throw new RepositoryException(e);
105         } finally {
106             method.releaseConnection();
107         }
108     }
109
110     public NavigationLookupResult lookup(String JavaDoc navigationPath, long requestedBranchId, long requestedLanguageId, LookupAlternative[] lookupAlternatives) throws RepositoryException {
111         DaisyHttpClient httpClient = repository.getHttpClient();
112         PostMethod method = new PostMethod("/navigationLookup");
113
114         NavigationLookupDocument navigationLookupDocument = NavigationLookupDocument.Factory.newInstance();
115         NavigationLookupDocument.NavigationLookup navigationLookupXml = navigationLookupDocument.addNewNavigationLookup();
116         navigationLookupXml.setNavigationPath(navigationPath);
117         if (requestedBranchId != -1)
118             navigationLookupXml.setRequestedBranchId(requestedBranchId);
119         if (requestedLanguageId != -1)
120             navigationLookupXml.setRequestedLanguageId(requestedLanguageId);
121
122         NavigationLookupDocument.NavigationLookup.LookupAlternative[] lookupAlternativesXml
123                 = new NavigationLookupDocument.NavigationLookup.LookupAlternative[lookupAlternatives.length];
124
125         for (int i = 0; i < lookupAlternatives.length; i++) {
126             LookupAlternative lookupAlternative = lookupAlternatives[i];
127             lookupAlternativesXml[i] = NavigationLookupDocument.NavigationLookup.LookupAlternative.Factory.newInstance();
128             lookupAlternativesXml[i].setName(lookupAlternative.getName());
129             lookupAlternativesXml[i].setCollectionId(lookupAlternative.getCollectionId());
130             VariantKey navDocKey = lookupAlternative.getNavigationDoc();
131             lookupAlternativesXml[i].setNavDocId(navDocKey.getDocumentId());
132             lookupAlternativesXml[i].setNavBranchId(navDocKey.getBranchId());
133             lookupAlternativesXml[i].setNavLangId(navDocKey.getLanguageId());
134             lookupAlternativesXml[i].setNavVersionMode(lookupAlternative.getVersionMode().toString());
135         }
136
137         navigationLookupXml.setLookupAlternativeArray(lookupAlternativesXml);
138
139         method.setRequestBody(navigationLookupDocument.newInputStream());
140         NavigationLookupResultDocument resultDocument = (NavigationLookupResultDocument)httpClient.executeMethod(method, NavigationLookupResultDocument.class, true);
141         return NavigationLookupResult.createFromXml(resultDocument.getNavigationLookupResult());
142     }
143
144     public String JavaDoc reverseLookup(VariantKey document, VariantKey navigationDoc, NavigationVersionMode versionMode) throws RepositoryException {
145         throw new RuntimeException JavaDoc("Method not yet implemented in remote implementation.");
146     }
147
148     public String JavaDoc reverseLookup(VariantKey document, VariantKey navigationDoc) throws RepositoryException {
149         throw new RuntimeException JavaDoc("Method not yet implemented in remote implementation.");
150     }
151 }
152
Popular Tags