Home | History | Annotate | Download | only in message
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/AbstractHttpMessage.java $
      3  * $Revision: 620287 $
      4  * $Date: 2008-02-10 07:15:53 -0800 (Sun, 10 Feb 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.message;
     33 
     34 import java.util.Iterator;
     35 
     36 import org.apache.http.Header;
     37 import org.apache.http.HeaderIterator;
     38 import org.apache.http.HttpMessage;
     39 import org.apache.http.params.HttpParams;
     40 import org.apache.http.params.BasicHttpParams;
     41 
     42 /**
     43  * Basic implementation of an HTTP message that can be modified.
     44  *
     45  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     46  *
     47  * @version $Revision: 620287 $
     48  *
     49  * @since 4.0
     50  */
     51 public abstract class AbstractHttpMessage implements HttpMessage {
     52 
     53     protected HeaderGroup headergroup;
     54 
     55     protected HttpParams params;
     56 
     57     protected AbstractHttpMessage(final HttpParams params) {
     58         super();
     59         this.headergroup = new HeaderGroup();
     60         this.params = params;
     61     }
     62 
     63     protected AbstractHttpMessage() {
     64         this(null);
     65     }
     66 
     67     // non-javadoc, see interface HttpMessage
     68     public boolean containsHeader(String name) {
     69         return this.headergroup.containsHeader(name);
     70     }
     71 
     72     // non-javadoc, see interface HttpMessage
     73     public Header[] getHeaders(final String name) {
     74         return this.headergroup.getHeaders(name);
     75     }
     76 
     77     // non-javadoc, see interface HttpMessage
     78     public Header getFirstHeader(final String name) {
     79         return this.headergroup.getFirstHeader(name);
     80     }
     81 
     82     // non-javadoc, see interface HttpMessage
     83     public Header getLastHeader(final String name) {
     84         return this.headergroup.getLastHeader(name);
     85     }
     86 
     87     // non-javadoc, see interface HttpMessage
     88     public Header[] getAllHeaders() {
     89         return this.headergroup.getAllHeaders();
     90     }
     91 
     92     // non-javadoc, see interface HttpMessage
     93     public void addHeader(final Header header) {
     94         this.headergroup.addHeader(header);
     95     }
     96 
     97     // non-javadoc, see interface HttpMessage
     98     public void addHeader(final String name, final String value) {
     99         if (name == null) {
    100             throw new IllegalArgumentException("Header name may not be null");
    101         }
    102         this.headergroup.addHeader(new BasicHeader(name, value));
    103     }
    104 
    105     // non-javadoc, see interface HttpMessage
    106     public void setHeader(final Header header) {
    107         this.headergroup.updateHeader(header);
    108     }
    109 
    110     // non-javadoc, see interface HttpMessage
    111     public void setHeader(final String name, final String value) {
    112         if (name == null) {
    113             throw new IllegalArgumentException("Header name may not be null");
    114         }
    115         this.headergroup.updateHeader(new BasicHeader(name, value));
    116     }
    117 
    118     // non-javadoc, see interface HttpMessage
    119     public void setHeaders(final Header[] headers) {
    120         this.headergroup.setHeaders(headers);
    121     }
    122 
    123     // non-javadoc, see interface HttpMessage
    124     public void removeHeader(final Header header) {
    125         this.headergroup.removeHeader(header);
    126     }
    127 
    128     // non-javadoc, see interface HttpMessage
    129     public void removeHeaders(final String name) {
    130         if (name == null) {
    131             return;
    132         }
    133         for (Iterator i = this.headergroup.iterator(); i.hasNext(); ) {
    134             Header header = (Header) i.next();
    135             if (name.equalsIgnoreCase(header.getName())) {
    136                 i.remove();
    137             }
    138         }
    139     }
    140 
    141     // non-javadoc, see interface HttpMessage
    142     public HeaderIterator headerIterator() {
    143         return this.headergroup.iterator();
    144     }
    145 
    146     // non-javadoc, see interface HttpMessage
    147     public HeaderIterator headerIterator(String name) {
    148         return this.headergroup.iterator(name);
    149     }
    150 
    151     // non-javadoc, see interface HttpMessage
    152     public HttpParams getParams() {
    153         if (this.params == null) {
    154             this.params = new BasicHttpParams();
    155         }
    156         return this.params;
    157     }
    158 
    159     // non-javadoc, see interface HttpMessage
    160     public void setParams(final HttpParams params) {
    161         if (params == null) {
    162             throw new IllegalArgumentException("HTTP parameters may not be null");
    163         }
    164         this.params = params;
    165     }
    166 }
    167