Coverage Report - org.apache.tiles.request.freemarker.FreemarkerRequest
 
Classes in this File Line Coverage Branch Coverage Complexity
FreemarkerRequest
100%
24/24
66%
4/6
1.444
 
 1  
 /*
 2  
  * $Id: FreemarkerRequest.java 1332186 2012-04-30 13:20:15Z nlebas $
 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  
 
 22  
 package org.apache.tiles.request.freemarker;
 23  
 
 24  
 import java.io.PrintWriter;
 25  
 import java.io.Writer;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Collections;
 28  
 import java.util.List;
 29  
 import java.util.Locale;
 30  
 import java.util.Map;
 31  
 
 32  
 import javax.servlet.http.HttpServletRequest;
 33  
 import javax.servlet.http.HttpServletResponse;
 34  
 
 35  
 import org.apache.tiles.request.AbstractViewRequest;
 36  
 import org.apache.tiles.request.ApplicationContext;
 37  
 import org.apache.tiles.request.DispatchRequest;
 38  
 import org.apache.tiles.request.servlet.ServletRequest;
 39  
 
 40  
 import freemarker.core.Environment;
 41  
 import freemarker.ext.servlet.HttpRequestHashModel;
 42  
 
 43  
 /**
 44  
  * The FreeMarker-specific request context.
 45  
  *
 46  
  * @version $Rev: 1332186 $ $Date: 2012-04-30 23:20:15 +1000 (Mon, 30 Apr 2012) $
 47  
  */
 48  
 public class FreemarkerRequest extends AbstractViewRequest {
 49  
 
 50  
     /**
 51  
      * The natively available scopes. In fact, only "page".
 52  
      */
 53  
     private List<String> scopes;
 54  
 
 55  
     /**
 56  
      * The FreeMarker current environment.
 57  
      */
 58  
     private Environment env;
 59  
 
 60  
     /**
 61  
      * The page scope map.
 62  
      */
 63  
     private Map<String, Object> pageScope;
 64  
 
 65  
     /**
 66  
      * Creates a new Freemarker request.
 67  
      *
 68  
      * @param applicationContext The application context.
 69  
      * @param env The Freemarker's environment object.
 70  
      * @return A new request.
 71  
      */
 72  
     public static FreemarkerRequest createServletFreemarkerRequest(
 73  
             ApplicationContext applicationContext, Environment env) {
 74  2
         HttpRequestHashModel requestModel = FreemarkerRequestUtil
 75  
                 .getRequestHashModel(env);
 76  2
         HttpServletRequest request = requestModel.getRequest();
 77  2
         HttpServletResponse response = requestModel.getResponse();
 78  2
         DispatchRequest enclosedRequest = new ServletRequest(
 79  
                 applicationContext, request, response);
 80  2
         return new FreemarkerRequest(enclosedRequest, env);
 81  
     }
 82  
 
 83  
     /**
 84  
      * Constructor.
 85  
      *
 86  
      * @param enclosedRequest
 87  
      *            The request that exposes non-FreeMarker specific properties
 88  
      * @param env
 89  
      *            The FreeMarker environment.
 90  
      */
 91  
     public FreemarkerRequest(DispatchRequest enclosedRequest,
 92  
             Environment env) {
 93  11
         super(enclosedRequest);
 94  11
         List<String> scopes = new ArrayList<String>();
 95  11
         scopes.addAll(enclosedRequest.getAvailableScopes());
 96  11
         scopes.add("page");
 97  11
         this.scopes = Collections.unmodifiableList(scopes);
 98  11
         this.env = env;
 99  11
     }
 100  
 
 101  
     /**
 102  
      * Returns the environment object.
 103  
      *
 104  
      * @return The environment.
 105  
      */
 106  
     public Environment getEnvironment() {
 107  4
         return env;
 108  
     }
 109  
 
 110  
     /** {@inheritDoc} */
 111  
     @Override
 112  
     public Locale getRequestLocale() {
 113  1
         return env.getLocale();
 114  
     }
 115  
 
 116  
     /**
 117  
      * Returns the page scope.
 118  
      *
 119  
      * @return The page scope.
 120  
      */
 121  
     public Map<String, Object> getPageScope() {
 122  1
         if (pageScope == null) {
 123  1
             pageScope = new EnvironmentScopeMap(env);
 124  
         }
 125  1
         return pageScope;
 126  
     }
 127  
 
 128  
     @Override
 129  
     public List<String> getAvailableScopes() {
 130  1
         return scopes;
 131  
     }
 132  
 
 133  
     /** {@inheritDoc} */
 134  
     @Override
 135  
     public PrintWriter getPrintWriter() {
 136  2
         Writer writer = env.getOut();
 137  2
         if (writer instanceof PrintWriter) {
 138  1
             return (PrintWriter) writer;
 139  
         }
 140  1
         return new PrintWriter(writer);
 141  
     }
 142  
 
 143  
     /** {@inheritDoc} */
 144  
     @Override
 145  
     public Writer getWriter() {
 146  1
         return env.getOut();
 147  
     }
 148  
 
 149  
     @Override
 150  
     public Map<String, Object> getContext(String scope) {
 151  1
         return "page".equals(scope) ? getPageScope() : super.getContext(scope);
 152  
     }
 153  
 
 154  
 }