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;
018
019import java.net.*;
020import java.time.*;
021import java.util.function.*;
022
023import org.apache.juneau.commons.http.*;
024import org.apache.juneau.http.header.*;
025
026/**
027 * Static factory methods for creating common HTTP headers.
028 *
029 * <p>
030 * This is a convenience class that provides shorthand access to all named header types in
031 * {@code org.apache.juneau.http.header}.
032 *
033 * <p class='bjava'>
034 *    <jc>// Import statically for clean DSL-style usage</jc>
035 *    import static HttpHeaders.*;
036 *
037 *    RestRequest <jv>req</jv> = client.get(<js>"/api/resource"</js>)
038 *       .header(accept(<js>"application/json"</js>))
039 *       .header(authorization(<js>"Bearer "</js> + token));
040 * </p>
041 *
042 * <h5 class='section'>See Also:</h5><ul>
043 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/NextGenRestClient">Next-Generation REST Client</a>
044 * </ul>
045 *
046 * @since 9.2.1
047 */
048public final class HttpHeaders {
049
050   private HttpHeaders() {}
051
052   /** @param value The header value. @return A new header. */
053   public static Accept accept(String value) { return Accept.of(value); }
054   /** @param value Supplier of the wire header value. @return A new header. */
055   public static Accept acceptLazyWire(Supplier<String> value) { return Accept.ofLazyWire(value); }
056   /** @param value Parsed media ranges. @return A new header. */
057   public static Accept accept(MediaRanges value) { return Accept.of(value); }
058   /** @param value Supplier of parsed media ranges. @return A new header. */
059   public static Accept acceptLazyParsed(Supplier<MediaRanges> value) { return Accept.ofLazyParsed(value); }
060
061   /** @param value The header value. @return A new header. */
062   public static AcceptCharset acceptCharset(String value) { return AcceptCharset.of(value); }
063   /** @param value Supplier of the wire header value. @return A new header. */
064   public static AcceptCharset acceptCharsetLazyWire(Supplier<String> value) { return AcceptCharset.ofLazyWire(value); }
065   /** @param value Parsed string ranges. @return A new header. */
066   public static AcceptCharset acceptCharset(StringRanges value) { return AcceptCharset.of(value); }
067   /** @param value Supplier of string ranges. @return A new header. */
068   public static AcceptCharset acceptCharsetLazyParsed(Supplier<StringRanges> value) { return AcceptCharset.ofLazyParsed(value); }
069
070   /** @param value The header value. @return A new header. */
071   public static AcceptEncoding acceptEncoding(String value) { return AcceptEncoding.of(value); }
072   /** @param value Supplier of the wire header value. @return A new header. */
073   public static AcceptEncoding acceptEncodingLazyWire(Supplier<String> value) { return AcceptEncoding.ofLazyWire(value); }
074   /** @param value Parsed string ranges. @return A new header. */
075   public static AcceptEncoding acceptEncoding(StringRanges value) { return AcceptEncoding.of(value); }
076   /** @param value Supplier of string ranges. @return A new header. */
077   public static AcceptEncoding acceptEncodingLazyParsed(Supplier<StringRanges> value) { return AcceptEncoding.ofLazyParsed(value); }
078
079   /** @param value The header value. @return A new header. */
080   public static AcceptLanguage acceptLanguage(String value) { return AcceptLanguage.of(value); }
081   /** @param value Supplier of the wire header value. @return A new header. */
082   public static AcceptLanguage acceptLanguageLazyWire(Supplier<String> value) { return AcceptLanguage.ofLazyWire(value); }
083   /** @param value Parsed string ranges. @return A new header. */
084   public static AcceptLanguage acceptLanguage(StringRanges value) { return AcceptLanguage.of(value); }
085   /** @param value Supplier of string ranges. @return A new header. */
086   public static AcceptLanguage acceptLanguageLazyParsed(Supplier<StringRanges> value) { return AcceptLanguage.ofLazyParsed(value); }
087
088   /** @param value The header value. @return A new header. */
089   public static AcceptRanges acceptRanges(String value) { return AcceptRanges.of(value); }
090   /** @param value The header value supplier. @return A new header. */
091   public static AcceptRanges acceptRanges(Supplier<String> value) { return AcceptRanges.of(value); }
092
093   /** @param value The header value. @return A new header. */
094   public static Age age(String value) { return Age.of(value); }
095   /** @param seconds Age in seconds. @return A new header. */
096   public static Age age(int seconds) { return Age.of(seconds); }
097
098   /** @param value The header value. @return A new header. */
099   public static Allow allow(String value) { return Allow.of(value); }
100   /** @param methods Allowed methods. @return A new header. */
101   public static Allow allow(String... methods) { return Allow.of(methods); }
102   /** @param tokenSupplier Lazy token list. @return A new header. */
103   public static Allow allowLazyTokens(Supplier<String[]> tokenSupplier) { return Allow.ofLazyTokens(tokenSupplier); }
104
105   /** @param value The header value. @return A new header. */
106   public static Authorization authorization(String value) { return Authorization.of(value); }
107   /** @param value The header value supplier. @return A new header. */
108   public static Authorization authorization(Supplier<String> value) { return Authorization.of(value); }
109
110   /** @param value The header value. @return A new header. */
111   public static CacheControl cacheControl(String value) { return CacheControl.of(value); }
112   /** @param value The header value supplier. @return A new header. */
113   public static CacheControl cacheControl(Supplier<String> value) { return CacheControl.of(value); }
114
115   /** @param value The header value. @return A new header. */
116   public static Connection connection(String value) { return Connection.of(value); }
117
118   /** @param value The header value. @return A new header. */
119   public static ContentDisposition contentDisposition(String value) { return ContentDisposition.of(value); }
120   /** @param value Parsed string ranges. @return A new header. */
121   public static ContentDisposition contentDisposition(StringRanges value) { return ContentDisposition.of(value); }
122   /** @param value Supplier of string ranges. @return A new header. */
123   public static ContentDisposition contentDisposition(Supplier<StringRanges> value) { return ContentDisposition.ofLazyParsed(value); }
124
125   /** @param value The header value. @return A new header. */
126   public static ContentEncoding contentEncoding(String value) { return ContentEncoding.of(value); }
127
128   /** @param value The header value. @return A new header. */
129   public static ContentLanguage contentLanguage(String value) { return ContentLanguage.of(value); }
130
131   /** @param value The header value. @return A new header. */
132   public static ContentLength contentLength(String value) { return ContentLength.of(value); }
133   /** @param bytes The content length in bytes. @return A new header. */
134   public static ContentLength contentLength(long bytes) { return ContentLength.of(bytes); }
135
136   /** @param value The header value. @return A new header. */
137   public static ContentLocation contentLocation(String value) { return ContentLocation.of(value); }
138   /** @param uri Content location URI. @return A new header. */
139   public static ContentLocation contentLocation(URI uri) { return ContentLocation.of(uri); }
140
141   /** @param value The header value. @return A new header. */
142   public static ContentRange contentRange(String value) { return ContentRange.of(value); }
143
144   /** @param value The header value. @return A new header. */
145   public static ContentType contentType(String value) { return ContentType.of(value); }
146   /** @param value Supplier of the wire header value. @return A new header. */
147   public static ContentType contentTypeLazyWire(Supplier<String> value) { return ContentType.ofLazyWire(value); }
148   /** @param value Parsed media type. @return A new header. */
149   public static ContentType contentType(MediaType value) { return ContentType.of(value); }
150   /** @param value Supplier of media type. @return A new header. */
151   public static ContentType contentTypeLazyParsed(Supplier<MediaType> value) { return ContentType.ofLazyParsed(value); }
152
153   /** @param value The header value. @return A new header. */
154   public static Date date(String value) { return Date.of(value); }
155   /** @param value HTTP-date. @return A new header. */
156   public static Date date(ZonedDateTime value) { return Date.of(value); }
157
158   /** @param value The header value. @return A new header. */
159   public static ETag eTag(String value) { return ETag.of(value); }
160   /** @param value Parsed entity-tag. @return A new header. */
161   public static ETag eTag(EntityTag value) { return ETag.of(value); }
162
163   /** @param value The header value. @return A new header. */
164   public static Expect expect(String value) { return Expect.of(value); }
165
166   /** @param value The header value. @return A new header. */
167   public static Expires expires(String value) { return Expires.of(value); }
168   /** @param value HTTP-date. @return A new header. */
169   public static Expires expires(ZonedDateTime value) { return Expires.of(value); }
170
171   /** @param value The header value. @return A new header. */
172   public static Forwarded forwarded(String value) { return Forwarded.of(value); }
173
174   /** @param value The header value. @return A new header. */
175   public static From from(String value) { return From.of(value); }
176
177   /** @param value The header value. @return A new header. */
178   public static Host host(String value) { return Host.of(value); }
179
180   /** @param value The header value. @return A new header. */
181   public static IfMatch ifMatch(String value) { return IfMatch.of(value); }
182   /** @param value Parsed entity-tags. @return A new header. */
183   public static IfMatch ifMatch(EntityTags value) { return IfMatch.of(value); }
184
185   /** @param value The header value. @return A new header. */
186   public static IfModifiedSince ifModifiedSince(String value) { return IfModifiedSince.of(value); }
187   /** @param value HTTP-date. @return A new header. */
188   public static IfModifiedSince ifModifiedSince(ZonedDateTime value) { return IfModifiedSince.of(value); }
189
190   /** @param value The header value. @return A new header. */
191   public static IfNoneMatch ifNoneMatch(String value) { return IfNoneMatch.of(value); }
192   /** @param value Parsed entity-tags. @return A new header. */
193   public static IfNoneMatch ifNoneMatch(EntityTags value) { return IfNoneMatch.of(value); }
194
195   /** @param value The header value. @return A new header. */
196   public static IfRange ifRange(String value) { return IfRange.of(value); }
197   /** @param value Entity-tag. @return A new header. */
198   public static IfRange ifRange(EntityTag value) { return IfRange.of(value); }
199   /** @param value HTTP-date. @return A new header. */
200   public static IfRange ifRange(ZonedDateTime value) { return IfRange.of(value); }
201   /** @param value Supplier of tag or date. @return A new header. */
202   public static IfRange ifRange(Supplier<?> value) { return IfRange.of(value); }
203
204   /** @param value The header value. @return A new header. */
205   public static IfUnmodifiedSince ifUnmodifiedSince(String value) { return IfUnmodifiedSince.of(value); }
206   /** @param value HTTP-date. @return A new header. */
207   public static IfUnmodifiedSince ifUnmodifiedSince(ZonedDateTime value) { return IfUnmodifiedSince.of(value); }
208
209   /** @param value The header value. @return A new header. */
210   public static LastModified lastModified(String value) { return LastModified.of(value); }
211   /** @param value HTTP-date. @return A new header. */
212   public static LastModified lastModified(ZonedDateTime value) { return LastModified.of(value); }
213
214   /** @param value The header value. @return A new header. */
215   public static Location location(String value) { return Location.of(value); }
216   /** @param uri Redirect URI. @return A new header. */
217   public static Location location(URI uri) { return Location.of(uri); }
218
219   /** @param value The header value. @return A new header. */
220   public static MaxForwards maxForwards(String value) { return MaxForwards.of(value); }
221   /** @param hops Max forwards count. @return A new header. */
222   public static MaxForwards maxForwards(int hops) { return MaxForwards.of(hops); }
223
224   /** @param value The header value. @return A new header. */
225   public static Origin origin(String value) { return Origin.of(value); }
226
227   /** @param value The header value. @return A new header. */
228   public static Pragma pragma(String value) { return Pragma.of(value); }
229
230   /** @param value The header value. @return A new header. */
231   public static ProxyAuthenticate proxyAuthenticate(String value) { return ProxyAuthenticate.of(value); }
232
233   /** @param value The header value. @return A new header. */
234   public static ProxyAuthorization proxyAuthorization(String value) { return ProxyAuthorization.of(value); }
235
236   /** @param value The header value. @return A new header. */
237   public static Range range(String value) { return Range.of(value); }
238
239   /** @param value The header value. @return A new header. */
240   public static Referer referer(String value) { return Referer.of(value); }
241   /** @param uri Referer URI. @return A new header. */
242   public static Referer referer(URI uri) { return Referer.of(uri); }
243
244   /** @param value The header value. @return A new header. */
245   public static RetryAfter retryAfter(String value) { return RetryAfter.of(value); }
246   /** @param seconds Delta seconds. @return A new header. */
247   public static RetryAfter retryAfter(int seconds) { return RetryAfter.of(Integer.valueOf(seconds)); }
248   /** @param value HTTP-date. @return A new header. */
249   public static RetryAfter retryAfter(ZonedDateTime value) { return RetryAfter.of(value); }
250   /** @param value Supplier of seconds or date. @return A new header. */
251   public static RetryAfter retryAfter(Supplier<?> value) { return RetryAfter.of(value); }
252
253   /** @param value The header value. @return A new header. */
254   public static Server server(String value) { return Server.of(value); }
255
256   /** @param value The header value. @return A new header. */
257   public static TE te(String value) { return TE.of(value); }
258   /** @param value Parsed string ranges. @return A new header. */
259   public static TE te(StringRanges value) { return TE.of(value); }
260   /** @param value Supplier of the wire header value. @return A new header. */
261   public static TE teLazyWire(Supplier<String> value) { return TE.ofLazyWire(value); }
262   /** @param value Supplier of string ranges. @return A new header. */
263   public static TE teLazyParsed(Supplier<StringRanges> value) { return TE.ofLazyParsed(value); }
264
265   /** @param value The header value. @return A new header. */
266   public static Trailer trailer(String value) { return Trailer.of(value); }
267
268   /** @param value The header value. @return A new header. */
269   public static TransferEncoding transferEncoding(String value) { return TransferEncoding.of(value); }
270
271   /** @param value The header value. @return A new header. */
272   public static Upgrade upgrade(String value) { return Upgrade.of(value); }
273
274   /** @param value The header value. @return A new header. */
275   public static UserAgent userAgent(String value) { return UserAgent.of(value); }
276   /** @param value The header value supplier. @return A new header. */
277   public static UserAgent userAgent(Supplier<String> value) { return UserAgent.of(value); }
278
279   /** @param value The header value. @return A new header. */
280   public static Vary vary(String value) { return Vary.of(value); }
281
282   /** @param value The header value. @return A new header. */
283   public static Via via(String value) { return Via.of(value); }
284
285   /** @param value The header value. @return A new header. */
286   public static Warning warning(String value) { return Warning.of(value); }
287
288   /** @param value The header value. @return A new header. */
289   public static WwwAuthenticate wwwAuthenticate(String value) { return WwwAuthenticate.of(value); }
290
291   // Juneau-specific headers
292   /** @param value The header value. @return A new header. */
293   public static NoTrace noTrace(String value) { return NoTrace.of(value); }
294   /** @return A no-trace header with value {@code "true"}. */
295   public static NoTrace noTrace() { return NoTrace.of("true"); }
296
297   /** @param value The header value. @return A new header. */
298   public static Debug debug(String value) { return Debug.of(value); }
299   /** @return A debug header with value {@code "true"}. */
300   public static Debug debug() { return Debug.of("true"); }
301
302   /** @param value The header value. @return A new header. */
303   public static ClientVersion clientVersion(String value) { return ClientVersion.of(value); }
304
305   /** @param value The header value. @return A new header. */
306   public static Thrown thrown(String value) { return Thrown.of(value); }
307
308   /**
309    * Creates a generic {@link HttpHeader} with the given name and value.
310    *
311    * @param name The header name. Must not be <jk>null</jk>.
312    * @param value The header value. May be <jk>null</jk>.
313    * @return A new header. Never <jk>null</jk>.
314    */
315   public static HttpHeader header(String name, String value) {
316      return HttpHeaderBean.of(name, value);
317   }
318
319   /**
320    * Creates a generic {@link HttpHeader} with a lazy value supplier.
321    *
322    * @param name The header name. Must not be <jk>null</jk>.
323    * @param valueSupplier Supplier for the header value. Must not be <jk>null</jk>.
324    * @return A new header. Never <jk>null</jk>.
325    */
326   public static HttpHeader header(String name, Supplier<String> valueSupplier) {
327      return HttpHeaderBean.of(name, valueSupplier);
328   }
329}