KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > AbstractDocumentApple


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.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 /**
34  * This class provides basic handling for all types of request related to a specific document,
35  * more specifically the determination of the document variant (id, branch, language) to be
36  * handled based on the URL path, the SiteConf and request parameters.
37  */

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 JavaDoc branch;
44     private String JavaDoc language;
45     private long versionId;
46     private String JavaDoc variantParams;
47     private String JavaDoc variantQueryString;
48     private SiteConf siteConf;
49     private RemoteRepositoryImpl repository;
50     private String JavaDoc 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 JavaDoc {
57         if (needsInitialisation()) {
58             Request request = appleRequest.getCocoonRequest();
59             siteConf = WikiHelper.getSiteConf(request);
60             repository = (RemoteRepositoryImpl)WikiHelper.getRepository(request, serviceManager);
61
62             //
63
// Determine document ID
64
//
65

66             requestedNavigationPath = appleRequest.getSitemapParameter("navigationPath");
67             if (requestedNavigationPath == null)
68                 throw new Exception JavaDoc("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 JavaDoc suffix = appleRequest.getSitemapParameter("suffix");
80                 if (suffix == null)
81                     suffix = ".html";
82                 String JavaDoc 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 JavaDoc("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             //
94
// Set the selected branch and/or language
95
//
96

97             setBranchAndLanguage(variantKey.getBranchId(), variantKey.getLanguageId());
98
99             //
100
// Determine version
101
//
102

103             String JavaDoc versionIdParam = appleRequest.getSitemapParameter("versionId", null);
104             try {
105                 if (versionIdParam == null)
106                     versionId = -3; // -3 == default according to publisher version mode
107
else if (versionIdParam.equalsIgnoreCase("live"))
108                     versionId = -1; // -1 == live version
109
else if (versionIdParam.equalsIgnoreCase("last"))
110                     versionId = -2; // -2 == last version
111
else
112                     versionId = Long.parseLong(versionIdParam);
113             } catch (NumberFormatException JavaDoc e) {
114                 throw new Exception JavaDoc("Invalid version id: " + versionIdParam);
115             }
116         }
117
118         processDocumentRequest(appleRequest, appleResponse);
119     }
120
121     protected void setBranchAndLanguage(long branchId, long languageId) throws Exception JavaDoc {
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         // prepare params view can use
129
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 JavaDoc;
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 JavaDoc getBranch() {
155         return branch;
156     }
157
158     protected String JavaDoc getLanguage() {
159         return language;
160     }
161
162     protected long getVersionId() {
163         return versionId;
164     }
165
166     protected String JavaDoc getVariantParams() {
167         return variantParams;
168     }
169
170     protected String JavaDoc 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 JavaDoc getRequestedNavigationPath() {
187         return requestedNavigationPath;
188     }
189 }
190
Popular Tags