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 static java.time.format.DateTimeFormatter.*;
020import static org.apache.juneau.commons.utils.ThrowableUtils.*;
021import static org.apache.juneau.commons.utils.Utils.*;
022
023import java.time.*;
024import java.util.*;
025import java.util.function.*;
026
027import org.apache.juneau.http.annotation.*;
028
029/**
030 * Represents a parsed <l>If-Range</l> HTTP request header.
031 *
032 * <p>
033 * If the entity is unchanged, send me the part(s) that I am missing; otherwise, send me the entire new entity.
034 *
035 * <h5 class='figure'>Example</h5>
036 * <p class='bcode'>
037 *    If-Range: "737060cd8c284d8af7ad3082f209582d"
038 * </p>
039 *
040 * <h5 class='topic'>RFC2616 Specification</h5>
041 *
042 * 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
043 * in its cache, it could use the Range request-header with a conditional GET (using either or both of
044 * If-Unmodified-Since and If-Match.)
045 * However, if the condition fails because the entity has been modified, the client would then have to make a second
046 * request to obtain the entire current entity-body.
047 *
048 * <p>
049 * The If-Range header allows a client to "short-circuit" the second request.
050 * Informally, its meaning is `if the entity is unchanged, send me the part(s) that I am missing; otherwise, send me
051 * the entire new entity'.
052 * <p class='bcode'>
053 *    If-Range = "If-Range" ":" ( entity-tag | HTTP-date )
054 * </p>
055 *
056 * <p>
057 * If the client has no entity tag for an entity, but does have a Last- Modified date, it MAY use that date in an
058 * If-Range header.
059 * (The server can distinguish between a valid HTTP-date and any form of entity-tag by examining no more than two
060 * characters.)
061 * The If-Range header SHOULD only be used together with a Range header, and MUST be ignored if the request does not
062 * include a Range header, or if the server does not support the sub-range operation.
063 *
064 * <p>
065 * If the entity tag given in the If-Range header matches the current entity tag for the entity, then the server SHOULD
066 * provide the specified sub-range of the entity using a 206 (Partial content) response.
067 * If the entity tag does not match, then the server SHOULD return the entire entity using a 200 (OK) response.
068 *
069 * <h5 class='section'>See Also:</h5><ul>
070 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a>
071 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
072 * </ul>
073 *
074 * @serial exclude
075 */
076@Header("If-Range")
077public class IfRange extends BasicDateHeader {
078   private static final long serialVersionUID = 1L;
079   private static final String NAME = "If-Range";
080
081   /**
082    * Static creator.
083    *
084    * @param value
085    *    The header value.
086    *    <br>Can be <jk>null</jk>.
087    * @return A new header bean, or <jk>null</jk> if the name is <jk>null</jk> or empty or the value is <jk>null</jk>.
088    */
089   public static IfRange of(EntityTag value) {
090      return value == null ? null : new IfRange(value);
091   }
092
093   /**
094    * Static creator.
095    *
096    * @param value
097    *    The header value.
098    *    <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>).
099    *    <br>Can be <jk>null</jk>.
100    * @return A new header bean, or <jk>null</jk> if the name is <jk>null</jk> or empty or the value is <jk>null</jk>.
101    */
102   public static IfRange of(String value) {
103      return value == null ? null : new IfRange(value);
104   }
105
106   /**
107    * Static creator with delayed value.
108    *
109    * <p>
110    * Header value is re-evaluated on each call to {@link #getValue()}.
111    *
112    * @param value
113    *    The supplier of the header value.
114    *    <br>Supplier must supply either {@link EntityTag} or {@link ZonedDateTime} objects.
115    *    <br>Can be <jk>null</jk>.
116    * @return A new header bean, or <jk>null</jk> if the name is <jk>null</jk> or empty or the value is <jk>null</jk>.
117    */
118   public static IfRange of(Supplier<?> value) {
119      return value == null ? null : new IfRange(value);
120   }
121
122   /**
123    * Static creator.
124    *
125    * @param value
126    *    The header value.
127    *    <br>Can be <jk>null</jk>.
128    * @return A new header bean, or <jk>null</jk> if the name is <jk>null</jk> or empty or the value is <jk>null</jk>.
129    */
130   public static IfRange of(ZonedDateTime value) {
131      return value == null ? null : new IfRange(value);
132   }
133
134   private static boolean isEtag(String s) {
135      return s.startsWith("\"") || s.startsWith("W/");
136   }
137
138   private final EntityTag value;
139
140   private final Supplier<?> supplier;
141
142   /**
143    * Constructor.
144    *
145    * @param value
146    *    The header value.
147    *    <br>Can be <jk>null</jk>.
148    */
149   public IfRange(EntityTag value) {
150      super(NAME, (String)null);
151      this.value = value;
152      this.supplier = null;
153   }
154
155   /**
156    * Constructor.
157    *
158    * @param value
159    *    The header value.
160    *    <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>).
161    *    <br>Can be <jk>null</jk>.
162    */
163   public IfRange(String value) {
164      super(NAME, isEtag(value) ? null : value);
165      this.value = isEtag(value) ? EntityTag.of(value) : null;
166      this.supplier = null;
167   }
168
169   /**
170    * Constructor with delayed value.
171    *
172    * <p>
173    * Header value is re-evaluated on each call to {@link #getValue()}.
174    *
175    * @param value
176    *    The supplier of the header value.
177    *    <br>Supplier must supply either {@link EntityTag} or {@link ZonedDateTime} objects.
178    *    <br>Can be <jk>null</jk>.
179    */
180   public IfRange(Supplier<?> value) {
181      super(NAME, (String)null);
182      this.value = null;
183      supplier = value;
184   }
185
186   /**
187    * Constructor.
188    *
189    * @param value
190    *    The header value.
191    *    <br>Can be <jk>null</jk>.
192    */
193   public IfRange(ZonedDateTime value) {
194      super(NAME, value);
195      this.value = null;
196      this.supplier = null;
197   }
198
199   /**
200    * Returns this header as an {@link EntityTag}.
201    *
202    * @return This header as an {@link EntityTag}.
203    */
204   public Optional<EntityTag> asEntityTag() {
205      if (nn(supplier)) {
206         Object o = supplier.get();
207         return opt(o instanceof EntityTag o2 ? o2 : null);
208      }
209      return opt(value);
210   }
211
212   @Override /* Overridden from Header */
213   public String getValue() {
214      if (nn(supplier)) {
215         Object o = supplier.get();
216         if (o == null)
217            return null;
218         if (o instanceof EntityTag o2) {
219            return o2.toString();
220         } else if (o instanceof ZonedDateTime o2) {
221            return RFC_1123_DATE_TIME.format(o2);
222         }
223         throw rex("Invalid object type returned by supplier: {0}", cn(o));
224      }
225      if (nn(value))
226         return s(value);
227      return super.getValue();
228   }
229}