Coverage Report - org.kuali.rice.krad.util.UrlFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
UrlFactory
0%
0/32
0%
0/12
8
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.krad.util;
 17  
 
 18  
 import org.apache.commons.codec.EncoderException;
 19  
 import org.apache.commons.codec.net.URLCodec;
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.apache.log4j.Logger;
 22  
 
 23  
 import java.util.Properties;
 24  
 
 25  
 /**
 26  
  * This class Provides utility methods for re/building URLs.
 27  
  * 
 28  
  * 
 29  
  */
 30  
 
 31  0
 public class UrlFactory {
 32  0
     private static Logger LOG = Logger.getLogger(UrlFactory.class);
 33  
     /**
 34  
      * Creates a new URL by taking the given URL and appending the parameter names and values from the given Properties instance to
 35  
      * it. Note: parameter names must be non-blank; parameter values must be non-null.
 36  
      * 
 37  
      * @param baseUrl the URL string used as the basis for reconstruction
 38  
      * @param params Properties instance containing the desired parameters and their values
 39  
      * @throws IllegalArgumentException if the given url is null or empty
 40  
      * @throws IllegalArgumentException if the given Properties instance is null
 41  
      * @throws IllegalArgumentException if a parameter name is null or empty, or a parameter value is null
 42  
      * @throws RuntimeException if there is a problem encoding a parameter name or value into UTF-8
 43  
      * @return a newly-constructed URL string which has the given parameters and their values appended to it
 44  
      */
 45  0
     private static URLCodec urlCodec = new URLCodec("UTF-8");
 46  
     
 47  
     public static String parameterizeUrl(String baseUrl, Properties params) {
 48  0
         baseUrl = StringUtils.trim(baseUrl);
 49  0
         if (StringUtils.isEmpty(baseUrl)) {
 50  0
             throw new IllegalArgumentException("invalid (blank) base URL");
 51  
         }
 52  0
         if (params == null) {
 53  0
             throw new IllegalArgumentException("invalid (null) Properties");
 54  
         }
 55  
 
 56  
 
 57  0
         StringBuffer ret = new StringBuffer(baseUrl);
 58  
         // Only start with ? if it has not been added to the url
 59  0
         String delimiter = (ret.indexOf("?") == -1) ? "?" : "&";
 60  0
         for ( Object key : params.keySet() ) {
 61  0
             String paramName = StringUtils.trim( (String)key );
 62  0
             String paramValue = params.getProperty(paramName);
 63  0
             ret.append( delimiter );
 64  0
             if (StringUtils.isEmpty(paramName)) {
 65  0
                 throw new IllegalArgumentException("invalid (blank) paramName");
 66  
             }
 67  0
             if (paramValue == null) {
 68  0
                 ret.append( paramName );
 69  0
                 ret.append( "=" );
 70  
             } else {
 71  
                 try {
 72  0
                     ret.append( paramName );
 73  0
                     ret.append( "=" );
 74  0
                     ret.append( urlCodec.encode(paramValue) );
 75  0
                 } catch ( EncoderException ex ) {
 76  0
                     LOG.error("Unable to encode parameter name or value: " + paramName + "=" + paramValue, ex);
 77  0
                     throw new RuntimeException( "Unable to encode parameter name or value: " + paramName + "=" + paramValue, ex );
 78  0
                 }
 79  
             }
 80  0
             delimiter = "&";
 81  0
         }
 82  
 
 83  0
         return ret.toString();
 84  
     }
 85  
 
 86  
     public static String encode( String value ) {
 87  
         try {
 88  0
             return urlCodec.encode(value);
 89  0
         } catch ( EncoderException ex ) {
 90  0
             throw new RuntimeException( "Unable to encode value: " + value, ex );
 91  
         }
 92  
     }
 93  
 }