001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http.header;
014
015import java.util.function.*;
016
017import org.apache.juneau.http.annotation.*;
018
019/**
020 * Represents a parsed <l>Cache-Control</l> HTTP request header.
021 *
022 * <p>
023 * Used to specify directives that must be obeyed by all caching mechanisms along the request-response chain.
024 *
025 * <h5 class='figure'>Example</h5>
026 * <p class='bcode'>
027 *    Cache-Control: no-cache
028 * </p>
029 *
030 * <h5 class='topic'>RFC2616 Specification</h5>
031 *
032 * The Cache-Control general-header field is used to specify directives that MUST be obeyed by all caching mechanisms
033 * along the request/response chain.
034 * The directives specify behavior intended to prevent caches from adversely interfering with the request or response.
035 * These directives typically override the default caching algorithms.
036 * Cache directives are unidirectional in that the presence of a directive in a request does not imply that the same
037 * directive is to be given in the response.
038 *
039 * <p>
040 * Note that HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache (see section
041 * 14.32).
042 *
043 * <p>
044 * Cache directives MUST be passed through by a proxy or gateway application, regardless of their significance to that
045 * application, since the directives might be applicable to all recipients along the request/response chain.
046 * It is not possible to specify a cache- directive for a specific cache.
047 *
048 * <p class='bcode'>
049 *    Cache-Control   = "Cache-Control" ":" 1#cache-directive
050 *    cache-directive = cache-request-directive
051 *         | cache-response-directive
052 *    cache-request-directive =
053 *           "no-cache"                          ; Section 14.9.1
054 *         | "no-store"                          ; Section 14.9.2
055 *         | "max-age" "=" delta-seconds         ; Section 14.9.3, 14.9.4
056 *         | "max-stale" [ "=" delta-seconds ]   ; Section 14.9.3
057 *         | "min-fresh" "=" delta-seconds       ; Section 14.9.3
058 *         | "no-transform"                      ; Section 14.9.5
059 *         | "only-if-cached"                    ; Section 14.9.4
060 *         | cache-extension                     ; Section 14.9.6
061 *    cache-response-directive =
062 *           "public"                               ; Section 14.9.1
063 *         | "private" [ "=" &lt;"&gt; 1#field-name &lt;"&gt; ] ; Section 14.9.1
064 *         | "no-cache" [ "=" &lt;"&gt; 1#field-name &lt;"&gt; ]; Section 14.9.1
065 *         | "no-store"                             ; Section 14.9.2
066 *         | "no-transform"                         ; Section 14.9.5
067 *         | "must-revalidate"                      ; Section 14.9.4
068 *         | "proxy-revalidate"                     ; Section 14.9.4
069 *         | "max-age" "=" delta-seconds            ; Section 14.9.3
070 *         | "s-maxage" "=" delta-seconds           ; Section 14.9.3
071 *         | cache-extension                        ; Section 14.9.6
072 *    cache-extension = token [ "=" ( token | quoted-string ) ]
073 * </p>
074 *
075 * <p>
076 * When a directive appears without any 1#field-name parameter, the directive applies to the entire request or response.
077 * When such a directive appears with a 1#field-name parameter, it applies only to the named field or fields, and not
078 * to the rest of the request or response. This mechanism supports extensibility; implementations of future versions
079 * of the HTTP protocol might apply these directives to header fields not defined in HTTP/1.1.
080 *
081 * <p>
082 * The cache-control directives can be broken down into these general categories:
083 * <ul>
084 *    <li>Restrictions on what are cacheable; these may only be imposed by the origin server.
085 *    <li>Restrictions on what may be stored by a cache; these may be imposed by either the origin server or the user
086 *       agent.
087 *    <li>Modifications of the basic expiration mechanism; these may be imposed by either the origin server or the
088 *       user agent.
089 *    <li>Controls over cache revalidation and reload; these may only be imposed by a user agent.
090 *    <li>Control over transformation of entities.
091 *    <li>Extensions to the caching system.
092 * </ul>
093 *
094 * <h5 class='section'>See Also:</h5><ul>
095 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
096 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
097 * </ul>
098 *
099 * @serial exclude
100 */
101@Header("Cache-Control")
102public class CacheControl extends BasicStringHeader {
103
104   //-----------------------------------------------------------------------------------------------------------------
105   // Static
106   //-----------------------------------------------------------------------------------------------------------------
107
108   private static final long serialVersionUID = 1L;
109   private static final String NAME = "Cache-Control";
110
111   /**
112    * Static creator.
113    *
114    * @param value
115    *    The header value.
116    *    <br>Can be <jk>null</jk>.
117    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
118    */
119   public static CacheControl of(String value) {
120      return value == null ? null : new CacheControl(value);
121   }
122
123   /**
124    * Static creator with delayed value.
125    *
126    * <p>
127    * Header value is re-evaluated on each call to {@link #getValue()}.
128    *
129    * @param value
130    *    The supplier of the header value.
131    *    <br>Can be <jk>null</jk>.
132    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
133    */
134   public static CacheControl of(Supplier<String> value) {
135      return value == null ? null : new CacheControl(value);
136   }
137
138   //-----------------------------------------------------------------------------------------------------------------
139   // Instance
140   //-----------------------------------------------------------------------------------------------------------------
141
142   /**
143    * Constructor.
144    *
145    * @param value
146    *    The header value.
147    *    <br>Can be <jk>null</jk>.
148    */
149   public CacheControl(String value) {
150      super(NAME, value);
151   }
152
153   /**
154    * Constructor with delayed value.
155    *
156    * <p>
157    * Header value is re-evaluated on each call to {@link #getValue()}.
158    *
159    * @param value
160    *    The supplier of the header value.
161    *    <br>Can be <jk>null</jk>.
162    */
163   public CacheControl(Supplier<String> value) {
164      super(NAME, value);
165   }
166}