View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.message;
29  
30  import java.io.Serializable;
31  import java.util.Objects;
32  
33  import org.apache.hc.core5.annotation.Contract;
34  import org.apache.hc.core5.annotation.ThreadingBehavior;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.util.Args;
37  
38  /**
39   * Immutable {@link Header}.
40   *
41   * @since 4.0
42   */
43  @Contract(threading = ThreadingBehavior.IMMUTABLE)
44  public class BasicHeader implements Header, Cloneable, Serializable {
45  
46      private static final long serialVersionUID = -5427236326487562174L;
47  
48      private final String name;
49      private final boolean sensitive;
50      private final String value;
51  
52      /**
53       * Default constructor
54       *
55       * @param name the header name
56       * @param value the header value, taken as the value's {@link #toString()}.
57       */
58      public BasicHeader(final String name, final Object value) {
59          this(name, value, false);
60      }
61  
62      /**
63       * Constructor with sensitivity flag
64       *
65       * @param name the header name
66       * @param value the header value, taken as the value's {@link #toString()}.
67       * @param sensitive sensitive flag
68       *
69       * @since 5.0
70       */
71      public BasicHeader(final String name, final Object value, final boolean sensitive) {
72          super();
73          this.name = Args.notNull(name, "Name");
74          this.value = Objects.toString(value, null);
75          this.sensitive = sensitive;
76      }
77  
78      @Override
79      public String getName() {
80          return name;
81      }
82  
83      @Override
84      public String getValue() {
85          return value;
86      }
87  
88      @Override
89      public boolean isSensitive() {
90          return this.sensitive;
91      }
92  
93      @Override
94      public String toString() {
95          final StringBuilder buf = new StringBuilder();
96          buf.append(this.getName()).append(": ");
97          if (this.getValue() != null) {
98              buf.append(this.getValue());
99          }
100         return buf.toString();
101     }
102 
103 }