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