• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java $
3  * $Revision: 581981 $
4  * $Date: 2007-10-04 11:26:26 -0700 (Thu, 04 Oct 2007) $
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 
35 import java.util.NoSuchElementException;
36 
37 import org.apache.http.Header;
38 import org.apache.http.HeaderIterator;
39 
40 
41 /**
42  * Basic implementation of a {@link HeaderIterator}.
43  *
44  * @version $Revision: 581981 $
45  *
46  * @deprecated Please use {@link java.net.URL#openConnection} instead.
47  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
48  *     for further details.
49  */
50 @Deprecated
51 public class BasicHeaderIterator implements HeaderIterator {
52 
53     /**
54      * An array of headers to iterate over.
55      * Not all elements of this array are necessarily part of the iteration.
56      * This array will never be modified by the iterator.
57      * Derived implementations are expected to adhere to this restriction.
58      */
59     protected final Header[] allHeaders;
60 
61 
62     /**
63      * The position of the next header in {@link #allHeaders allHeaders}.
64      * Negative if the iteration is over.
65      */
66     protected int currentIndex;
67 
68 
69     /**
70      * The header name to filter by.
71      * <code>null</code> to iterate over all headers in the array.
72      */
73     protected String headerName;
74 
75 
76 
77     /**
78      * Creates a new header iterator.
79      *
80      * @param headers   an array of headers over which to iterate
81      * @param name      the name of the headers over which to iterate, or
82      *                  <code>null</code> for any
83      */
BasicHeaderIterator(Header[] headers, String name)84     public BasicHeaderIterator(Header[] headers, String name) {
85         if (headers == null) {
86             throw new IllegalArgumentException
87                 ("Header array must not be null.");
88         }
89 
90         this.allHeaders = headers;
91         this.headerName = name;
92         this.currentIndex = findNext(-1);
93     }
94 
95 
96     /**
97      * Determines the index of the next header.
98      *
99      * @param from      one less than the index to consider first,
100      *                  -1 to search for the first header
101      *
102      * @return  the index of the next header that matches the filter name,
103      *          or negative if there are no more headers
104      */
findNext(int from)105     protected int findNext(int from) {
106         if (from < -1)
107             return -1;
108 
109         final int to = this.allHeaders.length-1;
110         boolean found = false;
111         while (!found && (from < to)) {
112             from++;
113             found = filterHeader(from);
114         }
115         return found ? from : -1;
116     }
117 
118 
119     /**
120      * Checks whether a header is part of the iteration.
121      *
122      * @param index     the index of the header to check
123      *
124      * @return  <code>true</code> if the header should be part of the
125      *          iteration, <code>false</code> to skip
126      */
filterHeader(int index)127     protected boolean filterHeader(int index) {
128         return (this.headerName == null) ||
129             this.headerName.equalsIgnoreCase(this.allHeaders[index].getName());
130     }
131 
132 
133     // non-javadoc, see interface HeaderIterator
hasNext()134     public boolean hasNext() {
135         return (this.currentIndex >= 0);
136     }
137 
138 
139     /**
140      * Obtains the next header from this iteration.
141      *
142      * @return  the next header in this iteration
143      *
144      * @throws NoSuchElementException   if there are no more headers
145      */
nextHeader()146     public Header nextHeader()
147         throws NoSuchElementException {
148 
149         final int current = this.currentIndex;
150         if (current < 0) {
151             throw new NoSuchElementException("Iteration already finished.");
152         }
153 
154         this.currentIndex = findNext(current);
155 
156         return this.allHeaders[current];
157     }
158 
159 
160     /**
161      * Returns the next header.
162      * Same as {@link #nextHeader nextHeader}, but not type-safe.
163      *
164      * @return  the next header in this iteration
165      *
166      * @throws NoSuchElementException   if there are no more headers
167      */
next()168     public final Object next()
169         throws NoSuchElementException {
170         return nextHeader();
171     }
172 
173 
174     /**
175      * Removing headers is not supported.
176      *
177      * @throws UnsupportedOperationException    always
178      */
remove()179     public void remove()
180         throws UnsupportedOperationException {
181 
182         throw new UnsupportedOperationException
183             ("Removing headers is not supported.");
184     }
185 }
186