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