1 15 package org.apache.tapestry.services.impl; 16 17 import java.util.Iterator ; 18 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 22 import org.apache.hivemind.ErrorHandler; 23 import org.apache.tapestry.IEngine; 24 import org.apache.tapestry.IRequestCycle; 25 import org.apache.tapestry.Tapestry; 26 import org.apache.tapestry.engine.IEngineService; 27 import org.apache.tapestry.engine.IMonitor; 28 import org.apache.tapestry.engine.IMonitorFactory; 29 import org.apache.tapestry.engine.RequestCycle; 30 import org.apache.tapestry.engine.RequestCycleEnvironment; 31 import org.apache.tapestry.engine.ServiceEncoder; 32 import org.apache.tapestry.engine.ServiceEncodingImpl; 33 import org.apache.tapestry.record.PropertyPersistenceStrategySource; 34 import org.apache.tapestry.request.RequestContext; 35 import org.apache.tapestry.services.AbsoluteURLBuilder; 36 import org.apache.tapestry.services.Infrastructure; 37 import org.apache.tapestry.services.RequestCycleFactory; 38 import org.apache.tapestry.services.ServiceConstants; 39 import org.apache.tapestry.util.QueryParameterMap; 40 import org.apache.tapestry.web.WebRequest; 41 42 49 public class RequestCycleFactoryImpl implements RequestCycleFactory 50 { 51 private ServiceEncoder[] _encoders; 52 53 private IMonitorFactory _monitorFactory; 54 55 private PropertyPersistenceStrategySource _strategySource; 56 57 private ErrorHandler _errorHandler; 58 59 private Infrastructure _infrastructure; 60 61 private AbsoluteURLBuilder _absoluteURLBuilder; 62 63 private RequestCycleEnvironment _environment; 64 65 private HttpServletRequest _servletRequest; 66 67 private HttpServletResponse _servletResponse; 68 69 public void initializeService() 70 { 71 RequestContext context = new RequestContext(_servletRequest, _servletResponse); 72 73 _environment = new RequestCycleEnvironment(_errorHandler, _infrastructure, context, 74 _strategySource, _absoluteURLBuilder); 75 } 76 77 public IRequestCycle newRequestCycle(IEngine engine) 78 { 79 WebRequest request = _infrastructure.getRequest(); 80 81 IMonitor monitor = _monitorFactory.createMonitor(request); 82 83 QueryParameterMap parameters = extractParameters(request); 84 85 decodeParameters(request.getActivationPath(), parameters); 86 87 IEngineService service = findService(parameters); 88 89 return new RequestCycle(engine, parameters, service, monitor, _environment); 90 } 91 92 private IEngineService findService(QueryParameterMap parameters) 93 { 94 String serviceName = parameters.getParameterValue(ServiceConstants.SERVICE); 95 96 if (serviceName == null) 97 serviceName = Tapestry.HOME_SERVICE; 98 99 return _infrastructure.getServiceMap().getService(serviceName); 100 } 101 102 107 108 private QueryParameterMap extractParameters(WebRequest request) 109 { 110 QueryParameterMap result = new QueryParameterMap(); 111 112 Iterator i = request.getParameterNames().iterator(); 113 114 while (i.hasNext()) 115 { 116 String name = (String ) i.next(); 117 118 String [] values = request.getParameterValues(name); 119 120 if (values.length == 1) 121 result.setParameterValue(name, values[0]); 122 else 123 result.setParameterValues(name, values); 124 } 125 126 return result; 127 } 128 129 private void decodeParameters(String servletPath, QueryParameterMap map) 130 { 131 ServiceEncodingImpl se = new ServiceEncodingImpl(servletPath, map); 132 133 for (int i = 0; i < _encoders.length; i++) 134 { 135 _encoders[i].decode(se); 136 137 if (se.isModified()) 138 return; 139 } 140 } 141 142 public void setEncoders(ServiceEncoder[] encoders) 143 { 144 _encoders = encoders; 145 } 146 147 public void setMonitorFactory(IMonitorFactory monitorFactory) 148 { 149 _monitorFactory = monitorFactory; 150 } 151 152 public void setStrategySource(PropertyPersistenceStrategySource strategySource) 153 { 154 _strategySource = strategySource; 155 } 156 157 public void setErrorHandler(ErrorHandler errorHandler) 158 { 159 _errorHandler = errorHandler; 160 } 161 162 public void setInfrastructure(Infrastructure infrastructure) 163 { 164 _infrastructure = infrastructure; 165 } 166 167 public void setAbsoluteURLBuilder(AbsoluteURLBuilder absoluteURLBuilder) 168 { 169 _absoluteURLBuilder = absoluteURLBuilder; 170 } 171 172 public void setServletRequest(HttpServletRequest servletRequest) 173 { 174 _servletRequest = servletRequest; 175 } 176 177 public void setServletResponse(HttpServletResponse servletResponse) 178 { 179 _servletResponse = servletResponse; 180 } 181 } | Popular Tags |