Coverage Report - org.apache.tapestry.services.impl.RequestCycleFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestCycleFactoryImpl
0%
0/48
0%
0/10
1.538
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.services.impl;
 16  
 
 17  
 import org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.hivemind.ErrorHandler;
 19  
 import org.apache.tapestry.IEngine;
 20  
 import org.apache.tapestry.IRequestCycle;
 21  
 import org.apache.tapestry.Tapestry;
 22  
 import org.apache.tapestry.engine.RequestCycle;
 23  
 import org.apache.tapestry.engine.RequestCycleEnvironment;
 24  
 import org.apache.tapestry.engine.ServiceEncoder;
 25  
 import org.apache.tapestry.engine.ServiceEncodingImpl;
 26  
 import org.apache.tapestry.record.PropertyPersistenceStrategySource;
 27  
 import org.apache.tapestry.services.*;
 28  
 import org.apache.tapestry.util.QueryParameterMap;
 29  
 import org.apache.tapestry.web.WebRequest;
 30  
 
 31  
 import java.io.IOException;
 32  
 import java.util.Iterator;
 33  
 
 34  
 /**
 35  
  * Service that creates instances of {@link org.apache.tapestry.IRequestCycle}on behalf of an
 36  
  * engine.
 37  
  *
 38  
  * @author Howard M. Lewis Ship
 39  
  * @since 4.0
 40  
  */
 41  0
 public class RequestCycleFactoryImpl implements RequestCycleFactory
 42  
 {
 43  
     private ServiceEncoder[] _encoders;
 44  
 
 45  
     private PropertyPersistenceStrategySource _strategySource;
 46  
 
 47  
     private ErrorHandler _errorHandler;
 48  
 
 49  
     private Infrastructure _infrastructure;
 50  
 
 51  
     private AbsoluteURLBuilder _absoluteURLBuilder;
 52  
 
 53  
     private RequestCycleEnvironment _environment;
 54  
 
 55  
     private RequestGlobals _requestGlobals;
 56  
 
 57  
     private ResponseDelegateFactory _responseDelegateFactory;
 58  
 
 59  
     public void initializeService()
 60  
     {
 61  0
         _environment = new RequestCycleEnvironment(_errorHandler, _infrastructure, _strategySource,
 62  
                                                    _absoluteURLBuilder);
 63  0
     }
 64  
 
 65  
     public IRequestCycle newRequestCycle(IEngine engine)
 66  
     {
 67  0
         WebRequest request = _infrastructure.getRequest();
 68  
 
 69  0
         QueryParameterMap parameters = extractParameters(request);
 70  
 
 71  0
         decodeParameters(request.getActivationPath(), request.getPathInfo(), parameters);
 72  
 
 73  0
         String serviceName = findService(parameters);
 74  
 
 75  0
         IRequestCycle cycle = new RequestCycle(engine, parameters, serviceName, _environment);
 76  
 
 77  0
         _requestGlobals.store(cycle);
 78  
 
 79  
         try {
 80  
 
 81  0
             _requestGlobals.store(_responseDelegateFactory.getResponseBuilder(cycle));
 82  
 
 83  0
             cycle.setResponseBuilder(_requestGlobals.getResponseBuilder());
 84  
 
 85  0
         } catch (IOException e) {
 86  0
             throw new ApplicationRuntimeException("Error creating response builder.", e);
 87  0
         }
 88  
 
 89  0
         return cycle;
 90  
     }
 91  
 
 92  
     private String findService(QueryParameterMap parameters)
 93  
     {
 94  0
         String serviceName = parameters.getParameterValue(ServiceConstants.SERVICE);
 95  
 
 96  0
         return serviceName == null ? Tapestry.HOME_SERVICE : serviceName;
 97  
     }
 98  
 
 99  
     /**
 100  
      * Constructs a {@link org.apache.tapestry.util.QueryParameterMap}using the parameters
 101  
      * available from the {@link WebRequest} (but ignoring any
 102  
      * file upload parameters!).
 103  
      */
 104  
 
 105  
     private QueryParameterMap extractParameters(WebRequest request)
 106  
     {
 107  0
         QueryParameterMap result = new QueryParameterMap();
 108  
 
 109  0
         Iterator i = request.getParameterNames().iterator();
 110  
 
 111  0
         while (i.hasNext())
 112  
         {
 113  0
             String name = (String) i.next();
 114  
 
 115  0
             String[] values = request.getParameterValues(name);
 116  
 
 117  0
             if (values.length == 1)
 118  0
                 result.setParameterValue(name, values[0]);
 119  
             else
 120  0
                 result.setParameterValues(name, values);
 121  0
         }
 122  
 
 123  0
         return result;
 124  
     }
 125  
 
 126  
     private void decodeParameters(String servletPath, String pathInfo, QueryParameterMap map)
 127  
     {
 128  0
         ServiceEncodingImpl se = new ServiceEncodingImpl(servletPath, pathInfo, map);
 129  
 
 130  0
         for (int i = 0; i < _encoders.length; i++)
 131  
         {
 132  0
             _encoders[i].decode(se);
 133  
 
 134  0
             if (se.isModified())
 135  0
                 return;
 136  
         }
 137  0
     }
 138  
 
 139  
     public void setEncoders(ServiceEncoder[] encoders)
 140  
     {
 141  0
         _encoders = encoders;
 142  0
     }
 143  
 
 144  
     public void setStrategySource(PropertyPersistenceStrategySource strategySource)
 145  
     {
 146  0
         _strategySource = strategySource;
 147  0
     }
 148  
 
 149  
     public void setErrorHandler(ErrorHandler errorHandler)
 150  
     {
 151  0
         _errorHandler = errorHandler;
 152  0
     }
 153  
 
 154  
     public void setInfrastructure(Infrastructure infrastructure)
 155  
     {
 156  0
         _infrastructure = infrastructure;
 157  0
     }
 158  
 
 159  
     public void setAbsoluteURLBuilder(AbsoluteURLBuilder absoluteURLBuilder)
 160  
     {
 161  0
         _absoluteURLBuilder = absoluteURLBuilder;
 162  0
     }
 163  
 
 164  
     public void setRequestGlobals(RequestGlobals requestGlobals)
 165  
     {
 166  0
         _requestGlobals = requestGlobals;
 167  0
     }
 168  
 
 169  
     /**
 170  
      * For injection.
 171  
      */
 172  
     public void setResponseDelegateFactory(ResponseDelegateFactory responseDelegate)
 173  
     {
 174  0
         _responseDelegateFactory = responseDelegate;
 175  0
     }
 176  
 
 177  
     /**
 178  
      * For subclass access.
 179  
      */
 180  
     public ResponseDelegateFactory getResponseDelegateFactory()
 181  
     {
 182  0
         return _responseDelegateFactory;
 183  
     }
 184  
 }