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 java.util.NoSuchElementException;
31  
32  import org.apache.http.FormattedHeader;
33  import org.apache.http.Header;
34  import org.apache.http.HeaderElement;
35  import org.apache.http.HeaderElementIterator;
36  import org.apache.http.HeaderIterator;
37  import org.apache.http.util.Args;
38  import org.apache.http.util.CharArrayBuffer;
39  
40  /**
41   * Basic implementation of a {@link HeaderElementIterator}.
42   *
43   * @since 4.0
44   */
45  public class BasicHeaderElementIterator implements HeaderElementIterator {
46  
47      private final HeaderIterator headerIt;
48      private final HeaderValueParser parser;
49  
50      private HeaderElement currentElement = null;
51      private CharArrayBuffer buffer = null;
52      private ParserCursor cursor = null;
53  
54      /**
55       * Creates a new instance of BasicHeaderElementIterator
56       */
57      public BasicHeaderElementIterator(
58              final HeaderIterator headerIterator,
59              final HeaderValueParser parser) {
60          this.headerIt = Args.notNull(headerIterator, "Header iterator");
61          this.parser = Args.notNull(parser, "Parser");
62      }
63  
64  
65      public BasicHeaderElementIterator(final HeaderIterator headerIterator) {
66          this(headerIterator, BasicHeaderValueParser.INSTANCE);
67      }
68  
69  
70      private void bufferHeaderValue() {
71          this.cursor = null;
72          this.buffer = null;
73          while (this.headerIt.hasNext()) {
74              final Header h = this.headerIt.nextHeader();
75              if (h instanceof FormattedHeader) {
76                  this.buffer = ((FormattedHeader) h).getBuffer();
77                  this.cursor = new ParserCursor(0, this.buffer.length());
78                  this.cursor.updatePos(((FormattedHeader) h).getValuePos());
79                  break;
80              }
81              final String value = h.getValue();
82              if (value != null) {
83                  this.buffer = new CharArrayBuffer(value.length());
84                  this.buffer.append(value);
85                  this.cursor = new ParserCursor(0, this.buffer.length());
86                  break;
87              }
88          }
89      }
90  
91      private void parseNextElement() {
92          // loop while there are headers left to parse
93          while (this.headerIt.hasNext() || this.cursor != null) {
94              if (this.cursor == null || this.cursor.atEnd()) {
95                  // get next header value
96                  bufferHeaderValue();
97              }
98              // Anything buffered?
99              if (this.cursor != null) {
100                 // loop while there is data in the buffer
101                 while (!this.cursor.atEnd()) {
102                     final HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor);
103                     if (!(e.getName().isEmpty() && e.getValue() == null)) {
104                         // Found something
105                         this.currentElement = e;
106                         return;
107                     }
108                 }
109                 // if at the end of the buffer
110                 if (this.cursor.atEnd()) {
111                     // discard it
112                     this.cursor = null;
113                     this.buffer = null;
114                 }
115             }
116         }
117     }
118 
119     @Override
120     public boolean hasNext() {
121         if (this.currentElement == null) {
122             parseNextElement();
123         }
124         return this.currentElement != null;
125     }
126 
127     @Override
128     public HeaderElement nextElement() throws NoSuchElementException {
129         if (this.currentElement == null) {
130             parseNextElement();
131         }
132 
133         if (this.currentElement == null) {
134             throw new NoSuchElementException("No more header elements available");
135         }
136 
137         final HeaderElement element = this.currentElement;
138         this.currentElement = null;
139         return element;
140     }
141 
142     @Override
143     public final Object next() throws NoSuchElementException {
144         return nextElement();
145     }
146 
147     @Override
148     public void remove() throws UnsupportedOperationException {
149         throw new UnsupportedOperationException("Remove not supported");
150     }
151 
152 }