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