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 static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.util.*;
022import java.util.function.*;
023
024import org.apache.http.*;
025import org.apache.juneau.bct.assertions.*;
026
027/**
028 * Category of headers that consist of a single string value.
029 *
030 * <h2 class='figure'>Example</h2>
031 * <p class='bcode'>
032 *    Host: www.myhost.com:8080
033 * </p>
034 *
035 * <h2 class='section'>See Also:</h2><ul>
036 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a>
037 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
038 * </ul>
039 *
040 * @serial exclude
041 */
042@SuppressWarnings({
043   "java:S2160" // equals() inherited from BasicHeader compares name+value; typed String field is accessed via getValue()
044})
045public class BasicStringHeader extends BasicHeader {
046   private static final long serialVersionUID = 1L;
047
048   /**
049    * Static creator.
050    *
051    * @param name The header name.
052    * @param value
053    *    The header value.
054    *    <br>Can be <jk>null</jk>.
055    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
056    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
057    */
058   public static BasicStringHeader of(String name, String value) {
059      return value == null ? null : new BasicStringHeader(name, value);
060   }
061
062   /**
063    * Static creator with delayed value.
064    *
065    * <p>
066    * Header value is re-evaluated on each call to {@link #getValue()}.
067    *
068    * @param name The header name.
069    * @param value
070    *    The supplier of the header value.
071    *    <br>Can be <jk>null</jk>.
072    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
073    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
074    */
075   public static BasicStringHeader of(String name, Supplier<String> value) {
076      return value == null ? null : new BasicStringHeader(name, value);
077   }
078
079   /**
080    * Creates a {@link Header} from a name/value pair string (e.g. <js>"Foo: bar"</js>)
081    *
082    * @param pair The pair string.
083    * @return A new header bean.
084    */
085   public static BasicStringHeader ofPair(String pair) {
086      if (pair == null)
087         return null;
088      var i = pair.indexOf(':');
089      if (i == -1)
090         i = pair.indexOf('=');
091      if (i == -1)
092         return of(pair, "");
093      return of(pair.substring(0, i).trim(), pair.substring(i + 1).trim());
094   }
095
096   private final String value;
097   private final transient Supplier<String> supplier;
098
099   /**
100    * Constructor.
101    *
102    * @param name The header name.
103    * @param value
104    *    The header value.
105    *    <br>Can be <jk>null</jk> to create a header with no value.
106    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
107    */
108   public BasicStringHeader(String name, String value) {
109      super(name, value);
110      this.value = value;
111      this.supplier = null;
112   }
113
114   /**
115    * Constructor with delayed value.
116    *
117    * <p>
118    * Header value is re-evaluated on each call to {@link #getValue()}.
119    *
120    * @param name The header name.
121    * @param value
122    *    The supplier of the header value.
123    *    <br>Can be <jk>null</jk> to create a header with no value.
124    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
125    */
126   public BasicStringHeader(String name, Supplier<String> value) {
127      super(name, null);
128      this.value = null;
129      supplier = value;
130   }
131
132   /**
133    * Provides the ability to perform fluent-style assertions on this header.
134    *
135    * <h4 class='section'>Examples:</h4>
136    * <p class='bjava'>
137    *    <jc>// Validates the content type header is provided.</jc>
138    *    <jv>client</jv>
139    *       .get(<jsf>URL</jsf>)
140    *       .run()
141    *       .getHeader(<js>"Content-Type"</js>).assertString().exists();
142    * </p>
143    *
144    * @return A new fluent assertion object.
145    * @throws AssertionError If assertion failed.
146    */
147   public FluentStringAssertion<BasicStringHeader> assertString() {
148      return new FluentStringAssertion<>(value(), this);
149   }
150
151   @Override /* Overridden from BasicHeader */
152   public Optional<String> asString() {
153      return o(value());
154   }
155
156   @Override /* Overridden from Header */
157   public String getValue() { return s(value()); }
158
159   /**
160    * Return the value if present, otherwise return <c>other</c>.
161    *
162    * <p>
163    * This is a shortened form for calling <c>asString().orElse(<jv>other</jv>)</c>.
164    *
165    * @param other The value to be returned if there is no value present, can be <jk>null</jk>.
166    * @return The value, if present, otherwise <c>other</c>.
167    */
168   @Override
169   public String orElse(String other) {
170      String x = value();
171      return nn(x) ? x : other;
172   }
173
174   private String value() {
175      if (nn(supplier))
176         return supplier.get();
177      return value;
178   }
179}