01: /*
02: * Copyright 2005-2007 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: // Created on Dec 6, 2006
18: package edu.iu.uis.eden.server;
19:
20: import java.io.IOException;
21: import java.text.MessageFormat;
22: import java.util.Enumeration;
23: import java.util.LinkedHashMap;
24: import java.util.Map;
25: import java.util.regex.Matcher;
26: import java.util.regex.Pattern;
27:
28: import javax.servlet.RequestDispatcher;
29: import javax.servlet.ServletConfig;
30: import javax.servlet.ServletException;
31: import javax.servlet.http.HttpServlet;
32: import javax.servlet.http.HttpServletRequest;
33: import javax.servlet.http.HttpServletResponse;
34:
35: /**
36: * Servlet that forwards requests via the RequestDispatcher based on regular expression
37: * matching and replacement on the URI
38: * @author Aaron Hamid (arh14 at cornell dot edu)
39: */
40: public class RequestForwardingServlet extends HttpServlet {
41: private LinkedHashMap<Pattern, MessageFormat> forwardTable = new LinkedHashMap<Pattern, MessageFormat>();
42:
43: @Override
44: public void init() throws ServletException {
45: super .init();
46:
47: ServletConfig cfg = getServletConfig();
48: Enumeration paramNames = cfg.getInitParameterNames();
49: while (paramNames.hasMoreElements()) {
50: String name = (String) paramNames.nextElement();
51: Pattern pattern = Pattern.compile(name);
52: String forwardPath = cfg.getInitParameter(name);
53: MessageFormat messageFormat = new MessageFormat(forwardPath);
54: forwardTable.put(pattern, messageFormat);
55: }
56: }
57:
58: @Override
59: protected void doGet(HttpServletRequest request,
60: HttpServletResponse response) throws ServletException,
61: IOException {
62: String requestURI = request.getRequestURI();
63: for (Map.Entry<Pattern, MessageFormat> entry : forwardTable
64: .entrySet()) {
65: Pattern pattern = entry.getKey();
66: Matcher matcher = pattern.matcher(requestURI);
67: if (matcher.matches()) {
68: Object[] params = new Object[matcher.groupCount()];
69: for (int i = 0; i < params.length; i++) {
70: params[i] = matcher.group(i + 1); // offset by 1 because group at index 0 denotes the "entire pattern"
71: }
72: String forwardPath = entry.getValue().format(params);
73: RequestDispatcher rd = request
74: .getRequestDispatcher(forwardPath);
75: if (rd == null) {
76: response.sendError(
77: HttpServletResponse.SC_NOT_FOUND,
78: "Internal resource '" + forwardPath
79: + "' not found");
80: } else {
81: rd.forward(request, response);
82: }
83: return;
84: }
85: }
86:
87: }
88: }
|