Ignore:
Timestamp:
2015-12-26T23:42:00+01:00 (5 years ago)
Author:
simon04
Message:

see #12231 - Use HttpClient instead of some Utils.openHttpConnection usages

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r9169 r9171  
    88import java.io.IOException;
    99import java.io.InputStream;
    10 import java.io.InputStreamReader;
    1110import java.io.OutputStream;
    1211import java.net.HttpURLConnection;
    1312import java.net.URL;
    14 import java.nio.charset.StandardCharsets;
    1513import java.util.Map;
     14import java.util.Scanner;
    1615import java.util.concurrent.ConcurrentHashMap;
    1716import java.util.zip.GZIPInputStream;
     
    2019import org.openstreetmap.josm.data.Version;
    2120import org.openstreetmap.josm.io.Compression;
     21import org.openstreetmap.josm.io.UTFInputStreamReader;
    2222
    2323/**
     
    3838    private final Map<String, String> headers = new ConcurrentHashMap<>();
    3939    private int maxRedirects = Main.pref.getInteger("socket.maxredirects", 5);
     40    private boolean useCache;
     41    private boolean keepAlive;
    4042
    4143    private HttpClient(URL url, String requestMethod) {
     
    7072            connection.setIfModifiedSince(ifModifiedSince);
    7173        }
     74        connection.setUseCaches(useCache);
     75        if (!useCache) {
     76            connection.setRequestProperty("Cache-Control", "no-cache");
     77        }
     78        if (!keepAlive) {
     79            connection.setRequestProperty("Connection", "close");
     80        }
    7281        for (Map.Entry<String, String> header : headers.entrySet()) {
    7382            connection.setRequestProperty(header.getKey(), header.getValue());
     
    7887            try {
    7988                connection.connect();
     89                Main.info("{0} {1} => {2}", requestMethod, url, connection.getResponseCode());
    8090            } catch (IOException e) {
    8191                //noinspection ThrowableResultOfMethodCallIgnored
     
    157167
    158168        /**
    159          * Returns {@link #getContent()} wrapped in a buffered reader
     169         * Returns {@link #getContent()} wrapped in a buffered reader.
     170         *
     171         * Detects Unicode charset in use utilizing {@link UTFInputStreamReader}.
    160172         */
    161173        public BufferedReader getContentReader() throws IOException {
    162             return new BufferedReader(new InputStreamReader(getContent(), StandardCharsets.UTF_8));
     174            return new BufferedReader(
     175                    UTFInputStreamReader.create(getContent())
     176            );
     177        }
     178
     179        /**
     180         * Fetches the HTTP response as String.
     181         * @return the response
     182         * @throws IOException
     183         */
     184        public String fetchContent() throws IOException {
     185            try (Scanner scanner = new Scanner(getContentReader())) {
     186                return scanner.useDelimiter("\\A").next();
     187            }
    163188        }
    164189
     
    187212
    188213        /**
     214         * Returns the {@code Content-Length} header.
     215         */
     216        public long getContentLength() {
     217            return connection.getContentLengthLong();
     218        }
     219
     220        /**
    189221         * @see HttpURLConnection#disconnect()
    190222         */
     
    213245    public static HttpClient create(URL url, String requestMethod) {
    214246        return new HttpClient(url, requestMethod);
     247    }
     248
     249    /**
     250     * Sets whether not to set header {@code Cache-Control=no-cache}
     251     *
     252     * @param useCache whether not to set header {@code Cache-Control=no-cache}
     253     * @return {@code this}
     254     * @see HttpURLConnection#setUseCaches(boolean)
     255     */
     256    public HttpClient useCache(boolean useCache) {
     257        this.useCache = useCache;
     258        return this;
     259    }
     260
     261    /**
     262     * Sets whether not to set header {@code Connection=close}
     263     * <p/>
     264     * This might fix #7640, see <a href='https://web.archive.org/web/20140118201501/http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive'>here</a>.
     265     *
     266     * @param keepAlive whether not to set header {@code Connection=close}
     267     * @return {@code this}
     268     */
     269    public HttpClient keepAlive(boolean keepAlive) {
     270        this.keepAlive = keepAlive;
     271        return this;
    215272    }
    216273
Note: See TracChangeset for help on using the changeset viewer.