KickJava   Java API By Example, From Geeks To Geeks.

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


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 trackback.
34  */

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

79         if(pathInfo != null && pathInfo.trim().length() > 0) {
80             
81             // we should only ever get 2 path elements
82
String JavaDoc[] pathElements = pathInfo.split("/");
83             if(pathElements.length == 2) {
84                 
85                 String JavaDoc context = pathElements[0];
86                 if("entry".equals(context)) {
87                     try {
88                         this.weblogAnchor =
89                                 URLDecoder.decode(pathElements[1], "UTF-8");
90                     } catch (UnsupportedEncodingException JavaDoc ex) {
91                         // should never happen
92
log.error(ex);
93                     }
94                     
95                 } else {
96                     throw new InvalidRequestException("bad path info, "+
97                             request.getRequestURL());
98                 }
99                 
100             } else {
101                 throw new InvalidRequestException("bad path info, "+
102                         request.getRequestURL());
103             }
104             
105         } else {
106             // bad request
107
throw new InvalidRequestException("bad path info, "+
108                     request.getRequestURL());
109         }
110         
111         
112         /*
113          * parse request parameters
114          *
115          * the only params we currently care about are:
116          * blog_name - comment author
117          * url - comment referring url
118          * excerpt - comment contents
119          * title - comment title
120          */

121         if(request.getParameter("blog_name") != null) {
122             this.blogName = request.getParameter("blog_name");
123         }
124         
125         if(request.getParameter("url") != null) {
126             this.url = request.getParameter("url");
127         }
128         
129         if(request.getParameter("excerpt") != null) {
130             this.excerpt = request.getParameter("excerpt");
131         }
132         
133         if(request.getParameter("title") != null) {
134             this.title = request.getParameter("title");
135         }
136         
137         // a little bit of validation, trackbacks enforce that all params
138
// must have a value, so any nulls equals a bad request
139
if(this.blogName == null || this.url == null ||
140                 this.excerpt == null || this.title == null) {
141             throw new InvalidRequestException("bad request data. did not "+
142                     "receive values for all trackback params (blog_name, url, excerpt, title)");
143         }
144         
145         if(log.isDebugEnabled()) {
146             log.debug("name = "+this.blogName);
147             log.debug("url = "+this.url);
148             log.debug("excerpt = "+this.excerpt);
149             log.debug("title = "+this.title);
150             log.debug("weblogAnchor = "+this.weblogAnchor);
151         }
152     }
153
154     public String JavaDoc getBlogName() {
155         return blogName;
156     }
157
158     public void setBlogName(String JavaDoc blogName) {
159         this.blogName = blogName;
160     }
161
162     public String JavaDoc getUrl() {
163         return url;
164     }
165
166     public void setUrl(String JavaDoc url) {
167         this.url = url;
168     }
169
170     public String JavaDoc getExcerpt() {
171         return excerpt;
172     }
173
174     public void setExcerpt(String JavaDoc excerpt) {
175         this.excerpt = excerpt;
176     }
177
178     public String JavaDoc getTitle() {
179         return title;
180     }
181
182     public void setTitle(String JavaDoc title) {
183         this.title = title;
184     }
185
186     public String JavaDoc getWeblogAnchor() {
187         return weblogAnchor;
188     }
189
190     public void setWeblogAnchor(String JavaDoc weblogAnchor) {
191         this.weblogAnchor = weblogAnchor;
192     }
193
194     public WeblogEntryData getWeblogEntry() {
195         
196         if(weblogEntry == null && weblogAnchor != null) {
197             try {
198                 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
199                 weblogEntry = wmgr.getWeblogEntryByAnchor(getWeblog(), weblogAnchor);
200             } catch (RollerException ex) {
201                 log.error("Error getting weblog entry "+weblogAnchor, ex);
202             }
203         }
204         
205         return weblogEntry;
206     }
207
208     public void setWeblogEntry(WeblogEntryData weblogEntry) {
209         this.weblogEntry = weblogEntry;
210     }
211     
212 }
213
Popular Tags