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>Upgrade</l> HTTP request header.
025 *
026 * <p>
027 * Ask the client to upgrade to another protocol.
028 *
029 * <h5 class='figure'>Example</h5>
030 * <p class='bcode'>
031 *    Upgrade: HTTP/2.0, HTTPS/1.3, IRC/6.9, RTA/x11, websocket
032 * </p>
033 *
034 * <h5 class='topic'>RFC2616 Specification</h5>
035 *
036 * The Upgrade general-header allows the client to specify what additional communication protocols it supports and
037 * would like to use if the server finds it appropriate to switch protocols.
038 * The server MUST use the Upgrade header field within a 101 (Switching Protocols) response to indicate which
039 * protocol(s) are being switched.
040 *
041 * <p class='bcode'>
042 *    Upgrade        = "Upgrade" ":" 1#product
043 * </p>
044 *
045 * <p>
046 * For example,
047 * <p class='bcode'>
048 *    Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
049 * </p>
050 *
051 * <p>
052 * The Upgrade header field is intended to provide a simple mechanism for transition from HTTP/1.1 to some other,
053 * incompatible protocol.
054 * It does so by allowing the client to advertise its desire to use another protocol, such as a later version of HTTP
055 * with a higher major version number, even though the current request has been made using HTTP/1.1.
056 * This eases the difficult transition between incompatible protocols by allowing the client to initiate a request in
057 * the more commonly supported protocol while indicating to the server that it would like to use a "better" protocol if
058 * available (where "better" is determined by the server, possibly according to the nature of the method and/or resource
059 * being requested).
060 *
061 * <p>
062 * The Upgrade header field only applies to switching application-layer protocols upon the existing transport-layer
063 * connection.
064 * Upgrade cannot be used to insist on a protocol change; its acceptance and use by the server is optional.
065 * The capabilities and nature of the application-layer communication after the protocol change is entirely dependent
066 * upon the new protocol chosen, although the first action after changing the protocol MUST be a response to the initial
067 * HTTP request containing the Upgrade header field.
068 *
069 * <p>
070 * The Upgrade header field only applies to the immediate connection.
071 * Therefore, the upgrade keyword MUST be supplied within a Connection header field (section 14.10) whenever Upgrade is
072 * present in an HTTP/1.1 message.
073 *
074 * <p>
075 * The Upgrade header field cannot be used to indicate a switch to a protocol on a different connection.
076 * For that purpose, it is more appropriate to use a 301, 302, 303, or 305 redirection response.
077 *
078 * <p>
079 * This specification only defines the protocol name "HTTP" for use by the family of Hypertext Transfer Protocols, as
080 * defined by the HTTP version rules of section 3.1 and future updates to this specification.
081 * Any token can be used as a protocol name; however, it will only be useful if both the client and server associate
082 * the name with the same protocol.
083 *
084 * <h5 class='section'>See Also:</h5><ul>
085 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a>
086 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
087 * </ul>
088 *
089 * @serial exclude
090 */
091@Header("Upgrade")
092public class Upgrade extends BasicCsvHeader {
093   private static final long serialVersionUID = 1L;
094   private static final String NAME = "Upgrade";
095
096   /**
097    * Static creator.
098    *
099    * @param value
100    *    The header value.
101    *    <br>Can be <jk>null</jk>.
102    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
103    */
104   public static Upgrade of(String value) {
105      return value == null ? null : new Upgrade(value);
106   }
107
108   /**
109    * Static creator.
110    *
111    * @param value
112    *    The header value.
113    *    <br>Can be <jk>null</jk>.
114    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
115    */
116   public static Upgrade of(String...value) {
117      return value == null ? null : new Upgrade(value);
118   }
119
120   /**
121    * Static creator with delayed value.
122    *
123    * <p>
124    * Header value is re-evaluated on each call to {@link #getValue()}.
125    *
126    * @param value
127    *    The supplier of the header value.
128    *    <br>Can be <jk>null</jk>.
129    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
130    */
131   public static Upgrade of(Supplier<String[]> value) {
132      return value == null ? null : new Upgrade(value);
133   }
134
135   /**
136    * Constructor.
137    *
138    * @param value
139    *    The header value.
140    *    <br>Can be <jk>null</jk>.
141    */
142   public Upgrade(String value) {
143      super(NAME, value);
144   }
145
146   /**
147    * Constructor.
148    *
149    * @param value
150    *    The header value.
151    *    <br>Can be <jk>null</jk>.
152    */
153   public Upgrade(String...value) {
154      super(NAME, value);
155   }
156
157   /**
158    * Constructor with delayed value.
159    *
160    * <p>
161    * Header value is re-evaluated on each call to {@link #getValue()}.
162    *
163    * @param value
164    *    The supplier of the header value.
165    *    <br>Can be <jk>null</jk>.
166    */
167   public Upgrade(Supplier<String[]> value) {
168      super(NAME, value);
169   }
170}