001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.servlet;
066:
067: import com.jcorporate.expresso.core.misc.ConfigManager;
068: import com.jcorporate.expresso.core.misc.ConfigPathFixedParam;
069: import com.jcorporate.expresso.core.misc.ConfigPathMapping;
070: import com.jcorporate.expresso.core.misc.ConfigPathParam;
071: import com.jcorporate.expresso.core.misc.ConfigurationException;
072: import org.apache.log4j.Logger;
073: import org.apache.oro.text.regex.Pattern;
074: import org.apache.oro.text.regex.PatternMatcher;
075: import org.apache.oro.text.regex.Perl5Matcher;
076:
077: import javax.servlet.RequestDispatcher;
078: import javax.servlet.ServletException;
079: import javax.servlet.http.HttpServletRequest;
080: import javax.servlet.http.HttpServletResponse;
081: import java.io.IOException;
082: import java.util.Enumeration;
083: import java.util.StringTokenizer;
084: import java.util.Vector;
085:
086: /**
087: * Special servlet designed to interact with server-side Controller objects
088: *
089: * @author Michael Nash
090: */
091: public class PathHandler extends DBServlet {
092: private static Logger log = Logger.getLogger(PathHandler.class);
093: private static PatternMatcher matcher = new Perl5Matcher();
094:
095: public PathHandler() {
096: }
097:
098: /**
099: * Utility method to set up the log for this object
100: */
101: private static void setupLog() {
102: }
103:
104: /**
105: * Process the path to get the correct configuration
106: *
107: * @param req The Servlet Request
108: * @param res The servlet response.
109: */
110: private void process(HttpServletRequest req, HttpServletResponse res)
111: throws ServletException, IOException {
112: String originalPath = req.getPathInfo();
113:
114: try {
115: for (Enumeration e = ConfigManager.getContext(
116: getDBName(req)).getPathMappings().elements(); e
117: .hasMoreElements();) {
118: ConfigPathMapping oneMapping = (ConfigPathMapping) e
119: .nextElement();
120: if (log.isDebugEnabled()) {
121: log.debug("Mapping:" + oneMapping.getUrlPattern()
122: + ", " + oneMapping.getPath());
123: }
124:
125: if (log.isDebugEnabled()) {
126: for (Enumeration ep = oneMapping.getParams()
127: .elements(); ep.hasMoreElements();) {
128: ConfigPathParam oneParam = (ConfigPathParam) ep
129: .nextElement();
130: log.debug("-- Param:" + oneParam.getNumber()
131: + ", " + oneParam.getName());
132: }
133: }
134:
135: if (log.isDebugEnabled()) {
136: for (Enumeration epf = oneMapping.getFixedParams()
137: .elements(); epf.hasMoreElements();) {
138: ConfigPathFixedParam oneFixedParam = (ConfigPathFixedParam) epf
139: .nextElement();
140: log.debug("-- Fixed:" + oneFixedParam.getName()
141: + ", " + oneFixedParam.getValue());
142: }
143: }
144:
145: Pattern p = oneMapping.getCompiledUrlPattern();
146:
147: boolean result;
148: synchronized (matcher) {
149: result = matcher.matches(originalPath, p);
150: }
151:
152: if (result) {
153: log.debug("Path Matches!");
154: redirect(req, res, oneMapping);
155: }
156: }
157: } catch (ConfigurationException ce) {
158: throw new ServletException(ce.getMessage());
159: }
160: }
161:
162: private void redirect(HttpServletRequest req,
163: HttpServletResponse res, ConfigPathMapping mapping)
164: throws ServletException, IOException {
165: String queryString = req.getQueryString();
166: String originalPath = req.getPathInfo();
167:
168: if (log.isDebugEnabled()) {
169: log.debug("Path:" + originalPath);
170: log.debug("Query:" + queryString);
171: }
172:
173: Vector params = new Vector();
174: StringTokenizer stk = new StringTokenizer(originalPath, "/");
175:
176: while (stk.hasMoreElements()) {
177: params.addElement(stk.nextElement());
178: }
179:
180: ConfigPathParam oneParam = null;
181: ConfigPathFixedParam oneFixedParam = null;
182: int numberNeeded = 0;
183: boolean needAmpersand = false;
184: StringBuffer newURL = new StringBuffer(mapping.getPath() + "?");
185:
186: for (Enumeration ep = mapping.getParams().elements(); ep
187: .hasMoreElements();) {
188: oneParam = (ConfigPathParam) ep.nextElement();
189:
190: if (needAmpersand) {
191: newURL.append("&");
192: }
193:
194: numberNeeded = oneParam.getNumber();
195:
196: if (numberNeeded <= params.size()) {
197: newURL.append(oneParam.getName() + "="
198: + params.elementAt(numberNeeded));
199: }
200:
201: needAmpersand = true;
202: }
203: for (Enumeration efp = mapping.getFixedParams().elements(); efp
204: .hasMoreElements();) {
205: if (needAmpersand) {
206: newURL.append("&");
207: }
208:
209: oneFixedParam = (ConfigPathFixedParam) efp.nextElement();
210: newURL.append(oneFixedParam.getName() + "="
211: + oneFixedParam.getValue());
212: } /* for */
213:
214: log.debug("New URL:" + newURL);
215:
216: RequestDispatcher rd = getServletConfig().getServletContext()
217: .getRequestDispatcher(newURL.toString());
218:
219: if (rd == null) {
220: throw new ServletException("Can't forward");
221: }
222:
223: rd.forward(req, res);
224: }
225:
226: /**
227: * Both GET and POST for the PathHandler simply pass thru to the
228: * "process" method.
229: */
230: public void doGet(HttpServletRequest request,
231: HttpServletResponse response) throws ServletException,
232: IOException {
233: super .doGet(request, response);
234: setupLog();
235: process(request, response);
236: } /* doGet(HttpServletRequest, HttpServletResponse) */
237:
238: /**
239: * Both GET and POST for the PathHandler simply pass thru to the
240: * "process" method.
241: */
242: public void doPost(HttpServletRequest request,
243: HttpServletResponse response) throws ServletException,
244: IOException {
245: super .doGet(request, response);
246: setupLog();
247: process(request, response);
248: } /* doGet(HttpServletRequest, HttpServletResponse) */
249:
250: }
|