1 16 package org.outerj.daisy.frontend; 17 18 import org.outerj.daisy.frontend.util.AbstractDaisyApple; 19 import org.outerj.daisy.frontend.util.EncodingUtil; 20 import org.outerj.daisy.frontend.components.siteconf.SiteConf; 21 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryImpl; 22 import org.outerj.daisy.repository.VariantKey; 23 import org.outerj.daisy.repository.variant.VariantManager; 24 import org.outerj.daisy.navigation.NavigationManager; 25 import org.outerj.daisy.navigation.NavigationLookupResult; 26 import org.apache.avalon.framework.service.Serviceable; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.cocoon.components.flow.apples.AppleRequest; 29 import org.apache.cocoon.components.flow.apples.AppleResponse; 30 import org.apache.cocoon.ResourceNotFoundException; 31 import org.apache.cocoon.environment.Request; 32 33 38 public abstract class AbstractDocumentApple extends AbstractDaisyApple implements Serviceable { 39 private ServiceManager serviceManager; 40 private long documentId; 41 private long branchId; 42 private long languageId; 43 private String branch; 44 private String language; 45 private long versionId; 46 private String variantParams; 47 private String variantQueryString; 48 private SiteConf siteConf; 49 private RemoteRepositoryImpl repository; 50 private String requestedNavigationPath; 51 52 public final void service(ServiceManager serviceManager) { 53 this.serviceManager = serviceManager; 54 } 55 56 protected final void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception { 57 if (needsInitialisation()) { 58 Request request = appleRequest.getCocoonRequest(); 59 siteConf = WikiHelper.getSiteConf(request); 60 repository = (RemoteRepositoryImpl)WikiHelper.getRepository(request, serviceManager); 61 62 66 requestedNavigationPath = appleRequest.getSitemapParameter("navigationPath"); 67 if (requestedNavigationPath == null) 68 throw new Exception ("Missing sitemap parameter: navigationPath"); 69 70 long requestedBranchId = RequestUtil.getBranchId(request, -1, repository); 71 long requestedLanguageId = RequestUtil.getLanguageId(request, -1, repository); 72 73 NavigationManager navigationManager = (NavigationManager)repository.getExtension("NavigationManager"); 74 NavigationLookupResult lookupResult = navigationManager.lookup(requestedNavigationPath, requestedBranchId, requestedLanguageId, 75 siteConf.getNavigationLookupAlternatives(WikiHelper.getVersionMode(request))); 76 if (lookupResult.isNotFound()) { 77 throw new ResourceNotFoundException("Path not found: " + requestedNavigationPath); 78 } else if (lookupResult.isRedirect() && request.getMethod().equals("GET")) { 79 String suffix = appleRequest.getSitemapParameter("suffix"); 80 if (suffix == null) 81 suffix = ".html"; 82 String queryString = request.getQueryString(); 83 queryString = queryString == null ? "" : "?" + queryString; 84 appleResponse.redirectTo(EncodingUtil.encodePathQuery(getMountPoint() + "/" + lookupResult.getLookupAlternativeName() + lookupResult.getNavigationPath() + suffix + queryString)); 85 return; 86 } else if (lookupResult.isRedirect() && lookupResult.getVariantKey() == null) { 87 throw new Exception ("Requested path \"" + requestedNavigationPath + "\" redirects to \"" + lookupResult.getNavigationPath() + "\" but request method is " + request.getMethod() + " thus cannot redirect."); 88 } 89 90 VariantKey variantKey = lookupResult.getVariantKey(); 91 documentId = variantKey.getDocumentId(); 92 93 97 setBranchAndLanguage(variantKey.getBranchId(), variantKey.getLanguageId()); 98 99 103 String versionIdParam = appleRequest.getSitemapParameter("versionId", null); 104 try { 105 if (versionIdParam == null) 106 versionId = -3; else if (versionIdParam.equalsIgnoreCase("live")) 108 versionId = -1; else if (versionIdParam.equalsIgnoreCase("last")) 110 versionId = -2; else 112 versionId = Long.parseLong(versionIdParam); 113 } catch (NumberFormatException e) { 114 throw new Exception ("Invalid version id: " + versionIdParam); 115 } 116 } 117 118 processDocumentRequest(appleRequest, appleResponse); 119 } 120 121 protected void setBranchAndLanguage(long branchId, long languageId) throws Exception { 122 this.branchId = branchId; 123 this.languageId = languageId; 124 VariantManager variantManager = repository.getVariantManager(); 125 branch = variantManager.getBranch(branchId, false).getName(); 126 language = variantManager.getLanguage(languageId, false).getName(); 127 128 if (branchId != siteConf.getBranchId() || languageId != siteConf.getLanguageId()) { 130 variantParams = "&branch=" + branch + "&language=" + language; 131 variantQueryString = "?branch=" + branch + "&language=" + language; 132 } else { 133 variantParams = ""; 134 variantQueryString = ""; 135 } 136 } 137 138 protected abstract void processDocumentRequest(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception ; 139 140 protected abstract boolean needsInitialisation(); 141 142 protected long getDocumentId() { 143 return documentId; 144 } 145 146 protected long getBranchId() { 147 return branchId; 148 } 149 150 protected long getLanguageId() { 151 return languageId; 152 } 153 154 protected String getBranch() { 155 return branch; 156 } 157 158 protected String getLanguage() { 159 return language; 160 } 161 162 protected long getVersionId() { 163 return versionId; 164 } 165 166 protected String getVariantParams() { 167 return variantParams; 168 } 169 170 protected String getVariantQueryString() { 171 return variantQueryString; 172 } 173 174 protected SiteConf getSiteConf() { 175 return siteConf; 176 } 177 178 protected RemoteRepositoryImpl getRepository() { 179 return repository; 180 } 181 182 protected ServiceManager getServiceManager() { 183 return serviceManager; 184 } 185 186 protected String getRequestedNavigationPath() { 187 return requestedNavigationPath; 188 } 189 } 190 | Popular Tags |