| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UrlFactory |
|
| 7.5;7.5 |
| 1 | /* | |
| 2 | * Copyright 2005-2007 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 | 1 | 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 | 1 | private static URLCodec urlCodec = new URLCodec("UTF-8"); |
| 46 | ||
| 47 | public static String parameterizeUrl(String baseUrl, Properties params) { | |
| 48 | 1 | baseUrl = StringUtils.trim(baseUrl); |
| 49 | 1 | if (StringUtils.isEmpty(baseUrl)) { |
| 50 | 0 | throw new IllegalArgumentException("invalid (blank) base URL"); |
| 51 | } | |
| 52 | 1 | if (params == null) { |
| 53 | 0 | throw new IllegalArgumentException("invalid (null) Properties"); |
| 54 | } | |
| 55 | ||
| 56 | ||
| 57 | 1 | StringBuffer ret = new StringBuffer(baseUrl); |
| 58 | ||
| 59 | 1 | String delimiter = "?"; |
| 60 | 1 | for ( Object key : params.keySet() ) { |
| 61 | 4 | String paramName = StringUtils.trim( (String)key ); |
| 62 | 4 | String paramValue = params.getProperty(paramName); |
| 63 | 4 | ret.append( delimiter ); |
| 64 | 4 | if (StringUtils.isEmpty(paramName)) { |
| 65 | 0 | throw new IllegalArgumentException("invalid (blank) paramName"); |
| 66 | } | |
| 67 | 4 | if (paramValue == null) { |
| 68 | 0 | ret.append( paramName ); |
| 69 | 0 | ret.append( "=" ); |
| 70 | } else { | |
| 71 | try { | |
| 72 | 4 | ret.append( paramName ); |
| 73 | 4 | ret.append( "=" ); |
| 74 | 4 | 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 | 4 | } |
| 79 | } | |
| 80 | 4 | delimiter = "&"; |
| 81 | 4 | } |
| 82 | ||
| 83 | 1 | return ret.toString(); |
| 84 | } | |
| 85 | ||
| 86 | public static String encode( String value ) { | |
| 87 | try { | |
| 88 | 1 | return urlCodec.encode(value); |
| 89 | 0 | } catch ( EncoderException ex ) { |
| 90 | 0 | throw new RuntimeException( "Unable to encode value: " + value, ex ); |
| 91 | } | |
| 92 | } | |
| 93 | } |