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 java.time.temporal.ChronoUnit.*; 021 022import java.time.*; 023import java.util.*; 024import java.util.function.*; 025 026import org.apache.juneau.assertions.*; 027import org.apache.juneau.common.utils.*; 028 029/** 030 * Category of headers that consist of a single HTTP-date. 031 * 032 * <p> 033 * <h5 class='figure'>Example</h5> 034 * <p class='bcode'> 035 * If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT 036 * </p> 037 * 038 * <h5 class='section'>See Also:</h5><ul> 039 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a> 040 * <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a> 041 * </ul> 042 * 043 * @serial exclude 044 */ 045public class BasicDateHeader extends BasicHeader { 046 047 //----------------------------------------------------------------------------------------------------------------- 048 // Static 049 //----------------------------------------------------------------------------------------------------------------- 050 051 private static final long serialVersionUID = 1L; 052 053 /** 054 * Static creator. 055 * 056 * @param name The header name. 057 * @param value 058 * The header value. 059 * <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>). 060 * <br>Can be <jk>null</jk>. 061 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 062 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 063 */ 064 public static BasicDateHeader of(String name, String value) { 065 return value == null ? null : new BasicDateHeader(name, value); 066 } 067 068 /** 069 * Static creator. 070 * 071 * @param name The header name. 072 * @param value 073 * The header value. 074 * <br>Can be <jk>null</jk>. 075 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 076 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 077 */ 078 public static BasicDateHeader of(String name, ZonedDateTime value) { 079 return value == null ? null : new BasicDateHeader(name, value); 080 } 081 082 /** 083 * Static creator with delayed value. 084 * 085 * <p> 086 * Header value is re-evaluated on each call to {@link #getValue()}. 087 * 088 * @param name The header name. 089 * @param value 090 * The supplier of the header value. 091 * <br>Can be <jk>null</jk>. 092 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 093 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 094 */ 095 public static BasicDateHeader of(String name, Supplier<ZonedDateTime> value) { 096 return value == null ? null : new BasicDateHeader(name, value); 097 } 098 099 //----------------------------------------------------------------------------------------------------------------- 100 // Instance 101 //----------------------------------------------------------------------------------------------------------------- 102 103 private final ZonedDateTime value; 104 private final Supplier<ZonedDateTime> supplier; 105 106 /** 107 * Constructor. 108 * 109 * @param name The header name. 110 * @param value 111 * The header value. 112 * <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>). 113 * <br>Can be <jk>null</jk>. 114 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 115 */ 116 public BasicDateHeader(String name, String value) { 117 super(name, value); 118 this.value = Utils.isEmpty(value) ? null : ZonedDateTime.from(RFC_1123_DATE_TIME.parse(value.toString())).truncatedTo(SECONDS); 119 this.supplier = null; 120 } 121 122 /** 123 * Constructor. 124 * 125 * @param name The header name. 126 * @param value 127 * The header value. 128 * <br>Can be <jk>null</jk>. 129 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 130 */ 131 public BasicDateHeader(String name, ZonedDateTime value) { 132 super(name, value); 133 this.value = value; 134 this.supplier = null; 135 } 136 137 /** 138 * Constructor with delayed value. 139 * 140 * <p> 141 * Header value is re-evaluated on each call to {@link #getValue()}. 142 * 143 * @param name The header name. 144 * @param value 145 * The supplier of the header value. 146 * <br>Can be <jk>null</jk>. 147 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 148 */ 149 public BasicDateHeader(String name, Supplier<ZonedDateTime> value) { 150 super(name, null); 151 this.value = null; 152 this.supplier = value; 153 } 154 155 @Override /* Header */ 156 public String getValue() { 157 ZonedDateTime x = value(); 158 return x == null ? null : RFC_1123_DATE_TIME.format(x); 159 } 160 161 /** 162 * Returns the header value as a {@link ZonedDateTime} wrapped in an {@link Optional}. 163 * 164 * @return The header value as a {@link ZonedDateTime} wrapped in an {@link Optional}. Never <jk>null</jk>. 165 */ 166 public Optional<ZonedDateTime> asZonedDateTime() { 167 return Utils.opt(value()); 168 } 169 170 /** 171 * Returns the header value as a {@link ZonedDateTime}. 172 * 173 * @return The header value as a {@link ZonedDateTime}. Can be <jk>null</jk>. 174 */ 175 public ZonedDateTime toZonedDateTime() { 176 return value(); 177 } 178 179 /** 180 * Provides the ability to perform fluent-style assertions on this header. 181 * 182 * <h5 class='section'>Examples:</h5> 183 * <p class='bjava'> 184 * <jc>// Validates the response body content is not expired.</jc> 185 * <jv>client</jv> 186 * .get(<jsf>URL</jsf>) 187 * .run() 188 * .getHeader(<js>"Expires"</js>).asDateHeader().assertZonedDateTime().isLessThan(<jk>new</jk> Date()); 189 * </p> 190 * 191 * @return A new fluent assertion object. 192 * @throws AssertionError If assertion failed. 193 */ 194 public FluentZonedDateTimeAssertion<BasicDateHeader> assertZonedDateTime() { 195 return new FluentZonedDateTimeAssertion<>(value(), this); 196 } 197 198 /** 199 * Return the value if present, otherwise return <c>other</c>. 200 * 201 * <p> 202 * This is a shortened form for calling <c>asZonedDateTime().orElse(<jv>other</jv>)</c>. 203 * 204 * @param other The value to be returned if there is no value present, can be <jk>null</jk>. 205 * @return The value, if present, otherwise <c>other</c>. 206 */ 207 public ZonedDateTime orElse(ZonedDateTime other) { 208 ZonedDateTime x = value(); 209 return x != null ? x : other; 210 } 211 212 private ZonedDateTime value() { 213 if (supplier != null) 214 return supplier.get(); 215 return value; 216 } 217}