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