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