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