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.*;
018import org.apache.juneau.internal.*;
019
020/**
021 * Represents a parsed <l>Connection</l> HTTP request header.
022 *
023 * <p>
024 * Control options for the current connection and list of hop-by-hop request fields.
025 *
026 * <h5 class='figure'>Example</h5>
027 * <p class='bcode'>
028 *    Connection: keep-alive
029 *    Connection: Upgrade
030 * </p>
031 *
032 * <h5 class='topic'>RFC2616 Specification</h5>
033 *
034 * The Connection general-header field allows the sender to specify options that are desired for that particular
035 * connection and MUST NOT be communicated by proxies over further connections.
036 *
037 * <p>
038 * The Connection header has the following grammar:
039 * <p class='bcode'>
040 *    Connection = "Connection" ":" 1#(connection-token)
041 *    connection-token  = token
042 * </p>
043 *
044 * <p>
045 * HTTP/1.1 proxies MUST parse the Connection header field before a message is forwarded and, for each connection-token
046 * in this field, remove any header field(s) from the message with the same name as the connection-token.
047 * Connection options are signaled by the presence of a connection-token in the Connection header field, not by any
048 * corresponding additional header field(s), since the additional header field may not be sent if there are no
049 * parameters associated with that connection option.
050 *
051 * <p>
052 * Message headers listed in the Connection header MUST NOT include end-to-end headers, such as Cache-Control.
053 *
054 * <p>
055 * HTTP/1.1 defines the "close" connection option for the sender to signal that the connection will be closed after
056 * completion of the response.
057 * For example...
058 * <p class='bcode'>
059 *    Connection: close
060 * </p>
061 * <p>
062 * ...in either the request or the response header fields indicates that the connection SHOULD NOT be considered
063 * `persistent' (section 8.1) after the current request/response is complete.
064 *
065 * <p>
066 * HTTP/1.1 applications that do not support persistent connections MUST include the "close" connection option in
067 * every message.
068 *
069 * <p>
070 * A system receiving an HTTP/1.0 (or lower-version) message that includes a Connection header MUST, for each
071 * connection-token in this field, remove and ignore any header field(s) from the message with the same name as the
072 * connection-token.
073 * This protects against mistaken forwarding of such header fields by pre-HTTP/1.1 proxies.
074 * See section 19.6.2.
075 *
076 * <h5 class='section'>See Also:</h5><ul>
077 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
078 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
079 * </ul>
080 *
081 * @serial exclude
082 */
083@Header("Connection")
084public class Connection extends BasicStringHeader {
085
086   //-----------------------------------------------------------------------------------------------------------------
087   // Static
088   //-----------------------------------------------------------------------------------------------------------------
089
090   private static final long serialVersionUID = 1L;
091   private static final String NAME = "Connection";
092
093   private static final Cache<String,Connection> CACHE = Cache.of(String.class, Connection.class).build();
094
095   /**
096    * Static creator.
097    *
098    * @param value
099    *    The header value.
100    *    <br>Can be <jk>null</jk>.
101    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
102    */
103   public static Connection of(String value) {
104      return value == null ? null : CACHE.get(value, ()->new Connection(value));
105   }
106
107   /**
108    * Static creator with delayed value.
109    *
110    * <p>
111    * Header value is re-evaluated on each call to {@link #getValue()}.
112    *
113    * @param value
114    *    The supplier of the header value.
115    *    <br>Can be <jk>null</jk>.
116    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
117    */
118   public static Connection of(Supplier<String> value) {
119      return value == null ? null : new Connection(value);
120   }
121
122   //-----------------------------------------------------------------------------------------------------------------
123   // Instance
124   //-----------------------------------------------------------------------------------------------------------------
125
126   /**
127    * Constructor.
128    *
129    * @param value
130    *    The header value.
131    *    <br>Can be <jk>null</jk>.
132    */
133   public Connection(String value) {
134      super(NAME, value);
135   }
136
137   /**
138    * Constructor with delayed value.
139    *
140    * <p>
141    * Header value is re-evaluated on each call to {@link #getValue()}.
142    *
143    * @param value
144    *    The supplier of the header value.
145    *    <br>Can be <jk>null</jk>.
146    */
147   public Connection(Supplier<String> value) {
148      super(NAME, value);
149   }
150
151   /**
152    * Returns <jk>true</jk> if the header value is <c>close</c>.
153    *
154    * @return <jk>true</jk> if the header value is <c>close</c>.
155    */
156   public boolean isClose() {
157      return equalsIgnoreCase("close");
158   }
159
160   /**
161    * Returns <jk>true</jk> if the header value is <c>keep-alive</c>.
162    *
163    * @return <jk>true</jk> if the header value is <c>keep-alive</c>.
164    */
165   public boolean isKeepAlive() {
166      return equalsIgnoreCase("keep-alive");
167   }
168
169   /**
170    * Returns <jk>true</jk> if the header value is <c>upgrade</c>.
171    *
172    * @return <jk>true</jk> if the header value is <c>upgrade</c>.
173    */
174   public boolean isUpgrade() {
175      return equalsIgnoreCase("upgrade");
176   }
177}