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.header; 014 015import java.util.function.*; 016 017import org.apache.juneau.http.*; 018import org.apache.juneau.http.annotation.*; 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 ExtRFC2616} 062 * </ul> 063 */ 064@Header("If-Range") 065public class IfRange extends BasicDateHeader { 066 067 private static final long serialVersionUID = 1L; 068 069 private final Object value; // Only set if value is an entity tag. 070 071 /** 072 * Convenience creator. 073 * 074 * @param value 075 * The header value. 076 * <br>Can be any of the following: 077 * <ul> 078 * <li>{@link String} 079 * <li>Anything else - Converted to <c>String</c> then parsed. 080 * </ul> 081 * @return A new {@link IfRange} object. 082 */ 083 public static IfRange of(Object value) { 084 if (value == null) 085 return null; 086 return new IfRange(value); 087 } 088 089 /** 090 * Convenience creator using supplier. 091 * 092 * <p> 093 * Header value is re-evaluated on each call to {@link #getValue()}. 094 * 095 * @param value 096 * The header value supplier. 097 * <br>Can be any of the following: 098 * <ul> 099 * <li>{@link String} 100 * <li>Anything else - Converted to <c>String</c> then parsed. 101 * </ul> 102 * @return A new {@link IfRange} object. 103 */ 104 public static IfRange of(Supplier<?> value) { 105 if (value == null) 106 return null; 107 return new IfRange(value); 108 } 109 110 /** 111 * Constructor. 112 * 113 * @param value 114 * The header value. 115 * <br>Can be any of the following: 116 * <ul> 117 * <li>{@link String} 118 * <li>Anything else - Converted to <c>String</c> then parsed. 119 * <li>A {@link Supplier} of anything on this list. 120 * </ul> 121 */ 122 public IfRange(Object value) { 123 super("If-Range", dateValue(value)); 124 this.value = etagValue(value); 125 } 126 127 /** 128 * Constructor 129 * 130 * @param value 131 * The header value. 132 */ 133 public IfRange(String value) { 134 this((Object)value); 135 } 136 137 private static Object dateValue(Object o) { 138 Object o2 = unwrap(o); 139 if (o2 == null || isEtag(o2)) 140 return null; 141 return o; 142 } 143 144 private static Object etagValue(Object o) { 145 Object o2 = unwrap(o); 146 if (o2 == null || isEtag(o2)) 147 return o; 148 return null; 149 } 150 151 private static boolean isEtag(Object o) { 152 String s = o.toString(); 153 return s.startsWith("\"") || s.startsWith("W/"); 154 } 155 156 @Override /* Header */ 157 public String getValue() { 158 if (value == null) 159 return super.getValue(); 160 Object o = unwrap(value); 161 return (o == null ? null : o.toString()); 162 } 163 164 /** 165 * Returns this header as an {@link EntityTag}. 166 * 167 * @return This header as an {@link EntityTag}. 168 */ 169 public EntityTag asEntityTag() { 170 return (value == null ? null : EntityTag.of(value)); 171 } 172}