Source code: org/apache/http/message/AbstractHttpMessage.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/message/AbstractHttpMessage.java $
3 * $Revision: 385789 $
4 * $Date: 2006-03-14 12:24:05 +0100 (Tue, 14 Mar 2006) $
5 *
6 * ====================================================================
7 *
8 * Copyright 1999-2006 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ====================================================================
22 *
23 * This software consists of voluntary contributions made by many
24 * individuals on behalf of the Apache Software Foundation. For more
25 * information on the Apache Software Foundation, please see
26 * <http://www.apache.org/>.
27 *
28 */
29
30 package org.apache.http.message;
31
32 import java.util.Iterator;
33
34 import org.apache.http.Header;
35 import org.apache.http.HttpMessage;
36 import org.apache.http.impl.DefaultHttpParams;
37 import org.apache.http.impl.HeaderGroup;
38 import org.apache.http.params.HttpParams;
39
40 /**
41 * Basic implementation of an HTTP message that can be modified.
42 *
43 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
44 *
45 * @version $Revision: 385789 $
46 *
47 * @since 4.0
48 */
49 public abstract class AbstractHttpMessage implements HttpMessage {
50
51 private final HeaderGroup headergroup;
52
53 private HttpParams params = null;
54
55 protected AbstractHttpMessage() {
56 super();
57 this.headergroup = new HeaderGroup();
58 this.params = new DefaultHttpParams(null);
59 }
60
61 public boolean containsHeader(String name) {
62 return this.headergroup.containsHeader(name);
63 }
64
65 public Header[] getHeaders(final String name) {
66 return this.headergroup.getHeaders(name);
67 }
68
69 public Header getFirstHeader(final String name) {
70 return this.headergroup.getFirstHeader(name);
71 }
72
73 public Header getLastHeader(final String name) {
74 return this.headergroup.getLastHeader(name);
75 }
76
77 public Header[] getAllHeaders() {
78 return this.headergroup.getAllHeaders();
79 }
80
81 public void addHeader(final Header header) {
82 this.headergroup.addHeader(header);
83 }
84
85 public void setHeader(final Header header) {
86 if (header == null) {
87 return;
88 }
89 // clean up
90 for (;;) {
91 Header oldheader = this.headergroup.getLastHeader(header.getName());
92 if (oldheader == null) {
93 break;
94 } else {
95 this.headergroup.removeHeader(oldheader);
96 }
97 }
98 this.headergroup.addHeader(header);
99 }
100
101 public void removeHeader(final Header header) {
102 this.headergroup.removeHeader(header);
103 }
104
105 public void removeHeaders(final String name) {
106 if (name == null) {
107 return;
108 }
109 for (Iterator i = this.headergroup.iterator(); i.hasNext(); ) {
110 Header header = (Header) i.next();
111 if (name.equalsIgnoreCase(header.getName())) {
112 i.remove();
113 }
114 }
115 }
116
117 public Iterator headerIterator() {
118 return this.headergroup.iterator();
119 }
120
121 public HttpParams getParams() {
122 return this.params;
123 }
124
125 public void setParams(final HttpParams params) {
126 if (params == null) {
127 throw new IllegalArgumentException("HTTP parameters may not be null");
128 }
129 this.params = params;
130 }
131 }