001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.http;
019
020
021import java.io.BufferedReader;
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.InputStreamReader;
025import java.net.HttpURLConnection;
026import java.net.URL;
027import java.nio.charset.Charset;
028import javax.mail.internet.ContentType;
029import javax.mail.internet.ParseException;
030
031import com.nimbusds.jose.util.BoundedInputStream;
032import net.jcip.annotations.ThreadSafe;
033
034
035/**
036 * The default retriever of resources specified by URL. Provides setting of
037 * HTTP connect and read timeouts as well as a size limit of the retrieved
038 * entity. Caching header directives are not honoured.
039 */
040@ThreadSafe
041@Deprecated
042public class DefaultResourceRetriever extends AbstractRestrictedResourceRetriever implements RestrictedResourceRetriever {
043
044
045        /**
046         * The system line separator.
047         */
048        private final String lineSeparator;
049        
050        
051        /**
052         * Creates a new resource retriever. The HTTP timeouts and entity size
053         * limit are set to zero (infinite).
054         */
055        public DefaultResourceRetriever() {
056        
057                this(0, 0);     
058        }
059        
060        
061        /**
062         * Creates a new resource retriever. The HTTP entity size limit is set
063         * to zero (infinite).
064         *
065         * @param connectTimeout The HTTP connects timeout, in milliseconds, 
066         *                       zero for infinite. Must not be negative.
067         * @param readTimeout    The HTTP read timeout, in milliseconds, zero 
068         *                       for infinite. Must not be negative.
069         */
070        public DefaultResourceRetriever(final int connectTimeout, final int readTimeout) {
071
072                this(connectTimeout, readTimeout, 0);
073        }
074
075
076        /**
077         * Creates a new resource retriever.
078         *
079         * @param connectTimeout The HTTP connects timeout, in milliseconds,
080         *                       zero for infinite. Must not be negative.
081         * @param readTimeout    The HTTP read timeout, in milliseconds, zero
082         *                       for infinite. Must not be negative.
083         * @param sizeLimit      The HTTP entity size limit, in bytes, zero for
084         *                       infinite. Must not be negative.
085         */
086        public DefaultResourceRetriever(final int connectTimeout, final int readTimeout, final int sizeLimit) {
087        
088                super(connectTimeout, readTimeout, sizeLimit);
089                lineSeparator = System.getProperty("line.separator");
090        }
091
092
093        @Override
094        public Resource retrieveResource(final URL url)
095                throws IOException {
096                
097                HttpURLConnection con;
098                try {
099                        con = (HttpURLConnection)url.openConnection();
100                } catch (ClassCastException e) {
101                        throw new IOException("Couldn't open HTTP(S) connection: " + e.getMessage(), e);
102                }
103
104                con.setConnectTimeout(getConnectTimeout());
105                con.setReadTimeout(getReadTimeout());
106
107                StringBuilder sb = new StringBuilder();
108
109                InputStream inputStream = con.getInputStream();
110
111                if (getSizeLimit() > 0) {
112                        inputStream = new BoundedInputStream(inputStream, getSizeLimit());
113                }
114
115                BufferedReader input = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
116
117                String line;
118
119                while ((line = input.readLine()) != null) {
120
121                        sb.append(line);
122                        sb.append(lineSeparator);
123                }
124
125                input.close();
126
127                // Check HTTP code + message
128                final int statusCode = con.getResponseCode();
129                final String statusMessage = con.getResponseMessage();
130
131                // Ensure 2xx status code
132                if (statusCode > 299 || statusCode < 200) {
133                        throw new IOException("HTTP " + statusCode + ": " + statusMessage);
134                }
135
136                // Parse the Content-Type header
137                ContentType contentType = null;
138
139                if (con.getContentType() != null) {
140                        try {
141                                contentType = new ContentType(con.getContentType());
142                        } catch (ParseException e) {
143                                throw new IOException("Couldn't parse Content-Type header: " + e.getMessage(), e);
144                        }
145                }
146                
147                return new Resource(sb.toString(), contentType);
148        }
149}