Coverage Report - org.apache.tapestry.services.impl.LinkFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
LinkFactoryImpl
0%
0/67
0%
0/16
2.133
 
 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.commons.codec.net.URLCodec;
 18  
 import org.apache.hivemind.ApplicationRuntimeException;
 19  
 import org.apache.hivemind.ErrorLog;
 20  
 import org.apache.hivemind.order.Orderer;
 21  
 import org.apache.hivemind.util.Defense;
 22  
 import org.apache.tapestry.IEngine;
 23  
 import org.apache.tapestry.IRequestCycle;
 24  
 import org.apache.tapestry.Tapestry;
 25  
 import org.apache.tapestry.engine.*;
 26  
 import org.apache.tapestry.record.PropertyPersistenceStrategySource;
 27  
 import org.apache.tapestry.services.DataSqueezer;
 28  
 import org.apache.tapestry.services.LinkFactory;
 29  
 import org.apache.tapestry.services.ServiceConstants;
 30  
 import org.apache.tapestry.util.QueryParameterMap;
 31  
 import org.apache.tapestry.web.WebRequest;
 32  
 
 33  
 import java.util.Iterator;
 34  
 import java.util.List;
 35  
 import java.util.Map;
 36  
 
 37  
 /**
 38  
  * @author Howard M. Lewis Ship
 39  
  * @since 4.0
 40  
  */
 41  0
 public class LinkFactoryImpl implements LinkFactory
 42  
 {
 43  
     
 44  0
     protected URLCodec _codec = new URLCodec();
 45  
         
 46  
     protected PropertyPersistenceStrategySource _persistenceStrategySource;
 47  
     
 48  
     protected IRequestCycle _requestCycle;
 49  
     
 50  
     protected WebRequest _request;
 51  
     
 52  
     private DataSqueezer _dataSqueezer;
 53  
 
 54  
     private ErrorLog _errorLog;
 55  
 
 56  
     /**
 57  
      * List of {@link org.apache.tapestry.services.impl.ServiceEncoderContribution}.
 58  
      */
 59  
 
 60  
     private List _contributions;
 61  
 
 62  
     private ServiceEncoder[] _encoders;
 63  
 
 64  
     private String _servletPath;
 65  
 
 66  0
     private final Object[] _empty = new Object[0];
 67  
     
 68  
     public void initializeService()
 69  
     {
 70  0
         Orderer orderer = new Orderer(_errorLog, "encoder");
 71  
 
 72  0
         Iterator i = _contributions.iterator();
 73  
 
 74  0
         while (i.hasNext())
 75  
         {
 76  0
             ServiceEncoderContribution c = (ServiceEncoderContribution) i.next();
 77  
 
 78  0
             orderer.add(c, c.getId(), c.getAfter(), c.getBefore());
 79  0
         }
 80  
 
 81  0
         List ordered = orderer.getOrderedObjects();
 82  0
         int count = ordered.size();
 83  
 
 84  0
         _encoders = new ServiceEncoder[count];
 85  
 
 86  0
         for (int j = 0; j < count; j++)
 87  
         {
 88  0
             ServiceEncoderContribution c = (ServiceEncoderContribution) ordered.get(j);
 89  
 
 90  0
             _encoders[j] = c.getEncoder();
 91  
         }
 92  
 
 93  0
     }
 94  
 
 95  
     public ILink constructLink(IEngineService service, boolean post, Map parameters, boolean stateful)
 96  
     {
 97  0
         finalizeParameters(service, parameters);
 98  
 
 99  0
         IEngine engine = _requestCycle.getEngine();
 100  
 
 101  0
         QueryParameterMap qmap = new QueryParameterMap(parameters);
 102  
         
 103  0
         ServiceEncoding serviceEncoding = createServiceEncoding(qmap);
 104  
         
 105  
         // Give persistent property strategies a chance to store extra data
 106  
         // into the link.
 107  
 
 108  0
         if (stateful)
 109  0
             _persistenceStrategySource.addParametersForPersistentProperties(serviceEncoding, post);
 110  
 
 111  0
         String fullServletPath = _request.getContextPath() + serviceEncoding.getServletPath();
 112  
 
 113  0
         return new EngineServiceLink(_requestCycle, fullServletPath, engine.getOutputEncoding(),
 114  
                 _codec, _request, qmap, stateful);
 115  
     }
 116  
 
 117  
     protected void finalizeParameters(IEngineService service, Map parameters)
 118  
     {
 119  0
         Defense.notNull(service, "service");
 120  0
         Defense.notNull(parameters, "parameters");
 121  
         
 122  0
         String serviceName = service.getName();
 123  
 
 124  0
         if (serviceName == null)
 125  0
             throw new ApplicationRuntimeException(ImplMessages.serviceNameIsNull());
 126  
 
 127  0
         parameters.put(ServiceConstants.SERVICE, serviceName);
 128  
 
 129  0
         squeezeServiceParameters(parameters);
 130  0
     }
 131  
 
 132  
     public ServiceEncoder[] getServiceEncoders()
 133  
     {
 134  0
         return _encoders;
 135  
     }
 136  
 
 137  
     /**
 138  
      * Creates a new service encoding, and allows the encoders to modify it before returning.
 139  
      */
 140  
 
 141  
     protected ServiceEncoding createServiceEncoding(QueryParameterMap parameters)
 142  
     {
 143  0
         ServiceEncodingImpl result = new ServiceEncodingImpl(_servletPath, parameters);
 144  
 
 145  0
         for (int i = 0; i < _encoders.length; i++)
 146  
         {
 147  0
             _encoders[i].encode(result);
 148  
 
 149  0
             if (result.isModified())
 150  0
                 break;
 151  
         }
 152  
 
 153  0
         return result;
 154  
     }
 155  
 
 156  
     protected void squeezeServiceParameters(Map parameters)
 157  
     {
 158  0
         Object[] serviceParameters = (Object[]) parameters.get(ServiceConstants.PARAMETER);
 159  
 
 160  0
         if (serviceParameters == null)
 161  0
             return;
 162  
         
 163  0
         parameters.put(ServiceConstants.PARAMETER, squeeze(serviceParameters));
 164  0
     }
 165  
 
 166  
     public Object[] extractListenerParameters(IRequestCycle cycle)
 167  
     {
 168  0
         String[] squeezed = cycle.getParameters(ServiceConstants.PARAMETER);
 169  
 
 170  0
         if (Tapestry.size(squeezed) == 0)
 171  0
             return _empty;
 172  
 
 173  
         try
 174  
         {
 175  0
             return _dataSqueezer.unsqueeze(squeezed);
 176  
         }
 177  0
         catch (Exception ex)
 178  
         {
 179  0
             throw new ApplicationRuntimeException(ex);
 180  
         }
 181  
     }
 182  
 
 183  
     private String[] squeeze(Object[] input)
 184  
     {
 185  
         try
 186  
         {
 187  0
             return _dataSqueezer.squeeze(input);
 188  
         }
 189  0
         catch (Exception ex)
 190  
         {
 191  0
             throw new ApplicationRuntimeException(ex);
 192  
         }
 193  
     }
 194  
 
 195  
     public void setDataSqueezer(DataSqueezer dataSqueezer)
 196  
     {
 197  0
         _dataSqueezer = dataSqueezer;
 198  0
     }
 199  
 
 200  
     public void setContributions(List contributions)
 201  
     {
 202  0
         _contributions = contributions;
 203  0
     }
 204  
 
 205  
     public void setErrorLog(ErrorLog errorLog)
 206  
     {
 207  0
         _errorLog = errorLog;
 208  0
     }
 209  
 
 210  
     public void setServletPath(String servletPath)
 211  
     {
 212  0
         _servletPath = servletPath;
 213  0
     }
 214  
 
 215  
     public void setRequest(WebRequest request)
 216  
     {
 217  0
         _request = request;
 218  0
     }
 219  
 
 220  
     /**
 221  
      * This is kind of limiting; it's possible that other things beyond persistence strategies will
 222  
      * want to have a hand at encoding data into URLs. If that comes to pass, we'll need to
 223  
      * implement an event coordinator/listener combo to let implementations know about links being
 224  
      * generated.
 225  
      *
 226  
      * @param persistenceStrategySource
 227  
      *          The persistent strategy to use.
 228  
      */
 229  
 
 230  
     public void setPersistenceStrategySource(PropertyPersistenceStrategySource persistenceStrategySource)
 231  
     {
 232  0
         _persistenceStrategySource = persistenceStrategySource;
 233  0
     }
 234  
 
 235  
     public void setRequestCycle(IRequestCycle requestCycle)
 236  
     {
 237  0
         _requestCycle = requestCycle;
 238  0
     }
 239  
 }