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;
014
015/**
016 * Represents a parsed <l>If-Match</l> HTTP request header.
017 * 
018 * <p>
019 * Only perform the action if the client supplied entity matches the same entity on the server.
020 * This is mainly for methods like PUT to only update a resource if it has not been modified since the user last
021 * updated it.
022 * 
023 * <h5 class='figure'>Example</h5>
024 * <p class='bcode'>
025 *    If-Match: "737060cd8c284d8af7ad3082f209582d"
026 * </p>
027 * 
028 * <h5 class='topic'>RFC2616 Specification</h5>
029 * 
030 * The If-Match request-header field is used with a method to make it conditional.
031 * A client that has one or more entities previously obtained from the resource can verify that one of those entities
032 * is current by including a list of their associated entity tags in the If-Match header field.
033 * Entity tags are defined in section 3.11.
034 * The purpose of this feature is to allow efficient updates of cached information with a minimum amount of transaction
035 * overhead.
036 * It is also used, on updating requests, to prevent inadvertent modification of the wrong version of a resource.
037 * As a special case, the value "*" matches any current entity of the resource.
038 * 
039 * <p class='bcode'>
040 *    If-Match = "If-Match" ":" ( "*" | 1#entity-tag )
041 * </p>
042 * 
043 * <p>
044 * If any of the entity tags match the entity tag of the entity that would have been returned in the response to a
045 * similar GET request (without the If-Match header) on that resource, or if "*" is given and any current entity exists
046 * for that resource, then the server MAY perform the requested method as if the If-Match header field did not exist.
047 * 
048 * <p>
049 * A server MUST use the strong comparison function (see section 13.3.3) to compare the entity tags in If-Match.
050 * 
051 * <p>
052 * If none of the entity tags match, or if "*" is given and no current entity exists, the server MUST NOT perform the
053 * requested method, and MUST return a 412 (Precondition Failed) response.
054 * This behavior is most useful when the client wants to prevent an updating method, such as PUT, from modifying a
055 * resource that has changed since the client last retrieved it.
056 * 
057 * <p>
058 * If the request would, without the If-Match header field, result in anything other than a 2xx or 412 status, then the
059 * If-Match header MUST be ignored.
060 * 
061 * <p>
062 * The meaning of "If-Match: *" is that the method SHOULD be performed if the representation selected by the origin
063 * server (or by a cache, possibly using the Vary mechanism, see section 14.44) exists, and MUST NOT be performed if the
064 * representation does not exist.
065 * 
066 * <p>
067 * A request intended to update a resource (e.g., a PUT) MAY include an If-Match header field to signal that the request
068 * method MUST NOT be applied if the entity corresponding to the If-Match value (a single entity tag) is no longer a
069 * representation of that resource.
070 * This allows the user to indicate that they do not wish the request to be successful if the resource has been changed
071 * without their knowledge.
072 * 
073 * <p>
074 * Examples:
075 * <p class='bcode'>
076 *    If-Match: "xyzzy"
077 *    If-Match: "xyzzy", "r2d2xxxx", "c3piozzzz"
078 *    If-Match: *
079 * </p>
080 * 
081 * <p>
082 * The result of a request having both an If-Match header field and either an If-None-Match or an If-Modified-Since
083 * header fields is undefined by this specification.
084 * 
085 * <h5 class='section'>See Also:</h5>
086 * <ul class='doctree'>
087 *    <li class='extlink'><a class='doclink' href='https://www.w3.org/Protocols/rfc2616/rfc2616.html'>Hypertext Transfer Protocol -- HTTP/1.1</a>
088 * </ul>
089 */
090public final class IfMatch extends HeaderEntityValidatorArray {
091
092   /**
093    * Returns a parsed <code>If-Match</code> header.
094    * 
095    * @param value The <code>If-Match</code> header string.
096    * @return The parsed <code>If-Match</code> header, or <jk>null</jk> if the string was null.
097    */
098   public static IfMatch forString(String value) {
099      if (value == null)
100         return null;
101      return new IfMatch(value);
102   }
103
104   private IfMatch(String value) {
105      super(value);
106   }
107}