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.classic.header;
018
019import java.util.function.*;
020
021import org.apache.juneau.http.*;
022
023/**
024 * Represents a parsed <l>Via</l> HTTP response header.
025 *
026 * <p>
027 * Informs the client of proxies through which the response was sent.
028 *
029 * <h5 class='figure'>Example</h5>
030 * <p class='bcode'>
031 *    Via: 1.0 fred, 1.1 example.com (Apache/1.1)
032 * </p>
033 *
034 * <p>
035 * Informs the client of proxies through which the response was sent.
036 *
037 * <p>
038 * <h5 class='figure'>Example</h5>
039 * <p class='bcode'>
040 *    Via: 1.0 fred, 1.1 example.com (Apache/1.1)
041 * </p>
042 *
043 * <p>
044 * <h5 class='topic'>RFC2616 Specification</h5>
045 *
046 * The Via general-header field MUST be used by gateways and proxies to indicate the intermediate protocols and
047 * recipients between the user agent and the server on requests, and between the origin server and the client on
048 * responses.
049 * It is analogous to the "Received" field of RFC 822 and is intended to be used for tracking message forwards,
050 * avoiding request loops, and identifying the protocol capabilities of all senders along the request/response chain.
051 *
052 * <p class='bcode'>
053 *    Via =  "Via" ":" 1#( received-protocol received-by [ comment ] )
054 *    received-protocol = [ protocol-name "/" ] protocol-version
055 *    protocol-name     = token
056 *    protocol-version  = token
057 *    received-by       = ( host [ ":" port ] ) | pseudonym
058 *    pseudonym         = token
059 * </p>
060 *
061 * <p>
062 * The received-protocol indicates the protocol version of the message received by the server or client along each
063 * segment of the request/response chain.
064 * The received-protocol version is appended to the Via field value when the message is forwarded so that information
065 * about the protocol capabilities of upstream applications remains visible to all recipients.
066 *
067 * <p>
068 * The protocol-name is optional if and only if it would be "HTTP".
069 * The received-by field is normally the host and optional port number of a recipient server or client that subsequently
070 * forwarded the message.
071 * However, if the real host is considered to be sensitive information, it MAY be replaced by a pseudonym.
072 * If the port is not given, it MAY be assumed to be the default port of the received-protocol.
073 *
074 * <p>
075 * Multiple Via field values represents each proxy or gateway that has forwarded the message.
076 * Each recipient MUST append its information such that the end result is ordered according to the sequence of
077 * forwarding applications.
078 *
079 * <p>
080 * Comments MAY be used in the Via header field to identify the software of the recipient proxy or gateway, analogous
081 * to the User-Agent and Server header fields.
082 * However, all comments in the Via field are optional and MAY be removed by any recipient prior to forwarding the
083 * message.
084 *
085 * <p>
086 * For example, a request message could be sent from an HTTP/1.0 user agent to an internal proxy code-named "fred",
087 * which uses HTTP/1.1 to forward the request to a public proxy at nowhere.com, which completes the request by
088 * forwarding it to the origin server at www.ics.uci.edu.
089 * The request received by www.ics.uci.edu would then have the following Via header field:
090 * <p class='bcode'>
091 *    Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
092 * </p>
093 *
094 * <p>
095 * Proxies and gateways used as a portal through a network firewall SHOULD NOT, by default, forward the names and ports
096 * of hosts within the firewall region.
097 * This information SHOULD only be propagated if explicitly enabled.
098 * If not enabled, the received-by host of any host behind the firewall SHOULD be replaced by an appropriate pseudonym
099 * for that host.
100 *
101 * <p>
102 * For organizations that have strong privacy requirements for hiding internal structures, a proxy MAY combine an
103 * ordered subsequence of Via header field entries with identical received-protocol values into a single such entry.
104 * For example...
105 * <p class='bcode'>
106 *    Via: 1.0 ricky, 1.1 ethel, 1.1 fred, 1.0 lucy
107 * </p>
108 *
109 * <p>
110 * ...could be collapsed to...
111 * <p class='bcode'>
112 *    Via: 1.0 ricky, 1.1 mertz, 1.0 lucy
113 * </p>
114 *
115 * <p>
116 * Applications SHOULD NOT combine multiple entries unless they are all under the same organizational control and the
117 * hosts have already been replaced by pseudonyms.
118 * Applications MUST NOT combine entries which have different received-protocol values.
119 *
120 * <h5 class='section'>See Also:</h5><ul>
121 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a>
122 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
123 * </ul>
124 *
125 * @serial exclude
126 */
127@Header("Via")
128@SuppressWarnings({
129   "java:S9149" // Public Juneau DSL factory; hiding parent of()/create() is intentional.
130})
131public class Via extends BasicCsvHeader {
132   private static final long serialVersionUID = 1L;
133   private static final String NAME = "Via";
134
135   /**
136    * Static creator.
137    *
138    * @param value
139    *    The header value.
140    *    <br>Can be <jk>null</jk>.
141    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
142    */
143   public static Via of(String value) {
144      return value == null ? null : new Via(value);
145   }
146
147   /**
148    * Static creator.
149    *
150    * @param value
151    *    The header value.
152    *    <br>Can be <jk>null</jk>.
153    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
154    */
155   public static Via of(String...value) {
156      return value == null ? null : new Via(value);
157   }
158
159   /**
160    * Static creator for two CSV tokens.
161    *
162    * <p>
163    * This distinct fixed-arity overload ensures two literal {@link String} arguments resolve to this class's own
164    * factory (returning a {@link Via}) rather than the inherited fixed-arity {@link BasicCsvHeader#of(String,String)},
165    * which Java overload resolution would otherwise prefer.
166    *
167    * @param value1 The first CSV token.
168    * @param value2 The second CSV token.
169    * @return A new header bean.
170    */
171   public static Via of(String value1, String value2) {
172      return new Via(value1, value2);
173   }
174
175   /**
176    * Static creator with delayed value.
177    *
178    * <p>
179    * Header value is re-evaluated on each call to {@link #getValue()}.
180    *
181    * @param value
182    *    The supplier of the header value.
183    *    <br>Can be <jk>null</jk>.
184    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
185    */
186   public static Via of(Supplier<String[]> value) {
187      return value == null ? null : new Via(value);
188   }
189
190   /**
191    * Constructor.
192    *
193    * @param value
194    *    The header value.
195    *    <br>Can be <jk>null</jk> to create a header with no value.
196    */
197   public Via(String value) {
198      super(NAME, value);
199   }
200
201   /**
202    * Constructor.
203    *
204    * @param value
205    *    The header value.
206    *    <br>Can be <jk>null</jk> or empty to create a header with no value.
207    */
208   public Via(String...value) {
209      super(NAME, value);
210   }
211
212   /**
213    * Constructor with delayed value.
214    *
215    * <p>
216    * Header value is re-evaluated on each call to {@link #getValue()}.
217    *
218    * @param value
219    *    The supplier of the header value.
220    *    <br>Can be <jk>null</jk> to create a header with no value.
221    */
222   public Via(Supplier<String[]> value) {
223      super(NAME, value);
224   }
225}