1 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 ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.XMLReader ; 26 import org.xml.sax.InputSource ; 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 ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 import java.io.IOException ; 37 import java.util.ArrayList ; 38 import java.util.List ; 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 contentHandler, NavigationParams navigationParams, 48 VariantKey activeDocument, boolean handleErrors) throws RepositoryException, SAXException { 49 DaisyHttpClient httpClient = repository.getHttpClient(); 50 HttpMethod method = new GetMethod("/navigation"); 51 List params = new ArrayList (); 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 parserFactory = SAXParserFactory.newInstance(); 73 parserFactory.setNamespaceAware(true); 74 XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader(); 75 xmlReader.setContentHandler(contentHandler); 76 xmlReader.parse(new InputSource (method.getResponseBodyAsStream())); 77 } catch (IOException e) { 78 throw new RepositoryException(e); 79 } catch (ParserConfigurationException e) { 80 throw new RepositoryException(e); 81 } finally { 82 method.releaseConnection(); 83 } 84 } 85 86 public void generatePreviewNavigationTree(ContentHandler contentHandler, String navigationTreeXml, 87 long branchId, long languageId) throws RepositoryException, SAXException { 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 parserFactory = SAXParserFactory.newInstance(); 97 parserFactory.setNamespaceAware(true); 98 XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader(); 99 xmlReader.setContentHandler(contentHandler); 100 xmlReader.parse(new InputSource (method.getResponseBodyAsStream())); 101 } catch (IOException e) { 102 throw new RepositoryException(e); 103 } catch (ParserConfigurationException e) { 104 throw new RepositoryException(e); 105 } finally { 106 method.releaseConnection(); 107 } 108 } 109 110 public NavigationLookupResult lookup(String 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 reverseLookup(VariantKey document, VariantKey navigationDoc, NavigationVersionMode versionMode) throws RepositoryException { 145 throw new RuntimeException ("Method not yet implemented in remote implementation."); 146 } 147 148 public String reverseLookup(VariantKey document, VariantKey navigationDoc) throws RepositoryException { 149 throw new RuntimeException ("Method not yet implemented in remote implementation."); 150 } 151 } 152 | Popular Tags |