File Source: WeblogRequest.java
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.weblogger.ui.rendering.util;
20
21 import java.util.Locale;
22 import javax.servlet.http.HttpServletRequest;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.roller.weblogger.WebloggerException;
26 import org.apache.roller.weblogger.business.WebloggerFactory;
27 import org.apache.roller.weblogger.business.UserManager;
28 import org.apache.roller.weblogger.pojos.Weblog;
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 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 * NOTE: this class purposely exposes a getPathInfo() method which provides the
46 * path info specified by the request that has not been parsed by this
47 * particular class. this makes it relatively easy for subclasses to extend
48 * this class and simply pick up where it left off in the parsing process.
49 */
50 public class WeblogRequest extends ParsedRequest {
51
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.util.WeblogRequest__static_init
*
* Postconditions:
* init'ed(log)
*/
52 private static Log log = LogFactory.getLog(WeblogRequest.class);
53
54 // lightweight attributes
55 private String weblogHandle = null;
56 private String locale = null;
57 private String pathInfo = null;
58
59 // heavyweight attributes
60 private Weblog weblog = null;
61 private Locale localeInstance = null;
62
63
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.util.WeblogRequest()
*
* Postconditions:
* this.authenticUser == null
* this.locale == null
* this.localeInstance == null
* this.pathInfo == null
* this.request == null
* this.user == null
* this.weblog == null
* this.weblogHandle == null
*/
64 public WeblogRequest() {}
65
66
67 public WeblogRequest(HttpServletRequest request)
68 throws InvalidRequestException {
69
70 // let our parent take care of their business first
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.util.WeblogRequest(HttpServletRequest)
*
* Preconditions:
* log != null
* request != null
*
* Presumptions:
* java.lang.String:length(...)@89 >= 1
*
* Postconditions:
* java.lang.String:substring(...)._tainted == 0
* init'ed(this.authenticUser)
* init'ed(this.locale)
* this.localeInstance == null
* this.user == null
* this.weblog == null
* init'ed(this.pathInfo)
* this.request == request
* this.request != null
* this.weblogHandle == null
*
* Test Vectors:
* java.lang.String:endsWith(...)@84: {0}, {1}
* java.lang.String:length(...)@106: {0}, {1..232-1}
* java.lang.String:length(...)@78: {0,1}, {2..232-1}
* javax.servlet.http.HttpServletRequest:getPathInfo(...)@73: Addr_Set{null}, Inverse{null}
* org.apache.commons.logging.Log:isDebugEnabled(...)@122: {0}, {1}
*/
71 super(request);
72
73 String path = request.getPathInfo();
74
75 log.debug("parsing path "+path);
76
77 // first, cleanup extra slashes and extract the weblog weblogHandle
78 if(path != null && path.trim().length() > 1) {
79
80 // strip off the leading slash
81 path = path.substring(1);
82
83 // strip off trailing slash if needed
84 if(path.endsWith("/")) {
85 path = path.substring(0, path.length() - 1);
86 }
87
88 String[] pathElements = path.split("/", 2);
+ 89 if(pathElements[0].trim().length() > 0) {
+ 90 this.weblogHandle = pathElements[0];
91 } else {
92 // no weblogHandle in path info
93 throw new InvalidRequestException("not a weblog request, "+
94 request.getRequestURL());
95 }
96
97 // if there is more left of the path info then hold onto it
+ 98 if(pathElements.length == 2) {
+ 99 path = pathElements[1];
100 } else {
101 path = null;
102 }
103 }
104
105 // second, check if we have a locale, everything else is extra path info
106 if(path != null && path.trim().length() > 0) {
107
108 String[] pathElements = path.split("/", 2);
+ 109 if(this.isLocale(pathElements[0])) {
+ 110 this.locale = pathElements[0];
111
112 // everything else is path info
113 if(pathElements.length == 2) {
114 this.pathInfo = pathElements[1];
115 }
116 } else {
117 // no locale, just extra path info
118 this.pathInfo = path;
119 }
120 }
121
122 if(log.isDebugEnabled()) {
123 log.debug("handle = "+this.weblogHandle);
124 log.debug("locale = "+this.locale);
125 log.debug("pathInfo = "+this.pathInfo);
126 }
127 }
128
129
130 /**
131 * Convenience method which determines if the given string is a valid
132 * locale string.
133 */
134 protected boolean isLocale(String potentialLocale) {
135
/*
P/P * Method: bool isLocale(String)
*
* Postconditions:
* return_value == 0
*
* Test Vectors:
* potentialLocale: Addr_Set{null}, Inverse{null}
* java.lang.String:length(...)@139: {2}, {0,1, 3..232-1}
* java.lang.String:length(...)@139: {0..4, 6..232-1}, {5}
*/
136 boolean isLocale = false;
137
138 // we only support 2 or 5 character locale strings, so check that first
139 if(potentialLocale != null &&
140 (potentialLocale.length() == 2 || potentialLocale.length() == 5)) {
141
142 // now make sure that the format is proper ... e.g. "en_US"
143 // we are not going to be picky about capitalization
144 String[] langCountry = potentialLocale.split("_");
+ 145 if(langCountry.length == 1 &&
146 langCountry[0] != null && langCountry[0].length() == 2) {
147 isLocale = true;
148
+ 149 } else if(langCountry.length == 2 &&
150 langCountry[0] != null && langCountry[0].length() == 2 &&
151 langCountry[1] != null && langCountry[1].length() == 2) {
152
153 isLocale = true;
154 }
155 }
156
157 return isLocale;
158 }
159
160
161 public String getWeblogHandle() {
/*
P/P * Method: String getWeblogHandle()
*
* Preconditions:
* init'ed(this.weblogHandle)
*
* Postconditions:
* return_value == this.weblogHandle
* init'ed(return_value)
*/
162 return weblogHandle;
163 }
164
165 public void setWeblogHandle(String weblogHandle) {
/*
P/P * Method: void setWeblogHandle(String)
*
* Postconditions:
* this.weblogHandle == weblogHandle
* init'ed(this.weblogHandle)
*/
166 this.weblogHandle = weblogHandle;
167 }
168
169 public String getLocale() {
/*
P/P * Method: String getLocale()
*
* Preconditions:
* init'ed(this.locale)
*
* Postconditions:
* return_value == this.locale
* init'ed(return_value)
*/
170 return locale;
171 }
172
173 public void setLocale(String locale) {
/*
P/P * Method: void setLocale(String)
*
* Postconditions:
* this.locale == locale
* init'ed(this.locale)
*/
174 this.locale = locale;
175 }
176
177 public String getPathInfo() {
/*
P/P * Method: String getPathInfo()
*
* Preconditions:
* init'ed(this.pathInfo)
*
* Postconditions:
* return_value == this.pathInfo
* init'ed(return_value)
*/
178 return pathInfo;
179 }
180
181 public void setPathInfo(String pathInfo) {
/*
P/P * Method: void setPathInfo(String)
*
* Postconditions:
* this.pathInfo == pathInfo
* init'ed(this.pathInfo)
*/
182 this.pathInfo = pathInfo;
183 }
184
185 public Weblog getWeblog() {
186
/*
P/P * Method: Weblog getWeblog()
*
* Preconditions:
* init'ed(this.weblog)
* (soft) log != null
* (soft) init'ed(this.weblogHandle)
*
* Presumptions:
* init'ed(java.lang.Boolean.TRUE)
* org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@189 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@189 != null
*
* Postconditions:
* init'ed(return_value)
* this.weblog == return_value
*
* Test Vectors:
* this.weblog: Inverse{null}, Addr_Set{null}
* this.weblogHandle: Addr_Set{null}, Inverse{null}
*/
187 if(weblog == null && weblogHandle != null) {
188 try {
189 UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
190 weblog = umgr.getWebsiteByHandle(weblogHandle, Boolean.TRUE);
191 } catch (WebloggerException ex) {
192 log.error("Error looking up weblog "+weblogHandle, ex);
193 }
194 }
195
196 return weblog;
197 }
198
199 public void setWeblog(Weblog weblog) {
/*
P/P * Method: void setWeblog(Weblog)
*
* Postconditions:
* this.weblog == weblog
* init'ed(this.weblog)
*/
200 this.weblog = weblog;
201 }
202
203
204 /**
205 * Get the Locale instance to be used for this request.
206 *
207 * The Locale is determined via these rules ...
208 * 1. if a locale is explicitly specified, then it is used
209 * 2. if no locale is specified, then use the weblog default locale
210 */
211 public Locale getLocaleInstance() {
212
/*
P/P * Method: Locale getLocaleInstance()
*
* Preconditions:
* init'ed(this.localeInstance)
* (soft) log != null
* (soft) init'ed(this.locale)
* (soft) init'ed(this.weblogHandle)
*
* Postconditions:
* init'ed(return_value)
* this.localeInstance == return_value
* possibly_updated(this.weblog)
* new Locale(getLocaleInstance#1) num objects == 0
* new Locale(getLocaleInstance#2) num objects == 0
*
* Test Vectors:
* this.localeInstance: Inverse{null}, Addr_Set{null}
* this.locale: Addr_Set{null}, Inverse{null}
*/
213 if(localeInstance == null && locale != null) {
214 String[] langCountry = locale.split("_");
+ 215 if(langCountry.length == 1) {
+ 216 localeInstance = new Locale(langCountry[0]);
+ 217 } else if(langCountry.length == 2) {
+ 218 localeInstance = new Locale(langCountry[0], langCountry[1]);
219 }
220 } else if(localeInstance == null) {
+ 221 localeInstance = getWeblog().getLocaleInstance();
222 }
223
224 return localeInstance;
225 }
226
227 public void setLocaleInstance(Locale localeInstance) {
/*
P/P * Method: void setLocaleInstance(Locale)
*
* Postconditions:
* this.localeInstance == localeInstance
* init'ed(this.localeInstance)
*/
228 this.localeInstance = localeInstance;
229 }
230
231 }
SofCheck Inspector Build Version : 2.18479
| WeblogRequest.java |
2009-Jan-02 14:25:18 |
| WeblogRequest.class |
2009-Sep-04 03:12:46 |