File Source: WeblogFeedRequest.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.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.roller.weblogger.WebloggerException;
31 import org.apache.roller.weblogger.config.WebloggerConfig;
32 import org.apache.roller.weblogger.business.WebloggerFactory;
33 import org.apache.roller.weblogger.business.WeblogManager;
34 import org.apache.roller.weblogger.pojos.WeblogCategory;
35 import org.apache.roller.weblogger.util.URLUtilities;
36 import org.apache.roller.weblogger.util.Utilities;
37
38
39 /**
40 * Represents a request for a Roller weblog feed.
41 *
42 * /roller-ui/rendering/feeds/*
43 *
44 * We use this class as a helper to parse an incoming url and sort out the
45 * information embedded in the url for later use.
46 */
47 public class WeblogFeedRequest extends WeblogRequest {
48
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.util.WeblogFeedRequest__static_init
*
* Postconditions:
* init'ed(log)
*/
49 private static Log log = LogFactory.getLog(WeblogFeedRequest.class);
50
51 private static final String FEED_SERVLET = "/roller-ui/rendering/feed";
52
53 // lightweight attributes
54 private String type = null;
55 private String format = null;
56 private String weblogCategoryName = null;
57 private List tags = null;
58 private int page = 0;
59 private boolean excerpts = false;
60 private String term = null;
61
62 // heavyweight attributes
63 private WeblogCategory weblogCategory = null;
64
65
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.util.WeblogFeedRequest()
*
* Postconditions:
* this.authenticUser == null
* this.format == null
* this.locale == null
* this.localeInstance == null
* this.pathInfo == null
* this.request == null
* this.tags == null
* this.term == null
* this.type == null
* this.user == null
* ...
*/
66 public WeblogFeedRequest() {}
67
68
69 /**
70 * Construct the WeblogFeedRequest by parsing the incoming url
71 */
72 public WeblogFeedRequest(HttpServletRequest request)
73 throws InvalidRequestException {
74
75 // let our parent take care of their business first
76 // parent determines weblog handle and locale if specified
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.util.WeblogFeedRequest(HttpServletRequest)
* org.apache.roller.weblogger.ui.rendering.util.WeblogFeedRequest fails for all possible inputs
*
* Preconditions:
* (soft) log != null
* (soft) org/apache/roller/weblogger/ui/rendering/util/WeblogRequest.log != null
* (soft) request != null
*
* Presumptions:
* java.lang.String:equals(...)@88 == 1
* java.lang.String:length(...)@101 >= 2
* javax.servlet.http.HttpServletRequest:getServletPath(...)@79 != null
*/
77 super(request);
78
79 String servlet = request.getServletPath();
80
81 // we only want the path info left over from after our parents parsing
82 String pathInfo = this.getPathInfo();
83
84 // parse the request object and figure out what we've got
85 log.debug("parsing path "+pathInfo);
86
87 // was this request bound for the feed servlet?
88 if(servlet == null || !FEED_SERVLET.equals(servlet)) {
89 throw new InvalidRequestException("not a weblog feed request, "+
90 request.getRequestURL());
91 }
92
93
94 /*
95 * parse the path info.
96 *
97 * must look like this ...
98 *
99 * /<type>/<format>
100 */
101 if(pathInfo != null && pathInfo.trim().length() > 1) {
102
103 String[] pathElements = pathInfo.split("/");
+ 104 if(pathElements.length == 2) {
+ 105 this.type = pathElements[0];
106 this.format = pathElements[1];
107 } else {
+ 108 throw new InvalidRequestException("invalid feed path info, "+
109 request.getRequestURL());
110 }
111
112 } else {
+ 113 throw new InvalidRequestException("invalid feed path info, "+
114 request.getRequestURL());
115 }
116
117
118 /*
119 * parse request parameters
120 *
121 * the only params we currently care about are:
122 * cat - specifies a weblog category
123 * excerpts - specifies the feed should only include excerpts
124 *
125 */
126 if(request.getParameter("cat") != null) {
127 this.weblogCategoryName =
128 URLUtilities.decode(request.getParameter("cat"));
129
130 // all categories must start with a /
131 if(!this.weblogCategoryName.startsWith("/")) {
132 this.weblogCategoryName = "/"+this.weblogCategoryName;
133 }
134 }
135
136 if(request.getParameter("tags") != null) {
137 this.tags = Utilities.splitStringAsTags(request.getParameter("tags"));
138 int maxSize = WebloggerConfig.getIntProperty("tags.queries.maxIntersectionSize", 3);
139 if (this.tags.size() > maxSize)
140 throw new InvalidRequestException("max number of tags allowed is " + maxSize + ", "
141 + request.getRequestURL());
142 }
143
144 if(request.getParameter("excerpts") != null) {
145 this.excerpts = Boolean.valueOf(request.getParameter("excerpts")).booleanValue();
146 }
147
148 if(request.getParameter("page") != null) {
149 try {
150 this.page = Integer.parseInt(request.getParameter("page"));
151 } catch(NumberFormatException e) {
152 //
153 }
154 }
155
156 if(request.getParameter("q") != null &&
157 request.getParameter("q").trim().length() > 0) {
158 this.term = URLUtilities.decode(request.getParameter("q"));
159 }
160
161 if((this.tags != null && this.tags.size() > 0) && this.weblogCategoryName != null) {
162 throw new InvalidRequestException("please specify either category or tags but not both, " + request.getRequestURL());
163 }
164
165 if(log.isDebugEnabled()) {
166 log.debug("type = "+this.type);
167 log.debug("format = "+this.format);
168 log.debug("weblogCategory = "+this.weblogCategoryName);
169 log.debug("tags = "+this.tags);
170 log.debug("excerpts = "+this.excerpts);
171 }
172 }
173
174 public String getType() {
/*
P/P * Method: String getType()
*
* Preconditions:
* init'ed(this.type)
*
* Postconditions:
* return_value == this.type
* init'ed(return_value)
*/
175 return type;
176 }
177
178 public void setType(String type) {
/*
P/P * Method: void setType(String)
*
* Postconditions:
* this.type == type
* init'ed(this.type)
*/
179 this.type = type;
180 }
181
182 public String getFormat() {
/*
P/P * Method: String getFormat()
*
* Preconditions:
* init'ed(this.format)
*
* Postconditions:
* return_value == this.format
* init'ed(return_value)
*/
183 return format;
184 }
185
186 public void setFormat(String format) {
/*
P/P * Method: void setFormat(String)
*
* Postconditions:
* this.format == format
* init'ed(this.format)
*/
187 this.format = format;
188 }
189
190 public String getWeblogCategoryName() {
/*
P/P * Method: String getWeblogCategoryName()
*
* Preconditions:
* init'ed(this.weblogCategoryName)
*
* Postconditions:
* return_value == this.weblogCategoryName
* init'ed(return_value)
*/
191 return weblogCategoryName;
192 }
193
194 public void setWeblogCategoryName(String weblogCategory) {
/*
P/P * Method: void setWeblogCategoryName(String)
*
* Postconditions:
* this.weblogCategoryName == weblogCategory
* init'ed(this.weblogCategoryName)
*/
195 this.weblogCategoryName = weblogCategory;
196 }
197
198 public List getTags() {
/*
P/P * Method: List getTags()
*
* Preconditions:
* init'ed(this.tags)
*
* Postconditions:
* return_value == this.tags
* init'ed(return_value)
*/
199 return tags;
200 }
201
202 public void setTags(List tags) {
/*
P/P * Method: void setTags(List)
*
* Postconditions:
* this.tags == tags
* init'ed(this.tags)
*/
203 this.tags = tags;
204 }
205
206 public boolean isExcerpts() {
/*
P/P * Method: bool isExcerpts()
*
* Preconditions:
* init'ed(this.excerpts)
*
* Postconditions:
* return_value == this.excerpts
* init'ed(return_value)
*/
207 return excerpts;
208 }
209
210 public void setExcerpts(boolean excerpts) {
/*
P/P * Method: void setExcerpts(bool)
*
* Postconditions:
* this.excerpts == excerpts
* init'ed(this.excerpts)
*/
211 this.excerpts = excerpts;
212 }
213
214 public WeblogCategory getWeblogCategory() {
215
/*
P/P * Method: WeblogCategory getWeblogCategory()
*
* Preconditions:
* init'ed(this.weblogCategory)
* (soft) log != null
* (soft) init'ed(this.weblog)
* (soft) org/apache/roller/weblogger/ui/rendering/util/WeblogRequest.log != null
* (soft) init'ed(this.weblogCategoryName)
* (soft) init'ed(this.weblogHandle)
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@218 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@218 != null
*
* Postconditions:
* init'ed(return_value)
* this.weblogCategory == return_value
* init'ed(this.weblog)
*
* Test Vectors:
* this.weblogCategory: Inverse{null}, Addr_Set{null}
* this.weblogCategoryName: Addr_Set{null}, Inverse{null}
*/
216 if(weblogCategory == null && weblogCategoryName != null) {
217 try {
218 WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
219 weblogCategory = wmgr.getWeblogCategoryByPath(getWeblog(), weblogCategoryName);
220 } catch (WebloggerException ex) {
221 log.error("Error getting weblog category "+weblogCategoryName, ex);
222 }
223 }
224
225 return weblogCategory;
226 }
227
228 public void setWeblogCategory(WeblogCategory weblogCategory) {
/*
P/P * Method: void setWeblogCategory(WeblogCategory)
*
* Postconditions:
* this.weblogCategory == weblogCategory
* init'ed(this.weblogCategory)
*/
229 this.weblogCategory = weblogCategory;
230 }
231
232
233 public int getPage() {
/*
P/P * Method: int getPage()
*
* Preconditions:
* init'ed(this.page)
*
* Postconditions:
* return_value == this.page
* init'ed(return_value)
*/
234 return page;
235 }
236
237
238 public void setPage(int page) {
/*
P/P * Method: void setPage(int)
*
* Postconditions:
* this.page == page
* init'ed(this.page)
*/
239 this.page = page;
240 }
241
242 public String getTerm() {
/*
P/P * Method: String getTerm()
*
* Preconditions:
* init'ed(this.term)
*
* Postconditions:
* return_value == this.term
* init'ed(return_value)
*/
243 return term;
244 }
245
246
247 public void setTerm(String query) {
/*
P/P * Method: void setTerm(String)
*
* Postconditions:
* this.term == query
* init'ed(this.term)
*/
248 this.term = query;
249 }
250 }
SofCheck Inspector Build Version : 2.18479
| WeblogFeedRequest.java |
2009-Jan-02 14:25:40 |
| WeblogFeedRequest.class |
2009-Sep-04 03:12:46 |