001package com.nimbusds.oauth2.sdk.http;
002
003
004import java.io.BufferedReader;
005import java.io.IOException;
006import java.io.InputStream;
007import java.io.InputStreamReader;
008import java.net.HttpURLConnection;
009import java.net.URL;
010import javax.mail.internet.ContentType;
011import javax.mail.internet.ParseException;
012
013import net.jcip.annotations.ThreadSafe;
014import org.apache.commons.io.input.BoundedInputStream;
015
016
017/**
018 * The default retriever of resources specified by URL. Provides setting of
019 * HTTP connect and read timeouts as well as a size limit of the retrieved
020 * entity. Caching header directives are not honoured.
021 */
022@ThreadSafe
023@Deprecated
024public class DefaultResourceRetriever extends AbstractRestrictedResourceRetriever implements RestrictedResourceRetriever {
025
026
027        /**
028         * The system line separator.
029         */
030        private final String lineSeparator;
031        
032        
033        /**
034         * Creates a new resource retriever. The HTTP timeouts and entity size
035         * limit are set to zero (infinite).
036         */
037        public DefaultResourceRetriever() {
038        
039                this(0, 0);     
040        }
041        
042        
043        /**
044         * Creates a new resource retriever. The HTTP entity size limit is set
045         * to zero (infinite).
046         *
047         * @param connectTimeout The HTTP connects timeout, in milliseconds, 
048         *                       zero for infinite. Must not be negative.
049         * @param readTimeout    The HTTP read timeout, in milliseconds, zero 
050         *                       for infinite. Must not be negative.
051         */
052        public DefaultResourceRetriever(final int connectTimeout, final int readTimeout) {
053
054                this(connectTimeout, readTimeout, 0);
055        }
056
057
058        /**
059         * Creates a new resource retriever.
060         *
061         * @param connectTimeout The HTTP connects timeout, in milliseconds,
062         *                       zero for infinite. Must not be negative.
063         * @param readTimeout    The HTTP read timeout, in milliseconds, zero
064         *                       for infinite. Must not be negative.
065         * @param sizeLimit      The HTTP entity size limit, in bytes, zero for
066         *                       infinite. Must not be negative.
067         */
068        public DefaultResourceRetriever(final int connectTimeout, final int readTimeout, final int sizeLimit) {
069        
070                super(connectTimeout, readTimeout, sizeLimit);
071                lineSeparator = System.getProperty("line.separator");
072        }
073
074
075        @Override
076        public Resource retrieveResource(final URL url)
077                throws IOException {
078                
079                HttpURLConnection con;
080                try {
081                        con = (HttpURLConnection)url.openConnection();
082                } catch (ClassCastException e) {
083                        throw new IOException("Couldn't open HTTP(S) connection: " + e.getMessage(), e);
084                }
085
086                con.setConnectTimeout(getConnectTimeout());
087                con.setReadTimeout(getReadTimeout());
088
089                StringBuilder sb = new StringBuilder();
090
091                InputStream inputStream = con.getInputStream();
092
093                if (getSizeLimit() > 0) {
094                        inputStream = new BoundedInputStream(inputStream, getSizeLimit());
095                }
096
097                BufferedReader input = new BufferedReader(new InputStreamReader(inputStream));
098
099                String line;
100
101                while ((line = input.readLine()) != null) {
102
103                        sb.append(line);
104                        sb.append(lineSeparator);
105                }
106
107                input.close();
108
109                // Check HTTP code + message
110                final int statusCode = con.getResponseCode();
111                final String statusMessage = con.getResponseMessage();
112
113                // Ensure 2xx status code
114                if (statusCode > 299 || statusCode < 200) {
115                        throw new IOException("HTTP " + statusCode + ": " + statusMessage);
116                }
117
118                // Parse the Content-Type header
119                ContentType contentType = null;
120
121                if (con.getContentType() != null) {
122                        try {
123                                contentType = new ContentType(con.getContentType());
124                        } catch (ParseException e) {
125                                throw new IOException("Couldn't parse Content-Type header: " + e.getMessage(), e);
126                        }
127                }
128                
129                return new Resource(sb.toString(), contentType);
130        }
131}