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.classic.header; 018 019import static java.time.format.DateTimeFormatter.*; 020import static java.time.temporal.ChronoUnit.*; 021import static org.apache.juneau.commons.utils.Shorts.*; 022 023import java.time.*; 024import java.util.*; 025import java.util.function.*; 026 027import org.apache.juneau.bct.assertions.*; 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/JuneauRestCommon">juneau-rest-common</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 */ 045@SuppressWarnings({ 046 "java:S2160" // equals() inherited from BasicHeader compares name+value; typed ZonedDateTime field is accessed via getValue() 047}) 048public class BasicDateHeader extends BasicHeader { 049 private static final long serialVersionUID = 1L; 050 051 /** 052 * Static creator. 053 * 054 * @param name The header name. 055 * @param value 056 * The header value. 057 * <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>). 058 * <br>Can be <jk>null</jk>. 059 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 060 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 061 */ 062 public static BasicDateHeader of(String name, String value) { 063 return value == null ? null : new BasicDateHeader(name, value); 064 } 065 066 /** 067 * Static creator with delayed value. 068 * 069 * <p> 070 * Header value is re-evaluated on each call to {@link #getValue()}. 071 * 072 * @param name The header name. 073 * @param value 074 * The supplier of the header value. 075 * <br>Can be <jk>null</jk>. 076 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 077 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 078 */ 079 public static BasicDateHeader of(String name, Supplier<ZonedDateTime> value) { 080 return value == null ? null : new BasicDateHeader(name, value); 081 } 082 083 /** 084 * Static creator. 085 * 086 * @param name The header name. 087 * @param value 088 * The header value. 089 * <br>Can be <jk>null</jk>. 090 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 091 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 092 */ 093 public static BasicDateHeader of(String name, ZonedDateTime value) { 094 return value == null ? null : new BasicDateHeader(name, value); 095 } 096 097 private final ZonedDateTime value; 098 private final transient Supplier<ZonedDateTime> supplier; 099 100 /** 101 * Constructor. 102 * 103 * @param name The header name. 104 * @param value 105 * The header value. 106 * <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>). 107 * <br>Can be <jk>null</jk> to create a header with no value. 108 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 109 */ 110 public BasicDateHeader(String name, String value) { 111 super(name, value); 112 this.value = ie(value) ? null : ZonedDateTime.from(RFC_1123_DATE_TIME.parse(value)).truncatedTo(SECONDS); 113 this.supplier = null; 114 } 115 116 /** 117 * Constructor with delayed value. 118 * 119 * <p> 120 * Header value is re-evaluated on each call to {@link #getValue()}. 121 * 122 * @param name The header name. 123 * @param value 124 * The supplier of the header value. 125 * <br>Can be <jk>null</jk> to create a header with no value. 126 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 127 */ 128 public BasicDateHeader(String name, Supplier<ZonedDateTime> value) { 129 super(name, null); 130 this.value = null; 131 supplier = value; 132 } 133 134 /** 135 * Constructor. 136 * 137 * @param name The header name. 138 * @param value 139 * The header value. 140 * <br>Can be <jk>null</jk> to create a header with no value. 141 * @throws IllegalArgumentException If name is <jk>null</jk> or empty. 142 */ 143 public BasicDateHeader(String name, ZonedDateTime value) { 144 super(name, value); 145 this.value = value; 146 this.supplier = null; 147 } 148 149 /** 150 * Provides the ability to perform fluent-style assertions on this header. 151 * 152 * <h5 class='section'>Examples:</h5> 153 * <p class='bjava'> 154 * <jc>// Validates the response body content is not expired.</jc> 155 * <jv>client</jv> 156 * .get(<jsf>URL</jsf>) 157 * .run() 158 * .getHeader(<js>"Expires"</js>).asDateHeader().assertZonedDateTime().isLessThan(<jk>new</jk> Date()); 159 * </p> 160 * 161 * @return A new fluent assertion object. 162 * @throws AssertionError If assertion failed. 163 */ 164 public FluentZonedDateTimeAssertion<BasicDateHeader> assertZonedDateTime() { 165 return new FluentZonedDateTimeAssertion<>(value(), this); 166 } 167 168 /** 169 * Returns the header value as a {@link ZonedDateTime} wrapped in an {@link Optional}. 170 * 171 * @return The header value as a {@link ZonedDateTime} wrapped in an {@link Optional}. Never <jk>null</jk>. 172 */ 173 public Optional<ZonedDateTime> asZonedDateTime() { 174 return o(value()); 175 } 176 177 @Override /* Overridden from Header */ 178 public String getValue() { 179 ZonedDateTime x = value(); 180 return x == null ? null : RFC_1123_DATE_TIME.format(x); 181 } 182 183 /** 184 * Return the value if present, otherwise return <c>other</c>. 185 * 186 * <p> 187 * This is a shortened form for calling <c>asZonedDateTime().orElse(<jv>other</jv>)</c>. 188 * 189 * @param other The value to be returned if there is no value present, can be <jk>null</jk>. 190 * @return The value, if present, otherwise <c>other</c>. 191 */ 192 public ZonedDateTime orElse(ZonedDateTime other) { 193 ZonedDateTime x = value(); 194 return nn(x) ? x : other; 195 } 196 197 /** 198 * Returns the header value as a {@link ZonedDateTime}. 199 * 200 * @return The header value as a {@link ZonedDateTime}, or <jk>null</jk> if the header has no value. 201 */ 202 public ZonedDateTime toZonedDateTime() { 203 return value(); 204 } 205 206 private ZonedDateTime value() { 207 if (nn(supplier)) 208 return supplier.get(); 209 return value; 210 } 211}