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 org.apache.hc.core5.http.HeaderElement;
31  import org.apache.hc.core5.http.NameValuePair;
32  import org.apache.hc.core5.util.Args;
33  
34  /**
35   * Basic implementation of {@link HeaderElement}
36   *
37   * @since 4.0
38   */
39  public class BasicHeaderElement implements HeaderElement {
40  
41      private static final NameValuePairePair">NameValuePair[] EMPTY_NAME_VALUE_PAIR_ARRAY = new NameValuePair[0];
42  
43      private final String name;
44      private final String value;
45      private final NameValuePair[] parameters;
46  
47      /**
48       * Constructor with name, value and parameters.
49       *
50       * @param name header element name
51       * @param value header element value. May be {@code null}
52       * @param parameters header element parameters. May be {@code null}.
53       *   Parameters are copied by reference, not by value
54       */
55      public BasicHeaderElement(
56              final String name,
57              final String value,
58              final NameValuePair[] parameters) {
59          super();
60          this.name = Args.notNull(name, "Name");
61          this.value = value;
62          if (parameters != null) {
63              this.parameters = parameters;
64          } else {
65              this.parameters = EMPTY_NAME_VALUE_PAIR_ARRAY;
66          }
67      }
68  
69      /**
70       * Constructor with name and value.
71       *
72       * @param name header element name
73       * @param value header element value. May be {@code null}
74       */
75      public BasicHeaderElement(final String name, final String value) {
76         this(name, value, null);
77      }
78  
79      @Override
80      public String getName() {
81          return this.name;
82      }
83  
84      @Override
85      public String getValue() {
86          return this.value;
87      }
88  
89      @Override
90      public NameValuePair[] getParameters() {
91          return this.parameters.clone();
92      }
93  
94      @Override
95      public int getParameterCount() {
96          return this.parameters.length;
97      }
98  
99      @Override
100     public NameValuePair getParameter(final int index) {
101         // ArrayIndexOutOfBoundsException is appropriate
102         return this.parameters[index];
103     }
104 
105     @Override
106     public NameValuePair getParameterByName(final String name) {
107         Args.notNull(name, "Name");
108         NameValuePair found = null;
109         for (final NameValuePair current : this.parameters) {
110             if (current.getName().equalsIgnoreCase(name)) {
111                 found = current;
112                 break;
113             }
114         }
115         return found;
116     }
117 
118     @Override
119     public String toString() {
120         final StringBuilder buffer = new StringBuilder();
121         buffer.append(this.name);
122         if (this.value != null) {
123             buffer.append("=");
124             buffer.append(this.value);
125         }
126         for (final NameValuePair parameter : this.parameters) {
127             buffer.append("; ");
128             buffer.append(parameter);
129         }
130         return buffer.toString();
131     }
132 
133 }
134