Coverage Report - org.kuali.rice.core.web.RequestForwardingServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestForwardingServlet
0%
0/29
0%
0/10
3.5
 
 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  0
 public class RequestForwardingServlet extends HttpServlet {
 41  0
     private LinkedHashMap<Pattern, MessageFormat> forwardTable = new LinkedHashMap<Pattern, MessageFormat>();
 42  
 
 43  
     @Override
 44  
     public void init() throws ServletException {
 45  0
         super.init();
 46  
 
 47  0
         ServletConfig cfg = getServletConfig();
 48  0
         Enumeration paramNames = cfg.getInitParameterNames(); 
 49  0
         while (paramNames.hasMoreElements()) {
 50  0
             String name = (String) paramNames.nextElement();
 51  0
             Pattern pattern = Pattern.compile(name);
 52  0
             String forwardPath = cfg.getInitParameter(name);
 53  0
             MessageFormat messageFormat = new MessageFormat(forwardPath);
 54  0
             forwardTable.put(pattern, messageFormat);
 55  0
         }
 56  0
     }
 57  
 
 58  
     @Override
 59  
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 60  0
         String requestURI = request.getRequestURI();
 61  0
         for (Map.Entry<Pattern, MessageFormat> entry: forwardTable.entrySet()) {
 62  0
             Pattern pattern = entry.getKey();
 63  0
             Matcher matcher = pattern.matcher(requestURI);
 64  0
             if (matcher.matches()) {
 65  0
                 Object[] params = new Object[matcher.groupCount()];
 66  0
                 for (int i = 0; i < params.length; i++) {
 67  0
                     params[i] = matcher.group(i + 1); // offset by 1 because group at index 0 denotes the "entire pattern" 
 68  
                 }
 69  0
                 String forwardPath = entry.getValue().format(params);
 70  0
                 RequestDispatcher rd = request.getRequestDispatcher(forwardPath);
 71  0
                 if (rd == null) {
 72  0
                     response.sendError(HttpServletResponse.SC_NOT_FOUND, "Internal resource '" + forwardPath + "' not found");
 73  
                 } else {
 74  0
                     rd.forward(request, response);
 75  
                 }
 76  0
                 return;
 77  
             }
 78  0
         }
 79  
         
 80  0
     }
 81  
 }