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.http.message;
29  
30  import org.apache.http.Header;
31  import org.apache.http.HeaderIterator;
32  import org.apache.http.HttpMessage;
33  import org.apache.http.annotation.NotThreadSafe;
34  import org.apache.http.params.BasicHttpParams;
35  import org.apache.http.params.HttpParams;
36  import org.apache.http.util.Args;
37  
38  /**
39   * Basic implementation of {@link HttpMessage}.
40   *
41   * @since 4.0
42   */
43  @SuppressWarnings("deprecation")
44  @NotThreadSafe
45  public abstract class AbstractHttpMessage implements HttpMessage {
46  
47      protected HeaderGroup headergroup;
48  
49      @Deprecated
50      protected HttpParams params;
51  
52      /**
53       * @deprecated (4.3) use {@link AbstractHttpMessage#AbstractHttpMessage()}
54       */
55      @Deprecated
56      protected AbstractHttpMessage(final HttpParams params) {
57          super();
58          this.headergroup = new HeaderGroup();
59          this.params = params;
60      }
61  
62      protected AbstractHttpMessage() {
63          this(null);
64      }
65  
66      // non-javadoc, see interface HttpMessage
67      @Override
68      public boolean containsHeader(final String name) {
69          return this.headergroup.containsHeader(name);
70      }
71  
72      // non-javadoc, see interface HttpMessage
73      @Override
74      public Header[] getHeaders(final String name) {
75          return this.headergroup.getHeaders(name);
76      }
77  
78      // non-javadoc, see interface HttpMessage
79      @Override
80      public Header getFirstHeader(final String name) {
81          return this.headergroup.getFirstHeader(name);
82      }
83  
84      // non-javadoc, see interface HttpMessage
85      @Override
86      public Header getLastHeader(final String name) {
87          return this.headergroup.getLastHeader(name);
88      }
89  
90      // non-javadoc, see interface HttpMessage
91      @Override
92      public Header[] getAllHeaders() {
93          return this.headergroup.getAllHeaders();
94      }
95  
96      // non-javadoc, see interface HttpMessage
97      @Override
98      public void addHeader(final Header header) {
99          this.headergroup.addHeader(header);
100     }
101 
102     // non-javadoc, see interface HttpMessage
103     @Override
104     public void addHeader(final String name, final String value) {
105         Args.notNull(name, "Header name");
106         this.headergroup.addHeader(new BasicHeader(name, value));
107     }
108 
109     // non-javadoc, see interface HttpMessage
110     @Override
111     public void setHeader(final Header header) {
112         this.headergroup.updateHeader(header);
113     }
114 
115     // non-javadoc, see interface HttpMessage
116     @Override
117     public void setHeader(final String name, final String value) {
118         Args.notNull(name, "Header name");
119         this.headergroup.updateHeader(new BasicHeader(name, value));
120     }
121 
122     // non-javadoc, see interface HttpMessage
123     @Override
124     public void setHeaders(final Header[] headers) {
125         this.headergroup.setHeaders(headers);
126     }
127 
128     // non-javadoc, see interface HttpMessage
129     @Override
130     public void removeHeader(final Header header) {
131         this.headergroup.removeHeader(header);
132     }
133 
134     // non-javadoc, see interface HttpMessage
135     @Override
136     public void removeHeaders(final String name) {
137         if (name == null) {
138             return;
139         }
140         for (final HeaderIterator i = this.headergroup.iterator(); i.hasNext(); ) {
141             final Header header = i.nextHeader();
142             if (name.equalsIgnoreCase(header.getName())) {
143                 i.remove();
144             }
145         }
146     }
147 
148     // non-javadoc, see interface HttpMessage
149     @Override
150     public HeaderIterator headerIterator() {
151         return this.headergroup.iterator();
152     }
153 
154     // non-javadoc, see interface HttpMessage
155     @Override
156     public HeaderIterator headerIterator(final String name) {
157         return this.headergroup.iterator(name);
158     }
159 
160     /**
161      * @deprecated (4.3) use constructor parameters of configuration API provided by HttpClient
162      */
163     @Override
164     @Deprecated
165     public HttpParams getParams() {
166         if (this.params == null) {
167             this.params = new BasicHttpParams();
168         }
169         return this.params;
170     }
171 
172     /**
173      * @deprecated (4.3) use constructor parameters of configuration API provided by HttpClient
174      */
175     @Override
176     @Deprecated
177     public void setParams(final HttpParams params) {
178         this.params = Args.notNull(params, "HTTP parameters");
179     }
180 }