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