Clover coverage report - Code Coverage for tapestry release 4.0-beta-8
Coverage timestamp: Sat Sep 24 2005 11:50:34 EDT
file stats: LOC: 214   Methods: 10
NCLOC: 124   Classes: 1
 
 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  227 public EngineServiceLink(IRequestCycle cycle, String servletPath, String encoding,
 80    URLCodec codec, WebRequest request, Map parameters, boolean stateful)
 81    {
 82  227 Defense.notNull(cycle, "cycle");
 83  227 Defense.notNull(servletPath, "servletPath");
 84  227 Defense.notNull(encoding, "encoding");
 85  227 Defense.notNull(codec, "codec");
 86  227 Defense.notNull(request, "request");
 87  227 Defense.notNull(parameters, "parameters");
 88   
 89  227 _cycle = cycle;
 90  227 _servletPath = servletPath;
 91  227 _encoding = encoding;
 92  227 _codec = codec;
 93  227 _request = request;
 94  227 _parameters = new QueryParameterMap(parameters);
 95  227 _stateful = stateful;
 96    }
 97   
 98  84 public String getURL()
 99    {
 100  84 return getURL(null, true);
 101    }
 102   
 103  219 public String getURL(String anchor, boolean includeParameters)
 104    {
 105  219 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  223 private String constructURL(StringBuffer buffer, String anchor, boolean includeParameters)
 145    {
 146  223 buffer.append(_servletPath);
 147   
 148  223 if (includeParameters)
 149  178 addParameters(buffer);
 150   
 151  223 if (anchor != null)
 152    {
 153  3 buffer.append('#');
 154  3 buffer.append(anchor);
 155    }
 156   
 157  223 String result = buffer.toString();
 158   
 159  223 if (_stateful)
 160  137 result = _cycle.encodeURL(result);
 161   
 162  223 return result;
 163    }
 164   
 165  178 private void addParameters(StringBuffer buffer)
 166    {
 167  178 String[] names = getParameterNames();
 168   
 169  178 String sep = "?";
 170   
 171  178 for (int i = 0; i < names.length; i++)
 172    {
 173  590 String name = names[i];
 174  590 String[] values = getParameterValues(name);
 175   
 176  590 if (values == null)
 177  134 continue;
 178   
 179  456 for (int j = 0; j < values.length; j++)
 180    {
 181  473 buffer.append(sep);
 182  473 buffer.append(name);
 183  473 buffer.append("=");
 184  473 buffer.append(encode(values[j]));
 185   
 186  473 sep = "&";
 187    }
 188   
 189    }
 190    }
 191   
 192  473 private String encode(String value)
 193    {
 194  473 try
 195    {
 196  473 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  226 public String[] getParameterNames()
 206    {
 207  226 return _parameters.getParameterNames();
 208    }
 209   
 210  872 public String[] getParameterValues(String name)
 211    {
 212  872 return _parameters.getParameterValues(name);
 213    }
 214    }