1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URIUtils.java $ 3 * $Revision: 653041 $ 4 * $Date: 2008-05-03 03:39:28 -0700 (Sat, 03 May 2008) $ 5 * 6 * ==================================================================== 7 * 8 * Licensed to the Apache Software Foundation (ASF) under one or more 9 * contributor license agreements. See the NOTICE file distributed with 10 * this work for additional information regarding copyright ownership. 11 * The ASF licenses this file to You under the Apache License, Version 2.0 12 * (the "License"); you may not use this file except in compliance with 13 * the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the License for the specific language governing permissions and 21 * limitations under the License. 22 * ==================================================================== 23 * 24 * This software consists of voluntary contributions made by many 25 * individuals on behalf of the Apache Software Foundation. For more 26 * information on the Apache Software Foundation, please see 27 * <http://www.apache.org/>. 28 * 29 */ 30 package org.apache.http.client.utils; 31 32 import java.net.URI; 33 import java.net.URISyntaxException; 34 35 import org.apache.http.HttpHost; 36 37 /** 38 * A collection of utilities for {@link URI URIs}, to workaround 39 * bugs within the class or for ease-of-use features. 40 * 41 * @deprecated Please use {@link java.net.URL#openConnection} instead. 42 * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 43 * for further details. 44 */ 45 @Deprecated 46 public class URIUtils { 47 48 /** 49 * Constructs a {@link URI} using all the parameters. This should be 50 * used instead of 51 * {@link URI#URI(String, String, String, int, String, String, String)} 52 * or any of the other URI multi-argument URI constructors. 53 * 54 * See <a 55 * href="https://issues.apache.org/jira/browse/HTTPCLIENT-730">HTTPCLIENT-730</a> 56 * for more information. 57 * 58 * @param scheme 59 * Scheme name 60 * @param host 61 * Host name 62 * @param port 63 * Port number 64 * @param path 65 * Path 66 * @param query 67 * Query 68 * @param fragment 69 * Fragment 70 * 71 * @throws URISyntaxException 72 * If both a scheme and a path are given but the path is 73 * relative, if the URI string constructed from the given 74 * components violates RFC 2396, or if the authority 75 * component of the string is present but cannot be parsed 76 * as a server-based authority 77 */ 78 public static URI createURI( 79 final String scheme, 80 final String host, 81 int port, 82 final String path, 83 final String query, 84 final String fragment) throws URISyntaxException { 85 86 StringBuilder buffer = new StringBuilder(); 87 if (host != null) { 88 if (scheme != null) { 89 buffer.append(scheme); 90 buffer.append("://"); 91 } 92 buffer.append(host); 93 if (port > 0) { 94 buffer.append(':'); 95 buffer.append(port); 96 } 97 } 98 if (path == null || !path.startsWith("/")) { 99 buffer.append('/'); 100 } 101 if (path != null) { 102 buffer.append(path); 103 } 104 if (query != null) { 105 buffer.append('?'); 106 buffer.append(query); 107 } 108 if (fragment != null) { 109 buffer.append('#'); 110 buffer.append(fragment); 111 } 112 return new URI(buffer.toString()); 113 } 114 115 /** 116 * A convenience method for creating a new {@link URI} whose scheme, host 117 * and port are taken from the target host, but whose path, query and 118 * fragment are taken from the existing URI. The fragment is only used if 119 * dropFragment is false. 120 * 121 * @param uri 122 * Contains the path, query and fragment to use. 123 * @param target 124 * Contains the scheme, host and port to use. 125 * @param dropFragment 126 * True if the fragment should not be copied. 127 * 128 * @throws URISyntaxException 129 * If the resulting URI is invalid. 130 */ 131 public static URI rewriteURI( 132 final URI uri, 133 final HttpHost target, 134 boolean dropFragment) throws URISyntaxException { 135 if (uri == null) { 136 throw new IllegalArgumentException("URI may nor be null"); 137 } 138 if (target != null) { 139 return URIUtils.createURI( 140 target.getSchemeName(), 141 target.getHostName(), 142 target.getPort(), 143 uri.getRawPath(), 144 uri.getRawQuery(), 145 dropFragment ? null : uri.getRawFragment()); 146 } else { 147 return URIUtils.createURI( 148 null, 149 null, 150 -1, 151 uri.getRawPath(), 152 uri.getRawQuery(), 153 dropFragment ? null : uri.getRawFragment()); 154 } 155 } 156 157 /** 158 * A convenience method for 159 * {@link URIUtils#rewriteURI(URI, HttpHost, boolean)} that always keeps the 160 * fragment. 161 */ 162 public static URI rewriteURI( 163 final URI uri, 164 final HttpHost target) throws URISyntaxException { 165 return rewriteURI(uri, target, false); 166 } 167 168 /** 169 * Resolves a URI reference against a base URI. Work-around for bug in 170 * java.net.URI (<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4708535>) 171 * 172 * @param baseURI the base URI 173 * @param reference the URI reference 174 * @return the resulting URI 175 */ 176 public static URI resolve(final URI baseURI, final String reference) { 177 return URIUtils.resolve(baseURI, URI.create(reference)); 178 } 179 180 /** 181 * Resolves a URI reference against a base URI. Work-around for bug in 182 * java.net.URI (<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4708535>) 183 * 184 * @param baseURI the base URI 185 * @param reference the URI reference 186 * @return the resulting URI 187 */ 188 public static URI resolve(final URI baseURI, URI reference){ 189 if (baseURI == null) { 190 throw new IllegalArgumentException("Base URI may nor be null"); 191 } 192 if (reference == null) { 193 throw new IllegalArgumentException("Reference URI may nor be null"); 194 } 195 boolean emptyReference = reference.toString().length() == 0; 196 if (emptyReference) { 197 reference = URI.create("#"); 198 } 199 URI resolved = baseURI.resolve(reference); 200 if (emptyReference) { 201 String resolvedString = resolved.toString(); 202 resolved = URI.create(resolvedString.substring(0, 203 resolvedString.indexOf('#'))); 204 } 205 return resolved; 206 } 207 208 /** 209 * This class should not be instantiated. 210 */ 211 private URIUtils() { 212 } 213 214 } 215