View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.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  
18  package org.kuali.rice.core.web;
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 Kuali Rice Team (rice.collab@kuali.org)
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, HttpServletResponse response) throws ServletException, IOException {
60          String requestURI = request.getRequestURI();
61          for (Map.Entry<Pattern, MessageFormat> entry: forwardTable.entrySet()) {
62              Pattern pattern = entry.getKey();
63              Matcher matcher = pattern.matcher(requestURI);
64              if (matcher.matches()) {
65                  Object[] params = new Object[matcher.groupCount()];
66                  for (int i = 0; i < params.length; i++) {
67                      params[i] = matcher.group(i + 1); // offset by 1 because group at index 0 denotes the "entire pattern" 
68                  }
69                  String forwardPath = entry.getValue().format(params);
70                  RequestDispatcher rd = request.getRequestDispatcher(forwardPath);
71                  if (rd == null) {
72                      response.sendError(HttpServletResponse.SC_NOT_FOUND, "Internal resource '" + forwardPath + "' not found");
73                  } else {
74                      rd.forward(request, response);
75                  }
76                  return;
77              }
78          }
79          
80      }
81  }