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