KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > velocity > deprecated > OldPageRequest


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.velocity.deprecated;
20
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
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.RollerException;
30 import org.apache.roller.pojos.WeblogTemplate;
31 import org.apache.roller.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     
45     private static Log mLogger = LogFactory.getLog(OldPageRequest.class);
46     
47     // various page types
48
public static final String JavaDoc MAIN = "main";
49     public static final String JavaDoc PERMALINK = "permalink";
50     public static final String JavaDoc ARCHIVE = "archive";
51     
52     private String JavaDoc context = null;
53     private String JavaDoc pageType = null;
54     private String JavaDoc weblogHandle = null;
55     private String JavaDoc weblogAnchor = null;
56     private String JavaDoc weblogPage = null;
57     private String JavaDoc weblogCategory = null;
58     private String JavaDoc weblogDate = null;
59     
60     
61     /**
62      * Construct the WeblogPageRequest by parsing the incoming url
63      */

64     public OldPageRequest(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
65         
66         // parse the request object and figure out what we've got
67
mLogger.debug("parsing url "+request.getRequestURL());
68         
69         String JavaDoc servlet = request.getServletPath();
70         String JavaDoc 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 JavaDoc("not a weblog page request, "+request.getRequestURL());
82             }
83         } else {
84             throw new Exception JavaDoc("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 JavaDoc[] 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 JavaDoc("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 JavaDoc cat = request.getParameter("catname");
176             
177             this.weblogCategory = cat;
178             this.pageType = ARCHIVE;
179         }
180
181     }
182     
183     
184     private boolean isValidDateString(String JavaDoc dateString) {
185         return (dateString != null && dateString.length() > 3 && StringUtils.isNumeric(dateString));
186     }
187
188     public String JavaDoc getContext() {
189         return context;
190     }
191
192     public String JavaDoc getWeblogHandle() {
193         return weblogHandle;
194     }
195
196     public String JavaDoc getWeblogAnchor() {
197         return weblogAnchor;
198     }
199
200     public String JavaDoc getWeblogPage() {
201         return weblogPage;
202     }
203
204     public String JavaDoc getWeblogCategory() {
205         return weblogCategory;
206     }
207
208     public String JavaDoc getWeblogDate() {
209         return weblogDate;
210     }
211
212     public String JavaDoc getPageType() {
213         return pageType;
214     }
215     
216 }
217
Popular Tags