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.velocity.deprecated;
020:
021: import java.util.Enumeration;
022: import java.util.HashSet;
023: import java.util.Locale;
024: import java.util.Set;
025: import javax.servlet.http.HttpServletRequest;
026: import org.apache.commons.lang.StringUtils;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: /**
031: * Represents an *old* request for a Roller weblog comments permalink.
032: *
033: * any url from ... /comments/*
034: *
035: * While these urls are no longer used we do provide redirect support for them
036: * for users who have upgraded from earlier versions. We keep this class to
037: * help with parsing these urls since they are fairly complex.
038: */
039: public class OldCommentsRequest {
040:
041: private static Log log = LogFactory
042: .getLog(OldCommentsRequest.class);
043:
044: // various page types
045: public static final String MAIN = "main";
046: public static final String PERMALINK = "permalink";
047: public static final String ARCHIVE = "archive";
048:
049: private String context = null;
050: private String pageType = null;
051: private String weblogHandle = null;
052: private String weblogAnchor = null;
053: private String weblogPage = null;
054: private String weblogCategory = null;
055: private String weblogDate = null;
056:
057: /**
058: * Construct the WeblogPageRequest by parsing the incoming url
059: */
060: public OldCommentsRequest(HttpServletRequest request)
061: throws Exception {
062:
063: // parse the request object and figure out what we've got
064: log.debug("parsing url " + request.getRequestURL());
065:
066: String servlet = request.getServletPath();
067: String pathInfo = request.getPathInfo();
068:
069: // make sure this request was destined for the comments servlet
070: if (servlet != null) {
071: // strip off the leading slash
072: servlet = servlet.substring(1);
073:
074: if ("comments".equals(servlet)) {
075: this .context = "weblog";
076: } else {
077: // not a request to the page servlet
078: throw new Exception("not a weblog page request, "
079: + request.getRequestURL());
080: }
081: } else {
082: throw new Exception("not a weblog page request, "
083: + request.getRequestURL());
084: }
085:
086: /*
087: * parse path info
088: *
089: * we expect one of the following forms of urls ...
090: *
091: * [handle] - get default page for user for today's date
092: * [handle]/[date] - get default page for user for specified date
093: * [handle]/[pagelink] - get specified page for today's date
094: * [handle]/[pagelink]/[date] - get specified page for specified date
095: * [handle]/[pagelink]/[anchor] - get specified page & entry (by anchor)
096: * [handle]/[pagelink]/[date]/[anchor] - get specified page & entry (by anchor)
097: */
098: if (pathInfo != null && pathInfo.trim().length() > 1) {
099: // strip off the leading slash
100: pathInfo = pathInfo.substring(1);
101: String[] pathElements = pathInfo.split("/");
102:
103: if (pathElements.length == 1) {
104:
105: // /handle
106: this .weblogHandle = pathElements[0];
107: this .weblogPage = "Weblog";
108: this .pageType = MAIN;
109:
110: } else if (pathElements.length == 2) {
111:
112: // /handle/date or /handle/page
113: this .weblogHandle = pathElements[0];
114: this .weblogPage = "Weblog";
115:
116: if (this .isValidDateString(pathElements[1])) {
117: this .weblogDate = pathElements[1];
118: this .pageType = ARCHIVE;
119: } else {
120: this .weblogPage = pathElements[1];
121: this .pageType = MAIN;
122: }
123:
124: } else if (pathElements.length == 3) {
125:
126: // /handle/page/date or /handle/page/anchor
127: this .weblogHandle = pathElements[0];
128: this .weblogPage = pathElements[1];
129:
130: if (this .isValidDateString(pathElements[2])) {
131: this .weblogDate = pathElements[2];
132: this .pageType = ARCHIVE;
133: } else {
134: this .weblogAnchor = pathElements[2];
135: this .pageType = PERMALINK;
136: }
137:
138: } else if (pathElements.length == 4) {
139:
140: // /handle/page/date/anchor
141: this .weblogHandle = pathElements[0];
142: this .weblogPage = pathElements[1];
143: this .weblogDate = pathElements[2];
144: this .weblogAnchor = pathElements[3];
145: this .pageType = PERMALINK;
146: }
147:
148: } else {
149: // invalid request ... path info is empty
150: throw new Exception("not a weblog page request, "
151: + request.getRequestURL());
152: }
153:
154: /*
155: * parse request parameters
156: *
157: * the only params we currently care about are:
158: * anchor - specifies a weblog entry
159: * entry - specifies a weblog entry
160: * catname - specifies a weblog category
161: */
162: if (request.getParameter("anchor") != null) {
163: this .weblogAnchor = request.getParameter("anchor");
164: this .pageType = PERMALINK;
165: }
166:
167: if (request.getParameter("entry") != null) {
168: this .weblogAnchor = request.getParameter("entry");
169: this .pageType = PERMALINK;
170: }
171:
172: if (request.getParameter("catname") != null) {
173: String cat = request.getParameter("catname");
174:
175: this .weblogCategory = cat;
176: this .pageType = ARCHIVE;
177: }
178:
179: // comments only supported permalinks, so if anchor is null then error
180: if (this .weblogAnchor == null) {
181: throw new Exception("invalid comments request, no anchor");
182: }
183: }
184:
185: private boolean isValidDateString(String dateString) {
186: return (dateString != null && dateString.length() > 3 && StringUtils
187: .isNumeric(dateString));
188: }
189:
190: public String getContext() {
191: return context;
192: }
193:
194: public String getWeblogHandle() {
195: return weblogHandle;
196: }
197:
198: public String getWeblogAnchor() {
199: return weblogAnchor;
200: }
201:
202: public String getWeblogPage() {
203: return weblogPage;
204: }
205:
206: public String getWeblogCategory() {
207: return weblogCategory;
208: }
209:
210: public String getWeblogDate() {
211: return weblogDate;
212: }
213:
214: public String getPageType() {
215: return pageType;
216: }
217:
218: }
|