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.cache;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.ThreadingBehavior;
32  
33  /**
34   * Represents the value of the Cache-Control header in an HTTP request containing cache
35   * control directives.
36   *
37   * @since 5.4
38   */
39  @Contract(threading = ThreadingBehavior.IMMUTABLE)
40  public final class RequestCacheControl implements CacheControl {
41  
42      private final long maxAge;
43      private final long maxStale;
44      private final long minFresh;
45      private final boolean noCache;
46      private final boolean noStore;
47      private final boolean onlyIfCached;
48      private final long staleIfError;
49  
50      RequestCacheControl(final long maxAge, final long maxStale, final long minFresh, final boolean noCache,
51                                 final boolean noStore, final boolean onlyIfCached, final long staleIfError) {
52          this.maxAge = maxAge;
53          this.maxStale = maxStale;
54          this.minFresh = minFresh;
55          this.noCache = noCache;
56          this.noStore = noStore;
57          this.onlyIfCached = onlyIfCached;
58          this.staleIfError = staleIfError;
59      }
60  
61      /**
62       * Returns the max-age value from the Cache-Control header.
63       *
64       * @return The max-age value.
65       */
66      @Override
67      public long getMaxAge() {
68          return maxAge;
69      }
70  
71      /**
72       * Returns the max-stale value from the Cache-Control header.
73       *
74       * @return The max-stale value.
75       */
76      public long getMaxStale() {
77          return maxStale;
78      }
79  
80      /**
81       * Returns the min-fresh value from the Cache-Control header.
82       *
83       * @return The min-fresh value.
84       */
85      public long getMinFresh() {
86          return minFresh;
87      }
88  
89      /**
90       * Returns the no-cache flag from the Cache-Control header.
91       *
92       * @return The no-cache flag.
93       */
94      @Override
95      public boolean isNoCache() {
96          return noCache;
97      }
98  
99      /**
100      * Returns the no-store flag from the Cache-Control header.
101      *
102      * @return The no-store flag.
103      */
104     @Override
105     public boolean isNoStore() {
106         return noStore;
107     }
108 
109     /**
110      * Returns the only-if-cached flag from the Cache-Control header.
111      *
112      * @return The only-if-cached flag.
113      */
114     public boolean isOnlyIfCached() {
115         return onlyIfCached;
116     }
117 
118     /**
119      * Returns the stale-if-error value from the Cache-Control header.
120      *
121      * @return The stale-if-error value.
122      */
123     @Override
124     public long getStaleIfError() {
125         return staleIfError;
126     }
127 
128     @Override
129     public String toString() {
130         final StringBuilder buf = new StringBuilder();
131         buf.append("[");
132         if (maxAge >= 0) {
133             buf.append("max-age=").append(maxAge).append(",");
134         }
135         if (maxStale >= 0) {
136             buf.append("max-stale=").append(maxStale).append(",");
137         }
138         if (minFresh >= 0) {
139             buf.append("min-fresh=").append(minFresh).append(",");
140         }
141         if (noCache) {
142             buf.append("no-cache").append(",");
143         }
144         if (noStore) {
145             buf.append("no-store").append(",");
146         }
147         if (onlyIfCached) {
148             buf.append("only-if-cached").append(",");
149         }
150         if (staleIfError >= 0) {
151             buf.append("stale-if-error").append(staleIfError).append(",");
152         }
153         if (buf.charAt(buf.length() - 1) == ',') {
154             buf.setLength(buf.length() - 1);
155         }
156         buf.append("]");
157         return buf.toString();
158     }
159 
160     public static Builder builder() {
161         return new Builder();
162     }
163 
164     public static final RequestCacheControl DEFAULT = builder().build();
165 
166     public static class Builder {
167 
168         private long maxAge = -1;
169         private long maxStale = -1;
170         private long minFresh = -1;
171         private boolean noCache;
172         private boolean noStore;
173         private boolean onlyIfCached;
174         private long staleIfError = -1;
175 
176         Builder() {
177         }
178 
179         public long getMaxAge() {
180             return maxAge;
181         }
182 
183         public Builder setMaxAge(final long maxAge) {
184             this.maxAge = maxAge;
185             return this;
186         }
187 
188         public long getMaxStale() {
189             return maxStale;
190         }
191 
192         public Builder setMaxStale(final long maxStale) {
193             this.maxStale = maxStale;
194             return this;
195         }
196 
197         public long getMinFresh() {
198             return minFresh;
199         }
200 
201         public Builder setMinFresh(final long minFresh) {
202             this.minFresh = minFresh;
203             return this;
204         }
205 
206         public boolean isNoCache() {
207             return noCache;
208         }
209 
210         public Builder setNoCache(final boolean noCache) {
211             this.noCache = noCache;
212             return this;
213         }
214 
215         public boolean isNoStore() {
216             return noStore;
217         }
218 
219         public Builder setNoStore(final boolean noStore) {
220             this.noStore = noStore;
221             return this;
222         }
223 
224         public boolean isOnlyIfCached() {
225             return onlyIfCached;
226         }
227 
228         public Builder setOnlyIfCached(final boolean onlyIfCached) {
229             this.onlyIfCached = onlyIfCached;
230             return this;
231         }
232 
233         public long getStaleIfError() {
234             return staleIfError;
235         }
236 
237         public Builder setStaleIfError(final long staleIfError) {
238             this.staleIfError = staleIfError;
239             return this;
240         }
241 
242         public RequestCacheControl build() {
243             return new RequestCacheControl(maxAge, maxStale, minFresh, noCache, noStore, onlyIfCached, staleIfError);
244         }
245 
246     }
247 }