KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > util > WeblogRequest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.util;
20
21 import java.util.Locale JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.roller.RollerException;
26 import org.apache.roller.model.RollerFactory;
27 import org.apache.roller.model.UserManager;
28 import org.apache.roller.pojos.WebsiteData;
29
30
31 /**
32  * Represents a request to a weblog.
33  *
34  * This is a fairly generic parsed request which is only trying to figure out
35  * the elements of a weblog request which apply to all weblogs. We try to
36  * determine the weblog weblogHandle, if a locale was specified, and then what extra
37  * path info remains. The basic format is like this ...
38  *
39  * /<weblogHandle>[/locale][/extra/path/info]
40  *
41  * All weblog urls require a weblogHandle, so we ensure that part of the url is
42  * properly specified. locale is always optional, so we do our best to see
43  * if a locale is specified. and path info is always optional.
44  */

45 public class WeblogRequest extends ParsedRequest {
46     
47     private static Log log = LogFactory.getLog(WeblogRequest.class);
48     
49     // lightweight attributes
50
private String JavaDoc weblogHandle = null;
51     private String JavaDoc locale = null;
52     private String JavaDoc pathInfo = null;
53     
54     // heavyweight attributes
55
private WebsiteData weblog = null;
56     private Locale JavaDoc localeInstance = null;
57     
58     
59     public WeblogRequest() {}
60     
61     
62     public WeblogRequest(HttpServletRequest JavaDoc request)
63             throws InvalidRequestException {
64         
65         // let our parent take care of their business first
66
super(request);
67         
68         String JavaDoc path = request.getPathInfo();
69         
70         log.debug("parsing path "+path);
71         
72         // first, cleanup extra slashes and extract the weblog weblogHandle
73
if(path != null && path.trim().length() > 1) {
74             
75             // strip off the leading slash
76
path = path.substring(1);
77             
78             // strip off trailing slash if needed
79
if(path.endsWith("/")) {
80                 path = path.substring(0, path.length() - 1);
81             }
82             
83             String JavaDoc[] pathElements = path.split("/", 2);
84             if(pathElements[0].trim().length() > 0) {
85                 this.weblogHandle = pathElements[0];
86             } else {
87                 // no weblogHandle in path info
88
throw new InvalidRequestException("not a weblog request, "+
89                         request.getRequestURL());
90             }
91             
92             // if there is more left of the path info then hold onto it
93
if(pathElements.length == 2) {
94                 path = pathElements[1];
95             } else {
96                 path = null;
97             }
98         }
99         
100         // second, check if we have a locale, everything else is extra path info
101
if(path != null && path.trim().length() > 0) {
102             
103             String JavaDoc[] pathElements = path.split("/", 2);
104             if(this.isLocale(pathElements[0])) {
105                 this.locale = pathElements[0];
106                 
107                 // everything else is path info
108
if(pathElements.length == 2) {
109                     this.pathInfo = pathElements[1];
110                 }
111             } else {
112                 // no locale, just extra path info
113
this.pathInfo = path;
114             }
115         }
116         
117         if(log.isDebugEnabled()) {
118             log.debug("handle = "+this.weblogHandle);
119             log.debug("locale = "+this.locale);
120             log.debug("pathInfo = "+this.pathInfo);
121         }
122     }
123     
124
125     /**
126      * Convenience method which determines if the given string is a valid
127      * locale string.
128      */

129     private boolean isLocale(String JavaDoc potentialLocale) {
130         
131         boolean isLocale = false;
132         
133         // we only support 2 or 5 character locale strings, so check that first
134
if(potentialLocale != null &&
135                 (potentialLocale.length() == 2 || potentialLocale.length() == 5)) {
136             
137             // now make sure that the format is proper ... e.g. "en_US"
138
// we are not going to be picky about capitalization
139
String JavaDoc[] langCountry = potentialLocale.split("_");
140             if(langCountry.length == 1 &&
141                     langCountry[0] != null && langCountry[0].length() == 2) {
142                 isLocale = true;
143                 
144             } else if(langCountry.length == 2 &&
145                     langCountry[0] != null && langCountry[0].length() == 2 &&
146                     langCountry[1] != null && langCountry[1].length() == 2) {
147                 
148                 isLocale = true;
149             }
150         }
151         
152         return isLocale;
153     }
154     
155     
156     public String JavaDoc getWeblogHandle() {
157         return weblogHandle;
158     }
159
160     public void setWeblogHandle(String JavaDoc weblogHandle) {
161         this.weblogHandle = weblogHandle;
162     }
163     
164     public String JavaDoc getLocale() {
165         return locale;
166     }
167
168     public void setLocale(String JavaDoc locale) {
169         this.locale = locale;
170     }
171
172     public String JavaDoc getPathInfo() {
173         return pathInfo;
174     }
175
176     public void setPathInfo(String JavaDoc pathInfo) {
177         this.pathInfo = pathInfo;
178     }
179
180     public WebsiteData getWeblog() {
181         
182         if(weblog == null && weblogHandle != null) {
183             try {
184                 UserManager umgr = RollerFactory.getRoller().getUserManager();
185                 weblog = umgr.getWebsiteByHandle(weblogHandle, Boolean.TRUE);
186             } catch (RollerException ex) {
187                 log.error("Error looking up weblog "+weblogHandle, ex);
188             }
189         }
190         
191         return weblog;
192     }
193
194     public void setWeblog(WebsiteData weblog) {
195         this.weblog = weblog;
196     }
197     
198     
199     /**
200      * Get the Locale instance to be used for this request.
201      *
202      * The Locale is determined via these rules ...
203      * 1. if a locale is explicitly specified, then it is used
204      * 2. if no locale is specified, then use the weblog default locale
205      */

206     public Locale JavaDoc getLocaleInstance() {
207         
208         if(localeInstance == null && locale != null) {
209             String JavaDoc[] langCountry = locale.split("_");
210             if(langCountry.length == 1) {
211                 localeInstance = new Locale JavaDoc(langCountry[0]);
212             } else if(langCountry.length == 2) {
213                 localeInstance = new Locale JavaDoc(langCountry[0], langCountry[1]);
214             }
215         } else if(localeInstance == null) {
216             localeInstance = getWeblog().getLocaleInstance();
217         }
218         
219         return localeInstance;
220     }
221
222     public void setLocaleInstance(Locale JavaDoc localeInstance) {
223         this.localeInstance = localeInstance;
224     }
225     
226 }
227
Popular Tags