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              } else {
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  
92      private void parseNextElement() {
93          // loop while there are headers left to parse
94          while (this.headerIt.hasNext() || this.cursor != null) {
95              if (this.cursor == null || this.cursor.atEnd()) {
96                  // get next header value
97                  bufferHeaderValue();
98              }
99              // Anything buffered?
100             if (this.cursor != null) {
101                 // loop while there is data in the buffer
102                 while (!this.cursor.atEnd()) {
103                     final HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor);
104                     if (!(e.getName().length() == 0 && e.getValue() == null)) {
105                         // Found something
106                         this.currentElement = e;
107                         return;
108                     }
109                 }
110                 // if at the end of the buffer
111                 if (this.cursor.atEnd()) {
112                     // discard it
113                     this.cursor = null;
114                     this.buffer = null;
115                 }
116             }
117         }
118     }
119 
120     @Override
121     public boolean hasNext() {
122         if (this.currentElement == null) {
123             parseNextElement();
124         }
125         return this.currentElement != null;
126     }
127 
128     @Override
129     public HeaderElement nextElement() throws NoSuchElementException {
130         if (this.currentElement == null) {
131             parseNextElement();
132         }
133 
134         if (this.currentElement == null) {
135             throw new NoSuchElementException("No more header elements available");
136         }
137 
138         final HeaderElement element = this.currentElement;
139         this.currentElement = null;
140         return element;
141     }
142 
143     @Override
144     public final Object next() throws NoSuchElementException {
145         return nextElement();
146     }
147 
148     @Override
149     public void remove() throws UnsupportedOperationException {
150         throw new UnsupportedOperationException("Remove not supported");
151     }
152 
153 }