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>If-Match</l> HTTP request header.
021 *
022 * <p>
023 * Only perform the action if the client supplied entity matches the same entity on the server.
024 * This is mainly for methods like PUT to only update a resource if it has not been modified since the user last
025 * updated it.
026 *
027 * <h5 class='figure'>Example</h5>
028 * <p class='bcode'>
029 *    If-Match: "737060cd8c284d8af7ad3082f209582d"
030 * </p>
031 *
032 * <h5 class='topic'>RFC2616 Specification</h5>
033 *
034 * The If-Match request-header field is used with a method to make it conditional.
035 * A client that has one or more entities previously obtained from the resource can verify that one of those entities
036 * is current by including a list of their associated entity tags in the If-Match header field.
037 * Entity tags are defined in section 3.11.
038 * The purpose of this feature is to allow efficient updates of cached information with a minimum amount of transaction
039 * overhead.
040 * It is also used, on updating requests, to prevent inadvertent modification of the wrong version of a resource.
041 * As a special case, the value "*" matches any current entity of the resource.
042 *
043 * <p class='bcode'>
044 *    If-Match = "If-Match" ":" ( "*" | 1#entity-tag )
045 * </p>
046 *
047 * <p>
048 * If any of the entity tags match the entity tag of the entity that would have been returned in the response to a
049 * similar GET request (without the If-Match header) on that resource, or if "*" is given and any current entity exists
050 * for that resource, then the server MAY perform the requested method as if the If-Match header field did not exist.
051 *
052 * <p>
053 * A server MUST use the strong comparison function (see section 13.3.3) to compare the entity tags in If-Match.
054 *
055 * <p>
056 * If none of the entity tags match, or if "*" is given and no current entity exists, the server MUST NOT perform the
057 * requested method, and MUST return a 412 (Precondition Failed) response.
058 * This behavior is most useful when the client wants to prevent an updating method, such as PUT, from modifying a
059 * resource that has changed since the client last retrieved it.
060 *
061 * <p>
062 * If the request would, without the If-Match header field, result in anything other than a 2xx or 412 status, then the
063 * If-Match header MUST be ignored.
064 *
065 * <p>
066 * The meaning of "If-Match: *" is that the method SHOULD be performed if the representation selected by the origin
067 * server (or by a cache, possibly using the Vary mechanism, see section 14.44) exists, and MUST NOT be performed if the
068 * representation does not exist.
069 *
070 * <p>
071 * A request intended to update a resource (e.g., a PUT) MAY include an If-Match header field to signal that the request
072 * method MUST NOT be applied if the entity corresponding to the If-Match value (a single entity tag) is no longer a
073 * representation of that resource.
074 * This allows the user to indicate that they do not wish the request to be successful if the resource has been changed
075 * without their knowledge.
076 *
077 * <p>
078 * Examples:
079 * <p class='bcode'>
080 *    If-Match: "xyzzy"
081 *    If-Match: "xyzzy", "r2d2xxxx", "c3piozzzz"
082 *    If-Match: *
083 * </p>
084 *
085 * <p>
086 * The result of a request having both an If-Match header field and either an If-None-Match or an If-Modified-Since
087 * header fields is undefined by this specification.
088 *
089 * <h5 class='section'>See Also:</h5><ul>
090 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
091 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
092 * </ul>
093 *
094 * @serial exclude
095 */
096@Header("If-Match")
097public class IfMatch extends BasicEntityTagsHeader {
098
099   //-----------------------------------------------------------------------------------------------------------------
100   // Static
101   //-----------------------------------------------------------------------------------------------------------------
102
103   private static final long serialVersionUID = 1L;
104   private static final String NAME = "If-Match";
105
106   /**
107    * Static creator.
108    *
109    * @param value
110    *    The header value.
111    *    <br>Must be a comma-delimited list of entity validator values (e.g. <js>"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""</js>).
112    *    <br>Can be <jk>null</jk>.
113    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
114    */
115   public static IfMatch of(String value) {
116      return value == null ? null : new IfMatch(value);
117   }
118
119   /**
120    * Static creator.
121    *
122    * @param value
123    *    The header value.
124    *    <br>Can be <jk>null</jk>.
125    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
126    */
127   public static IfMatch of(EntityTags value) {
128      return value == null ? null : new IfMatch(value);
129   }
130
131   /**
132    * Static creator with delayed value.
133    *
134    * <p>
135    * Header value is re-evaluated on each call to {@link #getValue()}.
136    *
137    * @param value
138    *    The supplier of the header value.
139    *    <br>Can be <jk>null</jk>.
140    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
141    */
142   public static IfMatch of(Supplier<EntityTags> value) {
143      return value == null ? null : new IfMatch(value);
144   }
145
146   //-----------------------------------------------------------------------------------------------------------------
147   // Instance
148   //-----------------------------------------------------------------------------------------------------------------
149
150   /**
151    * Constructor.
152    *
153    * @param value
154    *    The header value.
155    *    <br>Must be a comma-delimited list of entity validator values (e.g. <js>"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""</js>).
156    *    <br>Can be <jk>null</jk>.
157    */
158   public IfMatch(String value) {
159      super(NAME, value);
160   }
161
162   /**
163    * Constructor.
164    *
165    * @param value
166    *    The header value.
167    *    <br>Can be <jk>null</jk>.
168    */
169   public IfMatch(EntityTags value) {
170      super(NAME, value);
171   }
172
173   /**
174    * Constructor with delayed value.
175    *
176    * <p>
177    * Header value is re-evaluated on each call to {@link #getValue()}.
178    *
179    * @param value
180    *    The supplier of the header value.
181    *    <br>Can be <jk>null</jk>.
182    */
183   public IfMatch(Supplier<EntityTags> value) {
184      super(NAME, value);
185   }
186}