Coverage Report - org.apache.tapestry.engine.EngineServiceLink
 
Classes in this File Line Coverage Branch Coverage Complexity
EngineServiceLink
0%
0/72
0%
0/30
2.5
 
 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.engine;
 16  
 
 17  
 import org.apache.commons.codec.net.URLCodec;
 18  
 import org.apache.hivemind.ApplicationRuntimeException;
 19  
 import org.apache.hivemind.util.Defense;
 20  
 import org.apache.tapestry.IRequestCycle;
 21  
 import org.apache.tapestry.Tapestry;
 22  
 import org.apache.tapestry.util.QueryParameterMap;
 23  
 import org.apache.tapestry.web.WebRequest;
 24  
 
 25  
 import java.io.UnsupportedEncodingException;
 26  
 
 27  
 /**
 28  
  * A EngineServiceLink represents a possible action within the client web browser; either clicking a
 29  
  * link or submitting a form, which is constructed primarily from the servlet path, with some
 30  
  * additional query parameters. A full URL for the EngineServiceLink can be generated, or the query
 31  
  * parameters for the EngineServiceLink can be extracted (separately from the servlet path). The
 32  
  * latter case is used when submitting constructing {@link org.apache.tapestry.form.Form forms}.
 33  
  * 
 34  
  * @author Howard Lewis Ship
 35  
  * @since 3.0
 36  
  */
 37  
 
 38  
 public class EngineServiceLink implements ILink
 39  
 {
 40  
     private static final int DEFAULT_HTTP_PORT = 80;
 41  
     
 42  
     private static final int DEFAULT_HTTPS_PORT = 443;
 43  
 
 44  
     private final String _servletPath;
 45  
 
 46  
     private final URLCodec _codec;
 47  
 
 48  
     private IRequestCycle _cycle;
 49  
     
 50  
     private boolean _stateful;
 51  
     
 52  
     private String _encoding;
 53  
 
 54  
     /** @since 4.0 */
 55  
     private final QueryParameterMap _parameters;
 56  
 
 57  
     /** @since 4.0 */
 58  
 
 59  
     private final WebRequest _request;
 60  
     
 61  
     /**
 62  
      * Creates a new EngineServiceLink.
 63  
      * 
 64  
      * @param servletPath
 65  
      *            The path used to invoke the Tapestry servlet.
 66  
      * @param codec
 67  
      *            A codec for converting strings into URL-safe formats.
 68  
      * @param encoding
 69  
      *            The output encoding for the request.
 70  
      * @param parameters
 71  
      *            The query parameters to be encoded into the url. Keys are strings, values are
 72  
      *            null, string or array of string. The map is retained, not copied.
 73  
      * @param stateful
 74  
      *            if true, the service which generated the EngineServiceLink is stateful and expects
 75  
      *            that the final URL will be passed through {@link IRequestCycle#encodeURL(String)}.
 76  
      */
 77  
     
 78  
     public EngineServiceLink(String servletPath, String encoding,
 79  
             URLCodec codec, WebRequest request, QueryParameterMap parameters, boolean stateful)
 80  0
     {
 81  0
         Defense.notNull(servletPath, "servletPath");
 82  0
         Defense.notNull(encoding, "encoding");
 83  0
         Defense.notNull(codec, "codec");
 84  0
         Defense.notNull(request, "request");
 85  0
         Defense.notNull(parameters, "parameters");
 86  
         
 87  0
         _servletPath = servletPath;
 88  0
         _encoding = encoding;
 89  0
         _codec = codec;
 90  0
         _request = request;
 91  0
         _stateful = stateful;
 92  0
         _parameters = parameters;
 93  0
     }
 94  
     
 95  
     /**
 96  
      * Creates a new EngineServiceLink. Primarily used in portlet applications with the
 97  
      * additional {@link IRequestCycle} parameter being used to encode asset urls.
 98  
      * 
 99  
      * @param cycle
 100  
      *            The {@link IRequestCycle}  the EngineServiceLink is to be created for.
 101  
      * @param servletPath
 102  
      *            The path used to invoke the Tapestry servlet.
 103  
      * @param codec
 104  
      *            A codec for converting strings into URL-safe formats.
 105  
      * @param encoding
 106  
      *            The output encoding for the request.
 107  
      * @param parameters
 108  
      *            The query parameters to be encoded into the url. Keys are strings, values are
 109  
      *            null, string or array of string. The map is retained, not copied.
 110  
      * @param stateful
 111  
      *            if true, the service which generated the EngineServiceLink is stateful and expects
 112  
      *            that the final URL will be passed through {@link IRequestCycle#encodeURL(String)}.
 113  
      */
 114  
 
 115  
     public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding,
 116  
             URLCodec codec, WebRequest request, QueryParameterMap parameters, boolean stateful)
 117  0
     {
 118  0
         Defense.notNull(cycle, "cycle");
 119  0
         Defense.notNull(servletPath, "servletPath");
 120  0
         Defense.notNull(encoding, "encoding");
 121  0
         Defense.notNull(codec, "codec");
 122  0
         Defense.notNull(request, "request");
 123  0
         Defense.notNull(parameters, "parameters");
 124  
         
 125  0
         _cycle = cycle;
 126  0
         _servletPath = servletPath;
 127  0
         _encoding = encoding;
 128  0
         _codec = codec;
 129  0
         _request = request;
 130  0
         _stateful = stateful;
 131  0
         _parameters = parameters;
 132  0
     }
 133  
 
 134  
     public String getURL()
 135  
     {
 136  0
         return getURL(null, true);
 137  
     }
 138  
 
 139  
     public String getURL(String anchor, boolean includeParameters)
 140  
     {
 141  0
         return constructURL(new StringBuffer(), anchor, includeParameters);
 142  
     }
 143  
 
 144  
     public String getAbsoluteURL()
 145  
     {
 146  0
         return getAbsoluteURL(null, null, 0, null, true);
 147  
     }
 148  
 
 149  
     public String getURL(String scheme, String server, int port, String anchor,
 150  
             boolean includeParameters)
 151  
     {
 152  0
         boolean useAbsolute = EngineUtils.needAbsoluteURL(scheme, server, port, _request);
 153  
 
 154  0
         return useAbsolute ? getAbsoluteURL(scheme, server, port, anchor, includeParameters)
 155  
                 : getURL(anchor, includeParameters);
 156  
     }
 157  
 
 158  
     public String getAbsoluteURL(String scheme, String server, int port, String anchor,
 159  
             boolean includeParameters)
 160  
     {
 161  0
         StringBuffer buffer = new StringBuffer();
 162  
         
 163  0
         int nport = port == 0 ? _request.getServerPort() : port;
 164  0
         String nscheme = scheme == null ? _request.getScheme() : scheme;
 165  
         
 166  0
         buffer.append(nscheme);
 167  0
         buffer.append("://");
 168  
         
 169  0
         buffer.append(server == null ? _request.getServerName() : server);
 170  
         
 171  0
         if (!(nscheme.equals("http") && nport == DEFAULT_HTTP_PORT) && !(nscheme.equals("https") && nport == DEFAULT_HTTPS_PORT))
 172  
         {
 173  0
             buffer.append(':');
 174  0
             buffer.append(nport);
 175  
         }
 176  
         
 177  
         // Add the servlet path and the rest of the URL & query parameters.
 178  
         // The servlet path starts with a leading slash.
 179  
 
 180  0
         return constructURL(buffer, anchor, includeParameters);
 181  
     }
 182  
 
 183  
     private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters)
 184  
     {
 185  0
         buffer.append(_servletPath);
 186  
 
 187  0
         if (includeParameters)
 188  0
             addParameters(buffer);
 189  
 
 190  0
         if (anchor != null)
 191  
         {
 192  0
             buffer.append('#');
 193  0
             buffer.append(anchor);
 194  
         }
 195  
         
 196  0
         String result = buffer.toString();
 197  
         
 198  
         // TODO: This is somewhat questionable right now, was added in to support TAPESTRY-802
 199  0
         if (_cycle != null && _stateful)
 200  
         {    
 201  0
             result = _cycle.encodeURL(result);
 202  
         }
 203  
         
 204  0
         return result;
 205  
     }
 206  
 
 207  
     private void addParameters(StringBuffer buffer)
 208  
     {
 209  0
         String[] names = getParameterNames();
 210  
 
 211  0
         String sep = "?";
 212  
 
 213  0
         for (int i = 0; i < names.length; i++)
 214  
         {
 215  0
             String name = names[i];
 216  0
             String[] values = getParameterValues(name);
 217  
 
 218  0
             if (values == null)
 219  0
                 continue;
 220  
 
 221  0
             for (int j = 0; j < values.length; j++)
 222  
             {
 223  0
                 buffer.append(sep);
 224  0
                 buffer.append(name);
 225  0
                 buffer.append("=");
 226  0
                 buffer.append(encode(values[j]));
 227  
 
 228  0
                 sep = "&";
 229  
             }
 230  
 
 231  
         }
 232  0
     }
 233  
 
 234  
     private String encode(String value)
 235  
     {
 236  
         try
 237  
         {
 238  0
             return _codec.encode(value, _encoding);
 239  
         }
 240  0
         catch (UnsupportedEncodingException ex)
 241  
         {
 242  0
             throw new ApplicationRuntimeException(Tapestry.format("illegal-encoding", _encoding),
 243  
                     ex);
 244  
         }
 245  
     }
 246  
 
 247  
     public String[] getParameterNames()
 248  
     {
 249  0
         return _parameters.getParameterNames();
 250  
     }
 251  
 
 252  
     public String[] getParameterValues(String name)
 253  
     {
 254  0
         return _parameters.getParameterValues(name);
 255  
     }
 256  
 }