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.util.ArrayList;
022: import java.util.Arrays;
023: import java.util.List;
024:
025: import javax.servlet.http.HttpServletRequest;
026:
027: import org.apache.commons.lang.StringUtils;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.roller.RollerException;
031: import org.apache.roller.config.RollerConfig;
032: import org.apache.roller.business.RollerFactory;
033: import org.apache.roller.business.WeblogManager;
034: import org.apache.roller.pojos.WeblogCategoryData;
035: import org.apache.roller.util.URLUtilities;
036: import org.apache.roller.util.Utilities;
037:
038: /**
039: * Represents a request for a Roller weblog feed.
040: *
041: * /roller-ui/rendering/feeds/*
042: *
043: * We use this class as a helper to parse an incoming url and sort out the
044: * information embedded in the url for later use.
045: */
046: public class WeblogFeedRequest extends WeblogRequest {
047:
048: private static Log log = LogFactory.getLog(WeblogFeedRequest.class);
049:
050: private static final String FEED_SERVLET = "/roller-ui/rendering/feed";
051:
052: // lightweight attributes
053: private String type = null;
054: private String format = null;
055: private String weblogCategoryName = null;
056: private List tags = null;
057: private boolean excerpts = false;
058:
059: // heavyweight attributes
060: private WeblogCategoryData weblogCategory = null;
061:
062: public WeblogFeedRequest() {
063: }
064:
065: /**
066: * Construct the WeblogFeedRequest by parsing the incoming url
067: */
068: public WeblogFeedRequest(HttpServletRequest request)
069: throws InvalidRequestException {
070:
071: // let our parent take care of their business first
072: // parent determines weblog handle and locale if specified
073: super (request);
074:
075: String servlet = request.getServletPath();
076:
077: // we only want the path info left over from after our parents parsing
078: String pathInfo = this .getPathInfo();
079:
080: // parse the request object and figure out what we've got
081: log.debug("parsing path " + pathInfo);
082:
083: // was this request bound for the feed servlet?
084: if (servlet == null || !FEED_SERVLET.equals(servlet)) {
085: throw new InvalidRequestException(
086: "not a weblog feed request, "
087: + request.getRequestURL());
088: }
089:
090: /*
091: * parse the path info.
092: *
093: * must look like this ...
094: *
095: * /<type>/<format>
096: */
097: if (pathInfo != null && pathInfo.trim().length() > 1) {
098:
099: String[] pathElements = pathInfo.split("/");
100: if (pathElements.length == 2) {
101: this .type = pathElements[0];
102: this .format = pathElements[1];
103: } else {
104: throw new InvalidRequestException(
105: "invalid feed path info, "
106: + request.getRequestURL());
107: }
108:
109: } else {
110: throw new InvalidRequestException(
111: "invalid feed path info, "
112: + request.getRequestURL());
113: }
114:
115: /*
116: * parse request parameters
117: *
118: * the only params we currently care about are:
119: * cat - specifies a weblog category
120: * excerpts - specifies the feed should only include excerpts
121: *
122: */
123: if (request.getParameter("cat") != null) {
124: this .weblogCategoryName = URLUtilities.decode(request
125: .getParameter("cat"));
126:
127: // all categories must start with a /
128: if (!this .weblogCategoryName.startsWith("/")) {
129: this .weblogCategoryName = "/" + this .weblogCategoryName;
130: }
131: }
132:
133: if (request.getParameter("tags") != null) {
134: this .tags = Utilities.splitStringAsTags(request
135: .getParameter("tags"));
136: int maxSize = RollerConfig.getIntProperty(
137: "tags.queries.maxIntersectionSize", 3);
138: if (this .tags.size() > maxSize)
139: throw new InvalidRequestException(
140: "max number of tags allowed is " + maxSize
141: + ", " + request.getRequestURL());
142: }
143:
144: if (request.getParameter("excerpts") != null) {
145: this .excerpts = Boolean.valueOf(
146: request.getParameter("excerpts")).booleanValue();
147: }
148:
149: if ((this .tags != null && this .tags.size() > 0)
150: && this .weblogCategoryName != null) {
151: throw new InvalidRequestException(
152: "please specify either category or tags but not both, "
153: + request.getRequestURL());
154: }
155:
156: if (log.isDebugEnabled()) {
157: log.debug("type = " + this .type);
158: log.debug("format = " + this .format);
159: log.debug("weblogCategory = " + this .weblogCategoryName);
160: log.debug("tags = " + this .tags);
161: log.debug("excerpts = " + this .excerpts);
162: }
163: }
164:
165: public String getType() {
166: return type;
167: }
168:
169: public void setType(String type) {
170: this .type = type;
171: }
172:
173: public String getFormat() {
174: return format;
175: }
176:
177: public void setFormat(String format) {
178: this .format = format;
179: }
180:
181: public String getWeblogCategoryName() {
182: return weblogCategoryName;
183: }
184:
185: public void setWeblogCategoryName(String weblogCategory) {
186: this .weblogCategoryName = weblogCategory;
187: }
188:
189: public List getTags() {
190: return tags;
191: }
192:
193: public void setTags(List tags) {
194: this .tags = tags;
195: }
196:
197: public boolean isExcerpts() {
198: return excerpts;
199: }
200:
201: public void setExcerpts(boolean excerpts) {
202: this .excerpts = excerpts;
203: }
204:
205: public WeblogCategoryData getWeblogCategory() {
206:
207: if (weblogCategory == null && weblogCategoryName != null) {
208: try {
209: WeblogManager wmgr = RollerFactory.getRoller()
210: .getWeblogManager();
211: weblogCategory = wmgr.getWeblogCategoryByPath(
212: getWeblog(), weblogCategoryName);
213: } catch (RollerException ex) {
214: log.error("Error getting weblog category "
215: + weblogCategoryName, ex);
216: }
217: }
218:
219: return weblogCategory;
220: }
221:
222: public void setWeblogCategory(WeblogCategoryData weblogCategory) {
223: this.weblogCategory = weblogCategory;
224: }
225:
226: }
|