001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.util;
039:
040: import java.net.MalformedURLException;
041: import java.net.URL;
042:
043: /**
044: * URL utilities class that makes it easy to create new URLs based off of old URLs
045: * without having to assemble or parse them yourself.
046: *
047: * @version $Revision: 2132 $
048: * @author Daniel Gredler
049: */
050: public final class UrlUtils {
051:
052: /**
053: * Disallow instantiation of this class.
054: */
055: private UrlUtils() {
056: // Empty.
057: }
058:
059: /**
060: * Creates and returns a new URL identical to the specified URL, except using the specified protocol.
061: * @param u The URL on which to base the returned URL.
062: * @param newProtocol The new protocol to use in the returned URL.
063: * @return A new URL identical to the specified URL, except using the specified protocol.
064: * @throws MalformedURLException If there is a problem creating the new URL.
065: */
066: public static URL getUrlWithNewProtocol(final URL u,
067: final String newProtocol) throws MalformedURLException {
068: return createNewUrl(newProtocol, u.getHost(), u.getPort(), u
069: .getPath(), u.getRef(), u.getQuery());
070: }
071:
072: /**
073: * Creates and returns a new URL identical to the specified URL, except using the specified host.
074: * @param u The URL on which to base the returned URL.
075: * @param newHost The new host to use in the returned URL.
076: * @return A new URL identical to the specified URL, except using the specified host.
077: * @throws MalformedURLException If there is a problem creating the new URL.
078: */
079: public static URL getUrlWithNewHost(final URL u,
080: final String newHost) throws MalformedURLException {
081: return createNewUrl(u.getProtocol(), newHost, u.getPort(), u
082: .getPath(), u.getRef(), u.getQuery());
083: }
084:
085: /**
086: * Creates and returns a new URL identical to the specified URL, except using the specified port.
087: * @param u The URL on which to base the returned URL.
088: * @param newPort The new port to use in the returned URL.
089: * @return A new URL identical to the specified URL, except using the specified port.
090: * @throws MalformedURLException If there is a problem creating the new URL.
091: */
092: public static URL getUrlWithNewPort(final URL u, final int newPort)
093: throws MalformedURLException {
094: return createNewUrl(u.getProtocol(), u.getHost(), newPort, u
095: .getPath(), u.getRef(), u.getQuery());
096: }
097:
098: /**
099: * Creates and returns a new URL identical to the specified URL, except using the specified path.
100: * @param u The URL on which to base the returned URL.
101: * @param newPath The new path to use in the returned URL.
102: * @return A new URL identical to the specified URL, except using the specified path.
103: * @throws MalformedURLException If there is a problem creating the new URL.
104: */
105: public static URL getUrlWithNewPath(final URL u,
106: final String newPath) throws MalformedURLException {
107: return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(),
108: newPath, u.getRef(), u.getQuery());
109: }
110:
111: /**
112: * Creates and returns a new URL identical to the specified URL, except using the specified reference.
113: * @param u The URL on which to base the returned URL.
114: * @param newRef The new reference to use in the returned URL.
115: * @return A new URL identical to the specified URL, except using the specified reference.
116: * @throws MalformedURLException If there is a problem creating the new URL.
117: */
118: public static URL getUrlWithNewRef(final URL u, final String newRef)
119: throws MalformedURLException {
120: return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(),
121: u.getPath(), newRef, u.getQuery());
122: }
123:
124: /**
125: * Creates and returns a new URL identical to the specified URL, except using the specified query string.
126: * @param u The URL on which to base the returned URL.
127: * @param newQuery The new query string to use in the returned URL.
128: * @return A new URL identical to the specified URL, except using the specified query string.
129: * @throws MalformedURLException If there is a problem creating the new URL.
130: */
131: public static URL getUrlWithNewQuery(final URL u,
132: final String newQuery) throws MalformedURLException {
133: return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(),
134: u.getPath(), u.getRef(), newQuery);
135: }
136:
137: /**
138: * Creates a new URL based on the specified fragments.
139: * @param protocol The protocol to use. May not be <tt>null</tt>.
140: * @param host The host to use. May not be <tt>null</tt>.
141: * @param port The port to use. May be <tt>-1</tt> if no port is specified.
142: * @param path The path to use. May be <tt>null</tt> and may omit the initial <tt>'/'</tt>.
143: * @param ref The reference to use. May be <tt>null</tt>. Must not include the <tt>'#'</tt>.
144: * @param query The query to use. May be <tt>null</tt>. Must not include the <tt>'?'</tt>.
145: * @return A new URL based on the specified fragments.
146: * @throws MalformedURLException If there is a problem creating the new URL.
147: */
148: private static URL createNewUrl(final String protocol,
149: final String host, final int port, final String path,
150: final String ref, final String query)
151: throws MalformedURLException {
152: final StringBuffer s = new StringBuffer();
153: s.append(protocol);
154: s.append("://");
155: s.append(host);
156: if (port != -1) {
157: s.append(":").append(port);
158: }
159: if (path != null && path.length() > 0) {
160: if (!path.startsWith("/")) {
161: s.append("/");
162: }
163: s.append(path);
164: }
165: if (query != null) {
166: s.append("?").append(query);
167: }
168: if (ref != null) {
169: s.append("#").append(ref);
170: }
171: final URL url = new URL(s.toString());
172: return url;
173: }
174:
175: }
|