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
015import static org.apache.juneau.internal.StringUtils.*;
016
017import org.apache.juneau.internal.*;
018
019/**
020 * Represents a parsed <l>If-Range</l> HTTP request header.
021 * 
022 * <p>
023 * If the entity is unchanged, send me the part(s) that I am missing; otherwise, send me the entire new entity.
024 * 
025 * <h5 class='figure'>Example</h5>
026 * <p class='bcode'>
027 *    If-Range: "737060cd8c284d8af7ad3082f209582d"
028 * </p>
029 * 
030 * <h5 class='topic'>RFC2616 Specification</h5>
031 * 
032 * If a client has a partial copy of an entity in its cache, and wishes to have an up-to-date copy of the entire entity
033 * in its cache, it could use the Range request-header with a conditional GET (using either or both of
034 * If-Unmodified-Since and If-Match.)
035 * However, if the condition fails because the entity has been modified, the client would then have to make a second
036 * request to obtain the entire current entity-body.
037 * 
038 * <p>
039 * The If-Range header allows a client to "short-circuit" the second request.
040 * Informally, its meaning is `if the entity is unchanged, send me the part(s) that I am missing; otherwise, send me
041 * the entire new entity'.
042 * <p class='bcode'>
043 *    If-Range = "If-Range" ":" ( entity-tag | HTTP-date )
044 * </p>
045 * 
046 * <p>
047 * If the client has no entity tag for an entity, but does have a Last- Modified date, it MAY use that date in an
048 * If-Range header.
049 * (The server can distinguish between a valid HTTP-date and any form of entity-tag by examining no more than two
050 * characters.)
051 * The If-Range header SHOULD only be used together with a Range header, and MUST be ignored if the request does not
052 * include a Range header, or if the server does not support the sub-range operation.
053 * 
054 * <p>
055 * If the entity tag given in the If-Range header matches the current entity tag for the entity, then the server SHOULD
056 * provide the specified sub-range of the entity using a 206 (Partial content) response.
057 * If the entity tag does not match, then the server SHOULD return the entire entity using a 200 (OK) response.
058 * 
059 * <h5 class='section'>See Also:</h5>
060 * <ul class='doctree'>
061 *    <li class='extlink'><a class='doclink' href='https://www.w3.org/Protocols/rfc2616/rfc2616.html'>Hypertext Transfer Protocol -- HTTP/1.1</a>
062 * </ul>
063 */
064public final class IfRange extends HeaderString {
065
066   /**
067    * Returns a parsed <code>If-Range</code> header.
068    * 
069    * @param value The <code>If-Range</code> header string.
070    * @return The parsed <code>If-Range</code> header, or <jk>null</jk> if the string was null.
071    */
072   public static IfRange forString(String value) {
073      if (value == null)
074         return null;
075      return new IfRange(value);
076   }
077
078   private IfRange(String value) {
079      super(value);
080   }
081
082   /**
083    * Returns this header value as a {@link java.util.Date} object.
084    * 
085    * @return This header value as a {@link java.util.Date} object, or <jk>null</jk> if the value is not a date.
086    */
087   public java.util.Date asDate() {
088      char c0 = charAt(value, 0), c1 = charAt(value, 1);
089      if (c0 == '*' || c0 == '"' || (c0 == 'W' && c1 == '/'))
090         return null;
091      return DateUtils.parseDate(toString());
092   }
093
094   /**
095    * Returns this header value as an {@link EntityValidator} object.
096    * 
097    * @return
098    *    This header value as a {@link EntityValidator} object, or <jk>null</jk> if the value is not an entity
099    *    validator.
100    */
101   public EntityValidator asValidator() {
102      char c0 = charAt(value, 0), c1 = charAt(value, 1);
103      if (c0 == '*' || c0 == '"' || (c0 == 'W' && c1 == '/'))
104         return new EntityValidator(value);
105      return null;
106   }
107}