001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http.header;
014
015import java.util.function.*;
016
017import org.apache.juneau.http.annotation.*;
018
019/**
020 * Represents a parsed <l>Upgrade</l> HTTP request header.
021 *
022 * <p>
023 * Ask the client to upgrade to another protocol.
024 *
025 * <h5 class='figure'>Example</h5>
026 * <p class='bcode'>
027 *    Upgrade: HTTP/2.0, HTTPS/1.3, IRC/6.9, RTA/x11, websocket
028 * </p>
029 *
030 * <h5 class='topic'>RFC2616 Specification</h5>
031 *
032 * The Upgrade general-header allows the client to specify what additional communication protocols it supports and
033 * would like to use if the server finds it appropriate to switch protocols.
034 * The server MUST use the Upgrade header field within a 101 (Switching Protocols) response to indicate which
035 * protocol(s) are being switched.
036 *
037 * <p class='bcode'>
038 *    Upgrade        = "Upgrade" ":" 1#product
039 * </p>
040 *
041 * <p>
042 * For example,
043 * <p class='bcode'>
044 *    Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
045 * </p>
046 *
047 * <p>
048 * The Upgrade header field is intended to provide a simple mechanism for transition from HTTP/1.1 to some other,
049 * incompatible protocol.
050 * It does so by allowing the client to advertise its desire to use another protocol, such as a later version of HTTP
051 * with a higher major version number, even though the current request has been made using HTTP/1.1.
052 * This eases the difficult transition between incompatible protocols by allowing the client to initiate a request in
053 * the more commonly supported protocol while indicating to the server that it would like to use a "better" protocol if
054 * available (where "better" is determined by the server, possibly according to the nature of the method and/or resource
055 * being requested).
056 *
057 * <p>
058 * The Upgrade header field only applies to switching application-layer protocols upon the existing transport-layer
059 * connection.
060 * Upgrade cannot be used to insist on a protocol change; its acceptance and use by the server is optional.
061 * The capabilities and nature of the application-layer communication after the protocol change is entirely dependent
062 * upon the new protocol chosen, although the first action after changing the protocol MUST be a response to the initial
063 * HTTP request containing the Upgrade header field.
064 *
065 * <p>
066 * The Upgrade header field only applies to the immediate connection.
067 * Therefore, the upgrade keyword MUST be supplied within a Connection header field (section 14.10) whenever Upgrade is
068 * present in an HTTP/1.1 message.
069 *
070 * <p>
071 * The Upgrade header field cannot be used to indicate a switch to a protocol on a different connection.
072 * For that purpose, it is more appropriate to use a 301, 302, 303, or 305 redirection response.
073 *
074 * <p>
075 * This specification only defines the protocol name "HTTP" for use by the family of Hypertext Transfer Protocols, as
076 * defined by the HTTP version rules of section 3.1 and future updates to this specification.
077 * Any token can be used as a protocol name; however, it will only be useful if both the client and server associate
078 * the name with the same protocol.
079 *
080 * <h5 class='section'>See Also:</h5><ul>
081 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
082 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
083 * </ul>
084 *
085 * @serial exclude
086 */
087@Header("Upgrade")
088public class Upgrade extends BasicCsvHeader {
089
090   //-----------------------------------------------------------------------------------------------------------------
091   // Static
092   //-----------------------------------------------------------------------------------------------------------------
093
094   private static final long serialVersionUID = 1L;
095   private static final String NAME = "Upgrade";
096
097   /**
098    * Static creator.
099    *
100    * @param value
101    *    The header value.
102    *    <br>Can be <jk>null</jk>.
103    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
104    */
105   public static Upgrade of(String value) {
106      return value == null ? null : new Upgrade(value);
107   }
108
109   /**
110    * Static creator.
111    *
112    * @param value
113    *    The header value.
114    *    <br>Can be <jk>null</jk>.
115    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
116    */
117   public static Upgrade of(String...value) {
118      return value == null ? null : new Upgrade(value);
119   }
120
121   /**
122    * Static creator with delayed value.
123    *
124    * <p>
125    * Header value is re-evaluated on each call to {@link #getValue()}.
126    *
127    * @param value
128    *    The supplier of the header value.
129    *    <br>Can be <jk>null</jk>.
130    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
131    */
132   public static Upgrade of(Supplier<String[]> value) {
133      return value == null ? null : new Upgrade(value);
134   }
135
136   //-----------------------------------------------------------------------------------------------------------------
137   // Instance
138   //-----------------------------------------------------------------------------------------------------------------
139
140   /**
141    * Constructor.
142    *
143    * @param value
144    *    The header value.
145    *    <br>Can be <jk>null</jk>.
146    */
147   public Upgrade(String value) {
148      super(NAME, value);
149   }
150
151   /**
152    * Constructor.
153    *
154    * @param value
155    *    The header value.
156    *    <br>Can be <jk>null</jk>.
157    */
158   public Upgrade(String...value) {
159      super(NAME, value);
160   }
161
162   /**
163    * Constructor with delayed value.
164    *
165    * <p>
166    * Header value is re-evaluated on each call to {@link #getValue()}.
167    *
168    * @param value
169    *    The supplier of the header value.
170    *    <br>Can be <jk>null</jk>.
171    */
172   public Upgrade(Supplier<String[]> value) {
173      super(NAME, value);
174   }
175}