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.hc.core5.http.support;
29  
30  import java.util.Iterator;
31  
32  import org.apache.hc.core5.http.Header;
33  import org.apache.hc.core5.http.HttpMessage;
34  import org.apache.hc.core5.http.ProtocolVersion;
35  import org.apache.hc.core5.http.message.BasicHeader;
36  import org.apache.hc.core5.http.message.HeaderGroup;
37  
38  /**
39   * Abstract {@link HttpMessage} builder.
40   *
41   * @since 5.1
42   */
43  public abstract class AbstractMessageBuilder<T> {
44  
45      private ProtocolVersion version;
46      private HeaderGroup headerGroup;
47  
48      protected AbstractMessageBuilder() {
49      }
50  
51      protected void digest(final HttpMessage message) {
52          if (message == null) {
53              return;
54          }
55          setVersion(message.getVersion());
56          setHeaders(message.headerIterator());
57      }
58  
59      public ProtocolVersion getVersion() {
60          return version;
61      }
62  
63      public AbstractMessageBuilder<T> setVersion(final ProtocolVersion version) {
64          this.version = version;
65          return this;
66      }
67  
68      public Header[] getHeaders() {
69          return headerGroup != null ? headerGroup.getHeaders() : null;
70      }
71  
72      public Header[] getHeaders(final String name) {
73          return headerGroup != null ? headerGroup.getHeaders(name) : null;
74      }
75  
76      public AbstractMessageBuilder<T> setHeaders(final Header... headers) {
77          if (headerGroup == null) {
78              headerGroup = new HeaderGroup();
79          }
80          headerGroup.setHeaders(headers);
81          return this;
82      }
83  
84      public AbstractMessageBuilder<T> setHeaders(final Iterator<Header> it) {
85          if (headerGroup == null) {
86              headerGroup = new HeaderGroup();
87          } else {
88              headerGroup.clear();
89          }
90          while (it.hasNext()) {
91              headerGroup.addHeader(it.next());
92          }
93          return this;
94      }
95  
96      public Header[] getFirstHeaders() {
97          return headerGroup != null ? headerGroup.getHeaders() : null;
98      }
99  
100     public Header getFirstHeader(final String name) {
101         return headerGroup != null ? headerGroup.getFirstHeader(name) : null;
102     }
103 
104     public Header getLastHeader(final String name) {
105         return headerGroup != null ? headerGroup.getLastHeader(name) : null;
106     }
107 
108     public AbstractMessageBuilder<T> addHeader(final Header header) {
109         if (headerGroup == null) {
110             headerGroup = new HeaderGroup();
111         }
112         headerGroup.addHeader(header);
113         return this;
114     }
115 
116     public AbstractMessageBuilder<T> addHeader(final String name, final String value) {
117         if (headerGroup == null) {
118             headerGroup = new HeaderGroup();
119         }
120         headerGroup.addHeader(new BasicHeader(name, value));
121         return this;
122     }
123 
124     public AbstractMessageBuilder<T> removeHeader(final Header header) {
125         if (headerGroup == null) {
126             headerGroup = new HeaderGroup();
127         }
128         headerGroup.removeHeader(header);
129         return this;
130     }
131 
132     public AbstractMessageBuilder<T> removeHeaders(final String name) {
133         if (name == null || headerGroup == null) {
134             return this;
135         }
136         for (final Iterator<Header> i = headerGroup.headerIterator(); i.hasNext(); ) {
137             final Header header = i.next();
138             if (name.equalsIgnoreCase(header.getName())) {
139                 i.remove();
140             }
141         }
142         return this;
143     }
144 
145     public AbstractMessageBuilder<T> setHeader(final Header header) {
146         if (headerGroup == null) {
147             headerGroup = new HeaderGroup();
148         }
149         headerGroup.setHeader(header);
150         return this;
151     }
152 
153     public AbstractMessageBuilder<T> setHeader(final String name, final String value) {
154         if (headerGroup == null) {
155             headerGroup = new HeaderGroup();
156         }
157         headerGroup.setHeader(new BasicHeader(name, value));
158         return this;
159     }
160 
161     protected abstract T build();
162 
163 }