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