Clover coverage report - Code Coverage for tapestry release 4.0.3
Coverage timestamp: Fri May 5 2006 21:17:42 CDT
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  597 public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding,
 81    URLCodec codec, WebRequest request, Map parameters, boolean stateful)
 82    {
 83  597 Defense.notNull(cycle, "cycle");
 84  597 Defense.notNull(servletPath, "servletPath");
 85  597 Defense.notNull(encoding, "encoding");
 86  597 Defense.notNull(codec, "codec");
 87  597 Defense.notNull(request, "request");
 88  597 Defense.notNull(parameters, "parameters");
 89   
 90  597 _cycle = cycle;
 91  597 _servletPath = servletPath;
 92  597 _encoding = encoding;
 93  597 _codec = codec;
 94  597 _request = request;
 95  597 _parameters = new QueryParameterMap(parameters);
 96  597 _stateful = stateful;
 97    }
 98   
 99  240 public String getURL()
 100    {
 101  240 return getURL(null, true);
 102    }
 103   
 104  576 public String getURL(String anchor, boolean includeParameters)
 105    {
 106  576 return constructURL(new StringBuffer(), anchor, includeParameters);
 107    }
 108   
 109  3 public String getAbsoluteURL()
 110    {
 111  3 return getAbsoluteURL(null, null, 0, null, true);
 112    }
 113   
 114  330 public String getURL(String scheme, String server, int port, String anchor,
 115    boolean includeParameters)
 116    {
 117  330 boolean useAbsolute = EngineUtils.needAbsoluteURL(scheme, server, port, _request);
 118   
 119  330 return useAbsolute ? getAbsoluteURL(scheme, server, port, anchor, includeParameters)
 120    : getURL(anchor, includeParameters);
 121    }
 122   
 123  9 public String getAbsoluteURL(String scheme, String server, int port, String anchor,
 124    boolean includeParameters)
 125    {
 126  9 StringBuffer buffer = new StringBuffer();
 127   
 128  9 if (scheme == null)
 129  3 scheme = _request.getScheme();
 130   
 131  9 buffer.append(scheme);
 132  9 buffer.append("://");
 133   
 134  9 if (server == null)
 135  3 server = _request.getServerName();
 136   
 137  9 buffer.append(server);
 138   
 139  9 if (port == 0)
 140  3 port = _request.getServerPort();
 141   
 142  9 if (!(scheme.equals("http") && port == DEFAULT_HTTP_PORT))
 143    {
 144  9 buffer.append(':');
 145  9 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  9 return constructURL(buffer, anchor, includeParameters);
 152    }
 153   
 154  585 private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters)
 155    {
 156  585 buffer.append(_servletPath);
 157   
 158  585 if (includeParameters)
 159  450 addParameters(buffer);
 160   
 161  585 if (anchor != null)
 162    {
 163  12 buffer.append('#');
 164  12 buffer.append(anchor);
 165    }
 166   
 167  585 String result = buffer.toString();
 168   
 169  585 result = _cycle.encodeURL(result);
 170   
 171  585 return result;
 172    }
 173   
 174  450 private void addParameters(StringBuffer buffer)
 175    {
 176  450 String[] names = getParameterNames();
 177   
 178  450 String sep = "?";
 179   
 180  450 for (int i = 0; i < names.length; i++)
 181    {
 182  1353 String name = names[i];
 183  1353 String[] values = getParameterValues(name);
 184   
 185  1353 if (values == null)
 186  219 continue;
 187   
 188  1134 for (int j = 0; j < values.length; j++)
 189    {
 190  1185 buffer.append(sep);
 191  1185 buffer.append(name);
 192  1185 buffer.append("=");
 193  1185 buffer.append(encode(values[j]));
 194   
 195  1185 sep = "&";
 196    }
 197   
 198    }
 199    }
 200   
 201  1185 private String encode(String value)
 202    {
 203  1185 try
 204    {
 205  1185 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  594 public String[] getParameterNames()
 215    {
 216  594 return _parameters.getParameterNames();
 217    }
 218   
 219  2199 public String[] getParameterValues(String name)
 220    {
 221  2199 return _parameters.getParameterValues(name);
 222    }
 223    }