001 package org.apache.myfaces.tobago.servlet; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import org.apache.commons.logging.Log; 021 import org.apache.commons.logging.LogFactory; 022 import org.apache.myfaces.tobago.config.Attribute; 023 import org.apache.myfaces.tobago.config.MappingRule; 024 import org.apache.myfaces.tobago.config.TobagoConfig; 025 026 import javax.faces.context.FacesContext; 027 import javax.servlet.RequestDispatcher; 028 import javax.servlet.ServletException; 029 import javax.servlet.ServletRequest; 030 import javax.servlet.http.HttpServlet; 031 import javax.servlet.http.HttpServletRequest; 032 import javax.servlet.http.HttpServletResponse; 033 import java.io.IOException; 034 import java.util.Iterator; 035 036 /** 037 * @deprecated Please use JSP 2.0 tag-files or an other template mechanism. 038 */ 039 @Deprecated 040 public class TemplateServlet extends HttpServlet { 041 042 private static final Log LOG = LogFactory.getLog(TemplateServlet.class); 043 044 public void service( 045 HttpServletRequest request, HttpServletResponse response) 046 throws ServletException, IOException { 047 048 String viewId = request.getRequestURI().substring( 049 request.getContextPath().length()); 050 051 if (LOG.isDebugEnabled()) { 052 LOG.debug("viewId = '" + viewId + "'"); 053 } 054 String requestUri = remap(request, viewId); 055 if (LOG.isDebugEnabled()) { 056 LOG.debug("requestUri = '" + requestUri + "'"); 057 } 058 059 if (requestUri.endsWith(".view")) { // TODO: make .view configurable 060 String error = "cannot find URI in config file: '" + requestUri + "'"; 061 LOG.error(error); 062 throw new ServletException(error); 063 } 064 065 if (LOG.isDebugEnabled()) { 066 LOG.debug("requestUri = '" + requestUri + "'"); 067 } 068 069 try { 070 RequestDispatcher dispatcher = request.getRequestDispatcher(requestUri); 071 dispatcher.forward(request, response); 072 } catch (IOException e) { 073 LOG.error("requestUri '" + requestUri + "' " 074 + "viewId '" + viewId + "' ", e); 075 throw e; 076 } catch (ServletException e) { 077 LOG.error("requestUri '" + requestUri + "' " 078 + "viewId '" + viewId + "' ", e); 079 throw e; 080 } 081 } 082 083 private String remap(ServletRequest request, String requestURI) { 084 FacesContext facesContext = FacesContext.getCurrentInstance(); 085 TobagoConfig config = TobagoConfig.getInstance(facesContext); 086 MappingRule mappingRule = config.getMappingRule(requestURI); 087 if (mappingRule == null) { 088 return requestURI; 089 } 090 for (Iterator i = mappingRule.getAttributes().iterator(); i.hasNext();) { 091 Attribute attribute = (Attribute) i.next(); 092 request.setAttribute(attribute.getKey(), attribute.getValue()); 093 } 094 return mappingRule.getForwardUri(); 095 } 096 097 }