com.nimbusds.jose.util.DefaultResourceRetriever Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nimbus-jose-jwt Show documentation
Show all versions of nimbus-jose-jwt Show documentation
Java library for Javascript Object Signing and Encryption (JOSE) and
JSON Web Tokens (JWT)
/*
* nimbus-jose-jwt
*
* Copyright 2012-2016, Connect2id Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.nimbusds.jose.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import net.jcip.annotations.ThreadSafe;
/**
* The default retriever of resources specified by URL. Provides setting of
* HTTP connect and read timeouts as well as a size limit of the retrieved
* entity. Caching header directives are not honoured.
*
* @author Vladimir Dzhuvinov
* @version 2016-11-28
*/
@ThreadSafe
public class DefaultResourceRetriever extends AbstractRestrictedResourceRetriever implements RestrictedResourceRetriever {
/**
* Creates a new resource retriever. The HTTP timeouts and entity size
* limit are set to zero (infinite).
*/
public DefaultResourceRetriever() {
this(0, 0);
}
/**
* Creates a new resource retriever. The HTTP entity size limit is set
* to zero (infinite).
*
* @param connectTimeout The HTTP connects timeout, in milliseconds,
* zero for infinite. Must not be negative.
* @param readTimeout The HTTP read timeout, in milliseconds, zero
* for infinite. Must not be negative.
*/
public DefaultResourceRetriever(final int connectTimeout, final int readTimeout) {
this(connectTimeout, readTimeout, 0);
}
/**
* Creates a new resource retriever.
*
* @param connectTimeout The HTTP connects timeout, in milliseconds,
* zero for infinite. Must not be negative.
* @param readTimeout The HTTP read timeout, in milliseconds, zero
* for infinite. Must not be negative.
* @param sizeLimit The HTTP entity size limit, in bytes, zero for
* infinite. Must not be negative.
*/
public DefaultResourceRetriever(final int connectTimeout, final int readTimeout, final int sizeLimit) {
super(connectTimeout, readTimeout, sizeLimit);
}
@Override
public Resource retrieveResource(final URL url)
throws IOException {
HttpURLConnection con;
try {
con = (HttpURLConnection)url.openConnection();
} catch (ClassCastException e) {
throw new IOException("Couldn't open HTTP(S) connection: " + e.getMessage(), e);
}
con.setConnectTimeout(getConnectTimeout());
con.setReadTimeout(getReadTimeout());
InputStream inputStream = con.getInputStream();
if (getSizeLimit() > 0) {
inputStream = new BoundedInputStream(inputStream, getSizeLimit());
}
final String content;
try {
content = IOUtils.readInputStreamToString(inputStream, Charset.forName("UTF-8"));
} finally {
inputStream.close();
}
// Check HTTP code + message
final int statusCode = con.getResponseCode();
final String statusMessage = con.getResponseMessage();
// Ensure 2xx status code
if (statusCode > 299 || statusCode < 200) {
throw new IOException("HTTP " + statusCode + ": " + statusMessage);
}
return new Resource(content, con.getContentType());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy