View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.impl.cache;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.Internal;
32  import org.apache.hc.core5.annotation.ThreadingBehavior;
33  
34  /**
35   * Represents the values of the Cache-Control header in an HTTP request, which indicate whether and for how long
36   * the client is willing to cache the response.
37   *
38   * @since 5.3
39   */
40  @Internal
41  @Contract(threading = ThreadingBehavior.IMMUTABLE)
42  final class RequestCacheControl implements CacheControl {
43  
44      private final long maxAge;
45      private final long maxStale;
46      private final long minFresh;
47      private final boolean noCache;
48      private final boolean noStore;
49      private final boolean onlyIfCached;
50      private final long staleIfError;
51  
52      /**
53       * Flag for the 'no-transform' Cache-Control directive.
54       * If this field is true, then the 'no-transform' directive is present in the Cache-Control header.
55       * According to RFC 'no-transform' directive indicates that the cache MUST NOT transform the payload.
56       */
57      private final boolean noTransform;
58  
59      RequestCacheControl(final long maxAge, final long maxStale, final long minFresh, final boolean noCache,
60                                 final boolean noStore, final boolean onlyIfCached, final long staleIfError, final boolean noTransform) {
61          this.maxAge = maxAge;
62          this.maxStale = maxStale;
63          this.minFresh = minFresh;
64          this.noCache = noCache;
65          this.noStore = noStore;
66          this.onlyIfCached = onlyIfCached;
67          this.staleIfError = staleIfError;
68          this.noTransform = noTransform;
69      }
70  
71      /**
72       * Returns the max-age value from the Cache-Control header.
73       *
74       * @return The max-age value.
75       */
76      @Override
77      public long getMaxAge() {
78          return maxAge;
79      }
80  
81      /**
82       * Returns the max-stale value from the Cache-Control header.
83       *
84       * @return The max-stale value.
85       */
86      public long getMaxStale() {
87          return maxStale;
88      }
89  
90      /**
91       * Returns the min-fresh value from the Cache-Control header.
92       *
93       * @return The min-fresh value.
94       */
95      public long getMinFresh() {
96          return minFresh;
97      }
98  
99      /**
100      * Returns the no-cache flag from the Cache-Control header.
101      *
102      * @return The no-cache flag.
103      */
104     @Override
105     public boolean isNoCache() {
106         return noCache;
107     }
108 
109     /**
110      * Returns the no-store flag from the Cache-Control header.
111      *
112      * @return The no-store flag.
113      */
114     @Override
115     public boolean isNoStore() {
116         return noStore;
117     }
118 
119     /**
120      * Returns the only-if-cached flag from the Cache-Control header.
121      *
122      * @return The only-if-cached flag.
123      */
124     public boolean isOnlyIfCached() {
125         return onlyIfCached;
126     }
127 
128     /**
129      * Returns the stale-if-error value from the Cache-Control header.
130      *
131      * @return The stale-if-error value.
132      */
133     @Override
134     public long getStaleIfError() {
135         return staleIfError;
136     }
137 
138     @Override
139     public String toString() {
140         final StringBuilder buf = new StringBuilder();
141         buf.append("[");
142         if (maxAge >= 0) {
143             buf.append("max-age=").append(maxAge).append(",");
144         }
145         if (maxStale >= 0) {
146             buf.append("max-stale=").append(maxStale).append(",");
147         }
148         if (minFresh >= 0) {
149             buf.append("max-fresh=").append(minFresh).append(",");
150         }
151         if (noCache) {
152             buf.append("no-cache").append(",");
153         }
154         if (noStore) {
155             buf.append("no-store").append(",");
156         }
157         if (onlyIfCached) {
158             buf.append("only-if-cached").append(",");
159         }
160         if (staleIfError >= 0) {
161             buf.append("stale-if-error").append(staleIfError).append(",");
162         }
163         if (noTransform) {
164             buf.append("no-transform").append(",");
165         }
166         if (buf.charAt(buf.length() - 1) == ',') {
167             buf.setLength(buf.length() - 1);
168         }
169         buf.append("]");
170         return buf.toString();
171     }
172 
173     static Builder builder() {
174         return new Builder();
175     }
176 
177     static class Builder {
178 
179         private long maxAge = -1;
180         private long maxStale = -1;
181         private long minFresh = -1;
182         private boolean noCache;
183         private boolean noStore;
184         private boolean onlyIfCached;
185         private long staleIfError = -1;
186         private boolean noTransform;
187 
188         Builder() {
189         }
190 
191         public long getMaxAge() {
192             return maxAge;
193         }
194 
195         public Builder setMaxAge(final long maxAge) {
196             this.maxAge = maxAge;
197             return this;
198         }
199 
200         public long getMaxStale() {
201             return maxStale;
202         }
203 
204         public Builder setMaxStale(final long maxStale) {
205             this.maxStale = maxStale;
206             return this;
207         }
208 
209         public long getMinFresh() {
210             return minFresh;
211         }
212 
213         public Builder setMinFresh(final long minFresh) {
214             this.minFresh = minFresh;
215             return this;
216         }
217 
218         public boolean isNoCache() {
219             return noCache;
220         }
221 
222         public Builder setNoCache(final boolean noCache) {
223             this.noCache = noCache;
224             return this;
225         }
226 
227         public boolean isNoStore() {
228             return noStore;
229         }
230 
231         public Builder setNoStore(final boolean noStore) {
232             this.noStore = noStore;
233             return this;
234         }
235 
236         public boolean isOnlyIfCached() {
237             return onlyIfCached;
238         }
239 
240         public Builder setOnlyIfCached(final boolean onlyIfCached) {
241             this.onlyIfCached = onlyIfCached;
242             return this;
243         }
244 
245         public long getStaleIfError() {
246             return staleIfError;
247         }
248 
249         public Builder setStaleIfError(final long staleIfError) {
250             this.staleIfError = staleIfError;
251             return this;
252         }
253 
254         public boolean isNoTransform() {
255             return noTransform;
256         }
257 
258         public Builder setNoTransform(final boolean noTransform) {
259             this.noTransform = noTransform;
260             return this;
261         }
262 
263 
264         public RequestCacheControl build() {
265             return new RequestCacheControl(maxAge, maxStale, minFresh, noCache, noStore, onlyIfCached, staleIfError, noTransform);
266         }
267 
268     }
269 }