KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.cocoon.environment.*;
19 import org.apache.avalon.framework.service.ServiceManager;
20 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
21 import org.outerj.daisy.repository.Repository;
22 import org.outerj.daisy.repository.RepositoryManager;
23 import org.outerj.daisy.repository.Credentials;
24
25 import java.util.Locale JavaDoc;
26
27 public class WikiHelper {
28     public static String JavaDoc getMountPoint(Request request) {
29         String JavaDoc mountPoint = (String JavaDoc)request.getAttribute("mountPoint");
30         if (mountPoint == null)
31             throw new RuntimeException JavaDoc("Could not find mountPoint, is MountPointAction in the pipeline?");
32         return mountPoint;
33     }
34
35     public static String JavaDoc getDaisyContextPath(Request request) {
36         String JavaDoc daisyContextPath = (String JavaDoc)request.getAttribute("daisyContextPath");
37         if (daisyContextPath == null)
38             throw new RuntimeException JavaDoc("Could not find daisyContextPath, is MountPointAction in the pipeline?");
39         return daisyContextPath;
40     }
41
42     public static String JavaDoc getDaisyCocoonPath(Request request) {
43         String JavaDoc daisyCocoonPath = (String JavaDoc)request.getAttribute("daisyCocoonPath");
44         if (daisyCocoonPath == null)
45             throw new RuntimeException JavaDoc("Could not find daisyCocoonPath, is MountPointAction in the pipeline?");
46         return daisyCocoonPath;
47     }
48
49     public static SiteConf getSiteConf(Request request) {
50         SiteConf siteConf = (SiteConf)request.getAttribute("siteConf");
51         if (siteConf == null)
52             throw new RuntimeException JavaDoc("Could not find SiteConf.");
53         return siteConf;
54     }
55
56     public static Locale JavaDoc getLocale(Request request) {
57         Locale JavaDoc locale = (Locale JavaDoc)request.getAttribute("locale");
58         if (locale == null)
59             throw new RuntimeException JavaDoc("Missing \"locale\" attribute on request.");
60         return locale;
61     }
62
63     public static String JavaDoc getLocaleAsString(Request request) {
64         String JavaDoc localeAsString = (String JavaDoc)request.getAttribute("localeAsString");
65         if (localeAsString == null)
66             throw new RuntimeException JavaDoc("Missing \"localeAsString\" attribute on request.");
67         return localeAsString;
68     }
69
70     public static String JavaDoc getSkin(Request request) {
71         String JavaDoc skin = (String JavaDoc)request.getAttribute("skin");
72         if (skin == null)
73             throw new RuntimeException JavaDoc("Missing \"skin\" attribute on request.");
74         return skin;
75     }
76
77     public static Repository getRepository(Request request, ServiceManager serviceManager) throws Exception JavaDoc {
78         Session session = request.getSession(false);
79         Repository repository = null;
80         if (session != null)
81             repository = (Repository)session.getAttribute("daisy-repository");
82         if (repository == null) {
83             return getGuestRepository(serviceManager);
84         }
85         return repository;
86     }
87
88     public static Repository getGuestRepository(ServiceManager serviceManager) throws Exception JavaDoc {
89         GuestRepositoryProvider guestRepositoryProvider = null;
90         try {
91             guestRepositoryProvider = (GuestRepositoryProvider)serviceManager.lookup(GuestRepositoryProvider.ROLE);
92             Repository repository = guestRepositoryProvider.getGuestRepository();
93             return repository;
94         } finally {
95             if (guestRepositoryProvider != null)
96                 serviceManager.release(guestRepositoryProvider);
97         }
98     }
99
100     public static Repository login(String JavaDoc login, String JavaDoc password, Request request, ServiceManager serviceManager) throws Exception JavaDoc {
101         RepositoryManager repositoryManager = (RepositoryManager)serviceManager.lookup("daisy-repository-manager");
102         try {
103             Repository repository = repositoryManager.getRepository(new Credentials(login, password));
104             Session session = request.getSession(true);
105             session.setAttribute("daisy-repository", repository);
106             return repository;
107         } finally {
108             serviceManager.release(repositoryManager);
109         }
110     }
111
112     public static Repository getRepository(String JavaDoc login, String JavaDoc password, ServiceManager serviceManager) throws Exception JavaDoc {
113         RepositoryManager repositoryManager = (RepositoryManager)serviceManager.lookup("daisy-repository-manager");
114         try {
115             Repository repository = repositoryManager.getRepository(new Credentials(login, password));
116             return repository;
117         } finally {
118             serviceManager.release(repositoryManager);
119         }
120     }
121
122     /**
123      * Returns the layoutType specified on the request, or null if not specified.
124      */

125     public static String JavaDoc getLayoutType(Request request) {
126         return request.getParameter("layoutType");
127     }
128
129     public static void changeLocale(Locale JavaDoc locale, Request request) {
130         request.setAttribute("locale", locale);
131         request.setAttribute("localeAsString", locale.toString());
132     }
133
134     public static void setLocaleCookie(String JavaDoc locale, Response response) {
135         Cookie cookie = response.createCookie("locale", locale);
136         cookie.setPath("/");
137         cookie.setMaxAge(3 * 31 * 24 * 60 * 60); // about three months
138
response.addCookie(cookie);
139     }
140
141     public static WikiVersionMode getVersionMode(Request request) {
142         Session session = request.getSession(false);
143         WikiVersionMode versionMode = null;
144         if (session != null)
145             versionMode = (WikiVersionMode)session.getAttribute("PublisherVersionMode");
146         if (versionMode == null)
147             versionMode = WikiVersionMode.LIVE;
148         return versionMode;
149     }
150
151     public static void setVersionMode(Request request, WikiVersionMode versionMode) {
152         Session session = request.getSession(true);
153         session.setAttribute("PublisherVersionMode", versionMode);
154     }
155 }
156
Popular Tags