Clover coverage report - Code Coverage for tapestry release 4.0-beta-12
Coverage timestamp: Sun Oct 30 2005 16:22:01 EST
file stats: LOC: 223   Methods: 11
NCLOC: 131   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
EngineServiceLink.java 95.5% 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   
 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  200 public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding,
 80    URLCodec codec, WebRequest request, Map parameters, boolean stateful)
 81    {
 82  200 Defense.notNull(cycle, "cycle");
 83  200 Defense.notNull(servletPath, "servletPath");
 84  200 Defense.notNull(encoding, "encoding");
 85  200 Defense.notNull(codec, "codec");
 86  200 Defense.notNull(request, "request");
 87  200 Defense.notNull(parameters, "parameters");
 88   
 89  200 _cycle = cycle;
 90  200 _servletPath = servletPath;
 91  200 _encoding = encoding;
 92  200 _codec = codec;
 93  200 _request = request;
 94  200 _parameters = new QueryParameterMap(parameters);
 95  200 _stateful = stateful;
 96    }
 97   
 98  80 public String getURL()
 99    {
 100  80 return getURL(null, true);
 101    }
 102   
 103  193 public String getURL(String anchor, boolean includeParameters)
 104    {
 105  193 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  111 public String getURL(String scheme, String server, int port, String anchor,
 114    boolean includeParameters)
 115    {
 116  111 boolean useAbsolute = EngineUtils.needAbsoluteURL(scheme, server, port, _request);
 117   
 118  111 return useAbsolute ? getAbsoluteURL(scheme, server, port, anchor, includeParameters)
 119    : getURL(anchor, includeParameters);
 120    }
 121   
 122  3 public String getAbsoluteURL(String scheme, String server, int port, String anchor,
 123    boolean includeParameters)
 124    {
 125  3 StringBuffer buffer = new StringBuffer();
 126   
 127  3 if (scheme == null)
 128  1 scheme = _request.getScheme();
 129   
 130  3 buffer.append(scheme);
 131  3 buffer.append("://");
 132   
 133  3 if (server == null)
 134  1 server = _request.getServerName();
 135   
 136  3 buffer.append(server);
 137   
 138  3 if (port == 0)
 139  1 port = _request.getServerPort();
 140   
 141  3 if (!(scheme.equals("http") && port == DEFAULT_HTTP_PORT))
 142    {
 143  3 buffer.append(':');
 144  3 buffer.append(port);
 145    }
 146   
 147    // Add the servlet path and the rest of the URL & query parameters.
 148    // The servlet path starts with a leading slash.
 149   
 150  3 return constructURL(buffer, anchor, includeParameters);
 151    }
 152   
 153  196 private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters)
 154    {
 155  196 buffer.append(_servletPath);
 156   
 157  196 if (includeParameters)
 158  151 addParameters(buffer);
 159   
 160  196 if (anchor != null)
 161    {
 162  4 buffer.append('#');
 163  4 buffer.append(anchor);
 164    }
 165   
 166  196 String result = buffer.toString();
 167   
 168  196 if (_stateful)
 169  112 result = _cycle.encodeURL(result);
 170   
 171  196 return result;
 172    }
 173   
 174  151 private void addParameters(StringBuffer buffer)
 175    {
 176  151 String[] names = getParameterNames();
 177   
 178  151 String sep = "?";
 179   
 180  151 for (int i = 0; i < names.length; i++)
 181    {
 182  452 String name = names[i];
 183  452 String[] values = getParameterValues(name);
 184   
 185  452 if (values == null)
 186  73 continue;
 187   
 188  379 for (int j = 0; j < values.length; j++)
 189    {
 190  396 buffer.append(sep);
 191  396 buffer.append(name);
 192  396 buffer.append("=");
 193  396 buffer.append(encode(values[j]));
 194   
 195  396 sep = "&";
 196    }
 197   
 198    }
 199    }
 200   
 201  396 private String encode(String value)
 202    {
 203  396 try
 204    {
 205  396 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  199 public String[] getParameterNames()
 215    {
 216  199 return _parameters.getParameterNames();
 217    }
 218   
 219  734 public String[] getParameterValues(String name)
 220    {
 221  734 return _parameters.getParameterValues(name);
 222    }
 223    }