001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.util;
020:
021: import java.io.UnsupportedEncodingException;
022: import java.net.URLDecoder;
023: import javax.servlet.http.HttpServletRequest;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.roller.RollerException;
027: import org.apache.roller.business.RollerFactory;
028: import org.apache.roller.business.WeblogManager;
029: import org.apache.roller.pojos.WeblogEntryData;
030: import org.apache.roller.util.Utilities;
031:
032: /**
033: * Represents a request to post a weblog entry comment.
034: */
035: public class WeblogCommentRequest extends WeblogRequest {
036:
037: private static Log log = LogFactory
038: .getLog(WeblogCommentRequest.class);
039:
040: private static final String COMMENT_SERVLET = "/roller-ui/rendering/comment";
041:
042: // lightweight attributes
043: private String name = null;
044: private String email = null;
045: private String url = null;
046: private String content = null;
047: private boolean notify = false;
048: private String weblogAnchor = null;
049:
050: // heavyweight attributes
051: private WeblogEntryData weblogEntry = null;
052:
053: public WeblogCommentRequest() {
054: }
055:
056: public WeblogCommentRequest(HttpServletRequest request)
057: throws InvalidRequestException {
058:
059: // let our parent take care of their business first
060: // parent determines weblog handle and locale if specified
061: super (request);
062:
063: String servlet = request.getServletPath();
064:
065: // we only want the path info left over from after our parents parsing
066: String pathInfo = this .getPathInfo();
067:
068: // was this request bound for the comment servlet?
069: if (servlet == null || !COMMENT_SERVLET.equals(servlet)) {
070: throw new InvalidRequestException(
071: "not a weblog comment request, "
072: + request.getRequestURL());
073: }
074:
075: /*
076: * parse path info. we expect ...
077: *
078: * /entry/<anchor> - permalink
079: */
080: if (pathInfo != null && pathInfo.trim().length() > 0) {
081:
082: // we should only ever get 2 path elements
083: String[] pathElements = pathInfo.split("/");
084: if (pathElements.length == 2) {
085:
086: String context = pathElements[0];
087: if ("entry".equals(context)) {
088: try {
089: this .weblogAnchor = URLDecoder.decode(
090: pathElements[1], "UTF-8");
091: } catch (UnsupportedEncodingException ex) {
092: // should never happen
093: log.error(ex);
094: }
095:
096: } else {
097: throw new InvalidRequestException("bad path info, "
098: + request.getRequestURL());
099: }
100:
101: } else {
102: throw new InvalidRequestException("bad path info, "
103: + request.getRequestURL());
104: }
105:
106: } else {
107: // bad request
108: throw new InvalidRequestException("bad path info, "
109: + request.getRequestURL());
110: }
111:
112: /*
113: * parse request parameters
114: *
115: * the only params we currently care about are:
116: * name - comment author
117: * email - comment email
118: * url - comment referring url
119: * content - comment contents
120: * notify - if commenter wants to receive notifications
121: */
122: if (request.getParameter("name") != null) {
123: this .name = Utilities.removeHTML(request
124: .getParameter("name"));
125: }
126:
127: if (request.getParameter("email") != null) {
128: this .email = Utilities.removeHTML(request
129: .getParameter("email"));
130: }
131:
132: if (request.getParameter("url") != null) {
133: this .url = Utilities
134: .removeHTML(request.getParameter("url"));
135: }
136:
137: if (request.getParameter("content") != null) {
138: this .content = request.getParameter("content");
139: }
140:
141: if (request.getParameter("notify") != null) {
142: this .notify = Boolean.valueOf(
143: request.getParameter("notify")).booleanValue();
144: }
145:
146: if (log.isDebugEnabled()) {
147: log.debug("name = " + this .name);
148: log.debug("email = " + this .email);
149: log.debug("url = " + this .url);
150: log.debug("content = " + this .content);
151: log.debug("notify = " + this .notify);
152: log.debug("weblogAnchor = " + this .weblogAnchor);
153: }
154: }
155:
156: public String getName() {
157: return name;
158: }
159:
160: public void setName(String name) {
161: this .name = name;
162: }
163:
164: public String getEmail() {
165: return email;
166: }
167:
168: public void setEmail(String email) {
169: this .email = email;
170: }
171:
172: public String getUrl() {
173: return url;
174: }
175:
176: public void setUrl(String url) {
177: this .url = url;
178: }
179:
180: public String getContent() {
181: return content;
182: }
183:
184: public void setContent(String content) {
185: this .content = content;
186: }
187:
188: public boolean isNotify() {
189: return notify;
190: }
191:
192: public void setNotify(boolean notify) {
193: this .notify = notify;
194: }
195:
196: public String getWeblogAnchor() {
197: return weblogAnchor;
198: }
199:
200: public void setWeblogAnchor(String weblogAnchor) {
201: this .weblogAnchor = weblogAnchor;
202: }
203:
204: public WeblogEntryData getWeblogEntry() {
205:
206: if (weblogEntry == null && weblogAnchor != null) {
207: try {
208: WeblogManager wmgr = RollerFactory.getRoller()
209: .getWeblogManager();
210: weblogEntry = wmgr.getWeblogEntryByAnchor(getWeblog(),
211: weblogAnchor);
212: } catch (RollerException ex) {
213: log.error("Error getting weblog entry " + weblogAnchor,
214: ex);
215: }
216: }
217:
218: return weblogEntry;
219: }
220:
221: public void setWeblogEntry(WeblogEntryData weblogEntry) {
222: this.weblogEntry = weblogEntry;
223: }
224:
225: }
|