File Source: WeblogCommentRequest.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.io.UnsupportedEncodingException;
22 import java.net.URLDecoder;
23 import javax.servlet.http.HttpServletRequest;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.roller.weblogger.WebloggerException;
27 import org.apache.roller.weblogger.business.WebloggerFactory;
28 import org.apache.roller.weblogger.business.WeblogManager;
29 import org.apache.roller.weblogger.pojos.WeblogEntry;
30 import org.apache.roller.weblogger.util.Utilities;
31
32
33 /**
34 * Represents a request to post a weblog entry comment.
35 */
36 public class WeblogCommentRequest extends WeblogRequest {
37
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.util.WeblogCommentRequest__static_init
*
* Postconditions:
* init'ed(log)
*/
38 private static Log log = LogFactory.getLog(WeblogCommentRequest.class);
39
40 private static final String COMMENT_SERVLET = "/roller-ui/rendering/comment";
41
42 // lightweight attributes
43 private String name = null;
44 private String email = null;
45 private String url = null;
46 private String content = null;
47 private boolean notify = false;
48 private String weblogAnchor = null;
49
50 // heavyweight attributes
51 private WeblogEntry weblogEntry = null;
52
53
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.util.WeblogCommentRequest()
*
* Postconditions:
* this.authenticUser == null
* this.content == null
* this.email == null
* this.locale == null
* this.localeInstance == null
* this.name == null
* this.pathInfo == null
* this.request == null
* this.url == null
* this.user == null
* ...
*/
54 public WeblogCommentRequest() {}
55
56
57 public WeblogCommentRequest(HttpServletRequest request)
58 throws InvalidRequestException {
59
60 // let our parent take care of their business first
61 // parent determines weblog handle and locale if specified
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.util.WeblogCommentRequest(HttpServletRequest)
* org.apache.roller.weblogger.ui.rendering.util.WeblogCommentRequest fails for all possible inputs
*
* Preconditions:
* (soft) org/apache/roller/weblogger/ui/rendering/util/WeblogRequest.log != null
* (soft) request != null
*
* Presumptions:
* java.lang.String:equals(...)@70 == 1
* java.lang.String:length(...)@81 >= 1
* javax.servlet.http.HttpServletRequest:getServletPath(...)@64 != null
*/
62 super(request);
63
64 String servlet = request.getServletPath();
65
66 // we only want the path info left over from after our parents parsing
67 String pathInfo = this.getPathInfo();
68
69 // was this request bound for the comment servlet?
70 if(servlet == null || !COMMENT_SERVLET.equals(servlet)) {
71 throw new InvalidRequestException("not a weblog comment request, "+
72 request.getRequestURL());
73 }
74
75
76 /*
77 * parse path info. we expect ...
78 *
79 * /entry/<anchor> - permalink
80 */
81 if(pathInfo != null && pathInfo.trim().length() > 0) {
82
83 // we should only ever get 2 path elements
84 String[] pathElements = pathInfo.split("/");
+ 85 if(pathElements.length == 2) {
86
+ 87 String context = pathElements[0];
88 if("entry".equals(context)) {
89 try {
90 this.weblogAnchor =
91 URLDecoder.decode(pathElements[1], "UTF-8");
92 } catch (UnsupportedEncodingException ex) {
93 // should never happen
94 log.error(ex);
95 }
96
97 } else {
98 throw new InvalidRequestException("bad path info, "+
99 request.getRequestURL());
100 }
101
102 } else {
+ 103 throw new InvalidRequestException("bad path info, "+
104 request.getRequestURL());
105 }
106
107 } else {
108 // bad request
+ 109 throw new InvalidRequestException("bad path info, "+
110 request.getRequestURL());
111 }
112
113
114 /*
115 * parse request parameters
116 *
117 * the only params we currently care about are:
118 * name - comment author
119 * email - comment email
120 * url - comment referring url
121 * content - comment contents
122 * notify - if commenter wants to receive notifications
123 */
124 if(request.getParameter("name") != null) {
125 this.name = Utilities.removeHTML(request.getParameter("name"));
126 }
127
128 if(request.getParameter("email") != null) {
129 this.email = Utilities.removeHTML(request.getParameter("email"));
130 }
131
132 if(request.getParameter("url") != null) {
133 this.url = Utilities.removeHTML(request.getParameter("url"));
134 }
135
136 if(request.getParameter("content") != null) {
137 this.content = request.getParameter("content");
138 }
139
140 if(request.getParameter("notify") != null) {
141 this.notify = true;
142 }
143
144 if(log.isDebugEnabled()) {
145 log.debug("name = "+this.name);
146 log.debug("email = "+this.email);
147 log.debug("url = "+this.url);
148 log.debug("content = "+this.content);
149 log.debug("notify = "+this.notify);
150 log.debug("weblogAnchor = "+this.weblogAnchor);
151 }
152 }
153
154 public String getName() {
/*
P/P * Method: String getName()
*
* Preconditions:
* init'ed(this.name)
*
* Postconditions:
* return_value == this.name
* init'ed(return_value)
*/
155 return name;
156 }
157
158 public void setName(String name) {
/*
P/P * Method: void setName(String)
*
* Postconditions:
* this.name == name
* init'ed(this.name)
*/
159 this.name = name;
160 }
161
162 public String getEmail() {
/*
P/P * Method: String getEmail()
*
* Preconditions:
* init'ed(this.email)
*
* Postconditions:
* return_value == this.email
* init'ed(return_value)
*/
163 return email;
164 }
165
166 public void setEmail(String email) {
/*
P/P * Method: void setEmail(String)
*
* Postconditions:
* this.email == email
* init'ed(this.email)
*/
167 this.email = email;
168 }
169
170 public String getUrl() {
/*
P/P * Method: String getUrl()
*
* Preconditions:
* init'ed(this.url)
*
* Postconditions:
* return_value == this.url
* init'ed(return_value)
*/
171 return url;
172 }
173
174 public void setUrl(String url) {
/*
P/P * Method: void setUrl(String)
*
* Postconditions:
* this.url == url
* init'ed(this.url)
*/
175 this.url = url;
176 }
177
178 public String getContent() {
/*
P/P * Method: String getContent()
*
* Preconditions:
* init'ed(this.content)
*
* Postconditions:
* return_value == this.content
* init'ed(return_value)
*/
179 return content;
180 }
181
182 public void setContent(String content) {
/*
P/P * Method: void setContent(String)
*
* Postconditions:
* this.content == content
* init'ed(this.content)
*/
183 this.content = content;
184 }
185
186 public boolean isNotify() {
/*
P/P * Method: bool isNotify()
*
* Preconditions:
* init'ed(this.notify)
*
* Postconditions:
* return_value == this.notify
* init'ed(return_value)
*/
187 return notify;
188 }
189
190 public void setNotify(boolean notify) {
/*
P/P * Method: void setNotify(bool)
*
* Postconditions:
* this.notify == notify
* init'ed(this.notify)
*/
191 this.notify = notify;
192 }
193
194 public String getWeblogAnchor() {
/*
P/P * Method: String getWeblogAnchor()
*
* Preconditions:
* init'ed(this.weblogAnchor)
*
* Postconditions:
* return_value == this.weblogAnchor
* init'ed(return_value)
*/
195 return weblogAnchor;
196 }
197
198 public void setWeblogAnchor(String weblogAnchor) {
/*
P/P * Method: void setWeblogAnchor(String)
*
* Postconditions:
* this.weblogAnchor == weblogAnchor
* init'ed(this.weblogAnchor)
*/
199 this.weblogAnchor = weblogAnchor;
200 }
201
202 public WeblogEntry getWeblogEntry() {
203
/*
P/P * Method: WeblogEntry getWeblogEntry()
*
* Preconditions:
* init'ed(this.weblogEntry)
* (soft) log != null
* (soft) init'ed(this.weblog)
* (soft) org/apache/roller/weblogger/ui/rendering/util/WeblogRequest.log != null
* (soft) init'ed(this.weblogAnchor)
* (soft) init'ed(this.weblogHandle)
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@206 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@206 != null
*
* Postconditions:
* init'ed(return_value)
* this.weblogEntry == return_value
* init'ed(this.weblog)
*
* Test Vectors:
* this.weblogEntry: Inverse{null}, Addr_Set{null}
* this.weblogAnchor: Addr_Set{null}, Inverse{null}
*/
204 if(weblogEntry == null && weblogAnchor != null) {
205 try {
206 WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
207 weblogEntry = wmgr.getWeblogEntryByAnchor(getWeblog(), weblogAnchor);
208 } catch (WebloggerException ex) {
209 log.error("Error getting weblog entry "+weblogAnchor, ex);
210 }
211 }
212
213 return weblogEntry;
214 }
215
216 public void setWeblogEntry(WeblogEntry weblogEntry) {
/*
P/P * Method: void setWeblogEntry(WeblogEntry)
*
* Postconditions:
* this.weblogEntry == weblogEntry
* init'ed(this.weblogEntry)
*/
217 this.weblogEntry = weblogEntry;
218 }
219
220 }
SofCheck Inspector Build Version : 2.18479
| WeblogCommentRequest.java |
2009-Jan-02 14:25:02 |
| WeblogCommentRequest.class |
2009-Sep-04 03:12:46 |