Changeset 9720 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2016-02-03T00:53:09+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/Compression.java
r9170 r9720 8 8 import java.io.InputStream; 9 9 import java.io.OutputStream; 10 import java.net.URL;11 10 import java.nio.charset.StandardCharsets; 12 11 import java.util.zip.GZIPOutputStream; … … 103 102 104 103 /** 105 * Returns an un-compressing {@link InputStream} for the {@link URL} {@code url}.106 * @param url URL107 * @return un-compressing input stream108 *109 * @throws IOException if any I/O error occurs110 * @deprecated Use {@link org.openstreetmap.josm.tools.HttpClient} instead111 */112 @Deprecated113 public static InputStream getUncompressedURLInputStream(URL url) throws IOException {114 return Utils.openURLAndDecompress(url, true);115 }116 117 /**118 104 * Returns a compressing {@link OutputStream} for {@code out}. 119 105 * @param out raw output stream -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r9645 r9720 25 25 import java.io.InputStreamReader; 26 26 import java.io.UnsupportedEncodingException; 27 import java.net.HttpURLConnection;28 27 import java.net.MalformedURLException; 29 28 import java.net.URL; 30 import java.net.URLConnection;31 29 import java.net.URLDecoder; 32 30 import java.net.URLEncoder; … … 68 66 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; 69 67 import org.openstreetmap.josm.Main; 70 import org.openstreetmap.josm.data.Version;71 68 import org.xml.sax.InputSource; 72 69 import org.xml.sax.SAXException; … … 827 824 } 828 825 829 private static final Pattern HTTP_PREFFIX_PATTERN = Pattern.compile("https?");830 831 /**832 * Opens a HTTP connection to the given URL and sets the User-Agent property to JOSM's one.833 * @param httpURL The HTTP url to open (must use http:// or https://)834 * @return An open HTTP connection to the given URL835 * @throws java.io.IOException if an I/O exception occurs.836 * @since 5587837 * @deprecated Use {@link HttpClient} instead838 */839 @Deprecated840 public static HttpURLConnection openHttpConnection(URL httpURL) throws IOException {841 if (httpURL == null || !HTTP_PREFFIX_PATTERN.matcher(httpURL.getProtocol()).matches()) {842 throw new IllegalArgumentException("Invalid HTTP url");843 }844 if (Main.isDebugEnabled()) {845 Main.debug("Opening HTTP connection to "+httpURL.toExternalForm());846 }847 HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection();848 connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());849 connection.setUseCaches(false);850 return connection;851 }852 853 /**854 * Opens a connection to the given URL and sets the User-Agent property to JOSM's one.855 * @param url The url to open856 * @return An stream for the given URL857 * @throws java.io.IOException if an I/O exception occurs.858 * @since 5867859 * @deprecated Use {@link HttpClient} instead860 */861 @Deprecated862 public static InputStream openURL(URL url) throws IOException {863 return HttpClient.create(url).connect().getContent();864 }865 866 /**867 * Opens a connection to the given URL, sets the User-Agent property to JOSM's one, and decompresses stream if necessary.868 * @param url The url to open869 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link BZip2CompressorInputStream}870 * if the {@code Content-Type} header is set accordingly.871 * @return An stream for the given URL872 * @throws IOException if an I/O exception occurs.873 * @since 6421874 * @deprecated Use {@link HttpClient} instead875 */876 @Deprecated877 public static InputStream openURLAndDecompress(final URL url, final boolean decompress) throws IOException {878 return HttpClient.create(url).connect().uncompress(decompress).getContent();879 }880 881 826 /** 882 827 * Returns a Bzip2 input stream wrapping given input stream. … … 925 870 } 926 871 return zis; 927 }928 929 /***930 * Setups the given URL connection to match JOSM needs by setting its User-Agent and timeout properties.931 * @param connection The connection to setup932 * @return {@code connection}, with updated properties933 * @since 5887934 * @deprecated Use {@link HttpClient} instead935 */936 @Deprecated937 public static URLConnection setupURLConnection(URLConnection connection) {938 if (connection != null) {939 connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());940 connection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect", 15)*1000);941 connection.setReadTimeout(Main.pref.getInteger("socket.timeout.read", 30)*1000);942 }943 return connection;944 }945 946 /**947 * Opens a connection to the given URL and sets the User-Agent property to JOSM's one.948 * @param url The url to open949 * @return An buffered stream reader for the given URL (using UTF-8)950 * @throws java.io.IOException if an I/O exception occurs.951 * @since 5868952 * @deprecated Use {@link HttpClient} instead953 */954 @Deprecated955 public static BufferedReader openURLReader(URL url) throws IOException {956 return HttpClient.create(url).connect().getContentReader();957 }958 959 /**960 * Opens a connection to the given URL and sets the User-Agent property to JOSM's one.961 * @param url The url to open962 * @param decompress whether to wrap steam in a {@link GZIPInputStream} or {@link BZip2CompressorInputStream}963 * if the {@code Content-Type} header is set accordingly.964 * @return An buffered stream reader for the given URL (using UTF-8)965 * @throws IOException if an I/O exception occurs.966 * @since 6421967 * @deprecated Use {@link HttpClient} instead968 */969 @Deprecated970 public static BufferedReader openURLReaderAndDecompress(final URL url, final boolean decompress) throws IOException {971 return HttpClient.create(url).connect().uncompress(decompress).getContentReader();972 }973 974 /**975 * Opens a HTTP connection to the given URL, sets the User-Agent property to JOSM's one and optionnaly disables Keep-Alive.976 * @param httpURL The HTTP url to open (must use http:// or https://)977 * @param keepAlive whether not to set header {@code Connection=close}978 * @return An open HTTP connection to the given URL979 * @throws java.io.IOException if an I/O exception occurs.980 * @since 5587981 * @deprecated Use {@link HttpClient} instead982 */983 @Deprecated984 public static HttpURLConnection openHttpConnection(URL httpURL, boolean keepAlive) throws IOException {985 HttpURLConnection connection = openHttpConnection(httpURL);986 if (!keepAlive) {987 connection.setRequestProperty("Connection", "close");988 }989 if (Main.isDebugEnabled()) {990 try {991 Main.debug("REQUEST: "+ connection.getRequestProperties());992 } catch (IllegalStateException e) {993 Main.warn(e);994 }995 }996 return connection;997 }998 999 /**1000 * Opens a HTTP connection to given URL, sets the User-Agent property to JOSM's one, optionally disables Keep-Alive, and1001 * optionally - follows redirects. It means, that it's not possible to send custom headers with method1002 *1003 * @param httpURL The HTTP url to open (must use http:// or https://)1004 * @param keepAlive whether not to set header {@code Connection=close}1005 * @param followRedirects wheter or not to follow HTTP(S) redirects1006 * @return An open HTTP connection to the given URL1007 * @throws IOException if an I/O exception occurs1008 * @since 86501009 * @deprecated Use {@link HttpClient} instead1010 */1011 @Deprecated1012 public static HttpURLConnection openHttpConnection(URL httpURL, boolean keepAlive, boolean followRedirects) throws IOException {1013 HttpURLConnection connection = openHttpConnection(httpURL, keepAlive);1014 if (followRedirects) {1015 for (int i = 0; i < 5; i++) {1016 if (connection.getResponseCode() == 302) {1017 connection = openHttpConnection(new URL(connection.getHeaderField("Location")), keepAlive);1018 } else {1019 break;1020 }1021 }1022 }1023 return connection;1024 872 } 1025 873
Note:
See TracChangeset
for help on using the changeset viewer.