File Source: OldPageRequest.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.velocity.deprecated;
20
21 import java.util.Enumeration;
22 import java.util.HashSet;
23 import java.util.Locale;
24 import java.util.Set;
25 import javax.servlet.http.HttpServletRequest;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.roller.weblogger.WebloggerException;
30 import org.apache.roller.weblogger.pojos.WeblogTemplate;
31 import org.apache.roller.weblogger.util.Utilities;
32
33
34 /**
35 * Represents an *old* request for a Roller weblog page.
36 *
37 * any url from ... /page/*
38 *
39 * While these urls are no longer used we do provide redirect support for them
40 * for users who have upgraded from earlier versions. We keep this class to
41 * help with parsing these urls since they are fairly complex.
42 */
43 public class OldPageRequest {
44
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.velocity.deprecated.OldPageRequest__static_init
*
* Postconditions:
* init'ed(mLogger)
*/
45 private static Log mLogger = LogFactory.getLog(OldPageRequest.class);
46
47 // various page types
48 public static final String MAIN = "main";
49 public static final String PERMALINK = "permalink";
50 public static final String ARCHIVE = "archive";
51
52 private String context = null;
53 private String pageType = null;
54 private String weblogHandle = null;
55 private String weblogAnchor = null;
56 private String weblogPage = null;
57 private String weblogCategory = null;
58 private String weblogDate = null;
59
60
61 /**
62 * Construct the WeblogPageRequest by parsing the incoming url
63 */
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.velocity.deprecated.OldPageRequest(HttpServletRequest)
*
* Preconditions:
* mLogger != null
* request != null
*
* Presumptions:
* java.lang.String:equals(...)@77 == 1
* java.lang.String:length(...)@100 >= 2
* javax.servlet.http.HttpServletRequest:getPathInfo(...)@70 != null
* javax.servlet.http.HttpServletRequest:getServletPath(...)@69 != null
*
* Postconditions:
* this.context == &"weblog"
* this.pageType in Addr_Set{null,&"permalink",&"archive"}
* init'ed(this.weblogAnchor)
* init'ed(this.weblogCategory)
* this.weblogDate == null
* this.weblogHandle == null
* this.weblogPage == null
*
* Test Vectors:
* javax.servlet.http.HttpServletRequest:getParameter(...)@164: Addr_Set{null}, Inverse{null}
* javax.servlet.http.HttpServletRequest:getParameter(...)@169: Addr_Set{null}, Inverse{null}
* javax.servlet.http.HttpServletRequest:getParameter(...)@174: Addr_Set{null}, Inverse{null}
*/
64 public OldPageRequest(HttpServletRequest request) throws Exception {
65
66 // parse the request object and figure out what we've got
67 mLogger.debug("parsing url "+request.getRequestURL());
68
69 String servlet = request.getServletPath();
70 String pathInfo = request.getPathInfo();
71
72 // make sure this request was destined for the page servlet
73 if(servlet != null) {
74 // strip off the leading slash
75 servlet = servlet.substring(1);
76
77 if("page".equals(servlet)) {
78 this.context = "weblog";
79 } else {
80 // not a request to the page servlet
81 throw new Exception("not a weblog page request, "+request.getRequestURL());
82 }
83 } else {
84 throw new Exception("not a weblog page request, "+request.getRequestURL());
85 }
86
87
88 /*
89 * parse path info
90 *
91 * we expect one of the following forms of urls ...
92 *
93 * [handle] - get default page for user for today's date
94 * [handle]/[date] - get default page for user for specified date
95 * [handle]/[pagelink] - get specified page for today's date
96 * [handle]/[pagelink]/[date] - get specified page for specified date
97 * [handle]/[pagelink]/[anchor] - get specified page & entry (by anchor)
98 * [handle]/[pagelink]/[date]/[anchor] - get specified page & entry (by anchor)
99 */
100 if(pathInfo != null && pathInfo.trim().length() > 1) {
101 // strip off the leading slash
102 pathInfo = pathInfo.substring(1);
103 String[] pathElements = pathInfo.split("/");
104
+ 105 if ( pathElements.length == 1 ) {
106
107 // /handle
+ 108 this.weblogHandle = pathElements[0];
109 this.weblogPage = WeblogTemplate.DEFAULT_PAGE;
110 this.pageType = MAIN;
111
+ 112 } else if ( pathElements.length == 2 ) {
113
114 // /handle/date or /handle/page
+ 115 this.weblogHandle = pathElements[0];
116 this.weblogPage = WeblogTemplate.DEFAULT_PAGE;
117
118 if(this.isValidDateString(pathElements[1])) {
119 this.weblogDate = pathElements[1];
120 this.pageType = ARCHIVE;
121 } else {
122 this.weblogPage = pathElements[1];
123 this.pageType = MAIN;
124 }
125
+ 126 } else if ( pathElements.length == 3 ) {
127
128 // /handle/page/date or /handle/page/anchor
+ 129 this.weblogHandle = pathElements[0];
130 this.weblogPage = pathElements[1];
131
132 if(this.isValidDateString(pathElements[2])) {
133 this.weblogDate = pathElements[2];
134 this.pageType = ARCHIVE;
135 } else {
136 this.weblogAnchor = pathElements[2];
137 this.pageType = PERMALINK;
138 }
139
+ 140 } else if ( pathElements.length == 4 ) {
141
142 // /handle/page/date/anchor
+ 143 this.weblogHandle = pathElements[0];
144 this.weblogPage = pathElements[1];
145 this.weblogDate = pathElements[2];
146 this.weblogAnchor = pathElements[3];
147 this.pageType = PERMALINK;
148 }
149
150 } else {
151 // invalid request ... path info is empty
152 throw new Exception("not a weblog page request, "+request.getRequestURL());
153 }
154
155
156 /*
157 * parse request parameters
158 *
159 * the only params we currently care about are:
160 * anchor - specifies a weblog entry
161 * entry - specifies a weblog entry
162 * catname - specifies a weblog category
163 */
164 if(request.getParameter("anchor") != null) {
165 this.weblogAnchor = request.getParameter("anchor");
166 this.pageType = PERMALINK;
167 }
168
169 if(request.getParameter("entry") != null) {
170 this.weblogAnchor = request.getParameter("entry");
171 this.pageType = PERMALINK;
172 }
173
174 if(request.getParameter("catname") != null) {
175 String cat = request.getParameter("catname");
176
177 this.weblogCategory = cat;
178 this.pageType = ARCHIVE;
179 }
180
181 }
182
183
184 private boolean isValidDateString(String dateString) {
/*
P/P * Method: bool isValidDateString(String)
*
* Postconditions:
* init'ed(return_value)
*/
185 return (dateString != null && dateString.length() > 3 && StringUtils.isNumeric(dateString));
186 }
187
188 public String getContext() {
/*
P/P * Method: String getContext()
*
* Preconditions:
* init'ed(this.context)
*
* Postconditions:
* return_value == this.context
* init'ed(return_value)
*/
189 return context;
190 }
191
192 public String getWeblogHandle() {
/*
P/P * Method: String getWeblogHandle()
*
* Preconditions:
* init'ed(this.weblogHandle)
*
* Postconditions:
* return_value == this.weblogHandle
* init'ed(return_value)
*/
193 return weblogHandle;
194 }
195
196 public String getWeblogAnchor() {
/*
P/P * Method: String getWeblogAnchor()
*
* Preconditions:
* init'ed(this.weblogAnchor)
*
* Postconditions:
* return_value == this.weblogAnchor
* init'ed(return_value)
*/
197 return weblogAnchor;
198 }
199
200 public String getWeblogPage() {
/*
P/P * Method: String getWeblogPage()
*
* Preconditions:
* init'ed(this.weblogPage)
*
* Postconditions:
* return_value == this.weblogPage
* init'ed(return_value)
*/
201 return weblogPage;
202 }
203
204 public String getWeblogCategory() {
/*
P/P * Method: String getWeblogCategory()
*
* Preconditions:
* init'ed(this.weblogCategory)
*
* Postconditions:
* return_value == this.weblogCategory
* init'ed(return_value)
*/
205 return weblogCategory;
206 }
207
208 public String getWeblogDate() {
/*
P/P * Method: String getWeblogDate()
*
* Preconditions:
* init'ed(this.weblogDate)
*
* Postconditions:
* return_value == this.weblogDate
* init'ed(return_value)
*/
209 return weblogDate;
210 }
211
212 public String getPageType() {
/*
P/P * Method: String getPageType()
*
* Preconditions:
* init'ed(this.pageType)
*
* Postconditions:
* return_value == this.pageType
* init'ed(return_value)
*/
213 return pageType;
214 }
215
216 }
SofCheck Inspector Build Version : 2.18479
| OldPageRequest.java |
2009-Jan-02 14:25:44 |
| OldPageRequest.class |
2009-Sep-04 03:12:45 |