001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.outerj.daisy.frontend;
017:
018: import org.apache.cocoon.environment.Request;
019: import org.apache.cocoon.environment.Session;
020: import org.apache.cocoon.environment.Response;
021: import org.apache.cocoon.environment.Cookie;
022: import org.apache.cocoon.xml.SaxBuffer;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.avalon.framework.logger.Logger;
026: import org.outerj.daisy.frontend.components.siteconf.SiteConf;
027: import org.outerj.daisy.frontend.components.siteconf.SitesManager;
028: import org.outerj.daisy.frontend.components.config.ConfigurationManager;
029: import org.outerj.daisy.repository.Repository;
030: import org.outerj.daisy.repository.RepositoryManager;
031: import org.outerj.daisy.repository.Credentials;
032:
033: import java.util.Locale;
034:
035: /**
036: * Provides access to front end (= "Daisy Wiki") context.
037: *
038: * <p>Since FrontEndContext is specific for a request, it should not be stored
039: * in places that exist longer than the duration of the request (e.g. session,
040: * stateful flow variables, ...)
041: */
042: public class FrontEndContext {
043: /** Reference to the request to which this FrontEndContext is associated */
044: private Request request;
045: private ServiceManager serviceManager;
046: private String mountPoint;
047: private String daisyContextPath;
048: private String daisyCocoonPath;
049: private SiteConf siteConf;
050: private String skin;
051: private Locale locale;
052: private String localeAsString;
053: private Logger logger;
054:
055: public static final String REQUEST_ATTR_KEY = "daisyFrontEndContext";
056:
057: protected FrontEndContext(Request request,
058: ServiceManager serviceManager, Logger logger,
059: String mountPoint, String daisyCocoonPath,
060: String daisyContextPath) {
061: this .request = request;
062: this .serviceManager = serviceManager;
063: this .logger = logger;
064: setMountPoint(mountPoint);
065: setDaisyCocoonPath(daisyCocoonPath);
066: setDaisyContextPath(daisyContextPath);
067: }
068:
069: /**
070: * Gets the front end context associated with the given request object.
071: * Throws an exception if it is missing.
072: */
073: public static FrontEndContext get(Request request) {
074: FrontEndContext frontEndContext = (FrontEndContext) request
075: .getAttribute(REQUEST_ATTR_KEY);
076: if (frontEndContext == null)
077: throw new RuntimeException("No FrontEndContext available.");
078: return frontEndContext;
079: }
080:
081: /**
082: * The place where the frontend is mounted in the URL space.
083: * Could e.g. by "/daisy" or an empty string. Useful for building
084: * up absolute URLs.
085: */
086: public String getMountPoint() {
087: return mountPoint;
088: }
089:
090: private void setMountPoint(String mountPoint) {
091: this .mountPoint = mountPoint;
092: // for backwards compatibility, set it also as an attribute on the request
093: request.setAttribute("mountPoint", mountPoint);
094: }
095:
096: /**
097: * Path on disk to the location of the Daisy front end application
098: * (= subdir within the Cocoon webapp).
099: */
100: public String getDaisyContextPath() {
101: return daisyContextPath;
102: }
103:
104: private void setDaisyContextPath(String daisyContextPath) {
105: this .daisyContextPath = daisyContextPath;
106: // for backwards compatibility, set it also as an attribute on the request
107: request.setAttribute("daisyContextPath", this .daisyContextPath);
108: }
109:
110: /**
111: * Place where Daisy is mounted in the Cocoon URL space. In other
112: * words, what you need to put after "cocoon://" to reach the root
113: * Daisy sitemap.
114: *
115: * <p>This string should correspond to a part (or whole) of the
116: * {@link #getMountPoint mount point}.
117: */
118: public String getDaisyCocoonPath() {
119: return daisyCocoonPath;
120: }
121:
122: private void setDaisyCocoonPath(String daisyCocoonPath) {
123: this .daisyCocoonPath = daisyCocoonPath;
124: // for backwards compatibility, set it also as an attribute on the request
125: request.setAttribute("daisyCocoonPath", daisyCocoonPath);
126: }
127:
128: public boolean inSite() {
129: return this .siteConf != null;
130: }
131:
132: public SiteConf getSiteConf() {
133: if (this .siteConf == null)
134: throw new RuntimeException("Site is not set.");
135: return siteConf;
136: }
137:
138: public void setSiteConf(SiteConf siteConf) {
139: this .siteConf = siteConf;
140:
141: // set request attribute for backwards compatibility
142: request.setAttribute("siteConf", siteConf);
143: }
144:
145: public boolean isSkinSet() {
146: return skin != null;
147: }
148:
149: public String getSkin() {
150: if (skin == null)
151: throw new RuntimeException("Skin is not set.");
152: return skin;
153: }
154:
155: public void setSkin(String skin) {
156: this .skin = skin;
157:
158: // set request attribute for backwards compatibility
159: request.setAttribute("skin", skin);
160: }
161:
162: /**
163: * Test is the locale is set. Usually this is always the case, except if an error would
164: * occur before this initialization.
165: */
166: public boolean isLocaleSet() {
167: return locale != null;
168: }
169:
170: public Locale getLocale() {
171: if (locale == null)
172: throw new RuntimeException("Locale is not set.");
173: return locale;
174: }
175:
176: public void setLocale(Locale locale) {
177: this .locale = locale;
178: this .localeAsString = locale.toString();
179:
180: // set request attributes for backwards compatibility
181: request.setAttribute("locale", locale);
182: request.setAttribute("localeAsString", localeAsString);
183: }
184:
185: public String getLocaleAsString() {
186: if (locale == null)
187: throw new RuntimeException("Locale is not set.");
188: return localeAsString;
189: }
190:
191: public Repository getRepository() throws Exception {
192: Session session = request.getSession(false);
193: Repository repository = null;
194: if (session != null)
195: repository = (Repository) session
196: .getAttribute("daisy-repository");
197: if (repository == null) {
198: return getGuestRepository();
199: }
200: return repository;
201: }
202:
203: public Repository getGuestRepository() throws Exception {
204: GuestRepositoryProvider guestRepositoryProvider = null;
205: try {
206: guestRepositoryProvider = (GuestRepositoryProvider) serviceManager
207: .lookup(GuestRepositoryProvider.ROLE);
208: Repository repository = guestRepositoryProvider
209: .getGuestRepository();
210: return repository;
211: } finally {
212: if (guestRepositoryProvider != null)
213: serviceManager.release(guestRepositoryProvider);
214: }
215: }
216:
217: public Repository login(String login, String password)
218: throws Exception {
219: RepositoryManager repositoryManager = (RepositoryManager) serviceManager
220: .lookup("daisy-repository-manager");
221: try {
222: Repository repository = repositoryManager
223: .getRepository(new Credentials(login, password));
224: Session session = request.getSession(true);
225: session.setAttribute("daisy-repository", repository);
226: return repository;
227: } finally {
228: serviceManager.release(repositoryManager);
229: }
230: }
231:
232: /**
233: * Gets access to the repository object for a specific user, without changing
234: * the current repository of the session.
235: */
236: public Repository getRepository(String login, String password)
237: throws Exception {
238: RepositoryManager repositoryManager = (RepositoryManager) serviceManager
239: .lookup("daisy-repository-manager");
240: try {
241: Repository repository = repositoryManager
242: .getRepository(new Credentials(login, password));
243: return repository;
244: } finally {
245: serviceManager.release(repositoryManager);
246: }
247: }
248:
249: public WikiVersionMode getVersionMode() {
250: Session session = request.getSession(false);
251: WikiVersionMode versionMode = null;
252: if (session != null)
253: versionMode = (WikiVersionMode) session
254: .getAttribute("VersionMode");
255: if (versionMode == null)
256: versionMode = WikiVersionMode.LIVE;
257: return versionMode;
258: }
259:
260: public void setVersionMode(WikiVersionMode versionMode) {
261: Session session = request.getSession(true);
262: session.setAttribute("VersionMode", versionMode);
263: }
264:
265: public static void setLocaleCookie(String locale, Response response) {
266: Cookie cookie = response.createCookie("locale", locale);
267: cookie.setPath("/");
268: cookie.setMaxAge(3 * 31 * 24 * 60 * 60); // about three months
269: response.addCookie(cookie);
270: }
271:
272: public SaxBuffer getGlobalSkinConf() throws Exception {
273: SitesManager sitesManager = (SitesManager) serviceManager
274: .lookup(SitesManager.ROLE);
275: try {
276: return sitesManager.getGlobalSkinConf();
277: } finally {
278: serviceManager.release(sitesManager);
279: }
280: }
281:
282: /**
283: * Returns the layoutType specified on the request, or null if not specified.
284: */
285: public String getLayoutType() {
286: return request.getParameter("layoutType");
287: }
288:
289: public PageContext getPageContext() {
290: return new PageContext(request);
291: }
292:
293: public PageContext getPageContext(String layoutType) {
294: return new PageContext(null, layoutType, request);
295: }
296:
297: public PageContext getPageContext(String layoutType,
298: Repository repository) {
299: return new PageContext(repository, layoutType, request);
300: }
301:
302: public PageContext getPageContext(Repository repository) {
303: return new PageContext(repository, null, request);
304: }
305:
306: public ConfigurationManager getConfigurationManager() {
307: ConfigurationManager configurationManager = null;
308: try {
309: configurationManager = (ConfigurationManager) serviceManager
310: .lookup(ConfigurationManager.ROLE);
311: } catch (ServiceException e) {
312: throw new RuntimeException(
313: "Error getting configuration manager.", e);
314: } finally {
315: if (configurationManager != null)
316: serviceManager.release(configurationManager);
317: }
318: return configurationManager;
319: }
320:
321: public Logger getLog() {
322: return logger;
323: }
324: }
|