/** * Expands a relative URL relative to the specified base. In most situations * this is the same as <code>new URL(baseUrl, relativeUrl)</code> but * there are some cases that URL doesn't handle correctly. See * <a href="http://www.faqs.org/rfcs/rfc1808.html">RFC1808</a> * regarding Relative Uniform Resource Locators for more information. * * @param baseUrl the base URL * @param relativeUrl the relative URL * @return the expansion of the specified base and relative URLs * @throws MalformedURLException if an error occurred when creating a URL object */ public static URL expandUrl(final URL baseUrl, final String relativeUrl) throws MalformedURLException { final String newUrl = UrlUtils.resolveUrl(baseUrl, relativeUrl); return UrlUtils.toUrlUnsafe(newUrl); }
/** * Creates and returns a new URL identical to the specified URL, except using the specified query string. * @param u the URL on which to base the returned URL * @param newQuery the new query string to use in the returned URL * @return a new URL identical to the specified URL, except using the specified query string * @throws MalformedURLException if there is a problem creating the new URL */ public static URL getUrlWithNewQuery(final URL u, final String newQuery) throws MalformedURLException { return createNewUrl(u.getProtocol(), u.getAuthority(), u.getPath(), u.getRef(), newQuery); }
/** * {@link CookieSpec#match(String, int, String, boolean, Cookie[])} doesn't like empty hosts and * negative ports, but these things happen if we're dealing with a local file. This method * allows us to work around this limitation in HttpClient by feeding it a bogus host and port. * * @param url the URL to replace if necessary * @return the replacement URL, or the original URL if no replacement was necessary */ private static URL replaceForCookieIfNecessary(URL url) { final String protocol = url.getProtocol(); final boolean file = "file".equals(protocol); if (file) { try { url = getUrlWithNewPort(getUrlWithNewHost(url, "LOCAL_FILESYSTEM"), 0); } catch (final MalformedURLException e) { throw new RuntimeException(e); } } return url; }
/** * Resolves a given relative URL against a base URL. See * <a href="http://www.faqs.org/rfcs/rfc1808.html">RFC1808</a> * Section 4 for more details. * * @param baseUrl The base URL in which to resolve the specification. * @param relativeUrl The relative URL to resolve against the base URL. * @return the resolved specification. */ public static String resolveUrl(final String baseUrl, final String relativeUrl) { if (baseUrl == null) { throw new IllegalArgumentException("Base URL must not be null"); } if (relativeUrl == null) { throw new IllegalArgumentException("Relative URL must not be null"); } final Url url = resolveUrl(parseUrl(baseUrl.trim()), relativeUrl.trim()); return url.toString(); }
final Url url = parseUrl(relativeUrl); url.path_ = removeLeadingSlashPoints(url.path_); return url; path = removeLeadingSlashPoints(path);
private static URL buildUrlWithNewPath(URL url, final String newPath) { try { url = UrlUtils.getUrlWithNewPath(url, newPath); } catch (final Exception e) { throw new RuntimeException("Cannot change path of URL: " + url.toExternalForm(), e); } return url; }
/** * Sets the search portion of the link's URL (the portion starting with '?' * and up to but not including any '#').. * @param search the new search portion of the link's URL * @throws Exception if an error occurs * @see <a href="http://msdn.microsoft.com/en-us/library/ms534620.aspx">MSDN Documentation</a> */ public void jsxSet_search(final String search) throws Exception { final String query; if (search == null || "?".equals(search) || "".equals(search)) { query = null; } else if (search.charAt(0) == '?') { query = search.substring(1); } else { query = search; } setUrl(UrlUtils.getUrlWithNewQuery(getUrl(), query)); }
/** * Sets the port portion of the location URL. * @param port the new port portion of the location URL * @throws Exception if an error occurs * @see <a href="http://msdn.microsoft.com/en-us/library/ms534342.aspx">MSDN Documentation</a> */ public void jsxSet_port(final String port) throws Exception { setUrl(UrlUtils.getUrlWithNewPort(getUrl(), Integer.parseInt(port))); }
/** * Sets the hostname portion of the location URL. * @param hostname the new hostname portion of the location URL * @throws Exception if an error occurs * @see <a href="http://msdn.microsoft.com/en-us/library/ms533785.aspx">MSDN Documentation</a> */ public void jsxSet_hostname(final String hostname) throws Exception { setUrl(UrlUtils.getUrlWithNewHost(getUrl(), hostname)); }
/** * Sets the protocol portion of the link's URL. * @param protocol the new protocol portion of the link's URL * @throws Exception if an error occurs * @see <a href="http://msdn.microsoft.com/en-us/library/ms534353.aspx">MSDN Documentation</a> */ public void jsxSet_protocol(final String protocol) throws Exception { final String bareProtocol = StringUtils.substringBefore(protocol, ":"); setUrl(UrlUtils.getUrlWithNewProtocol(getUrl(), bareProtocol)); }
/** * <p>Constructs a URL instance based on the specified URL string, taking into account the fact that the * specified URL string may represent an <tt>"about:..."</tt> URL, a <tt>"javascript:..."</tt> URL, or * a <tt>data:...</tt> URL.</p> * * <p>The caller should be sure that URL strings passed to this method will parse correctly as URLs, as * this method never expects to have to handle {@link MalformedURLException}s.</p> * * @param url the URL string to convert into a URL instance * @return the constructed URL instance */ public static URL toUrlSafe(final String url) { try { return toUrlUnsafe(url); } catch (final MalformedURLException e) { // Should never happen. throw new RuntimeException(e); } }
/** * Sets the hash portion of the link's URL (the portion following the '#'). * @param hash the new hash portion of the link's URL * @throws Exception if an error occurs * @see <a href="http://msdn.microsoft.com/en-us/library/ms533775.aspx">MSDN Documentation</a> */ public void jsxSet_hash(final String hash) throws Exception { setUrl(UrlUtils.getUrlWithNewRef(getUrl(), hash)); }
/** * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br> * * @param href the href * @param page the HtmlPage * @return the calculated target url. * @throws MalformedURLException if an IO error occurs */ public static URL getTargetUrl(final String href, final HtmlPage page) throws MalformedURLException { URL url = page.getFullyQualifiedUrl(href); // fix for empty url if (StringUtils.isEmpty(href)) { final boolean dropFilename = page.getWebClient().getBrowserVersion() .hasFeature(ANCHOR_EMPTY_HREF_NO_FILENAME); if (dropFilename) { String path = url.getPath(); path = path.substring(0, path.lastIndexOf('/') + 1); url = UrlUtils.getUrlWithNewPath(url, path); url = UrlUtils.getUrlWithNewRef(url, null); } else { url = UrlUtils.getUrlWithNewRef(url, null); } } return url; }
if (isValidScheme(scheme)) { url.scheme_ = scheme; startIndex = colonIndex + 1;
/** * Resolves a given relative URL against a base URL. See * <a href="http://www.faqs.org/rfcs/rfc1808.html">RFC1808</a> * Section 4 for more details. * * @param baseUrl The base URL in which to resolve the specification. * @param relativeUrl The relative URL to resolve against the base URL. * @return the resolved specification. */ public static String resolveUrl(final String baseUrl, final String relativeUrl) { if (baseUrl == null) { throw new IllegalArgumentException("Base URL must not be null"); } if (relativeUrl == null) { throw new IllegalArgumentException("Relative URL must not be null"); } final Url url = resolveUrl(parseUrl(baseUrl.trim()), relativeUrl.trim()); return url.toString(); }
final Url url = parseUrl(relativeUrl); url.path_ = removeLeadingSlashPoints(url.path_); return url; path = removeLeadingSlashPoints(path);
private static URL buildUrlWithNewPath(URL url, final String newPath) { try { url = UrlUtils.getUrlWithNewPath(url, newPath); } catch (final Exception e) { throw new RuntimeException("Cannot change path of URL: " + url.toExternalForm(), e); } return url; }
/** * Sets the search portion of the link's URL (the portion starting with '?' * and up to but not including any '#').. * @param search the new search portion of the link's URL * @throws Exception if an error occurs * @see <a href="http://msdn.microsoft.com/en-us/library/ms534620.aspx">MSDN Documentation</a> */ public void jsxSet_search(final String search) throws Exception { final String query; if (search == null || "?".equals(search) || "".equals(search)) { query = null; } else if (search.charAt(0) == '?') { query = search.substring(1); } else { query = search; } setUrl(UrlUtils.getUrlWithNewQuery(getUrl(), query)); }
/** * Sets the port portion of the link's URL. * @param port the new port portion of the link's URL * @throws Exception if an error occurs * @see <a href="http://msdn.microsoft.com/en-us/library/ms534342.aspx">MSDN Documentation</a> */ public void jsxSet_port(final String port) throws Exception { setUrl(UrlUtils.getUrlWithNewPort(getUrl(), Integer.parseInt(port))); }
/** * Sets the hostname portion of the link's URL. * @param hostname the new hostname portion of the link's URL * @throws Exception if an error occurs * @see <a href="http://msdn.microsoft.com/en-us/library/ms533785.aspx">MSDN Documentation</a> */ public void jsxSet_hostname(final String hostname) throws Exception { setUrl(UrlUtils.getUrlWithNewHost(getUrl(), hostname)); }