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 java.util.function.*;
020
021import org.apache.juneau.http.annotation.*;
022
023/**
024 * Represents a parsed <l>Authorization</l> HTTP request header.
025 *
026 * <p>
027 * Authentication credentials for HTTP authentication.
028 *
029 * <h5 class='figure'>Example</h5>
030 * <p class='bcode'>
031 *    Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
032 * </p>
033 *
034 * <h5 class='topic'>RFC2616 Specification</h5>
035 *
036 * A user agent that wishes to authenticate itself with a server--usually, but not necessarily, after receiving a 401
037 * response--does so by including an Authorization request-header field with the request.
038 *
039 * <p>
040 * The Authorization field value consists of credentials containing the authentication information of the user agent for
041 * the realm of the resource being requested.
042 *
043 * <p class='bcode'>
044 *    Authorization  = "Authorization" ":" credentials
045 * </p>
046 *
047 * <p>
048 * HTTP access authentication is described in "HTTP Authentication: Basic and Digest Access Authentication".
049 *
050 * <p>
051 * If a request is authenticated and a realm specified, the same credentials SHOULD be valid for all other requests
052 * within this realm (assuming that the authentication scheme itself does not require otherwise, such as credentials
053 * that vary according to a challenge value or using synchronized clocks).
054 *
055 * <p>
056 * When a shared cache (see section 13.7) receives a request containing an Authorization field, it MUST NOT return the
057 * corresponding response as a reply to any other request, unless one of the following specific exceptions holds:
058 * <ol>
059 *    <li>If the response includes the "s-maxage" cache-control directive, the cache MAY use that response in replying
060 *       to a subsequent request.
061 *       But (if the specified maximum age has passed) a proxy cache MUST first revalidate it with the origin
062 *       server, using the request-headers from the new request to allow the origin server to authenticate the new
063 *       request.
064 *       (This is the defined behavior for s-maxage.)
065 *       If the response includes "s-maxage=0", the proxy MUST always revalidate it before re-using it.
066 *    <li>If the response includes the "must-revalidate" cache-control directive, the cache MAY use that response in
067 *       replying to a subsequent request.
068 *       But if the response is stale, all caches MUST first revalidate it with the origin server, using the
069 *       request-headers from the new request to allow the origin server to authenticate the new request.
070 *    <li>If the response includes the "public" cache-control directive, it MAY be returned in reply to any subsequent
071 *       request.
072 * </ol>
073 *
074 * <h5 class='section'>See Also:</h5><ul>
075 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a>
076 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
077 * </ul>
078 *
079 * @serial exclude
080 */
081@Header("Authorization")
082public class Authorization extends BasicStringHeader {
083
084   //-----------------------------------------------------------------------------------------------------------------
085   // Static
086   //-----------------------------------------------------------------------------------------------------------------
087
088   private static final long serialVersionUID = 1L;
089   private static final String NAME = "Authorization";
090
091   /**
092    * Static creator.
093    *
094    * @param value
095    *    The header value.
096    *    <br>Can be <jk>null</jk>.
097    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
098    */
099   public static Authorization of(String value) {
100      return value == null ? null : new Authorization(value);
101   }
102
103   /**
104    * Static creator with delayed value.
105    *
106    * <p>
107    * Header value is re-evaluated on each call to {@link #getValue()}.
108    *
109    * @param value
110    *    The supplier of the header value.
111    *    <br>Can be <jk>null</jk>.
112    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
113    */
114   public static Authorization of(Supplier<String> value) {
115      return value == null ? null : new Authorization(value);
116   }
117
118   //-----------------------------------------------------------------------------------------------------------------
119   // Instance
120   //-----------------------------------------------------------------------------------------------------------------
121
122   /**
123    * Constructor.
124    *
125    * @param value
126    *    The header value.
127    *    <br>Can be <jk>null</jk>.
128    */
129   public Authorization(String value) {
130      super(NAME, value);
131   }
132
133   /**
134    * Constructor with delayed value.
135    *
136    * <p>
137    * Header value is re-evaluated on each call to {@link #getValue()}.
138    *
139    * @param value
140    *    The supplier of the header value.
141    *    <br>Can be <jk>null</jk>.
142    */
143   public Authorization(Supplier<String> value) {
144      super(NAME, value);
145   }
146}