Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 223   Methods: 11
NCLOC: 131   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
EngineServiceLink.java 95% 98.3% 100% 97.8%
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    private static final int DEFAULT_HTTPS_PORT = 443;
 43   
 44    private final IRequestCycle _cycle;
 45   
 46    private final String _servletPath;
 47   
 48    private final URLCodec _codec;
 49   
 50    private String _encoding;
 51   
 52    private boolean _stateful;
 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 cycle
 65    * The {@link IRequestCycle}  the EngineServiceLink is to be created for.
 66    * @param servletPath
 67    * The path used to invoke the Tapestry servlet.
 68    * @param codec
 69    * A codec for converting strings into URL-safe formats.
 70    * @param encoding
 71    * The output encoding for the request.
 72    * @param parameters
 73    * The query parameters to be encoded into the url. Keys are strings, values are
 74    * null, string or array of string. The map is retained, not copied.
 75    * @param stateful
 76    * if true, the service which generated the EngineServiceLink is stateful and expects
 77    * that the final URL will be passed through {@link IRequestCycle#encodeURL(String)}.
 78    */
 79   
 80  398 public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding,
 81    URLCodec codec, WebRequest request, Map parameters, boolean stateful)
 82    {
 83  398 Defense.notNull(cycle, "cycle");
 84  398 Defense.notNull(servletPath, "servletPath");
 85  398 Defense.notNull(encoding, "encoding");
 86  398 Defense.notNull(codec, "codec");
 87  398 Defense.notNull(request, "request");
 88  398 Defense.notNull(parameters, "parameters");
 89   
 90  398 _cycle = cycle;
 91  398 _servletPath = servletPath;
 92  398 _encoding = encoding;
 93  398 _codec = codec;
 94  398 _request = request;
 95  398 _parameters = new QueryParameterMap(parameters);
 96  398 _stateful = stateful;
 97    }
 98   
 99  160 public String getURL()
 100    {
 101  160 return getURL(null, true);
 102    }
 103   
 104  384 public String getURL(String anchor, boolean includeParameters)
 105    {
 106  384 return constructURL(new StringBuffer(), anchor, includeParameters);
 107    }
 108   
 109  2 public String getAbsoluteURL()
 110    {
 111  2 return getAbsoluteURL(null, null, 0, null, true);
 112    }
 113   
 114  220 public String getURL(String scheme, String server, int port, String anchor,
 115    boolean includeParameters)
 116    {
 117  220 boolean useAbsolute = EngineUtils.needAbsoluteURL(scheme, server, port, _request);
 118   
 119  220 return useAbsolute ? getAbsoluteURL(scheme, server, port, anchor, includeParameters)
 120    : getURL(anchor, includeParameters);
 121    }
 122   
 123  6 public String getAbsoluteURL(String scheme, String server, int port, String anchor,
 124    boolean includeParameters)
 125    {
 126  6 StringBuffer buffer = new StringBuffer();
 127   
 128  6 if (scheme == null)
 129  2 scheme = _request.getScheme();
 130   
 131  6 buffer.append(scheme);
 132  6 buffer.append("://");
 133   
 134  6 if (server == null)
 135  2 server = _request.getServerName();
 136   
 137  6 buffer.append(server);
 138   
 139  6 if (port == 0)
 140  2 port = _request.getServerPort();
 141   
 142  6 if (!(scheme.equals("http") && port == DEFAULT_HTTP_PORT))
 143    {
 144  6 buffer.append(':');
 145  6 buffer.append(port);
 146    }
 147   
 148    // Add the servlet path and the rest of the URL & query parameters.
 149    // The servlet path starts with a leading slash.
 150   
 151  6 return constructURL(buffer, anchor, includeParameters);
 152    }
 153   
 154  390 private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters)
 155    {
 156  390 buffer.append(_servletPath);
 157   
 158  390 if (includeParameters)
 159  300 addParameters(buffer);
 160   
 161  390 if (anchor != null)
 162    {
 163  8 buffer.append('#');
 164  8 buffer.append(anchor);
 165    }
 166   
 167  390 String result = buffer.toString();
 168   
 169  390 result = _cycle.encodeURL(result);
 170   
 171  390 return result;
 172    }
 173   
 174  300 private void addParameters(StringBuffer buffer)
 175    {
 176  300 String[] names = getParameterNames();
 177   
 178  300 String sep = "?";
 179   
 180  300 for (int i = 0; i < names.length; i++)
 181    {
 182  902 String name = names[i];
 183  902 String[] values = getParameterValues(name);
 184   
 185  902 if (values == null)
 186  146 continue;
 187   
 188  756 for (int j = 0; j < values.length; j++)
 189    {
 190  790 buffer.append(sep);
 191  790 buffer.append(name);
 192  790 buffer.append("=");
 193  790 buffer.append(encode(values[j]));
 194   
 195  790 sep = "&";
 196    }
 197   
 198    }
 199    }
 200   
 201  790 private String encode(String value)
 202    {
 203  790 try
 204    {
 205  790 return _codec.encode(value, _encoding);
 206    }
 207    catch (UnsupportedEncodingException ex)
 208    {
 209  0 throw new ApplicationRuntimeException(Tapestry.format("illegal-encoding", _encoding),
 210    ex);
 211    }
 212    }
 213   
 214  396 public String[] getParameterNames()
 215    {
 216  396 return _parameters.getParameterNames();
 217    }
 218   
 219  1466 public String[] getParameterValues(String name)
 220    {
 221  1466 return _parameters.getParameterValues(name);
 222    }
 223    }