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      public boolean containsHeader(final String name) {
68          return this.headergroup.containsHeader(name);
69      }
70  
71      // non-javadoc, see interface HttpMessage
72      public Header[] getHeaders(final String name) {
73          return this.headergroup.getHeaders(name);
74      }
75  
76      // non-javadoc, see interface HttpMessage
77      public Header getFirstHeader(final String name) {
78          return this.headergroup.getFirstHeader(name);
79      }
80  
81      // non-javadoc, see interface HttpMessage
82      public Header getLastHeader(final String name) {
83          return this.headergroup.getLastHeader(name);
84      }
85  
86      // non-javadoc, see interface HttpMessage
87      public Header[] getAllHeaders() {
88          return this.headergroup.getAllHeaders();
89      }
90  
91      // non-javadoc, see interface HttpMessage
92      public void addHeader(final Header header) {
93          this.headergroup.addHeader(header);
94      }
95  
96      // non-javadoc, see interface HttpMessage
97      public void addHeader(final String name, final String value) {
98          Args.notNull(name, "Header name");
99          this.headergroup.addHeader(new BasicHeader(name, value));
100     }
101 
102     // non-javadoc, see interface HttpMessage
103     public void setHeader(final Header header) {
104         this.headergroup.updateHeader(header);
105     }
106 
107     // non-javadoc, see interface HttpMessage
108     public void setHeader(final String name, final String value) {
109         Args.notNull(name, "Header name");
110         this.headergroup.updateHeader(new BasicHeader(name, value));
111     }
112 
113     // non-javadoc, see interface HttpMessage
114     public void setHeaders(final Header[] headers) {
115         this.headergroup.setHeaders(headers);
116     }
117 
118     // non-javadoc, see interface HttpMessage
119     public void removeHeader(final Header header) {
120         this.headergroup.removeHeader(header);
121     }
122 
123     // non-javadoc, see interface HttpMessage
124     public void removeHeaders(final String name) {
125         if (name == null) {
126             return;
127         }
128         for (final HeaderIterator i = this.headergroup.iterator(); i.hasNext(); ) {
129             final Header header = i.nextHeader();
130             if (name.equalsIgnoreCase(header.getName())) {
131                 i.remove();
132             }
133         }
134     }
135 
136     // non-javadoc, see interface HttpMessage
137     public HeaderIterator headerIterator() {
138         return this.headergroup.iterator();
139     }
140 
141     // non-javadoc, see interface HttpMessage
142     public HeaderIterator headerIterator(final String name) {
143         return this.headergroup.iterator(name);
144     }
145 
146     /**
147      * @deprecated (4.3) use constructor parameters of configuration API provided by HttpClient
148      */
149     @Deprecated
150     public HttpParams getParams() {
151         if (this.params == null) {
152             this.params = new BasicHttpParams();
153         }
154         return this.params;
155     }
156 
157     /**
158      * @deprecated (4.3) use constructor parameters of configuration API provided by HttpClient
159      */
160     @Deprecated
161     public void setParams(final HttpParams params) {
162         this.params = Args.notNull(params, "HTTP parameters");
163     }
164 }