Clover coverage report - Ledge Web - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:20:55 CET
file stats: LOC: 1,411   Methods: 66
NCLOC: 811   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
LinkTool.java 78.7% 82.3% 86.4% 82.1%
coverage coverage
 1    //
 2    //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 3    //All rights reserved.
 4    //
 5    //Redistribution and use in source and binary forms, with or without modification,
 6    //are permitted provided that the following conditions are met:
 7    //
 8    //* Redistributions of source code must retain the above copyright notice,
 9    //this list of conditions and the following disclaimer.
 10    //* Redistributions in binary form must reproduce the above copyright notice,
 11    //this list of conditions and the following disclaimer in the documentation
 12    //and/or other materials provided with the distribution.
 13    //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 14    //nor the names of its contributors may be used to endorse or promote products
 15    //derived from this software without specific prior written permission.
 16    //
 17    //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 18    //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19    //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 20    //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 21    //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 22    //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 23    //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 24    //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25    //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26    //POSSIBILITY OF SUCH DAMAGE.
 27    //
 28   
 29    package org.objectledge.web.mvc.tools;
 30   
 31    import java.io.UnsupportedEncodingException;
 32    import java.net.URLDecoder;
 33    import java.net.URLEncoder;
 34    import java.util.Enumeration;
 35    import java.util.HashMap;
 36    import java.util.HashSet;
 37    import java.util.Iterator;
 38    import java.util.List;
 39    import java.util.Map;
 40    import java.util.Set;
 41    import java.util.StringTokenizer;
 42   
 43    import org.jcontainer.dna.Configurable;
 44    import org.jcontainer.dna.ConfigurationException;
 45    import org.objectledge.ComponentInitializationError;
 46    import org.objectledge.parameters.Parameters;
 47    import org.objectledge.parameters.RequestParameters;
 48    import org.objectledge.parameters.SortedParameters;
 49    import org.objectledge.utils.StringUtils;
 50    import org.objectledge.web.HttpContext;
 51    import org.objectledge.web.WebConfigurator;
 52    import org.objectledge.web.mvc.MVCContext;
 53   
 54    /**
 55    * Context tool used to build web application links. It works in a pull manner. The template
 56    * designer provides instances of <code>LinkTool</code> with all the necessary parameters,
 57    * the <code>LinkTool</code> itself is responsible for generation of a proper URL string based on
 58    * this parameters. An example of <code>LinkTool</code> usage in Velocity template:
 59    *
 60    * <h4>template</h4>
 61    * <pre>
 62    * $link.view('somepackage.SomeView').action('somepackage.SomeAction').set('paramName','paramValue').fragment('this_line')
 63    * </pre>
 64    *
 65    * <h4>output</h4>
 66    * <pre>
 67    * /context/servlet/view/somepackage.SomeView?action=somepackage.SomeAction&amp;paramName=paramValue#this_line
 68    * </pre>
 69    *
 70    * <p>The links are encoded using UTF-8 encoding no matter what encoding is used in the generated
 71    * HTML page. This is in line with JavaScript URI encoding and decoding functions.</p>
 72    *
 73    * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
 74    * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
 75    * @version $Id: LinkTool.java,v 1.33 2006/04/24 13:35:50 zwierzem Exp $
 76    */
 77    public class LinkTool
 78    {
 79    /** utf encoding. */
 80    public static final String PARAMETER_ENCODING = "UTF-8";
 81   
 82    /** query string parameter values and content paths encoder. */
 83    private static final org.objectledge.encodings.URLEncoder URL_ENCODER =
 84    new org.objectledge.encodings.URLEncoder();
 85   
 86    /** configuration. */
 87    protected LinkTool.Configuration config;
 88   
 89    /** the http context. */
 90    protected HttpContext httpContext;
 91   
 92    /** the mvc context. */
 93    protected MVCContext mvcContext;
 94   
 95    /** the request parameters. */
 96    protected RequestParameters requestParameters;
 97   
 98    /** view, null for current request parameters view value, empty string for unset value */
 99    private String view;
 100   
 101    /** the action, empty string for unset value */
 102    private String action;
 103   
 104    /** is content link switch */
 105    private boolean contentLink;
 106   
 107    /** include session information */
 108    private boolean includeSession;
 109   
 110    /** show the protocol in link */
 111    private boolean showProtocolName;
 112   
 113    /** protocol name */
 114    private String protocolName;
 115   
 116    /** host name */
 117    private String host;
 118   
 119    /** the port */
 120    private int port;
 121   
 122    /** the content path */
 123    private String path;
 124   
 125    /** the special path info suffix */
 126    private String pathInfoSuffix;
 127   
 128    /** the fragment part of the url */
 129    private String fragment;
 130   
 131    /** the parameters */
 132    private Parameters parameters;
 133   
 134    /** the string buffer */
 135    private StringBuilder sb;
 136   
 137    /**
 138    * Tool constructor.
 139    *
 140    * @param httpContext the http context.
 141    * @param mvcContext the mvc context.
 142    * @param requestParameters the request parameters.
 143    * @param config the link tool configuraiton.
 144    */
 145  4186 public LinkTool(HttpContext httpContext, MVCContext mvcContext,
 146    RequestParameters requestParameters, LinkTool.Configuration config)
 147    {
 148  4186 this.config = config;
 149  4186 this.httpContext = httpContext;
 150  4186 this.mvcContext = mvcContext;
 151  4186 this.requestParameters = requestParameters;
 152  4186 contentLink = false;
 153  4186 includeSession = true;
 154  4186 showProtocolName = false;
 155  4186 protocolName = "";
 156  4186 port = 0;
 157  4186 host = null;
 158    // this means no override for view
 159  4186 view = null;
 160  4186 action = "";
 161  4186 pathInfoSuffix = null;
 162  4186 fragment = null;
 163  4186 if(config.stickyParameterNames.size() > 0)
 164    {
 165  3680 parameters = new SortedParameters(requestParameters);
 166  3680 parameters.removeExcept(config.stickyParameterNames);
 167    }
 168    else
 169    {
 170  506 parameters = new SortedParameters();
 171    }
 172    }
 173   
 174    /**
 175    * Set the protocol to http with default port on 80.
 176    *
 177    * @return the link tool.
 178    */
 179  46 public LinkTool http()
 180    {
 181  46 return http(80);
 182    }
 183   
 184    /**
 185    * Set the protocol to http with specified port.
 186    *
 187    * @param port the port.
 188    * @return the link tool.
 189    */
 190  92 public LinkTool http(int port)
 191    {
 192  92 LinkTool target = getLinkTool(this);
 193  92 target.showProtocolName = true;
 194  92 target.protocolName = "http";
 195  92 target.port = port;
 196  92 return target;
 197    }
 198   
 199    /**
 200    * Set the protocol to https with default port on 443.
 201    *
 202    * @return the link tool.
 203    */
 204  92 public LinkTool https()
 205    {
 206  92 return https(443);
 207    }
 208   
 209    /**
 210    * Set the protocol to https with specified port.
 211    *
 212    * @param port the port.
 213    * @return the link tool.
 214    */
 215  138 public LinkTool https(int port)
 216    {
 217  138 LinkTool target = getLinkTool(this);
 218  138 target.showProtocolName = true;
 219  138 target.protocolName = "https";
 220  138 target.port = port;
 221  138 return target;
 222    }
 223   
 224    /**
 225    * Generate an absolute link including protocol name (schema), server name and port number.
 226    *
 227    * @return the link tool.
 228    */
 229  138 public LinkTool absolute()
 230    {
 231  138 LinkTool target = getLinkTool(this);
 232  138 target.showProtocolName = true;
 233  138 if(target.port == 0)
 234    {
 235  92 target.port = httpContext.getRequest().getServerPort();
 236    }
 237  138 if(target.protocolName == null || target.protocolName.length()==0)
 238    {
 239  92 if(httpContext.getRequest().isSecure())
 240    {
 241  46 target.protocolName = "https";
 242    }
 243    else
 244    {
 245  46 target.protocolName = "http";
 246    }
 247    }
 248  138 return target;
 249    }
 250   
 251    /**
 252    * Geneate an absolute link to another host.
 253    *
 254    * @param host the host domain name.
 255    * @return the link tool.
 256    */
 257  0 public LinkTool host(String host)
 258    {
 259  0 LinkTool target = absolute();
 260  0 target.host = host;
 261  0 return target;
 262    }
 263   
 264    /**
 265    * Avoid session information in link - useful for content served using external HTTP server.
 266    *
 267    * @return the link tool.
 268    */
 269  598 public LinkTool sessionless()
 270    {
 271  598 LinkTool target = getLinkTool(this);
 272  598 target.includeSession = false;
 273  598 return target;
 274    }
 275   
 276    /**
 277    * Set link to point to the content stored in <code>/</code> directory of servlet context.
 278    * May be used to generate relative content paths (not recommended).
 279    *
 280    * @param path the path to content.
 281    * @return the link tool.
 282    */
 283  322 public LinkTool rootContent(String path)
 284    {
 285  322 LinkTool target = getLinkTool(this);
 286  322 target.contentLink = true;
 287  322 target.includeSession = this.includeSession && !config.externalContent;
 288  322 target.path = path;
 289  322 return target;
 290    }
 291   
 292    /**
 293    * Set link to point to the content stored in configured content (<code>/content</code>)
 294    * directory of servlet context.
 295    *
 296    * @param path the relative path to content.
 297    * @return the link tool.
 298    */
 299  322 public LinkTool content(String path)
 300    {
 301  322 if (path.length() == 0)
 302    {
 303  46 path = config.baseContentPath;
 304    }
 305  276 else if (path.charAt(0) != '/')
 306    {
 307  230 path = config.baseContentPath + '/' + path;
 308    }
 309    else
 310    {
 311  46 path = config.baseContentPath + path;
 312    }
 313  322 return rootContent(path);
 314    }
 315   
 316    // parameter set methods ----------------------------------------------------------------------
 317   
 318    /**
 319    * Sets a request parameter, replacing previously set value.
 320    * Unless configured differently it will be rendered in the link as query string parameter.
 321    *
 322    * @param name the name of the parameter.
 323    * @param value the value of the parameter.
 324    * @return the link tool.
 325    */
 326  276 public LinkTool set(String name, String value)
 327    {
 328  276 LinkTool target = getSetTargetLinkTool(name);
 329  276 target.parameters.set(name, value);
 330  276 return target;
 331    }
 332   
 333    /**
 334    * Sets a request parameter, replacing previously set value.
 335    * Unless configured differently it will be rendered in the link as query string parameter.
 336    *
 337    * @param name the name of the parameter.
 338    * @param value the value of the parameter.
 339    * @return the link tool.
 340    */
 341  46 public LinkTool set(String name, int value)
 342    {
 343  46 LinkTool target = getSetTargetLinkTool(name);
 344  46 target.parameters.set(name, value);
 345  46 return target;
 346    }
 347   
 348    /**
 349    * Sets a request parameter, replacing previously set value.
 350    * Unless configured differently it will be rendered in the link as query string parameter.
 351    *
 352    * @param name the name of the parameter.
 353    * @param value the value of the parameter.
 354    * @return the link tool.
 355    */
 356  46 public LinkTool set(String name, long value)
 357    {
 358  46 LinkTool target = getSetTargetLinkTool(name);
 359  46 target.parameters.set(name, value);
 360  46 return target;
 361    }
 362   
 363    /**
 364    * Sets a request parameter, replacing previously set value.
 365    * Unless configured differently it will be rendered in the link as query string parameter.
 366    *
 367    * @param name the name of the parameter.
 368    * @param value the value of the parameter.
 369    * @return the link tool.
 370    */
 371  46 public LinkTool set(String name, float value)
 372    {
 373  46 LinkTool target = getSetTargetLinkTool(name);
 374  46 target.parameters.set(name, value);
 375  46 return target;
 376    }
 377   
 378    /**
 379    * Sets a request parameter, replacing previously set value.
 380    * Unless configured differently it will be rendered in the link as query string parameter.
 381    *
 382    * @param name the name of the parameter.
 383    * @param value the value of the parameter.
 384    * @return the link tool.
 385    */
 386  46 public LinkTool set(String name, boolean value)
 387    {
 388  46 LinkTool target = getSetTargetLinkTool(name);
 389  46 target.parameters.set(name, value);
 390  46 return target;
 391    }
 392   
 393    /**
 394    * Sets multiple values of a parameter using a {@link java.util.List}.
 395    *
 396    * @param name the name of the parameter.
 397    * @param list a list of parameter values.
 398    * @return the link tool.
 399    */
 400  0 public LinkTool set(String name, List<String> list)
 401    {
 402  0 LinkTool target = getSetTargetLinkTool(name);
 403  0 target.parameters.remove(name);
 404  0 for (String value : list)
 405    {
 406  0 target.parameters.add(name, value);
 407    }
 408  0 return target;
 409    }
 410   
 411    /**
 412    * Adds a set of parameters defined as a {@link java.util.Map} to the request.
 413    * Removes old values of the parameters.
 414    *
 415    * @param map a set of parametres as a Map.
 416    * @return the link tool.
 417    */
 418  0 public LinkTool set(Map<String, String> map)
 419    {
 420  0 if (map.containsKey(config.viewToken))
 421    {
 422  0 checkSetParamName(config.viewToken);
 423    }
 424  0 else if (map.containsKey(config.actionToken))
 425    {
 426  0 checkSetParamName(config.actionToken);
 427    }
 428  0 LinkTool target = getLinkTool(this);
 429  0 for (Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator(); iter.hasNext();)
 430    {
 431  0 Map.Entry<String, String> e = iter.next();
 432  0 target.parameters.remove(e.getKey());
 433  0 target.parameters.add(e.getKey(), e.getValue());
 434    }
 435  0 return target;
 436    }
 437   
 438   
 439   
 440    /**
 441    * Sets the request parameters to be equal to contents of the specified
 442    * parameter container.
 443    *
 444    * @param parameters a set of paramters
 445    * @return the link tool.
 446    */
 447  276 public LinkTool set(Parameters parameters)
 448    {
 449  276 if (parameters.isDefined(config.viewToken))
 450    {
 451  46 checkSetParamName(config.viewToken);
 452    }
 453  230 else if (parameters.isDefined(config.actionToken))
 454    {
 455  46 checkSetParamName(config.actionToken);
 456    }
 457  184 LinkTool target = getLinkTool(this);
 458  184 target.parameters = new SortedParameters(parameters);
 459  184 return target;
 460    }
 461   
 462    /**
 463    * Checks the name of the set parameter and returns target link tool.
 464    * Method created for code reuse.
 465    *
 466    * @param paramName checked name of the set parameter.
 467    * @return target link tool
 468    */
 469  460 private LinkTool getSetTargetLinkTool(String paramName)
 470    {
 471  460 checkSetParamName(paramName);
 472  460 return getLinkTool(this);
 473    }
 474   
 475    // --------------------------------------------------------------------------------------------
 476   
 477    /**
 478    * Sets the path info suffix for this link.
 479    * May be used for file download links.
 480    *
 481    * @param pathInfoSuffix the path info suffix.
 482    * @return the link tool.
 483    */
 484  0 public LinkTool pathInfoSuffix(String pathInfoSuffix)
 485    {
 486  0 LinkTool target = getLinkTool(this);
 487  0 target.pathInfoSuffix = pathInfoSuffix;
 488  0 return target;
 489    }
 490