KickJava   Java API By Example, From Geeks To Geeks.

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


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
30
31 /**
32  * Represents an *old* request for a Roller weblog comments permalink.
33  *
34  * any url from ... /comments/*
35  *
36  * While these urls are no longer used we do provide redirect support for them
37  * for users who have upgraded from earlier versions. We keep this class to
38  * help with parsing these urls since they are fairly complex.
39  */

40 public class OldCommentsRequest {
41     
42     private static Log log = LogFactory.getLog(OldCommentsRequest.class);
43     
44     // various page types
45
public static final String JavaDoc MAIN = "main";
46     public static final String JavaDoc PERMALINK = "permalink";
47     public static final String JavaDoc ARCHIVE = "archive";
48     
49     private String JavaDoc context = null;
50     private String JavaDoc pageType = null;
51     private String JavaDoc weblogHandle = null;
52     private String JavaDoc weblogAnchor = null;
53     private String JavaDoc weblogPage = null;
54     private String JavaDoc weblogCategory = null;
55     private String JavaDoc weblogDate = null;
56     
57     
58     /**
59      * Construct the WeblogPageRequest by parsing the incoming url
60      */

61     public OldCommentsRequest(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
62         
63         // parse the request object and figure out what we've got
64
log.debug("parsing url "+request.getRequestURL());
65         
66         String JavaDoc servlet = request.getServletPath();
67         String JavaDoc pathInfo = request.getPathInfo();
68         
69         // make sure this request was destined for the comments servlet
70
if(servlet != null) {
71             // strip off the leading slash
72
servlet = servlet.substring(1);
73             
74             if("comments".equals(servlet)) {
75                 this.context = "weblog";
76             } else {
77                 // not a request to the page servlet
78
throw new Exception JavaDoc("not a weblog page request, "+request.getRequestURL());
79             }
80         } else {
81             throw new Exception JavaDoc("not a weblog page request, "+request.getRequestURL());
82         }
83         
84         
85         /*
86          * parse path info
87          *
88          * we expect one of the following forms of urls ...
89          *
90          * [handle] - get default page for user for today's date
91          * [handle]/[date] - get default page for user for specified date
92          * [handle]/[pagelink] - get specified page for today's date
93          * [handle]/[pagelink]/[date] - get specified page for specified date
94          * [handle]/[pagelink]/[anchor] - get specified page & entry (by anchor)
95          * [handle]/[pagelink]/[date]/[anchor] - get specified page & entry (by anchor)
96          */

97         if(pathInfo != null && pathInfo.trim().length() > 1) {
98             // strip off the leading slash
99
pathInfo = pathInfo.substring(1);
100             String JavaDoc[] pathElements = pathInfo.split("/");
101             
102             if ( pathElements.length == 1 ) {
103                 
104                 // /handle
105
this.weblogHandle = pathElements[0];
106                 this.weblogPage = "Weblog";
107                 this.pageType = MAIN;
108                 
109             } else if ( pathElements.length == 2 ) {
110                 
111                 // /handle/date or /handle/page
112
this.weblogHandle = pathElements[0];
113                 this.weblogPage = "Weblog";
114                 
115                 if(this.isValidDateString(pathElements[1])) {
116                     this.weblogDate = pathElements[1];
117                     this.pageType = ARCHIVE;
118                 } else {
119                     this.weblogPage = pathElements[1];
120                     this.pageType = MAIN;
121                 }
122                 
123             } else if ( pathElements.length == 3 ) {
124                 
125                 // /handle/page/date or /handle/page/anchor
126
this.weblogHandle = pathElements[0];
127                 this.weblogPage = pathElements[1];
128                 
129                 if(this.isValidDateString(pathElements[2])) {
130                     this.weblogDate = pathElements[2];
131                     this.pageType = ARCHIVE;
132                 } else {
133                     this.weblogAnchor = pathElements[2];
134                     this.pageType = PERMALINK;
135                 }
136                 
137             } else if ( pathElements.length == 4 ) {
138                 
139                 // /handle/page/date/anchor
140
this.weblogHandle = pathElements[0];
141                 this.weblogPage = pathElements[1];
142                 this.weblogDate = pathElements[2];
143                 this.weblogAnchor = pathElements[3];
144                 this.pageType = PERMALINK;
145             }
146             
147         } else {
148             // invalid request ... path info is empty
149
throw new Exception JavaDoc("not a weblog page request, "+request.getRequestURL());
150         }
151         
152         
153         /*
154          * parse request parameters
155          *
156          * the only params we currently care about are:
157          * anchor - specifies a weblog entry
158          * entry - specifies a weblog entry
159          * catname - specifies a weblog category
160          */

161         if(request.getParameter("anchor") != null) {
162             this.weblogAnchor = request.getParameter("anchor");
163             this.pageType = PERMALINK;
164         }
165         
166         if(request.getParameter("entry") != null) {
167             this.weblogAnchor = request.getParameter("entry");
168             this.pageType = PERMALINK;
169         }
170         
171         if(request.getParameter("catname") != null) {
172             String JavaDoc cat = request.getParameter("catname");
173             
174             this.weblogCategory = cat;
175             this.pageType = ARCHIVE;
176         }
177
178         // comments only supported permalinks, so if anchor is null then error
179
if(this.weblogAnchor == null) {
180             throw new Exception JavaDoc("invalid comments request, no anchor");
181         }
182     }
183     
184     
185     private boolean isValidDateString(String JavaDoc dateString) {
186         return (dateString != null && dateString.length() > 3 && StringUtils.isNumeric(dateString));
187     }
188
189     public String JavaDoc getContext() {
190         return context;
191     }
192
193     public String JavaDoc getWeblogHandle() {
194         return weblogHandle;
195     }
196
197     public String JavaDoc getWeblogAnchor() {
198         return weblogAnchor;
199     }
200
201     public String JavaDoc getWeblogPage() {
202         return weblogPage;
203     }
204
205     public String JavaDoc getWeblogCategory() {
206         return weblogCategory;
207     }
208
209     public String JavaDoc getWeblogDate() {
210         return weblogDate;
211     }
212
213     public String JavaDoc getPageType() {
214         return pageType;
215     }
216     
217 }
218
Popular Tags