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:
031: /**
032: * Represents a request to post a weblog entry trackback.
033: */
034: public class WeblogTrackbackRequest extends WeblogRequest {
035:
036: private static Log log = LogFactory
037: .getLog(WeblogTrackbackRequest.class);
038:
039: private static final String TRACKBACK_SERVLET = "/roller-ui/rendering/trackback";
040:
041: // lightweight attributes
042: private String blogName = null;
043: private String url = null;
044: private String excerpt = null;
045: private String title = null;
046: private String weblogAnchor = null;
047:
048: // heavyweight attributes
049: private WeblogEntryData weblogEntry = null;
050:
051: public WeblogTrackbackRequest() {
052: }
053:
054: public WeblogTrackbackRequest(HttpServletRequest request)
055: throws InvalidRequestException {
056:
057: // let our parent take care of their business first
058: // parent determines weblog handle and locale if specified
059: super (request);
060:
061: String servlet = request.getServletPath();
062:
063: // we only want the path info left over from after our parents parsing
064: String pathInfo = this .getPathInfo();
065:
066: // was this request bound for the comment servlet?
067: if (servlet == null || !TRACKBACK_SERVLET.equals(servlet)) {
068: throw new InvalidRequestException(
069: "not a weblog trackback request, "
070: + request.getRequestURL());
071: }
072:
073: /*
074: * parse path info. we expect ...
075: *
076: * /entry/<anchor> - permalink
077: */
078: if (pathInfo != null && pathInfo.trim().length() > 0) {
079:
080: // we should only ever get 2 path elements
081: String[] pathElements = pathInfo.split("/");
082: if (pathElements.length == 2) {
083:
084: String context = pathElements[0];
085: if ("entry".equals(context)) {
086: try {
087: this .weblogAnchor = URLDecoder.decode(
088: pathElements[1], "UTF-8");
089: } catch (UnsupportedEncodingException ex) {
090: // should never happen
091: log.error(ex);
092: }
093:
094: } else {
095: throw new InvalidRequestException("bad path info, "
096: + request.getRequestURL());
097: }
098:
099: } else {
100: throw new InvalidRequestException("bad path info, "
101: + request.getRequestURL());
102: }
103:
104: } else {
105: // bad request
106: throw new InvalidRequestException("bad path info, "
107: + request.getRequestURL());
108: }
109:
110: /*
111: * parse request parameters
112: *
113: * the only params we currently care about are:
114: * blog_name - comment author
115: * url - comment referring url
116: * excerpt - comment contents
117: * title - comment title
118: */
119: if (request.getParameter("blog_name") != null) {
120: this .blogName = request.getParameter("blog_name");
121: }
122:
123: if (request.getParameter("url") != null) {
124: this .url = request.getParameter("url");
125: }
126:
127: if (request.getParameter("excerpt") != null) {
128: this .excerpt = request.getParameter("excerpt");
129: }
130:
131: if (request.getParameter("title") != null) {
132: this .title = request.getParameter("title");
133: }
134:
135: // a little bit of validation, trackbacks enforce that all params
136: // must have a value, so any nulls equals a bad request
137: if (this .blogName == null || this .url == null
138: || this .excerpt == null || this .title == null) {
139: throw new InvalidRequestException(
140: "bad request data. did not "
141: + "receive values for all trackback params (blog_name, url, excerpt, title)");
142: }
143:
144: if (log.isDebugEnabled()) {
145: log.debug("name = " + this .blogName);
146: log.debug("url = " + this .url);
147: log.debug("excerpt = " + this .excerpt);
148: log.debug("title = " + this .title);
149: log.debug("weblogAnchor = " + this .weblogAnchor);
150: }
151: }
152:
153: public String getBlogName() {
154: return blogName;
155: }
156:
157: public void setBlogName(String blogName) {
158: this .blogName = blogName;
159: }
160:
161: public String getUrl() {
162: return url;
163: }
164:
165: public void setUrl(String url) {
166: this .url = url;
167: }
168:
169: public String getExcerpt() {
170: return excerpt;
171: }
172:
173: public void setExcerpt(String excerpt) {
174: this .excerpt = excerpt;
175: }
176:
177: public String getTitle() {
178: return title;
179: }
180:
181: public void setTitle(String title) {
182: this .title = title;
183: }
184:
185: public String getWeblogAnchor() {
186: return weblogAnchor;
187: }
188:
189: public void setWeblogAnchor(String weblogAnchor) {
190: this .weblogAnchor = weblogAnchor;
191: }
192:
193: public WeblogEntryData getWeblogEntry() {
194:
195: if (weblogEntry == null && weblogAnchor != null) {
196: try {
197: WeblogManager wmgr = RollerFactory.getRoller()
198: .getWeblogManager();
199: weblogEntry = wmgr.getWeblogEntryByAnchor(getWeblog(),
200: weblogAnchor);
201: } catch (RollerException ex) {
202: log.error("Error getting weblog entry " + weblogAnchor,
203: ex);
204: }
205: }
206:
207: return weblogEntry;
208: }
209:
210: public void setWeblogEntry(WeblogEntryData weblogEntry) {
211: this.weblogEntry = weblogEntry;
212: }
213:
214: }
|