Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 344A710A40 for ; Fri, 24 Oct 2014 07:16:55 +0000 (UTC) Received: (qmail 67197 invoked by uid 500); 24 Oct 2014 07:16:55 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 67152 invoked by uid 500); 24 Oct 2014 07:16:55 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 67143 invoked by uid 99); 24 Oct 2014 07:16:55 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 24 Oct 2014 07:16:55 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 24 Oct 2014 07:16:24 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id DFC7C23888D2 for ; Fri, 24 Oct 2014 07:15:20 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1634010 - in /httpcomponents/httpcore/trunk: httpcore-ab/src/main/java/org/apache/http/benchmark/ httpcore/src/main/java/org/apache/http/ httpcore/src/main/java/org/apache/http/impl/ httpcore/src/main/java/org/apache/http/impl/io/ httpcore... Date: Fri, 24 Oct 2014 07:15:20 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20141024071520.DFC7C23888D2@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Fri Oct 24 07:15:19 2014 New Revision: 1634010 URL: http://svn.apache.org/r1634010 Log: Refactored message element iterator classes Added: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHeaderElementIterator.java - copied, changed from r1634009, httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java Removed: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HeaderElementIterator.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HeaderIterator.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/TokenIterator.java Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpMessage.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHttpMessage.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderIterator.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicListHeaderIterator.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicTokenIterator.java httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/HeaderGroup.java httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderElementIterator.java httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicTokenIterator.java httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestHeaderGroup.java Modified: httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java (original) +++ httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/BenchmarkWorker.java Fri Oct 24 07:15:19 2014 @@ -31,12 +31,12 @@ import java.io.InputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.nio.charset.Charset; +import java.util.Iterator; import javax.net.SocketFactory; import org.apache.http.ConnectionReuseStrategy; import org.apache.http.Header; -import org.apache.http.HeaderIterator; import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpHost; @@ -254,8 +254,8 @@ class BenchmarkWorker implements Runnabl } private static void resetHeader(final HttpRequest request) { - for (final HeaderIterator it = request.headerIterator(); it.hasNext();) { - final Header header = it.nextHeader(); + for (final Iterator
it = request.headerIterator(); it.hasNext();) { + final Header header = it.next(); if (!(header instanceof DefaultHeader)) { it.remove(); } Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpMessage.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpMessage.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpMessage.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpMessage.java Fri Oct 24 07:15:19 2014 @@ -27,6 +27,8 @@ package org.apache.http; +import java.util.Iterator; + /** * HTTP messages consist of requests from client to server and responses * from server to client. @@ -171,7 +173,7 @@ public interface HttpMessage { * @return Iterator that returns Header objects in the sequence they are * sent over a connection. */ - HeaderIterator headerIterator(); + Iterator
headerIterator(); /** * Returns an iterator of the headers with a given name. @@ -182,6 +184,6 @@ public interface HttpMessage { * @return Iterator that returns Header objects with the argument name * in the sequence they are sent over a connection. */ - HeaderIterator headerIterator(String name); + Iterator
headerIterator(String name); } Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java Fri Oct 24 07:15:19 2014 @@ -27,15 +27,14 @@ package org.apache.http.impl; +import java.util.Iterator; + import org.apache.http.ConnectionReuseStrategy; import org.apache.http.Header; -import org.apache.http.HeaderIterator; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; -import org.apache.http.ParseException; import org.apache.http.ProtocolVersion; -import org.apache.http.TokenIterator; import org.apache.http.annotation.Immutable; import org.apache.http.message.BasicTokenIterator; import org.apache.http.protocol.HTTP; @@ -69,6 +68,11 @@ public class DefaultConnectionReuseStrat super(); } + @Override + public int hashCode() { + return super.hashCode(); + } + // see interface ConnectionReuseStrategy @Override public boolean keepAlive(final HttpResponse response, @@ -107,7 +111,7 @@ public class DefaultConnectionReuseStrat // Check for the "Connection" header. If that is absent, check for // the "Proxy-Connection" header. The latter is an unspecified and // broken but unfortunately common extension of HTTP. - HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE); + Iterator
hit = response.headerIterator(HTTP.CONN_DIRECTIVE); if (!hit.hasNext()) { hit = response.headerIterator("Proxy-Connection"); } @@ -136,28 +140,20 @@ public class DefaultConnectionReuseStrat // If there is no "close" but a "keep-alive", we take the hint. if (hit.hasNext()) { - try { - final TokenIterator ti = createTokenIterator(hit); - boolean keepalive = false; - while (ti.hasNext()) { - final String token = ti.nextToken(); - if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) { - return false; - } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) { - // continue the loop, there may be a "close" afterwards - keepalive = true; - } + final Iterator ti = new BasicTokenIterator(hit); + boolean keepalive = false; + while (ti.hasNext()) { + final String token = ti.next(); + if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) { + return false; + } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) { + // continue the loop, there may be a "close" afterwards + keepalive = true; } - if (keepalive) - { - return true; + } + if (keepalive) { // neither "close" nor "keep-alive", use default policy - } - - } catch (final ParseException px) { - // invalid connection header means no persistent connection - // we don't have logging in HttpCore, so the exception is lost - return false; + return true; } } @@ -165,20 +161,6 @@ public class DefaultConnectionReuseStrat return !ver.lessEquals(HttpVersion.HTTP_1_0); } - - /** - * Creates a token iterator from a header iterator. - * This method can be overridden to replace the implementation of - * the token iterator. - * - * @param hit the header iterator - * - * @return the token iterator - */ - protected TokenIterator createTokenIterator(final HeaderIterator hit) { - return new BasicTokenIterator(hit); - } - private boolean canResponseHaveBody(final HttpResponse response) { final int status = response.getStatusLine().getStatusCode(); return status >= HttpStatus.SC_OK Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractMessageWriter.java Fri Oct 24 07:15:19 2014 @@ -28,9 +28,9 @@ package org.apache.http.impl.io; import java.io.IOException; +import java.util.Iterator; import org.apache.http.Header; -import org.apache.http.HeaderIterator; import org.apache.http.HttpException; import org.apache.http.HttpMessage; import org.apache.http.annotation.NotThreadSafe; @@ -85,8 +85,8 @@ public abstract class AbstractMessageWri public void write(final T message) throws IOException, HttpException { Args.notNull(message, "HTTP message"); writeHeadLine(message); - for (final HeaderIterator it = message.headerIterator(); it.hasNext(); ) { - final Header header = it.nextHeader(); + for (final Iterator
it = message.headerIterator(); it.hasNext(); ) { + final Header header = it.next(); this.sessionBuffer.writeLine (lineFormatter.formatHeader(this.lineBuf, header)); } Copied: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHeaderElementIterator.java (from r1634009, httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java) URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHeaderElementIterator.java?p2=httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHeaderElementIterator.java&p1=httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java&r1=1634009&r2=1634010&rev=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHeaderElementIterator.java Fri Oct 24 07:15:19 2014 @@ -27,53 +27,41 @@ package org.apache.http.message; +import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.http.FormattedHeader; import org.apache.http.Header; -import org.apache.http.HeaderElement; -import org.apache.http.HeaderElementIterator; -import org.apache.http.HeaderIterator; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.util.Args; import org.apache.http.util.CharArrayBuffer; /** - * Basic implementation of a {@link HeaderElementIterator}. + * {@link java.util.Iterator} of {@link org.apache.http.HeaderElement}s. * - * @since 4.0 + * @since 5.0 */ @NotThreadSafe -public class BasicHeaderElementIterator implements HeaderElementIterator { +abstract class AbstractHeaderElementIterator implements Iterator { - private final HeaderIterator headerIt; - private final HeaderValueParser parser; + private final Iterator
headerIt; - private HeaderElement currentElement = null; + private T currentElement = null; private CharArrayBuffer buffer = null; private ParserCursor cursor = null; /** * Creates a new instance of BasicHeaderElementIterator */ - public BasicHeaderElementIterator( - final HeaderIterator headerIterator, - final HeaderValueParser parser) { + AbstractHeaderElementIterator(final Iterator
headerIterator) { this.headerIt = Args.notNull(headerIterator, "Header iterator"); - this.parser = Args.notNull(parser, "Parser"); } - - public BasicHeaderElementIterator(final HeaderIterator headerIterator) { - this(headerIterator, BasicHeaderValueParser.INSTANCE); - } - - private void bufferHeaderValue() { this.cursor = null; this.buffer = null; while (this.headerIt.hasNext()) { - final Header h = this.headerIt.nextHeader(); + final Header h = this.headerIt.next(); if (h instanceof FormattedHeader) { this.buffer = ((FormattedHeader) h).getBuffer(); this.cursor = new ParserCursor(0, this.buffer.length()); @@ -91,6 +79,8 @@ public class BasicHeaderElementIterator } } + abstract T parseHeaderElement(CharArrayBuffer buf, ParserCursor cursor); + private void parseNextElement() { // loop while there are headers left to parse while (this.headerIt.hasNext() || this.cursor != null) { @@ -102,8 +92,8 @@ public class BasicHeaderElementIterator if (this.cursor != null) { // loop while there is data in the buffer while (!this.cursor.atEnd()) { - final HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor); - if (!(e.getName().length() == 0 && e.getValue() == null)) { + final T e = parseHeaderElement(this.buffer, this.cursor); + if (e != null) { // Found something this.currentElement = e; return; @@ -128,7 +118,7 @@ public class BasicHeaderElementIterator } @Override - public HeaderElement nextElement() throws NoSuchElementException { + public T next() throws NoSuchElementException { if (this.currentElement == null) { parseNextElement(); } @@ -137,17 +127,12 @@ public class BasicHeaderElementIterator throw new NoSuchElementException("No more header elements available"); } - final HeaderElement element = this.currentElement; + final T element = this.currentElement; this.currentElement = null; return element; } @Override - public final Object next() throws NoSuchElementException { - return nextElement(); - } - - @Override public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException("Remove not supported"); } Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHttpMessage.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHttpMessage.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHttpMessage.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/AbstractHttpMessage.java Fri Oct 24 07:15:19 2014 @@ -27,8 +27,9 @@ package org.apache.http.message; +import java.util.Iterator; + import org.apache.http.Header; -import org.apache.http.HeaderIterator; import org.apache.http.HttpMessage; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.util.Args; @@ -121,8 +122,8 @@ public abstract class AbstractHttpMessag if (name == null) { return; } - for (final HeaderIterator i = this.headergroup.iterator(); i.hasNext(); ) { - final Header header = i.nextHeader(); + for (final Iterator
i = this.headergroup.iterator(); i.hasNext(); ) { + final Header header = i.next(); if (name.equalsIgnoreCase(header.getName())) { i.remove(); } @@ -131,13 +132,13 @@ public abstract class AbstractHttpMessag // non-javadoc, see interface HttpMessage @Override - public HeaderIterator headerIterator() { + public Iterator
headerIterator() { return this.headergroup.iterator(); } // non-javadoc, see interface HttpMessage @Override - public HeaderIterator headerIterator(final String name) { + public Iterator
headerIterator(final String name) { return this.headergroup.iterator(name); } Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java Fri Oct 24 07:15:19 2014 @@ -27,129 +27,46 @@ package org.apache.http.message; -import java.util.NoSuchElementException; +import java.util.Iterator; -import org.apache.http.FormattedHeader; import org.apache.http.Header; import org.apache.http.HeaderElement; -import org.apache.http.HeaderElementIterator; -import org.apache.http.HeaderIterator; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.util.Args; import org.apache.http.util.CharArrayBuffer; /** - * Basic implementation of a {@link HeaderElementIterator}. + * {@link java.util.Iterator} of {@link org.apache.http.HeaderElement}s. * * @since 4.0 */ @NotThreadSafe -public class BasicHeaderElementIterator implements HeaderElementIterator { +public class BasicHeaderElementIterator extends AbstractHeaderElementIterator { - private final HeaderIterator headerIt; private final HeaderValueParser parser; - private HeaderElement currentElement = null; - private CharArrayBuffer buffer = null; - private ParserCursor cursor = null; - /** * Creates a new instance of BasicHeaderElementIterator */ public BasicHeaderElementIterator( - final HeaderIterator headerIterator, + final Iterator
headerIterator, final HeaderValueParser parser) { - this.headerIt = Args.notNull(headerIterator, "Header iterator"); + super(headerIterator); this.parser = Args.notNull(parser, "Parser"); } - - public BasicHeaderElementIterator(final HeaderIterator headerIterator) { + public BasicHeaderElementIterator(final Iterator
headerIterator) { this(headerIterator, BasicHeaderValueParser.INSTANCE); } - - private void bufferHeaderValue() { - this.cursor = null; - this.buffer = null; - while (this.headerIt.hasNext()) { - final Header h = this.headerIt.nextHeader(); - if (h instanceof FormattedHeader) { - this.buffer = ((FormattedHeader) h).getBuffer(); - this.cursor = new ParserCursor(0, this.buffer.length()); - this.cursor.updatePos(((FormattedHeader) h).getValuePos()); - break; - } else { - final String value = h.getValue(); - if (value != null) { - this.buffer = new CharArrayBuffer(value.length()); - this.buffer.append(value); - this.cursor = new ParserCursor(0, this.buffer.length()); - break; - } - } - } - } - - private void parseNextElement() { - // loop while there are headers left to parse - while (this.headerIt.hasNext() || this.cursor != null) { - if (this.cursor == null || this.cursor.atEnd()) { - // get next header value - bufferHeaderValue(); - } - // Anything buffered? - if (this.cursor != null) { - // loop while there is data in the buffer - while (!this.cursor.atEnd()) { - final HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor); - if (!(e.getName().length() == 0 && e.getValue() == null)) { - // Found something - this.currentElement = e; - return; - } - } - // if at the end of the buffer - if (this.cursor.atEnd()) { - // discard it - this.cursor = null; - this.buffer = null; - } - } - } - } - - @Override - public boolean hasNext() { - if (this.currentElement == null) { - parseNextElement(); - } - return this.currentElement != null; - } - @Override - public HeaderElement nextElement() throws NoSuchElementException { - if (this.currentElement == null) { - parseNextElement(); - } - - if (this.currentElement == null) { - throw new NoSuchElementException("No more header elements available"); + HeaderElement parseHeaderElement(final CharArrayBuffer buf, final ParserCursor cursor) { + final HeaderElement e = this.parser.parseHeaderElement(buf, cursor); + if (!(e.getName().length() == 0 && e.getValue() == null)) { + return e; + } else { + return null; } - - final HeaderElement element = this.currentElement; - this.currentElement = null; - return element; - } - - @Override - public final Object next() throws NoSuchElementException { - return nextElement(); - } - - @Override - public void remove() throws UnsupportedOperationException { - throw new UnsupportedOperationException("Remove not supported"); } } Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderIterator.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderIterator.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderIterator.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderIterator.java Fri Oct 24 07:15:19 2014 @@ -27,20 +27,20 @@ package org.apache.http.message; +import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.http.Header; -import org.apache.http.HeaderIterator; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.util.Args; /** - * Basic implementation of a {@link HeaderIterator}. + * {@link java.util.Iterator} of {@link org.apache.http.Header}s. * * @since 4.0 */ @NotThreadSafe -public class BasicHeaderIterator implements HeaderIterator { +public class BasicHeaderIterator implements Iterator
{ /** * An array of headers to iterate over. @@ -48,23 +48,19 @@ public class BasicHeaderIterator impleme * This array will never be modified by the iterator. * Derived implementations are expected to adhere to this restriction. */ - protected final Header[] allHeaders; - + private final Header[] allHeaders; /** * The position of the next header in {@link #allHeaders allHeaders}. * Negative if the iteration is over. */ - protected int currentIndex; - + private int currentIndex; /** * The header name to filter by. * {@code null} to iterate over all headers in the array. */ - protected String headerName; - - + private String headerName; /** * Creates a new header iterator. @@ -80,7 +76,6 @@ public class BasicHeaderIterator impleme this.currentIndex = findNext(-1); } - /** * Determines the index of the next header. * @@ -90,7 +85,7 @@ public class BasicHeaderIterator impleme * @return the index of the next header that matches the filter name, * or negative if there are no more headers */ - protected int findNext(final int pos) { + private int findNext(final int pos) { int from = pos; if (from < -1) { return -1; @@ -105,7 +100,6 @@ public class BasicHeaderIterator impleme return found ? from : -1; } - /** * Checks whether a header is part of the iteration. * @@ -114,19 +108,16 @@ public class BasicHeaderIterator impleme * @return {@code true} if the header should be part of the * iteration, {@code false} to skip */ - protected boolean filterHeader(final int index) { + private boolean filterHeader(final int index) { return (this.headerName == null) || this.headerName.equalsIgnoreCase(this.allHeaders[index].getName()); } - - // non-javadoc, see interface HeaderIterator @Override public boolean hasNext() { return (this.currentIndex >= 0); } - /** * Obtains the next header from this iteration. * @@ -135,8 +126,7 @@ public class BasicHeaderIterator impleme * @throws NoSuchElementException if there are no more headers */ @Override - public Header nextHeader() - throws NoSuchElementException { + public Header next() throws NoSuchElementException { final int current = this.currentIndex; if (current < 0) { @@ -148,32 +138,14 @@ public class BasicHeaderIterator impleme return this.allHeaders[current]; } - - /** - * Returns the next header. - * Same as {@link #nextHeader nextHeader}, but not type-safe. - * - * @return the next header in this iteration - * - * @throws NoSuchElementException if there are no more headers - */ - @Override - public final Object next() - throws NoSuchElementException { - return nextHeader(); - } - - /** * Removing headers is not supported. * * @throws UnsupportedOperationException always */ @Override - public void remove() - throws UnsupportedOperationException { - - throw new UnsupportedOperationException - ("Removing headers is not supported."); + public void remove() throws UnsupportedOperationException { + throw new UnsupportedOperationException("Removing headers is not supported."); } + } Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicListHeaderIterator.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicListHeaderIterator.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicListHeaderIterator.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicListHeaderIterator.java Fri Oct 24 07:15:19 2014 @@ -27,52 +27,46 @@ package org.apache.http.message; +import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import org.apache.http.Header; -import org.apache.http.HeaderIterator; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.util.Args; import org.apache.http.util.Asserts; /** - * Implementation of a {@link HeaderIterator} based on a {@link List}. - * For use by {@link HeaderGroup}. + * {@link java.util.Iterator} of {@link org.apache.http.Header}s. For use by {@link HeaderGroup}. * * @since 4.0 */ @NotThreadSafe -public class BasicListHeaderIterator implements HeaderIterator { +class BasicListHeaderIterator implements Iterator
{ /** * A list of headers to iterate over. * Not all elements of this array are necessarily part of the iteration. */ - protected final List
allHeaders; - + private final List
allHeaders; /** * The position of the next header in {@link #allHeaders allHeaders}. * Negative if the iteration is over. */ - protected int currentIndex; - + private int currentIndex; /** * The position of the last returned header. * Negative if none has been returned so far. */ - protected int lastIndex; - + private int lastIndex; /** * The header name to filter by. * {@code null} to iterate over all headers in the array. */ - protected String headerName; - - + private String headerName; /** * Creates a new header iterator. @@ -89,7 +83,6 @@ public class BasicListHeaderIterator imp this.lastIndex = -1; } - /** * Determines the index of the next header. * @@ -114,7 +107,6 @@ public class BasicListHeaderIterator imp return found ? from : -1; } - /** * Checks whether a header is part of the iteration. * @@ -123,7 +115,7 @@ public class BasicListHeaderIterator imp * @return {@code true} if the header should be part of the * iteration, {@code false} to skip */ - protected boolean filterHeader(final int index) { + private boolean filterHeader(final int index) { if (this.headerName == null) { return true; } @@ -134,14 +126,11 @@ public class BasicListHeaderIterator imp return this.headerName.equalsIgnoreCase(name); } - - // non-javadoc, see interface HeaderIterator @Override public boolean hasNext() { return (this.currentIndex >= 0); } - /** * Obtains the next header from this iteration. * @@ -150,9 +139,7 @@ public class BasicListHeaderIterator imp * @throws NoSuchElementException if there are no more headers */ @Override - public Header nextHeader() - throws NoSuchElementException { - + public Header next() throws NoSuchElementException { final int current = this.currentIndex; if (current < 0) { throw new NoSuchElementException("Iteration already finished."); @@ -164,31 +151,15 @@ public class BasicListHeaderIterator imp return this.allHeaders.get(current); } - - /** - * Returns the next header. - * Same as {@link #nextHeader nextHeader}, but not type-safe. - * - * @return the next header in this iteration - * - * @throws NoSuchElementException if there are no more headers - */ - @Override - public final Object next() - throws NoSuchElementException { - return nextHeader(); - } - - /** * Removes the header that was returned last. */ @Override - public void remove() - throws UnsupportedOperationException { + public void remove() throws UnsupportedOperationException { Asserts.check(this.lastIndex >= 0, "No header to remove"); this.allHeaders.remove(this.lastIndex); this.lastIndex = -1; this.currentIndex--; // adjust for the removed element } + } Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicTokenIterator.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicTokenIterator.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicTokenIterator.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicTokenIterator.java Fri Oct 24 07:15:19 2014 @@ -27,393 +27,47 @@ package org.apache.http.message; -import java.util.NoSuchElementException; +import java.util.BitSet; +import java.util.Iterator; -import org.apache.http.HeaderIterator; -import org.apache.http.ParseException; -import org.apache.http.TokenIterator; +import org.apache.http.Header; import org.apache.http.annotation.NotThreadSafe; -import org.apache.http.util.Args; +import org.apache.http.util.CharArrayBuffer; +import org.apache.http.util.TextUtils; /** - * Basic implementation of a {@link TokenIterator}. - * This implementation parses {@code #token} sequences as - * defined by RFC 2616, section 2. - * It extends that definition somewhat beyond US-ASCII. + * {@link java.util.Iterator} of {@link org.apache.http.Header} tokens.. * * @since 4.0 */ @NotThreadSafe -public class BasicTokenIterator implements TokenIterator { +public class BasicTokenIterator extends AbstractHeaderElementIterator { - /** The HTTP separator characters. Defined in RFC 2616, section 2.2. */ - // the order of the characters here is adjusted to put the - // most likely candidates at the beginning of the collection - public final static String HTTP_SEPARATORS = " ,;=()<>@:\\\"/[]?{}\t"; - - - /** The iterator from which to obtain the next header. */ - protected final HeaderIterator headerIt; - - /** - * The value of the current header. - * This is the header value that includes {@link #currentToken}. - * Undefined if the iteration is over. - */ - protected String currentHeader; - - /** - * The token to be returned by the next call to {@link #nextToken()}. - * {@code null} if the iteration is over. - */ - protected String currentToken; - - /** - * The position after {@link #currentToken} in {@link #currentHeader}. - * Undefined if the iteration is over. - */ - protected int searchPos; + private static final BitSet COMMA = TokenParser.INIT_BITSET(','); + private final TokenParser parser; /** * Creates a new instance of {@link BasicTokenIterator}. * * @param headerIterator the iterator for the headers to tokenize */ - public BasicTokenIterator(final HeaderIterator headerIterator) { - super(); - this.headerIt = Args.notNull(headerIterator, "Header iterator"); - this.searchPos = findNext(-1); - } - - - // non-javadoc, see interface TokenIterator - @Override - public boolean hasNext() { - return (this.currentToken != null); - } - - - /** - * Obtains the next token from this iteration. - * - * @return the next token in this iteration - * - * @throws NoSuchElementException if the iteration is already over - * @throws ParseException if an invalid header value is encountered - */ - @Override - public String nextToken() - throws NoSuchElementException, ParseException { - - if (this.currentToken == null) { - throw new NoSuchElementException("Iteration already finished."); - } - - final String result = this.currentToken; - // updates currentToken, may trigger ParseException: - this.searchPos = findNext(this.searchPos); - - return result; - } - - - /** - * Returns the next token. - * Same as {@link #nextToken}, but with generic return type. - * - * @return the next token in this iteration - * - * @throws NoSuchElementException if there are no more tokens - * @throws ParseException if an invalid header value is encountered - */ - @Override - public final Object next() - throws NoSuchElementException, ParseException { - return nextToken(); + public BasicTokenIterator(final Iterator
headerIterator) { + super(headerIterator); + this.parser = TokenParser.INSTANCE; } - - /** - * Removing tokens is not supported. - * - * @throws UnsupportedOperationException always - */ @Override - public final void remove() - throws UnsupportedOperationException { - - throw new UnsupportedOperationException - ("Removing tokens is not supported."); - } - - - /** - * Determines the next token. - * If found, the token is stored in {@link #currentToken}. - * The return value indicates the position after the token - * in {@link #currentHeader}. If necessary, the next header - * will be obtained from {@link #headerIt}. - * If not found, {@link #currentToken} is set to {@code null}. - * - * @param pos the position in the current header at which to - * start the search, -1 to search in the first header - * - * @return the position after the found token in the current header, or - * negative if there was no next token - * - * @throws ParseException if an invalid header value is encountered - */ - protected int findNext(final int pos) throws ParseException { - int from = pos; - if (from < 0) { - // called from the constructor, initialize the first header - if (!this.headerIt.hasNext()) { - return -1; + String parseHeaderElement(final CharArrayBuffer buf, final ParserCursor cursor) { + final String token = this.parser.parseToken(buf, cursor, COMMA); + if (!cursor.atEnd()) { + final int pos = cursor.getPos(); + if (buf.charAt(pos) == ',') { + cursor.updatePos(pos + 1); } - this.currentHeader = this.headerIt.nextHeader().getValue(); - from = 0; - } else { - // called after a token, make sure there is a separator - from = findTokenSeparator(from); } - - final int start = findTokenStart(from); - if (start < 0) { - this.currentToken = null; - return -1; // nothing found - } - - final int end = findTokenEnd(start); - this.currentToken = createToken(this.currentHeader, start, end); - return end; - } - - - /** - * Creates a new token to be returned. - * Called from {@link #findNext findNext} after the token is identified. - * The default implementation simply calls - * {@link java.lang.String#substring String.substring}. - *

- * If header values are significantly longer than tokens, and some - * tokens are permanently referenced by the application, there can - * be problems with garbage collection. A substring will hold a - * reference to the full characters of the original string and - * therefore occupies more memory than might be expected. - * To avoid this, override this method and create a new string - * instead of a substring. - *

- * - * @param value the full header value from which to create a token - * @param start the index of the first token character - * @param end the index after the last token character - * - * @return a string representing the token identified by the arguments - */ - protected String createToken(final String value, final int start, final int end) { - return value.substring(start, end); + return !TextUtils.isBlank(token) ? token : null; } - - /** - * Determines the starting position of the next token. - * This method will iterate over headers if necessary. - * - * @param pos the position in the current header at which to - * start the search - * - * @return the position of the token start in the current header, - * negative if no token start could be found - */ - protected int findTokenStart(final int pos) { - int from = Args.notNegative(pos, "Search position"); - boolean found = false; - while (!found && (this.currentHeader != null)) { - - final int to = this.currentHeader.length(); - while (!found && (from < to)) { - - final char ch = this.currentHeader.charAt(from); - if (isTokenSeparator(ch) || isWhitespace(ch)) { - // whitspace and token separators are skipped - from++; - } else if (isTokenChar(this.currentHeader.charAt(from))) { - // found the start of a token - found = true; - } else { - throw new ParseException - ("Invalid character before token (pos " + from + - "): " + this.currentHeader); - } - } - if (!found) { - if (this.headerIt.hasNext()) { - this.currentHeader = this.headerIt.nextHeader().getValue(); - from = 0; - } else { - this.currentHeader = null; - } - } - } // while headers - - return found ? from : -1; - } - - - /** - * Determines the position of the next token separator. - * Because of multi-header joining rules, the end of a - * header value is a token separator. This method does - * therefore not need to iterate over headers. - * - * @param pos the position in the current header at which to - * start the search - * - * @return the position of a token separator in the current header, - * or at the end - * - * @throws ParseException - * if a new token is found before a token separator. - * RFC 2616, section 2.1 explicitly requires a comma between - * tokens for {@code #}. - */ - protected int findTokenSeparator(final int pos) { - int from = Args.notNegative(pos, "Search position"); - boolean found = false; - final int to = this.currentHeader.length(); - while (!found && (from < to)) { - final char ch = this.currentHeader.charAt(from); - if (isTokenSeparator(ch)) { - found = true; - } else if (isWhitespace(ch)) { - from++; - } else if (isTokenChar(ch)) { - throw new ParseException - ("Tokens without separator (pos " + from + - "): " + this.currentHeader); - } else { - throw new ParseException - ("Invalid character after token (pos " + from + - "): " + this.currentHeader); - } - } - - return from; - } - - - /** - * Determines the ending position of the current token. - * This method will not leave the current header value, - * since the end of the header value is a token boundary. - * - * @param from the position of the first character of the token - * - * @return the position after the last character of the token. - * The behavior is undefined if {@code from} does not - * point to a token character in the current header value. - */ - protected int findTokenEnd(final int from) { - Args.notNegative(from, "Search position"); - final int to = this.currentHeader.length(); - int end = from+1; - while ((end < to) && isTokenChar(this.currentHeader.charAt(end))) { - end++; - } - - return end; - } - - - /** - * Checks whether a character is a token separator. - * RFC 2616, section 2.1 defines comma as the separator for - * {@code #token} sequences. The end of a header value will - * also separate tokens, but that is not a character check. - * - * @param ch the character to check - * - * @return {@code true} if the character is a token separator, - * {@code false} otherwise - */ - protected boolean isTokenSeparator(final char ch) { - return (ch == ','); - } - - - /** - * Checks whether a character is a whitespace character. - * RFC 2616, section 2.2 defines space and horizontal tab as whitespace. - * The optional preceeding line break is irrelevant, since header - * continuation is handled transparently when parsing messages. - * - * @param ch the character to check - * - * @return {@code true} if the character is whitespace, - * {@code false} otherwise - */ - protected boolean isWhitespace(final char ch) { - - // we do not use Character.isWhitspace(ch) here, since that allows - // many control characters which are not whitespace as per RFC 2616 - return ((ch == '\t') || Character.isSpaceChar(ch)); - } - - - /** - * Checks whether a character is a valid token character. - * Whitespace, control characters, and HTTP separators are not - * valid token characters. The HTTP specification (RFC 2616, section 2.2) - * defines tokens only for the US-ASCII character set, this - * method extends the definition to other character sets. - * - * @param ch the character to check - * - * @return {@code true} if the character is a valid token start, - * {@code false} otherwise - */ - protected boolean isTokenChar(final char ch) { - - // common sense extension of ALPHA + DIGIT - if (Character.isLetterOrDigit(ch)) { - return true; - } - - // common sense extension of CTL - if (Character.isISOControl(ch)) { - return false; - } - - // no common sense extension for this - if (isHttpSeparator(ch)) { - return false; - } - - // RFC 2616, section 2.2 defines a token character as - // "any CHAR except CTLs or separators". The controls - // and separators are included in the checks above. - // This will yield unexpected results for Unicode format characters. - // If that is a problem, overwrite isHttpSeparator(char) to filter - // out the false positives. - return true; - } - - - /** - * Checks whether a character is an HTTP separator. - * The implementation in this class checks only for the HTTP separators - * defined in RFC 2616, section 2.2. If you need to detect other - * separators beyond the US-ASCII character set, override this method. - * - * @param ch the character to check - * - * @return {@code true} if the character is an HTTP separator - */ - protected boolean isHttpSeparator(final char ch) { - return (HTTP_SEPARATORS.indexOf(ch) >= 0); - } - - -} // class BasicTokenIterator +} Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/HeaderGroup.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/HeaderGroup.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/HeaderGroup.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/HeaderGroup.java Fri Oct 24 07:15:19 2014 @@ -30,11 +30,11 @@ package org.apache.http.message; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.Locale; import org.apache.http.Header; -import org.apache.http.HeaderIterator; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.util.CharArrayBuffer; @@ -267,7 +267,7 @@ public class HeaderGroup implements Clon * * @since 4.0 */ - public HeaderIterator iterator() { + public Iterator
iterator() { return new BasicListHeaderIterator(this.headers, null); } @@ -281,7 +281,7 @@ public class HeaderGroup implements Clon * * @since 4.0 */ - public HeaderIterator iterator(final String name) { + public Iterator
iterator(final String name) { return new BasicListHeaderIterator(this.headers, name); } Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultConnectionReuseStrategy.java Fri Oct 24 07:15:19 2014 @@ -211,15 +211,6 @@ public class TestDefaultConnectionReuseS } @Test - public void testConnectionTokensInvalid() throws Exception { - // Use HTTP 1.1 - final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); - response.addHeader("Transfer-Encoding", "chunked"); - response.addHeader("Connection", "keep-alive=true"); - Assert.assertFalse(reuseStrategy.keepAlive(response, context)); - } - - @Test public void testMultipleContentLength() throws Exception { // Use HTTP 1.1 final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderElementIterator.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderElementIterator.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderElementIterator.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderElementIterator.java Fri Oct 24 07:15:19 2014 @@ -26,11 +26,11 @@ */ package org.apache.http.message; +import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.http.Header; import org.apache.http.HeaderElement; -import org.apache.http.HeaderElementIterator; import org.junit.Assert; import org.junit.Test; @@ -47,17 +47,16 @@ public class TestBasicHeaderElementItera new BasicHeader("Name", "value1") }; - final HeaderElementIterator hei = - new BasicHeaderElementIterator( - new BasicHeaderIterator(headers, "Name")); + final Iterator hei = new BasicHeaderElementIterator( + new BasicHeaderIterator(headers, "Name")); Assert.assertTrue(hei.hasNext()); - HeaderElement elem = hei.nextElement(); + HeaderElement elem = hei.next(); Assert.assertEquals("The two header values must be equal", "value0", elem.getName()); Assert.assertTrue(hei.hasNext()); - elem = hei.nextElement(); + elem = hei.next(); Assert.assertEquals("The two header values must be equal", "value1", elem.getName()); @@ -85,22 +84,22 @@ public class TestBasicHeaderElementItera new BasicHeader("nAme", "cookie1=1,cookie2=2") }; - final HeaderElementIterator hei = - new BasicHeaderElementIterator(new BasicHeaderIterator(headers, "Name")); + final Iterator hei = new BasicHeaderElementIterator( + new BasicHeaderIterator(headers, "Name")); - HeaderElement elem = hei.nextElement(); + HeaderElement elem = hei.next(); Assert.assertEquals("The two header values must be equal", "value0", elem.getName()); - elem = hei.nextElement(); + elem = hei.next(); Assert.assertEquals("The two header values must be equal", "value1", elem.getName()); - elem = hei.nextElement(); + elem = hei.next(); Assert.assertEquals("The two header values must be equal", "cookie1", elem.getName()); Assert.assertEquals("The two header values must be equal", "1", elem.getValue()); - elem = hei.nextElement(); + elem = hei.next(); Assert.assertEquals("The two header values must be equal", "cookie2", elem.getName()); Assert.assertEquals("The two header values must be equal", @@ -115,9 +114,8 @@ public class TestBasicHeaderElementItera new BasicHeader("Name", ",,,") }; - final HeaderElementIterator hei = - new BasicHeaderElementIterator( - new BasicHeaderIterator(headers, "Name")); + final Iterator hei = new BasicHeaderElementIterator( + new BasicHeaderIterator(headers, "Name")); Assert.assertFalse(hei.hasNext()); try { Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java Fri Oct 24 07:15:19 2014 @@ -26,16 +26,16 @@ */ package org.apache.http.message; +import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.http.Header; -import org.apache.http.HeaderIterator; import org.junit.Assert; import org.junit.Test; /** - * Tests for {@link BasicHeaderIterator}. + * Tests for {@link java.util.Iterator} of {@link org.apache.http.Header}s. * */ public class TestBasicHeaderIterator { @@ -50,27 +50,27 @@ public class TestBasicHeaderIterator { }; // without filter - HeaderIterator hit = new BasicHeaderIterator(headers, null); + Iterator
hit = new BasicHeaderIterator(headers, null); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("0", headers[0], hit.nextHeader()); + Assert.assertEquals("0", headers[0], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("1", headers[1], hit.nextHeader()); + Assert.assertEquals("1", headers[1], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("2", headers[2], hit.nextHeader()); + Assert.assertEquals("2", headers[2], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("3", headers[3], hit.nextHeader()); + Assert.assertEquals("3", headers[3], hit.next()); Assert.assertFalse(hit.hasNext()); // with filter hit = new BasicHeaderIterator(headers, "name"); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("0", headers[0], hit.nextHeader()); + Assert.assertEquals("0", headers[0], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("1", headers[1], hit.nextHeader()); + Assert.assertEquals("1", headers[1], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("2", headers[2], hit.nextHeader()); + Assert.assertEquals("2", headers[2], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("3", headers[3], hit.nextHeader()); + Assert.assertEquals("3", headers[3], hit.next()); Assert.assertFalse(hit.hasNext()); } @@ -85,29 +85,29 @@ public class TestBasicHeaderIterator { }; // without filter - HeaderIterator hit = new BasicHeaderIterator(headers, null); + Iterator
hit = new BasicHeaderIterator(headers, null); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("0", headers[0], hit.nextHeader()); + Assert.assertEquals("0", headers[0], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("1", headers[1], hit.nextHeader()); + Assert.assertEquals("1", headers[1], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("2", headers[2], hit.nextHeader()); + Assert.assertEquals("2", headers[2], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("3", headers[3], hit.nextHeader()); + Assert.assertEquals("3", headers[3], hit.next()); Assert.assertFalse(hit.hasNext()); // with filter, first & last hit = new BasicHeaderIterator(headers, "match"); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("0", headers[0], hit.nextHeader()); + Assert.assertEquals("0", headers[0], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("3", headers[3], hit.nextHeader()); + Assert.assertEquals("3", headers[3], hit.next()); Assert.assertFalse(hit.hasNext()); // with filter, one match hit = new BasicHeaderIterator(headers, "single"); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("2", headers[2], hit.nextHeader()); + Assert.assertEquals("2", headers[2], hit.next()); Assert.assertFalse(hit.hasNext()); // with filter, no match @@ -137,79 +137,79 @@ public class TestBasicHeaderIterator { }; // without filter - HeaderIterator hit = new BasicHeaderIterator(headers, null); + Iterator
hit = new BasicHeaderIterator(headers, null); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("0", headers[0], hit.nextHeader()); + Assert.assertEquals("0", headers[0], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("1", headers[1], hit.nextHeader()); + Assert.assertEquals("1", headers[1], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("2", headers[2], hit.nextHeader()); + Assert.assertEquals("2", headers[2], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("3", headers[3], hit.nextHeader()); + Assert.assertEquals("3", headers[3], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("4", headers[4], hit.nextHeader()); + Assert.assertEquals("4", headers[4], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("5", headers[5], hit.nextHeader()); + Assert.assertEquals("5", headers[5], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("6", headers[6], hit.nextHeader()); + Assert.assertEquals("6", headers[6], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("7", headers[7], hit.nextHeader()); + Assert.assertEquals("7", headers[7], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("8", headers[8], hit.nextHeader()); + Assert.assertEquals("8", headers[8], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("9", headers[9], hit.nextHeader()); + Assert.assertEquals("9", headers[9], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("a", headers[10], hit.nextHeader()); + Assert.assertEquals("a", headers[10], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("b", headers[11], hit.nextHeader()); + Assert.assertEquals("b", headers[11], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("c", headers[12], hit.nextHeader()); + Assert.assertEquals("c", headers[12], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("d", headers[13], hit.nextHeader()); + Assert.assertEquals("d", headers[13], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("e", headers[14], hit.nextHeader()); + Assert.assertEquals("e", headers[14], hit.next()); Assert.assertFalse(hit.hasNext()); // yellow 0, 5, 9, 11, 13 hit = new BasicHeaderIterator(headers, "Yellow"); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("0", headers[0], hit.nextHeader()); + Assert.assertEquals("0", headers[0], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("5", headers[5], hit.nextHeader()); + Assert.assertEquals("5", headers[5], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("9", headers[9], hit.nextHeader()); + Assert.assertEquals("9", headers[9], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("b", headers[11], hit.nextHeader()); + Assert.assertEquals("b", headers[11], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("d", headers[13], hit.nextHeader()); + Assert.assertEquals("d", headers[13], hit.next()); Assert.assertFalse(hit.hasNext()); // maroon 1, 6, 7, 8, 10 hit = new BasicHeaderIterator(headers, "marOOn"); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("1", headers[1], hit.nextHeader()); + Assert.assertEquals("1", headers[1], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("6", headers[6], hit.nextHeader()); + Assert.assertEquals("6", headers[6], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("7", headers[7], hit.nextHeader()); + Assert.assertEquals("7", headers[7], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("8", headers[8], hit.nextHeader()); + Assert.assertEquals("8", headers[8], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("a", headers[10], hit.nextHeader()); + Assert.assertEquals("a", headers[10], hit.next()); Assert.assertFalse(hit.hasNext()); // orange 2, 3, 4, 12, 14 hit = new BasicHeaderIterator(headers, "OranGe"); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("2", headers[2], hit.nextHeader()); + Assert.assertEquals("2", headers[2], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("3", headers[3], hit.nextHeader()); + Assert.assertEquals("3", headers[3], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("4", headers[4], hit.nextHeader()); + Assert.assertEquals("4", headers[4], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("b", headers[12], hit.nextHeader()); + Assert.assertEquals("b", headers[12], hit.next()); Assert.assertTrue(hit.hasNext()); - Assert.assertEquals("e", headers[14], hit.nextHeader()); + Assert.assertEquals("e", headers[14], hit.next()); Assert.assertFalse(hit.hasNext()); } @@ -217,7 +217,7 @@ public class TestBasicHeaderIterator { @Test public void testInvalid() { - HeaderIterator hit = null; + Iterator
hit = null; try { hit = new BasicHeaderIterator(null, "whatever"); Assert.fail("null headers not detected"); @@ -231,7 +231,7 @@ public class TestBasicHeaderIterator { // but this is try { - hit.nextHeader(); + hit.next(); Assert.fail("next beyond end not detected"); } catch (final NoSuchElementException nsx) { // expected @@ -251,7 +251,7 @@ public class TestBasicHeaderIterator { }; // without filter, using plain next() - HeaderIterator hit = new BasicHeaderIterator(headers, null); + Iterator
hit = new BasicHeaderIterator(headers, null); Assert.assertTrue(hit.hasNext()); Assert.assertEquals("0", headers[0], hit.next()); Assert.assertTrue(hit.hasNext()); @@ -270,7 +270,5 @@ public class TestBasicHeaderIterator { } catch (final UnsupportedOperationException uox) { // expected } - - Assert.assertTrue("no next", ((BasicHeaderIterator)hit).findNext(-3) < 0); } } Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicTokenIterator.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicTokenIterator.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicTokenIterator.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicTokenIterator.java Fri Oct 24 07:15:19 2014 @@ -27,12 +27,10 @@ package org.apache.http.message; +import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.http.Header; -import org.apache.http.HeaderIterator; -import org.apache.http.ParseException; -import org.apache.http.TokenIterator; import org.junit.Assert; import org.junit.Test; @@ -48,17 +46,17 @@ public class TestBasicTokenIterator { Header[] headers = new Header[]{ new BasicHeader("Name", "token0,token1, token2 , token3") }; - HeaderIterator hit = new BasicHeaderIterator(headers, null); - TokenIterator ti = new BasicTokenIterator(hit); + Iterator
hit = new BasicHeaderIterator(headers, null); + Iterator ti = new BasicTokenIterator(hit); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token0", "token0", ti.nextToken()); + Assert.assertEquals("token0", "token0", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token1", "token1", ti.nextToken()); + Assert.assertEquals("token1", "token1", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token2", "token2", ti.nextToken()); + Assert.assertEquals("token2", "token2", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token3", "token3", ti.nextToken()); + Assert.assertEquals("token3", "token3", ti.next()); Assert.assertFalse(ti.hasNext()); @@ -69,7 +67,7 @@ public class TestBasicTokenIterator { ti = new BasicTokenIterator(hit); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token0", "token0", ti.nextToken()); + Assert.assertEquals("token0", "token0", ti.next()); Assert.assertFalse(ti.hasNext()); } @@ -85,19 +83,19 @@ public class TestBasicTokenIterator { new BasicHeader("Name", ","), new BasicHeader("Name", "token4"), }; - final HeaderIterator hit = new BasicHeaderIterator(headers, null); - final TokenIterator ti = new BasicTokenIterator(hit); + final Iterator
hit = new BasicHeaderIterator(headers, null); + final Iterator ti = new BasicTokenIterator(hit); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token0", "token0", ti.nextToken()); + Assert.assertEquals("token0", "token0", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token1", "token1", ti.nextToken()); + Assert.assertEquals("token1", "token1", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token2", "token2", ti.nextToken()); + Assert.assertEquals("token2", "token2", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token3", "token3", ti.nextToken()); + Assert.assertEquals("token3", "token3", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token4", "token4", ti.nextToken()); + Assert.assertEquals("token4", "token4", ti.next()); Assert.assertFalse(ti.hasNext()); } @@ -110,8 +108,8 @@ public class TestBasicTokenIterator { new BasicHeader("Name", ","), new BasicHeader("Name", " ,, "), }; - HeaderIterator hit = new BasicHeaderIterator(headers, null); - TokenIterator ti = new BasicTokenIterator(hit); + Iterator
hit = new BasicHeaderIterator(headers, null); + Iterator ti = new BasicTokenIterator(hit); Assert.assertFalse(ti.hasNext()); @@ -133,21 +131,21 @@ public class TestBasicTokenIterator { new BasicHeader("Name", ", token4"), new BasicHeader("Name", " , token5"), }; - final HeaderIterator hit = new BasicHeaderIterator(headers, null); - final TokenIterator ti = new BasicTokenIterator(hit); + final Iterator
hit = new BasicHeaderIterator(headers, null); + final Iterator ti = new BasicTokenIterator(hit); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token0", "token0", ti.nextToken()); + Assert.assertEquals("token0", "token0", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token1", "token1", ti.nextToken()); + Assert.assertEquals("token1", "token1", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token2", "token2", ti.nextToken()); + Assert.assertEquals("token2", "token2", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token3", "token3", ti.nextToken()); + Assert.assertEquals("token3", "token3", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token4", "token4", ti.nextToken()); + Assert.assertEquals("token4", "token4", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token5", "token5", ti.nextToken()); + Assert.assertEquals("token5", "token5", ti.next()); Assert.assertFalse(ti.hasNext()); } @@ -162,99 +160,24 @@ public class TestBasicTokenIterator { new BasicHeader("Name", "token4, "), new BasicHeader("Name", "token5 , "), }; - final HeaderIterator hit = new BasicHeaderIterator(headers, null); - final TokenIterator ti = new BasicTokenIterator(hit); + final Iterator
hit = new BasicHeaderIterator(headers, null); + final Iterator ti = new BasicTokenIterator(hit); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token0", "token0", ti.nextToken()); + Assert.assertEquals("token0", "token0", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token1", "token1", ti.nextToken()); + Assert.assertEquals("token1", "token1", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token2", "token2", ti.nextToken()); + Assert.assertEquals("token2", "token2", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token3", "token3", ti.nextToken()); + Assert.assertEquals("token3", "token3", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token4", "token4", ti.nextToken()); + Assert.assertEquals("token4", "token4", ti.next()); Assert.assertTrue(ti.hasNext()); - Assert.assertEquals("token5", "token5", ti.nextToken()); + Assert.assertEquals("token5", "token5", ti.next()); Assert.assertFalse(ti.hasNext()); } - - @Test - public void testTokenChar() { - final Header[] headers = new Header[]{ - new BasicHeader("Name", "token0") - }; - final HeaderIterator hit = new BasicHeaderIterator(headers, null); - final BasicTokenIterator bti = new BasicTokenIterator(hit); - - Assert.assertTrue ("letter" , bti.isTokenChar('j')); - Assert.assertFalse("control" , bti.isTokenChar('\b')); - Assert.assertFalse("separator", bti.isTokenChar('?')); - Assert.assertTrue ("other" , bti.isTokenChar('-')); - } - - - @Test - public void testInvalid() { - final Header[] headers = new Header[]{ - new BasicHeader("in", "token0=token1"), - new BasicHeader("no", "token0 token1"), - new BasicHeader("pre", " hit = new BasicHeaderIterator(headers, null); + final Iterator ti = new BasicTokenIterator(hit); try { - // call next() instead of nextToken() to get that covered, too + // call next() instead of next() to get that covered, too ti.next(); Assert.fail("next after end not detected"); } catch (final NoSuchElementException nsx) { @@ -290,36 +213,4 @@ public class TestBasicTokenIterator { } } - - @Test - public void testWrongProtected() { - - final Header[] headers = new Header[]{ - new BasicHeader("Name", "token1,token2") - }; - final HeaderIterator hit = new BasicHeaderIterator(headers, null); - final BasicTokenIterator bti = new BasicTokenIterator(hit); - - try { - bti.findTokenStart(-1); - Assert.fail("tokenStart: negative index not detected"); - } catch (final IllegalArgumentException iax) { - // expected - } - - try { - bti.findTokenSeparator(-1); - Assert.fail("tokenSeparator: negative index not detected"); - } catch (final IllegalArgumentException iax) { - // expected - } - - try { - bti.findTokenEnd(-1); - Assert.fail("tokenEnd: negative index not detected"); - } catch (final IllegalArgumentException iax) { - // expected - } - } - } Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestHeaderGroup.java URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestHeaderGroup.java?rev=1634010&r1=1634009&r2=1634010&view=diff ============================================================================== --- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestHeaderGroup.java (original) +++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestHeaderGroup.java Fri Oct 24 07:15:19 2014 @@ -31,9 +31,9 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.util.Iterator; import org.apache.http.Header; -import org.apache.http.HeaderIterator; import org.junit.Assert; import org.junit.Test; @@ -140,7 +140,7 @@ public class TestHeaderGroup { @Test public void testIterator() { final HeaderGroup headergroup = new HeaderGroup(); - final HeaderIterator i = headergroup.iterator(); + final Iterator
i = headergroup.iterator(); Assert.assertNotNull(i); Assert.assertFalse(i.hasNext()); } @@ -152,7 +152,7 @@ public class TestHeaderGroup { final Header header2 = new BasicHeader("name", "value2"); final Header header3 = new BasicHeader("name", "value3"); headergroup.setHeaders(new Header[] { header1, header2, header3 }); - final HeaderIterator i = headergroup.iterator(); + final Iterator
i = headergroup.iterator(); Assert.assertNotNull(i); Assert.assertTrue(i.hasNext()); i.next();