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