Coverage Report - org.apache.tiles.web.util.TilesDispatchServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
TilesDispatchServlet
0%
0/31
0%
0/10
2.4
TilesDispatchServlet$DefaultMutator
0%
0/2
N/A
2.4
 
 1  
 /*
 2  
  * $Id: TilesDispatchServlet.java 995228 2010-09-08 19:50:09Z apetrelli $
 3  
  *
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 package org.apache.tiles.web.util;
 22  
 
 23  
 import javax.servlet.ServletException;
 24  
 import javax.servlet.http.HttpServlet;
 25  
 import javax.servlet.http.HttpServletRequest;
 26  
 import javax.servlet.http.HttpServletResponse;
 27  
 
 28  
 import org.apache.tiles.AttributeContext;
 29  
 import org.apache.tiles.TilesContainer;
 30  
 import org.apache.tiles.access.TilesAccess;
 31  
 import org.apache.tiles.request.ApplicationContext;
 32  
 import org.apache.tiles.request.Request;
 33  
 import org.apache.tiles.request.reflect.ClassUtil;
 34  
 import org.apache.tiles.request.servlet.ServletRequest;
 35  
 import org.slf4j.Logger;
 36  
 import org.slf4j.LoggerFactory;
 37  
 
 38  
 /**
 39  
  * Tiles dispatching servlet.  Used to invoke
 40  
  * a definition directly.
 41  
  */
 42  0
 public class TilesDispatchServlet extends HttpServlet {
 43  
 
 44  
     /**
 45  
      * Init parameter to define the key of the container to use.
 46  
      *
 47  
      * @since 2.1.2
 48  
      */
 49  
     public static final String CONTAINER_KEY_INIT_PARAMETER =
 50  
         "org.apache.tiles.web.util.TilesDispatchServlet.CONTAINER_KEY";
 51  
 
 52  
     /**
 53  
      * The logging object.
 54  
      */
 55  0
     private final Logger log = LoggerFactory
 56  
             .getLogger(TilesDispatchServlet.class);
 57  
 
 58  
     /**
 59  
      * The key under which the container is stored.
 60  
      */
 61  
     private String containerKey;
 62  
 
 63  
     /**
 64  
      * The object that will mutate the attribute context so that it uses
 65  
      * different attributes.
 66  
      */
 67  
     private AttributeContextMutator mutator;
 68  
 
 69  
 
 70  
     /** {@inheritDoc} */
 71  
     @Override
 72  
     public void init() throws ServletException {
 73  0
         super.init();
 74  
 
 75  0
         containerKey = getServletConfig().getInitParameter(
 76  
                 CONTAINER_KEY_INIT_PARAMETER);
 77  
 
 78  0
         String temp = getInitParameter("mutator");
 79  0
         if (temp != null) {
 80  
             try {
 81  0
                 mutator = (AttributeContextMutator) ClassUtil.instantiate(temp);
 82  0
             } catch (Exception e) {
 83  0
                 throw new ServletException("Unable to instantiate specified context mutator.", e);
 84  0
             }
 85  
         } else {
 86  0
             mutator = new DefaultMutator();
 87  
         }
 88  0
     }
 89  
 
 90  
     /** {@inheritDoc} */
 91  
     @Override
 92  
     protected void doGet(HttpServletRequest req, HttpServletResponse res) {
 93  
 
 94  0
         ApplicationContext applicationContext = org.apache.tiles.request.servlet.ServletUtil
 95  
                 .getApplicationContext(getServletContext());
 96  0
         Request request = new ServletRequest(applicationContext,
 97  
                 req, res);
 98  0
         TilesContainer container = TilesAccess.getContainer(applicationContext,
 99  
                 containerKey);
 100  0
         mutator.mutate(container.getAttributeContext(request), req);
 101  0
         String definition = getDefinitionName(req);
 102  0
         if (log.isDebugEnabled()) {
 103  0
             log.info("Dispatching to tile '" + definition + "'");
 104  
         }
 105  0
         container.render(definition, request);
 106  0
     }
 107  
 
 108  
     /**
 109  
      * Returns the called definition name for the given request.
 110  
      *
 111  
      * @param request The request to parse.
 112  
      * @return The definition name to render.
 113  
      */
 114  
     protected String getDefinitionName(HttpServletRequest request) {
 115  0
         String path = (String) request.getAttribute("javax.servlet.include.servlet_path");
 116  0
         if (path == null) {
 117  0
             path = request.getServletPath();
 118  
         }
 119  
 
 120  0
         int start = path.startsWith("/") ? 1 : 0;
 121  0
         int end = path.endsWith(".tiles") ? path.indexOf(".tiles") : path.length();
 122  
 
 123  0
         return path.substring(start, end);
 124  
     }
 125  
 
 126  
     /** {@inheritDoc} */
 127  
     @Override
 128  
     protected void doPost(HttpServletRequest req, HttpServletResponse res) {
 129  0
         log.info("Tiles dispatch request received. Redirecting POST to GET.");
 130  0
         doGet(req, res);
 131  0
     }
 132  
 
 133  
     /**
 134  
      * Default no-op mutator.
 135  
      */
 136  0
     class DefaultMutator implements AttributeContextMutator {
 137  
 
 138  
         /** {@inheritDoc} */
 139  
         public void mutate(AttributeContext context, javax.servlet.ServletRequest request) {
 140  
             // noop;
 141  0
         }
 142  
     }
 143  
 }