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 long serialVersionUID = -646440036923109210L;
043    
044      private static final Log LOG = LogFactory.getLog(TemplateServlet.class);
045    
046      public void service(
047          HttpServletRequest request, HttpServletResponse response)
048          throws ServletException, IOException {
049    
050        String viewId = request.getRequestURI().substring(
051            request.getContextPath().length());
052    
053        if (LOG.isDebugEnabled()) {
054          LOG.debug("viewId = '" + viewId + "'");
055        }
056        String requestUri = remap(request, viewId);
057        if (LOG.isDebugEnabled()) {
058          LOG.debug("requestUri = '" + requestUri + "'");
059        }
060    
061        if (requestUri.endsWith(".view")) { // TODO: make .view configurable
062          String error = "cannot find URI in config file: '" + requestUri + "'";
063          LOG.error(error);
064          throw new ServletException(error);
065        }
066    
067        if (LOG.isDebugEnabled()) {
068          LOG.debug("requestUri = '" + requestUri + "'");
069        }
070    
071        try {
072          RequestDispatcher dispatcher = request.getRequestDispatcher(requestUri);
073          dispatcher.forward(request, response);
074        } catch (IOException e) {
075          LOG.error("requestUri '" + requestUri + "' "
076              + "viewId '" + viewId + "' ", e);
077          throw e;
078        } catch (ServletException e) {
079          LOG.error("requestUri '" + requestUri + "' "
080              + "viewId '" + viewId + "' ", e);
081          throw e;
082        }
083      }
084    
085      private String remap(ServletRequest request, String requestURI) {
086        FacesContext facesContext = FacesContext.getCurrentInstance();
087        TobagoConfig config = TobagoConfig.getInstance(facesContext);
088        MappingRule mappingRule = config.getMappingRule(requestURI);
089        if (mappingRule == null) {
090          return requestURI;
091        }
092        for (Iterator i = mappingRule.getAttributes().iterator(); i.hasNext();) {
093          Attribute attribute = (Attribute) i.next();
094          request.setAttribute(attribute.getKey(), attribute.getValue());
095        }
096        return mappingRule.getForwardUri();
097      }
098    
099    }