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 * <ul class='seealso'>
061 *    <li class='extlink'>{@doc RFC2616}
062 * </ul>
063 */
064@Header("If-Range")
065public final class IfRange extends HeaderString {
066
067   /**
068    * Returns a parsed <c>If-Range</c> header.
069    *
070    * @param value The <c>If-Range</c> header string.
071    * @return The parsed <c>If-Range</c> header, or <jk>null</jk> if the string was null.
072    */
073   public static IfRange forString(String value) {
074      if (value == null)
075         return null;
076      return new IfRange(value);
077   }
078
079   private IfRange(String value) {
080      super(value);
081   }
082
083   /**
084    * Returns this header value as a {@link java.util.Date} object.
085    *
086    * @return This header value as a {@link java.util.Date} object, or <jk>null</jk> if the value is not a date.
087    */
088   public java.util.Date asDate() {
089      char c0 = charAt(value, 0), c1 = charAt(value, 1);
090      if (c0 == '*' || c0 == '"' || (c0 == 'W' && c1 == '/'))
091         return null;
092      return DateUtils.parseDate(toString());
093   }
094
095   /**
096    * Returns this header value as an {@link EntityValidator} object.
097    *
098    * @return
099    *    This header value as a {@link EntityValidator} object, or <jk>null</jk> if the value is not an entity
100    *    validator.
101    */
102   public EntityValidator asValidator() {
103      char c0 = charAt(value, 0), c1 = charAt(value, 1);
104      if (c0 == '*' || c0 == '"' || (c0 == 'W' && c1 == '/'))
105         return new EntityValidator(value);
106      return null;
107   }
108}