KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.RollerException;
28 import org.apache.roller.model.RollerFactory;
29 import org.apache.roller.model.UserManager;
30 import org.apache.roller.model.WeblogManager;
31 import org.apache.roller.pojos.Template;
32 import org.apache.roller.pojos.WeblogCategoryData;
33 import org.apache.roller.pojos.WeblogEntryData;
34 import org.apache.roller.pojos.WeblogTemplate;
35 import org.apache.roller.util.URLUtilities;
36
37
38 /**
39  * Represents a request for a Roller weblog page.
40  *
41  * any url from ... /roller-ui/rendering/page/*
42  *
43  * We use this class as a helper to parse an incoming url and sort out the
44  * information embedded in the url for later use.
45  */

46 public class WeblogPageRequest extends WeblogRequest {
47     
48     private static Log log = LogFactory.getLog(WeblogPageRequest.class);
49     
50     private static final String JavaDoc PAGE_SERVLET = "/roller-ui/rendering/page";
51     
52     // lightweight attributes
53
private String JavaDoc context = null;
54     private String JavaDoc weblogAnchor = null;
55     private String JavaDoc weblogPageName = null;
56     private String JavaDoc weblogCategoryName = null;
57     private String JavaDoc weblogDate = null;
58     private int pageNum = 0;
59     private Map JavaDoc customParams = new HashMap JavaDoc();
60     
61     // heavyweight attributes
62
private WeblogEntryData weblogEntry = null;
63     private Template weblogPage = null;
64     private WeblogCategoryData weblogCategory = null;
65     
66     
67     public WeblogPageRequest() {}
68     
69     
70     /**
71      * Construct the WeblogPageRequest by parsing the incoming url
72      */

73     public WeblogPageRequest(HttpServletRequest JavaDoc request)
74             throws InvalidRequestException {
75         
76         // let our parent take care of their business first
77
// parent determines weblog handle and locale if specified
78
super(request);
79         
80         String JavaDoc servlet = request.getServletPath();
81         
82         // we only want the path info left over from after our parents parsing
83
String JavaDoc pathInfo = this.getPathInfo();
84         
85         // parse the request object and figure out what we've got
86
log.debug("parsing path "+pathInfo);
87         
88         // was this request bound for the right servlet?
89
if(!isValidDestination(servlet)) {
90             throw new InvalidRequestException("invalid destination for request, "+
91                     request.getRequestURL());
92         }
93         
94         
95         /*
96          * parse path info
97          *
98          * we expect one of the following forms of url ...
99          *
100          * /entry/<anchor> - permalink
101          * /date/<YYYYMMDD> - date collection view
102          * /category/<category> - category collection view
103          * /page/<pagelink> - custom page
104          *
105          * path info may be null, which indicates the weblog homepage
106          */

107         if(pathInfo != null && pathInfo.trim().length() > 0) {
108             
109             // all views use 2 path elements, except category
110
String JavaDoc[] pathElements = pathInfo.split("/");
111             if(pathElements.length > 1 && "category".equals(pathElements[0])) {
112                 
113                 // category may have multiple path elements, so re-split with max 2
114
pathElements = pathInfo.split("/", 2);
115                 this.context = pathElements[0];
116                 this.weblogCategoryName = "/"+URLUtilities.decode(pathElements[1]);
117                 
118                 // all categories must start with a /
119
if(!this.weblogCategoryName.startsWith("/")) {
120                     this.weblogCategoryName = "/"+this.weblogCategoryName;
121                 }
122
123             } else if(pathElements.length == 2) {
124                 
125                 this.context = pathElements[0];
126                 if("entry".equals(this.context)) {
127                     this.weblogAnchor = URLUtilities.decode(pathElements[1]);
128                     
129                 } else if("date".equals(this.context)) {
130                     if(this.isValidDateString(pathElements[1])) {
131                         this.weblogDate = pathElements[1];
132                     } else {
133                         throw new InvalidRequestException("invalid date, "+
134                             request.getRequestURL());
135                     }
136                     
137                 } else if("page".equals(this.context)) {
138                     this.weblogPageName = pathElements[1];
139                     
140                 } else {
141                     throw new InvalidRequestException("context "+this.context+
142                             "not supported, "+request.getRequestURL());
143                 }
144                 
145             } else {
146                 throw new InvalidRequestException("bad path info, "+
147                         request.getRequestURL());
148             }
149             
150         } else {
151             // default view, weblog homepage
152
}
153         
154         
155         /*
156          * parse request parameters
157          *
158          * the only params we currently allow are:
159          * date - specifies a weblog date string
160          * cat - specifies a weblog category
161          * anchor - specifies a weblog entry (old way)
162          * entry - specifies a weblog entry
163          *
164          * we only allow request params if the path info is null or on user
165          * defined pages (for backwards compatability). this way
166          * we prevent mixing of path based and query param style urls.
167          */

168         if(pathInfo == null || this.weblogPageName != null) {
169             
170             // check for entry/anchor params which indicate permalink
171
if(request.getParameter("entry") != null) {
172                 String JavaDoc anchor = request.getParameter("entry");
173                 if(StringUtils.isNotEmpty(anchor)) {
174                     this.weblogAnchor = anchor;
175                 }
176             } else if(request.getParameter("anchor") != null) {
177                 String JavaDoc anchor = request.getParameter("anchor");
178                 if(StringUtils.isNotEmpty(anchor)) {
179                     this.weblogAnchor = anchor;
180                 }
181             }
182             
183             // only check for other params if we didn't find an anchor above
184
if(this.weblogAnchor == null) {
185                 if(request.getParameter("date") != null) {
186                     String JavaDoc date = request.getParameter("date");
187                     if(this.isValidDateString(date)) {
188                         this.weblogDate = date;
189                     } else {
190                         throw new InvalidRequestException("invalid date, "+
191                                 request.getRequestURL());
192                     }
193                 }
194                 
195                 if(request.getParameter("cat") != null) {
196                     this.weblogCategoryName =
197                             URLUtilities.decode(request.getParameter("cat"));
198                     
199                     // all categories must start with a /
200
if(!this.weblogCategoryName.startsWith("/")) {
201                         this.weblogCategoryName = "/"+this.weblogCategoryName;
202                     }
203                 }
204             }
205         }
206         
207         // page request param is supported in all views
208
if(request.getParameter("page") != null) {
209             String JavaDoc pageInt = request.getParameter("page");
210             try {
211                 this.pageNum = Integer.parseInt(pageInt);
212             } catch(NumberFormatException JavaDoc e) {
213                 // ignored, bad input
214
}
215         }
216         
217         // build customParams Map, we remove built-in params because we only
218
// want this map to represent params defined by the template author
219
customParams = new HashMap JavaDoc(request.getParameterMap());
220         customParams.remove("entry");
221         customParams.remove("anchor");
222         customParams.remove("date");
223         customParams.remove("cat");
224         customParams.remove("page");
225             
226         if(log.isDebugEnabled()) {
227             log.debug("context = "+this.context);
228             log.debug("weblogAnchor = "+this.weblogAnchor);
229             log.debug("weblogDate = "+this.weblogDate);
230             log.debug("weblogCategory = "+this.weblogCategoryName);
231             log.debug("weblogPage = "+this.weblogPageName);
232             log.debug("pageNum = "+this.pageNum);
233         }
234     }
235     
236     
237     boolean isValidDestination(String JavaDoc servlet) {
238         return (servlet != null && PAGE_SERVLET.equals(servlet));
239     }
240     
241     
242     private boolean isValidDateString(String JavaDoc dateString) {
243         // string must be all numeric and 6 or 8 characters
244
return (dateString != null && StringUtils.isNumeric(dateString) &&
245                 (dateString.length() == 6 || dateString.length() == 8));
246     }
247
248     public String JavaDoc getContext() {
249         return context;
250     }
251
252     public void setContext(String JavaDoc context) {
253         this.context = context;
254     }
255
256     public String JavaDoc getWeblogAnchor() {
257         return weblogAnchor;
258     }
259
260     public void setWeblogAnchor(String JavaDoc weblogAnchor) {
261         this.weblogAnchor = weblogAnchor;
262     }
263
264     public String JavaDoc getWeblogPageName() {
265         return weblogPageName;
266     }
267
268     public void setWeblogPageName(String JavaDoc weblogPage) {
269         this.weblogPageName = weblogPage;
270     }
271
272     public String JavaDoc getWeblogCategoryName() {
273         return weblogCategoryName;
274     }
275
276     public void setWeblogCategoryName(String JavaDoc weblogCategory) {
277         this.weblogCategoryName = weblogCategory;
278     }
279
280     public String JavaDoc getWeblogDate() {
281         return weblogDate;
282     }
283
284     public void setWeblogDate(String JavaDoc weblogDate) {
285         this.weblogDate = weblogDate;
286     }
287
288     public int getPageNum() {
289         return pageNum;
290     }
291
292     public void setPageNum(int pageNum) {
293         this.pageNum = pageNum;
294     }
295
296     public Map JavaDoc getCustomParams() {
297         return customParams;
298     }
299
300     public void setCustomParams(Map JavaDoc customParams) {
301         this.customParams = customParams;
302     }
303     
304     public WeblogEntryData getWeblogEntry() {
305         
306         if(weblogEntry == null && weblogAnchor != null) {
307             try {
308                 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
309                 weblogEntry = wmgr.getWeblogEntryByAnchor(getWeblog(), weblogAnchor);
310             } catch (RollerException ex) {
311                 log.error("Error getting weblog entry "+weblogAnchor, ex);
312             }
313         }
314         
315         return weblogEntry;
316     }
317
318     public void setWeblogEntry(WeblogEntryData weblogEntry) {
319         this.weblogEntry = weblogEntry;
320     }
321
322     public Template getWeblogPage() {
323         
324         if(weblogPage == null && weblogPageName != null) {
325             try {
326                 UserManager umgr = RollerFactory.getRoller().getUserManager();
327                 weblogPage = getWeblog().getPageByLink(weblogPageName);
328             } catch (RollerException ex) {
329                 log.error("Error getting weblog page "+weblogPageName, ex);
330             }
331         }
332         
333         return weblogPage;
334     }
335
336     public void setWeblogPage(WeblogTemplate weblogPage) {
337         this.weblogPage = weblogPage;
338     }
339
340     public WeblogCategoryData getWeblogCategory() {
341         
342         if(weblogCategory == null && weblogCategoryName != null) {
343             try {
344                 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
345                 weblogCategory = wmgr.getWeblogCategoryByPath(getWeblog(), weblogCategoryName);
346             } catch (RollerException ex) {
347                 log.error("Error getting weblog category "+weblogCategoryName, ex);
348             }
349         }
350         
351         return weblogCategory;
352     }
353
354     public void setWeblogCategory(WeblogCategoryData weblogCategory) {
355         this.weblogCategory = weblogCategory;
356     }
357     
358 }
359
Popular Tags