KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.UnsupportedEncodingException JavaDoc;
22 import java.net.URLDecoder JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.roller.RollerException;
27 import org.apache.roller.model.RollerFactory;
28 import org.apache.roller.model.WeblogManager;
29 import org.apache.roller.pojos.WeblogEntryData;
30
31
32 /**
33  * Represents a request to post a weblog entry comment.
34  */

35 public class WeblogCommentRequest extends WeblogRequest {
36     
37     private static Log log = LogFactory.getLog(WeblogCommentRequest.class);
38     
39     private static final String JavaDoc COMMENT_SERVLET = "/roller-ui/rendering/comment";
40     
41     // lightweight attributes
42
private String JavaDoc name = null;
43     private String JavaDoc email = null;
44     private String JavaDoc url = null;
45     private String JavaDoc content = null;
46     private boolean notify = false;
47     private String JavaDoc weblogAnchor = null;
48     
49     // heavyweight attributes
50
private WeblogEntryData weblogEntry = null;
51     
52     
53     public WeblogCommentRequest() {}
54     
55     
56     public WeblogCommentRequest(HttpServletRequest JavaDoc request)
57             throws InvalidRequestException {
58         
59         // let our parent take care of their business first
60
// parent determines weblog handle and locale if specified
61
super(request);
62         
63         String JavaDoc servlet = request.getServletPath();
64         
65         // we only want the path info left over from after our parents parsing
66
String JavaDoc pathInfo = this.getPathInfo();
67         
68         // was this request bound for the comment servlet?
69
if(servlet == null || !COMMENT_SERVLET.equals(servlet)) {
70             throw new InvalidRequestException("not a weblog comment request, "+
71                     request.getRequestURL());
72         }
73         
74         
75         /*
76          * parse path info. we expect ...
77          *
78          * /entry/<anchor> - permalink
79          */

80         if(pathInfo != null && pathInfo.trim().length() > 0) {
81             
82             // we should only ever get 2 path elements
83
String JavaDoc[] pathElements = pathInfo.split("/");
84             if(pathElements.length == 2) {
85                 
86                 String JavaDoc context = pathElements[0];
87                 if("entry".equals(context)) {
88                     try {
89                         this.weblogAnchor =
90                                 URLDecoder.decode(pathElements[1], "UTF-8");
91                     } catch (UnsupportedEncodingException JavaDoc ex) {
92                         // should never happen
93
log.error(ex);
94                     }
95                     
96                 } else {
97                     throw new InvalidRequestException("bad path info, "+
98                             request.getRequestURL());
99                 }
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         /*
114          * parse request parameters
115          *
116          * the only params we currently care about are:
117          * name - comment author
118          * email - comment email
119          * url - comment referring url
120          * content - comment contents
121          * notify - if commenter wants to receive notifications
122          */

123         if(request.getParameter("name") != null) {
124             this.name = request.getParameter("name");
125         }
126         
127         if(request.getParameter("email") != null) {
128             this.email = request.getParameter("email");
129         }
130         
131         if(request.getParameter("url") != null) {
132             this.url = request.getParameter("url");
133         }
134         
135         if(request.getParameter("content") != null) {
136             this.content = request.getParameter("content");
137         }
138         
139         if(request.getParameter("notify") != null) {
140             this.notify = Boolean.valueOf(request.getParameter("notify")).booleanValue();
141         }
142         
143         if(log.isDebugEnabled()) {
144             log.debug("name = "+this.name);
145             log.debug("email = "+this.email);
146             log.debug("url = "+this.url);
147             log.debug("content = "+this.content);
148             log.debug("notify = "+this.notify);
149             log.debug("weblogAnchor = "+this.weblogAnchor);
150         }
151     }
152
153     public String JavaDoc getName() {
154         return name;
155     }
156
157     public void setName(String JavaDoc name) {
158         this.name = name;
159     }
160
161     public String JavaDoc getEmail() {
162         return email;
163     }
164
165     public void setEmail(String JavaDoc email) {
166         this.email = email;
167     }
168
169     public String JavaDoc getUrl() {
170         return url;
171     }
172
173     public void setUrl(String JavaDoc url) {
174         this.url = url;
175     }
176
177     public String JavaDoc getContent() {
178         return content;
179     }
180
181     public void setContent(String JavaDoc content) {
182         this.content = content;
183     }
184
185     public boolean isNotify() {
186         return notify;
187     }
188
189     public void setNotify(boolean notify) {
190         this.notify = notify;
191     }
192
193     public String JavaDoc getWeblogAnchor() {
194         return weblogAnchor;
195     }
196
197     public void setWeblogAnchor(String JavaDoc weblogAnchor) {
198         this.weblogAnchor = weblogAnchor;
199     }
200
201     public WeblogEntryData getWeblogEntry() {
202         
203         if(weblogEntry == null && weblogAnchor != null) {
204             try {
205                 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
206                 weblogEntry = wmgr.getWeblogEntryByAnchor(getWeblog(), weblogAnchor);
207             } catch (RollerException ex) {
208                 log.error("Error getting weblog entry "+weblogAnchor, ex);
209             }
210         }
211         
212         return weblogEntry;
213     }
214
215     public void setWeblogEntry(WeblogEntryData weblogEntry) {
216         this.weblogEntry = weblogEntry;
217     }
218     
219 }
220
Popular Tags