001/* 002 * nimbus-jose-jwt 003 * 004 * Copyright 2012-2016, Connect2id Ltd. 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.jose.util; 019 020 021import net.jcip.annotations.ThreadSafe; 022 023 024/** 025 * Abstract retrieval of resources by URL with HTTP timeout and entity size 026 * restrictions. 027 */ 028@ThreadSafe 029public abstract class AbstractRestrictedResourceRetriever implements RestrictedResourceRetriever { 030 031 032 /** 033 * The HTTP connect timeout, in milliseconds. 034 */ 035 private int connectTimeout; 036 037 038 /** 039 * The HTTP read timeout, in milliseconds. 040 */ 041 private int readTimeout; 042 043 044 /** 045 * The HTTP entity size limit, in bytes. 046 */ 047 private int sizeLimit; 048 049 050 /** 051 * Creates a new abstract restricted resource retriever. 052 * 053 * @param connectTimeout The HTTP connects timeout, in milliseconds, 054 * zero for infinite. Must not be negative. 055 * @param readTimeout The HTTP read timeout, in milliseconds, zero 056 * for infinite. Must not be negative. 057 * @param sizeLimit The HTTP entity size limit, in bytes, zero for 058 * infinite. Must not be negative. 059 */ 060 public AbstractRestrictedResourceRetriever(int connectTimeout, int readTimeout, int sizeLimit) { 061 setConnectTimeout(connectTimeout); 062 setReadTimeout(readTimeout); 063 setSizeLimit(sizeLimit); 064 } 065 066 067 @Override 068 public int getConnectTimeout() { 069 070 return connectTimeout; 071 } 072 073 074 @Override 075 public void setConnectTimeout(final int connectTimeoutMs) { 076 077 if (connectTimeoutMs < 0) { 078 throw new IllegalArgumentException("The connect timeout must not be negative"); 079 } 080 081 this.connectTimeout = connectTimeoutMs; 082 } 083 084 085 @Override 086 public int getReadTimeout() { 087 088 return readTimeout; 089 } 090 091 092 @Override 093 public void setReadTimeout(final int readTimeoutMs) { 094 095 if (readTimeoutMs < 0) { 096 throw new IllegalArgumentException("The read timeout must not be negative"); 097 } 098 099 this.readTimeout = readTimeoutMs; 100 } 101 102 103 @Override 104 public int getSizeLimit() { 105 106 return sizeLimit; 107 } 108 109 110 @Override 111 public void setSizeLimit(int sizeLimitBytes) { 112 113 if (sizeLimitBytes < 0) { 114 throw new IllegalArgumentException("The size limit must not be negative"); 115 } 116 117 this.sizeLimit = sizeLimitBytes; 118 } 119}