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 public class UrlFactory {
32 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 private static URLCodec urlCodec = new URLCodec("UTF-8");
46
47 public static String parameterizeUrl(String baseUrl, Properties params) {
48 baseUrl = StringUtils.trim(baseUrl);
49 if (StringUtils.isEmpty(baseUrl)) {
50 throw new IllegalArgumentException("invalid (blank) base URL");
51 }
52 if (params == null) {
53 throw new IllegalArgumentException("invalid (null) Properties");
54 }
55
56
57 StringBuffer ret = new StringBuffer(baseUrl);
58 // Only start with ? if it has not been added to the url
59 String delimiter = (ret.indexOf("?") == -1) ? "?" : "&";
60 for ( Object key : params.keySet() ) {
61 String paramName = StringUtils.trim( (String)key );
62 String paramValue = params.getProperty(paramName);
63 ret.append( delimiter );
64 if (StringUtils.isEmpty(paramName)) {
65 throw new IllegalArgumentException("invalid (blank) paramName");
66 }
67 if (paramValue == null) {
68 ret.append( paramName );
69 ret.append( "=" );
70 } else {
71 try {
72 ret.append( paramName );
73 ret.append( "=" );
74 ret.append( urlCodec.encode(paramValue) );
75 } catch ( EncoderException ex ) {
76 LOG.error("Unable to encode parameter name or value: " + paramName + "=" + paramValue, ex);
77 throw new RuntimeException( "Unable to encode parameter name or value: " + paramName + "=" + paramValue, ex );
78 }
79 }
80 delimiter = "&";
81 }
82
83 return ret.toString();
84 }
85
86 public static String encode( String value ) {
87 try {
88 return urlCodec.encode(value);
89 } catch ( EncoderException ex ) {
90 throw new RuntimeException( "Unable to encode value: " + value, ex );
91 }
92 }
93 }