Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 214   Methods: 10
NCLOC: 124   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
EngineServiceLink.java 100% 98.3% 100% 98.9%
coverage coverage
 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 java.io.UnsupportedEncodingException;
 18   
 import java.util.Map;
 19   
 
 20   
 import org.apache.commons.codec.net.URLCodec;
 21   
 import org.apache.hivemind.ApplicationRuntimeException;
 22   
 import org.apache.hivemind.util.Defense;
 23   
 import org.apache.tapestry.IRequestCycle;
 24   
 import org.apache.tapestry.Tapestry;
 25   
 import org.apache.tapestry.util.QueryParameterMap;
 26   
 import org.apache.tapestry.web.WebRequest;
 27   
 
 28   
 /**
 29   
  * A EngineServiceLink represents a possible action within the client web browser; either clicking a
 30   
  * link or submitting a form, which is constructed primarily from the servlet path, with some
 31   
  * additional query parameters. A full URL for the EngineServiceLink can be generated, or the query
 32   
  * parameters for the EngineServiceLink can be extracted (separately from the servlet path). The
 33   
  * latter case is used when submitting constructing {@link org.apache.tapestry.form.Form forms}.
 34   
  * 
 35   
  * @author Howard Lewis Ship
 36   
  * @since 3.0
 37   
  */
 38   
 
 39   
 public class EngineServiceLink implements ILink
 40   
 {
 41   
     private static final int DEFAULT_HTTP_PORT = 80;
 42   
 
 43   
     private final IRequestCycle _cycle;
 44   
 
 45   
     private final String _servletPath;
 46   
 
 47   
     private final URLCodec _codec;
 48   
 
 49   
     private String _encoding;
 50   
 
 51   
     private boolean _stateful;
 52   
 
 53   
     /** @since 4.0 */
 54   
     private final QueryParameterMap _parameters;
 55   
 
 56   
     /** @since 4.0 */
 57   
 
 58   
     private final WebRequest _request;
 59   
 
 60   
     /**
 61   
      * Creates a new EngineServiceLink.
 62   
      * 
 63   
      * @param cycle
 64   
      *            The {@link IRequestCycle}  the EngineServiceLink is to be created for.
 65   
      * @param servletPath
 66   
      *            The path used to invoke the Tapestry servlet.
 67   
      * @param codec
 68   
      *            A codec for converting strings into URL-safe formats.
 69   
      * @param encoding
 70   
      *            The output encoding for the request.
 71   
      * @param parameters
 72   
      *            The query parameters to be encoded into the url. Keys are strings, values are
 73   
      *            null, string or array of string. The map is retained, not copied.
 74   
      * @param stateful
 75   
      *            if true, the service which generated the EngineServiceLink is stateful and expects
 76   
      *            that the final URL will be passed through {@link IRequestCycle#encodeURL(String)}.
 77   
      */
 78   
 
 79  230
     public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding,
 80   
             URLCodec codec, WebRequest request, Map parameters, boolean stateful)
 81   
     {
 82  230
         Defense.notNull(cycle, "cycle");
 83  230
         Defense.notNull(servletPath, "servletPath");
 84  230
         Defense.notNull(encoding, "encoding");
 85  230
         Defense.notNull(codec, "codec");
 86  230
         Defense.notNull(request, "request");
 87  230
         Defense.notNull(parameters, "parameters");
 88   
 
 89  230
         _cycle = cycle;
 90  230
         _servletPath = servletPath;
 91  230
         _encoding = encoding;
 92  230
         _codec = codec;
 93  230
         _request = request;
 94  230
         _parameters = new QueryParameterMap(parameters);
 95  230
         _stateful = stateful;
 96   
     }
 97   
 
 98  65
     public String getURL()
 99   
     {
 100  65
         return getURL(null, true);
 101   
     }
 102   
 
 103  221
     public String getURL(String anchor, boolean includeParameters)
 104   
     {
 105  221
         return constructURL(new StringBuffer(), anchor, includeParameters);
 106   
     }
 107   
 
 108  1
     public String getAbsoluteURL()
 109   
     {
 110  1
         return getAbsoluteURL(null, null, 0, null, true);
 111   
     }
 112   
 
 113  4
     public String getAbsoluteURL(String scheme, String server, int port, String anchor,
 114   
             boolean includeParameters)
 115   
     {
 116  4
         StringBuffer buffer = new StringBuffer();
 117   
 
 118  4
         if (scheme == null)
 119  2
             scheme = _request.getScheme();
 120   
 
 121  4
         buffer.append(scheme);
 122  4
         buffer.append("://");
 123   
 
 124  4
         if (server == null)
 125  2
             server = _request.getServerName();
 126   
 
 127  4
         buffer.append(server);
 128   
 
 129  4
         if (port == 0)
 130  2
             port = _request.getServerPort();
 131   
 
 132  4
         if (!(scheme.equals("http") && port == DEFAULT_HTTP_PORT))
 133   
         {
 134  3
             buffer.append(':');
 135  3
             buffer.append(port);
 136   
         }
 137   
 
 138   
         // Add the servlet path and the rest of the URL & query parameters.
 139   
         // The servlet path starts with a leading slash.
 140   
 
 141  4
         return constructURL(buffer, anchor, includeParameters);
 142   
     }
 143   
 
 144  225
     private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters)
 145   
     {
 146  225
         buffer.append(_servletPath);
 147   
 
 148  225
         if (includeParameters)
 149  166
             addParameters(buffer);
 150   
 
 151  225
         if (anchor != null)
 152   
         {
 153  3
             buffer.append('#');
 154  3
             buffer.append(anchor);
 155   
         }
 156   
 
 157  225
         String result = buffer.toString();
 158   
 
 159  225
         if (_stateful)
 160  158
             result = _cycle.encodeURL(result);
 161   
 
 162  225
         return result;
 163   
     }
 164   
 
 165  166
     private void addParameters(StringBuffer buffer)
 166   
     {
 167  166
         String[] names = getParameterNames();
 168   
 
 169  166
         String sep = "?";
 170   
 
 171  166
         for (int i = 0; i < names.length; i++)
 172   
         {
 173  570
             String name = names[i];
 174  570
             String[] values = getParameterValues(name);
 175   
 
 176  570
             if (values == null)
 177  150
                 continue;
 178   
 
 179  420
             for (int j = 0; j < values.length; j++)
 180   
             {
 181  437
                 buffer.append(sep);
 182  437
                 buffer.append(name);
 183  437
                 buffer.append("=");
 184  437
                 buffer.append(encode(values[j]));
 185   
 
 186  437
                 sep = "&";
 187   
             }
 188   
 
 189   
         }
 190   
     }
 191   
 
 192  437
     private String encode(String value)
 193   
     {
 194  437
         try
 195   
         {
 196  437
             return _codec.encode(value, _encoding);
 197   
         }
 198   
         catch (UnsupportedEncodingException ex)
 199   
         {
 200  0
             throw new ApplicationRuntimeException(Tapestry.format("illegal-encoding", _encoding),
 201   
                     ex);
 202   
         }
 203   
     }
 204   
 
 205  229
     public String[] getParameterNames()
 206   
     {
 207  229
         return _parameters.getParameterNames();
 208   
     }
 209   
 
 210  942
     public String[] getParameterValues(String name)
 211   
     {
 212  942
         return _parameters.getParameterValues(name);
 213   
     }
 214   
 }