hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r581984 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http: HttpMessage.java message/AbstractHttpMessage.java util/HeaderGroup.java util/ListHeaderIterator.java
Date Thu, 04 Oct 2007 18:55:15 GMT
Author: rolandw
Date: Thu Oct  4 11:55:15 2007
New Revision: 581984

URL: http://svn.apache.org/viewvc?rev=581984&view=rev
Log:
Damn, I had forgotten about HeaderGroup. Added second implementation based on List, both HeaderGroup
and HttpMessage provide HeaderIterator instances.

Added:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ListHeaderIterator.java
  (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpMessage.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/AbstractHttpMessage.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/HeaderGroup.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpMessage.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpMessage.java?rev=581984&r1=581983&r2=581984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpMessage.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpMessage.java
Thu Oct  4 11:55:15 2007
@@ -162,7 +162,18 @@
      * @return Iterator that returns Header objects in the sequence they are
      *         sent over a connection.
      */
-    Iterator headerIterator();
+    HeaderIterator headerIterator();
+
+    /**
+     * Returns an iterator of the headers with a given name.
+     *
+     * @param name      the name of the headers over which to iterate, or
+     *                  <code>null</code> for all headers
+     *
+     * @return Iterator that returns Header objects with the argument name
+     *         in the sequence they are sent over a connection.
+     */
+    HeaderIterator headerIterator(String name);
 
     /**
      * Returns the parameters effective for this message as set by

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/AbstractHttpMessage.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/AbstractHttpMessage.java?rev=581984&r1=581983&r2=581984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/AbstractHttpMessage.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/AbstractHttpMessage.java
Thu Oct  4 11:55:15 2007
@@ -34,6 +34,7 @@
 import java.util.Iterator;
 
 import org.apache.http.Header;
+import org.apache.http.HeaderIterator;
 import org.apache.http.HttpMessage;
 import org.apache.http.util.HeaderGroup;
 import org.apache.http.params.HttpParams;
@@ -52,7 +53,7 @@
     
     private final HeaderGroup headergroup;
     
-    private HttpParams params = null;
+    private HttpParams params;
     
     protected AbstractHttpMessage() {
         super();
@@ -60,30 +61,37 @@
         this.params = new BasicHttpParams(null);
     }
 
+    // non-javadoc, see interface HttpMessage
     public boolean containsHeader(String name) {
         return this.headergroup.containsHeader(name);
     }
     
+    // non-javadoc, see interface HttpMessage
     public Header[] getHeaders(final String name) {
         return this.headergroup.getHeaders(name);
     }
 
+    // non-javadoc, see interface HttpMessage
     public Header getFirstHeader(final String name) {
         return this.headergroup.getFirstHeader(name);
     }
 
+    // non-javadoc, see interface HttpMessage
     public Header getLastHeader(final String name) {
         return this.headergroup.getLastHeader(name);
     }
 
+    // non-javadoc, see interface HttpMessage
     public Header[] getAllHeaders() {
         return this.headergroup.getAllHeaders();
     }
     
+    // non-javadoc, see interface HttpMessage
     public void addHeader(final Header header) {
         this.headergroup.addHeader(header);
     }
 
+    // non-javadoc, see interface HttpMessage
     public void addHeader(final String name, final String value) {
         if (name == null) {
             throw new IllegalArgumentException("Header name may not be null");
@@ -91,10 +99,12 @@
         this.headergroup.addHeader(new BasicHeader(name, value));
     }
 
+    // non-javadoc, see interface HttpMessage
     public void setHeader(final Header header) {
         this.headergroup.updateHeader(header);
     }
 
+    // non-javadoc, see interface HttpMessage
     public void setHeader(final String name, final String value) {
         if (name == null) {
             throw new IllegalArgumentException("Header name may not be null");
@@ -102,14 +112,17 @@
         this.headergroup.updateHeader(new BasicHeader(name, value));
     }
 
+    // non-javadoc, see interface HttpMessage
     public void setHeaders(final Header[] headers) {
         this.headergroup.setHeaders(headers);
     }
 
+    // non-javadoc, see interface HttpMessage
     public void removeHeader(final Header header) {
         this.headergroup.removeHeader(header);
     }
     
+    // non-javadoc, see interface HttpMessage
     public void removeHeaders(final String name) {
         if (name == null) {
             return;
@@ -122,14 +135,22 @@
         }
     }
     
-    public Iterator headerIterator() {
+    // non-javadoc, see interface HttpMessage
+    public HeaderIterator headerIterator() {
         return this.headergroup.iterator();
     }
+
+    // non-javadoc, see interface HttpMessage
+    public HeaderIterator headerIterator(String name) {
+        return this.headergroup.iterator(name);
+    }
     
+    // non-javadoc, see interface HttpMessage
     public HttpParams getParams() {
         return this.params;
     }
     
+    // non-javadoc, see interface HttpMessage
     public void setParams(final HttpParams params) {
         if (params == null) {
             throw new IllegalArgumentException("HTTP parameters may not be null");

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/HeaderGroup.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/HeaderGroup.java?rev=581984&r1=581983&r2=581984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/HeaderGroup.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/HeaderGroup.java
Thu Oct  4 11:55:15 2007
@@ -36,6 +36,7 @@
 import java.util.List;
 
 import org.apache.http.Header;
+import org.apache.http.HeaderIterator;
 import org.apache.http.message.BasicHeader;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -257,7 +258,21 @@
      * 
      * @since 4.0
      */
-    public Iterator iterator() {
-        return this.headers.iterator(); 
+    public HeaderIterator iterator() {
+        return new ListHeaderIterator(this.headers, null); 
+    }
+
+    /**
+     * Returns an iterator over the headers with a given name in this group.
+     *
+     * @param name      the name of the headers over which to iterate, or
+     *                  <code>null</code> for all headers
+     *
+     * @return iterator over some headers in this group.
+     * 
+     * @since 4.0
+     */
+    public HeaderIterator iterator(String name) {
+        return new ListHeaderIterator(this.headers, name);
     }
 }

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ListHeaderIterator.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ListHeaderIterator.java?rev=581984&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ListHeaderIterator.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ListHeaderIterator.java
Thu Oct  4 11:55:15 2007
@@ -0,0 +1,196 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.util;
+
+
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import org.apache.http.Header;
+import org.apache.http.HeaderIterator;
+
+
+/**
+ * Implementation of a {@link HeaderIterator} based on a {@link List}.
+ * For use by {@link HeaderGroup}.
+ * 
+ * @version $Revision$
+ */
+public class ListHeaderIterator implements HeaderIterator {
+
+    /**
+     * A list of headers to iterate over.
+     * Not all elements of this array are necessarily part of the iteration.
+     */
+    protected final List allHeaders;
+
+
+    /**
+     * The position of the next header in {@link #allHeaders allHeaders}.
+     * Negative if the iteration is over.
+     */
+    protected int currentIndex;
+
+
+    /**
+     * The position of the last returned header.
+     * Negative if none has been returned so far.
+     */
+    protected int lastIndex;
+
+
+    /**
+     * The header name to filter by.
+     * <code>null</code> to iterate over all headers in the array.
+     */
+    protected String headerName;
+
+
+
+    /**
+     * Creates a new header iterator.
+     *
+     * @param headers   a list of headers over which to iterate
+     * @param name      the name of the headers over which to iterate, or
+     *                  <code>null</code> for any
+     */
+    public ListHeaderIterator(List headers, String name) {
+        if (headers == null) {
+            throw new IllegalArgumentException
+                ("Header list must not be null.");
+        }
+
+        this.allHeaders = headers;
+        this.headerName = name;
+        this.currentIndex = findNext(-1);
+        this.lastIndex = -1;
+    }
+
+
+    /**
+     * Determines the index of the next header.
+     *
+     * @param from      one less than the index to consider first,
+     *                  -1 to search for the first header
+     *
+     * @return  the index of the next header that matches the filter name,
+     *          or negative if there are no more headers
+     */
+    protected int findNext(int from) {
+        if (from < -1)
+            return -1;
+
+        final int to = this.allHeaders.size()-1;
+        boolean found = false;
+        while (!found && (from < to)) {
+            from++;
+            found = filterHeader(from);
+        }
+        return found ? from : -1;
+    }
+
+
+    /**
+     * Checks whether a header is part of the iteration.
+     *
+     * @param index     the index of the header to check
+     *
+     * @return  <code>true</code> if the header should be part of the
+     *          iteration, <code>false</code> to skip
+     */
+    protected boolean filterHeader(int index) {
+        if (this.headerName == null)
+            return true;
+
+        // non-header elements, including null, will trigger exceptions
+        final String name = ((Header)this.allHeaders.get(index)).getName();
+
+        return this.headerName.equalsIgnoreCase(name);
+    }
+
+
+    // non-javadoc, see interface HeaderIterator
+    public boolean hasNext() {
+        return (this.currentIndex >= 0);
+    }
+
+
+    /**
+     * Obtains the next header from this iteration.
+     *
+     * @return  the next header in this iteration
+     *
+     * @throws NoSuchElementException   if there are no more headers
+     */
+    public Header nextHeader()
+        throws NoSuchElementException {
+
+        final int current = this.currentIndex;
+        if (current < 0) {
+            throw new NoSuchElementException("Iteration already finished.");
+        }
+
+        this.lastIndex    = current;
+        this.currentIndex = findNext(current);
+
+        return (Header) 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
+     */
+    public final Object next()
+        throws NoSuchElementException {
+        return nextHeader();
+    }
+
+
+    /**
+     * Removes the header that was returned last.
+     */
+    public void remove()
+        throws UnsupportedOperationException {
+
+        if (this.lastIndex < 0) {
+            throw new IllegalStateException("No header to remove.");
+        }
+        this.allHeaders.remove(this.lastIndex);
+        this.lastIndex = -1;
+        this.currentIndex--; // adjust for the removed element
+    }
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ListHeaderIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ListHeaderIterator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ListHeaderIterator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message