001/** 002 * Copyright 2005-2015 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.util; 017 018import org.apache.commons.codec.EncoderException; 019import org.apache.commons.codec.net.URLCodec; 020import org.apache.commons.lang.StringUtils; 021import org.apache.log4j.Logger; 022 023import java.util.Properties; 024 025/** 026 * Provides utility methods for re/building URLs. 027 * 028 * @author Kuali Rice Team (rice.collab@kuali.org) 029 */ 030public class UrlFactory { 031 private static Logger LOG = Logger.getLogger(UrlFactory.class); 032 033 /** 034 * Creates a new URL by taking the given URL and appending the parameter names and values from the given Properties instance to 035 * it. Note: parameter names must be non-blank; parameter values must be non-null. 036 * 037 * @param baseUrl the URL string used as the basis for reconstruction 038 * @param params Properties instance containing the desired parameters and their values 039 * @throws IllegalArgumentException if the given url is null or empty 040 * @throws IllegalArgumentException if the given Properties instance is null 041 * @throws IllegalArgumentException if a parameter name is null or empty, or a parameter value is null 042 * @throws RuntimeException if there is a problem encoding a parameter name or value into UTF-8 043 * @return a newly-constructed URL string which has the given parameters and their values appended to it 044 */ 045 private static URLCodec urlCodec = new URLCodec("UTF-8"); 046 047 public static String parameterizeUrl(String baseUrl, Properties params) { 048 baseUrl = StringUtils.trim(baseUrl); 049 if (StringUtils.isEmpty(baseUrl)) { 050 throw new IllegalArgumentException("invalid (blank) base URL"); 051 } 052 if (params == null) { 053 throw new IllegalArgumentException("invalid (null) Properties"); 054 } 055 056 057 StringBuffer ret = new StringBuffer(baseUrl); 058 // Only start with ? if it has not been added to the url 059 String delimiter = (ret.indexOf("?") == -1) ? "?" : "&"; 060 for ( Object key : params.keySet() ) { 061 String paramName = StringUtils.trim( (String)key ); 062 String paramValue = params.getProperty(paramName); 063 ret.append( delimiter ); 064 if (StringUtils.isEmpty(paramName)) { 065 throw new IllegalArgumentException("invalid (blank) paramName"); 066 } 067 if (paramValue == null) { 068 ret.append( paramName ); 069 ret.append( "=" ); 070 } else { 071 try { 072 ret.append( paramName ); 073 ret.append( "=" ); 074 ret.append( urlCodec.encode(paramValue) ); 075 } catch ( EncoderException ex ) { 076 LOG.error("Unable to encode parameter name or value: " + paramName + "=" + paramValue, ex); 077 throw new RuntimeException( "Unable to encode parameter name or value: " + paramName + "=" + paramValue, ex ); 078 } 079 } 080 delimiter = "&"; 081 } 082 083 return ret.toString(); 084 } 085 086 public static String encode( String value ) { 087 try { 088 return urlCodec.encode(value); 089 } catch ( EncoderException ex ) { 090 throw new RuntimeException( "Unable to encode value: " + value, ex ); 091 } 092 } 093}