001
002/*
003 * This file is originally coming from the Dasobert library.
004 * Author: Andreas Prlic
005 *
006 *                  BioJava development code
007 *
008 * This code may be freely distributed and modified under the
009 * terms of the GNU Lesser General Public Licence.  This should
010 * be distributed with the code.  If you do not have a copy,
011 * see:
012 *
013 *      http://www.gnu.org/copyleft/lesser.html
014 *
015 * Copyright for this code is held jointly by the individual
016 * authors.  These should be listed in @author doc comments.
017 *
018 * For more information on the BioJava project and its aims,
019 * or to join the biojava-l mailing list, visit the home page
020 * at:
021 *
022 *      http://www.biojava.org/
023 *
024 * Created on Jul 25, 2006
025 *
026 */
027
028package org.biojava.nbio.structure.align.util;
029
030import java.io.IOException;
031import java.io.InputStream;
032import java.io.OutputStreamWriter;
033import java.net.URL;
034import java.net.URLConnection;
035import java.util.zip.GZIPInputStream;
036
037
038
039/**
040 * A class that takes care about opening URLConnections and sets the proper timeouts
041 * @author Andreas Prlic
042 * @author Anthony Bradley
043 * @since 5.0
044 */
045public class URLConnectionTools {
046
047        /** The default connection timeout in ms - 15 seconds*/
048        public static final int    DEFAULT_CONNECTION_TIMEOUT = 30000;
049
050        /**
051         * Open HttpURLConnection. Recommended way to open URL connections in Java 1.7 and 1.8.
052         * https://eventuallyconsistent.net/2011/08/02/working-with-urlconnection-and-timeouts/
053         * @param url URL to open
054         * @param timeout timeout in milli seconds
055         * @throws IOException an error in opening the URL
056         */
057        public static URLConnection openURLConnection(URL url, int timeout) throws IOException {
058                URLConnection huc = url.openConnection();
059                huc.setReadTimeout(timeout);
060                huc.setConnectTimeout(timeout);
061                return huc;
062        }
063
064
065        /**
066         * Open HttpURLConnection. Recommended way to open
067         * HttpURLConnections, since this take care of setting timeouts
068         * properly for java 1.4 and 1.5
069         * uses the DEFAULT_CONNECTION_TIMEOUT (= 15 seconds)
070         * @param url a URL to open a http connection to
071         * @return HttpURLConnect the opened connection
072         * @throws IOException an error in opening the URL
073         *
074         */
075        public static URLConnection openURLConnection(URL url) throws IOException {
076                return openURLConnection(url,DEFAULT_CONNECTION_TIMEOUT);
077        }
078
079        /**
080         * Connect to server and return result as an InputStream.
081         * always asks for response to be in GZIP encoded
082         * <p>
083         * The caller is responsible to close the returned InputStream not to cause
084         * resource leaks.
085         * @param url the URL to connect to
086         * @param timeout the timeout for the connection
087         * @return an {@link InputStream} of response
088         * @throws IOException due to an error opening the URL
089         *
090         */
091        public static InputStream getInputStream(URL url, int timeout) throws IOException
092        {
093                return getInputStream(url,true, timeout);
094        }
095
096
097        /**
098         * Connect to a URL and return result as an InputStream.
099         * always asks for response to be in GZIP encoded
100         * <p>
101         * The caller is responsible to close the returned InputStream not to cause
102         * resource leaks.
103         * @param url the input URL to be read
104         * @return an {@link InputStream} of response
105         * @throws IOException due to an error opening the URL
106         */
107        public static InputStream getInputStream(URL url) throws IOException
108        {
109                return getInputStream(url,true, DEFAULT_CONNECTION_TIMEOUT);
110        }
111
112        /**
113         * Open a URL and return an InputStream to it
114         * if acceptGzipEncoding == true, use GZIPEncoding to
115         * compress communication.
116         * <p>
117         * The caller is responsible to close the returned InputStream not to cause
118         * resource leaks.
119         * @param url the input URL to be read
120         * @param acceptGzipEncoding whether to accept Gzip encoding
121         * @param timeout
122         * @return an {@link InputStream} of response
123         * @throws IOException due to an error opening the URL
124         */
125        public static InputStream getInputStream(URL url, boolean acceptGzipEncoding, int timeout) throws IOException {
126                InputStream inStream = null ;
127                URLConnection huc = URLConnectionTools.openURLConnection(url,timeout);
128
129                if ( acceptGzipEncoding) huc.setRequestProperty("Accept-Encoding", "gzip");
130
131                String contentEncoding = huc.getContentEncoding();
132
133                inStream = huc.getInputStream();
134
135                if (contentEncoding != null) {
136                        if (contentEncoding.contains("gzip")) {
137                                inStream = new GZIPInputStream(inStream);
138                        }
139                }
140
141                return inStream;
142
143        }
144
145        /**
146         * Do a POST to a URL and return the response stream for further processing elsewhere.
147         * <p>
148         * The caller is responsible to close the returned InputStream not to cause
149         * resource leaks.
150         * @param url  the input URL to be read
151         * @param data the post data
152         * @return an {@link InputStream} of response
153         * @throws IOException due to an error opening the URL
154         */
155        public static InputStream doPOST(URL url, String data) throws IOException
156        {
157                return doPOST(url,data,DEFAULT_CONNECTION_TIMEOUT);
158        }
159
160        /**
161         * Do a POST to a URL and return the response stream for further processing elsewhere.
162         * <p>
163         * The caller is responsible to close the returned InputStream not to cause
164         * resource leaks.
165         * @param url the input URL to be read
166         * @param data the post data
167         * @param timeout
168         * @return an {@link InputStream} of response
169         * @throws IOException due to an error opening the URL
170         */
171        public static InputStream doPOST(URL url, String data, int timeout) throws IOException
172        {
173                URLConnection conn = openURLConnection(url, timeout);
174                conn.setDoOutput(true);
175                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
176                wr.write(data);
177                wr.flush();
178                return conn.getInputStream();
179        }
180
181
182}