001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.core.util;
017:
018: import java.io.UnsupportedEncodingException;
019: import java.net.URLEncoder;
020: import java.util.Iterator;
021: import java.util.Properties;
022:
023: import org.apache.commons.lang.StringUtils;
024:
025: /**
026: * This class Provides utility methods for re/building URLs.
027: *
028: *
029: */
030:
031: public class UrlFactory {
032: /**
033: * Creates a new URL by taking the given URL and appending the parameter names and values from the given Properties instance to
034: * it. Note: parameter names must be non-blank; parameter values must be non-null.
035: *
036: * @param baseUrl the URL string used as the basis for reconstruction
037: * @param params Properties instance containing the desired parameters and their values
038: * @throws IllegalArgumentException if the given url is null or empty
039: * @throws IllegalArgumentException if the given Properties instance is null
040: * @throws IllegalArgumentException if a parameter name is null or empty, or a parameter value is null
041: * @throws RuntimeException if there is a problem encoding a parameter name or value into UTF-8
042: * @return a newly-constructed URL string which has the given parameters and their values appended to it
043: */
044: public static String parameterizeUrl(String baseUrl,
045: Properties params) {
046: baseUrl = StringUtils.trim(baseUrl);
047: if (StringUtils.isEmpty(baseUrl)) {
048: throw new IllegalArgumentException(
049: "invalid (blank) base URL");
050: }
051: if (params == null) {
052: throw new IllegalArgumentException(
053: "invalid (null) Properties");
054: }
055:
056: StringBuffer ret = new StringBuffer(baseUrl);
057:
058: if (params.size() > 0) {
059: Iterator i = params.keySet().iterator();
060: String paramName = (String) i.next();
061: ret.append(pathEntry("?", paramName, params
062: .getProperty(paramName)));
063:
064: while (i.hasNext()) {
065: paramName = (String) i.next();
066: ret.append(pathEntry("&", paramName, params
067: .getProperty(paramName)));
068: }
069: }
070:
071: return ret.toString();
072: }
073:
074: private static String pathEntry(String separator, String paramName,
075: String paramValue) {
076: paramName = StringUtils.trim(paramName);
077: if (StringUtils.isEmpty(paramName)) {
078: throw new IllegalArgumentException(
079: "invalid (blank) paramName");
080: }
081: if (paramValue == null) {
082: throw new IllegalArgumentException(
083: "invalid (null) paramValue");
084: }
085:
086: StringBuffer entry = new StringBuffer(separator);
087:
088: entry.append(encode(paramName));
089: entry.append("=");
090: entry.append(encode(paramValue));
091:
092: return entry.toString();
093: }
094:
095: public static String encode(String raw) {
096: String enc = null;
097: try {
098: enc = URLEncoder.encode(raw, "UTF-8");
099: } catch (UnsupportedEncodingException e) {
100: // this should never happen
101: throw new RuntimeException(e);
102: }
103:
104: return enc;
105: }
106:
107: /**
108: * Constructs a document action URL from the given documentTypeName, with the given Properties as URL parameters.
109: *
110: * @param documentTypeName
111: * @param urlParams
112: * @return document action URL
113: */
114: public static String buildDocumentActionUrl(
115: String documentTypeName, Properties urlParams) {
116: if (StringUtils.isBlank(documentTypeName)) {
117: throw new IllegalArgumentException(
118: "invalid (blank) documentTypeName");
119: }
120:
121: if (!documentTypeName.startsWith("Kuali")) {
122: throw new IllegalArgumentException("documentTypeName '"
123: + documentTypeName
124: + "' doesn't start with the literal string 'Kuali'");
125: }
126: if (!documentTypeName.endsWith("Document")) {
127: throw new IllegalArgumentException(
128: "documentTypeName '"
129: + documentTypeName
130: + "' doesn't end with the literal string 'Document'");
131: }
132:
133: String actionBase = "financial"
134: + StringUtils.substringBetween(documentTypeName,
135: "Kuali", "Document") + ".do";
136: String actionUrl = parameterizeUrl(actionBase, urlParams);
137:
138: return actionUrl;
139: }
140: }
|