[Htmlparser-cvs] htmlparser/src/org/htmlparser/http HttpHeader.java,NONE,1.1 ConnectionManager.java,
Brought to you by:
derrickoswald
From: Derrick O. <der...@us...> - 2005-06-19 12:01:23
|
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/http In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26937/htmlparser/src/org/htmlparser/http Modified Files: ConnectionManager.java package.html Added Files: HttpHeader.java Log Message: Changes to allow compilation of htmllexer.jar by gcj. Move non-JDK1.1 functionality to HttpHeader class. Unhook NodeList from filters by removing searchFor(cls) - use keepAllNodesThatMatch(new NodeClassFilter(cls)) instead. Include classes missing from closure set. Index: package.html =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/http/package.html,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** package.html 10 Apr 2005 23:20:43 -0000 1.2 --- package.html 19 Jun 2005 12:01:14 -0000 1.3 *************** *** 44,47 **** --- 44,48 ---- <a href="http://www.ietf.org/rfc/rfc2616.txt">HTTP protocol</a>. Each of these capabilities requires conditioning the HTTP connection. + A HTTP header utility class is also included. <p>The {@link org.htmlparser.http.ConnectionMonitor} interface is a callback mechanism for the ConnectionManager to notify an interested application *************** *** 72,85 **** manager.setUser ("FredB"); manager.setPassword ("holy$cow"); ! // set up an inner class for callbacks ConnectionMonitor monitor = new ConnectionMonitor () { public void preConnect (HttpURLConnection connection) { ! System.out.println (ConnectionManager.getRequestHeader (connection)); } public void postConnect (HttpURLConnection connection) { ! System.out.println (ConnectionManager.getResponseHeader (connection)); } }; --- 73,86 ---- manager.setUser ("FredB"); manager.setPassword ("holy$cow"); ! // set up (an inner class) for callbacks ConnectionMonitor monitor = new ConnectionMonitor () { public void preConnect (HttpURLConnection connection) { ! System.out.println (HttpHeader.getRequestHeader (connection)); } public void postConnect (HttpURLConnection connection) { ! System.out.println (HttpHeader.getResponseHeader (connection)); } }; --- NEW FILE: HttpHeader.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2004 Derrick Oswald // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/http/HttpHeader.java,v $ // $Author: derrickoswald $ // $Date: 2005/06/19 12:01:13 $ // $Revision: 1.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.http; import java.io.IOException; import java.net.HttpURLConnection; import java.util.Iterator; import java.util.List; import java.util.Map; /** * Utility methods to display HTTP headers. */ public class HttpHeader { /** * Private constructor. * This class is completely static. */ private HttpHeader () { } /** * Gets the request header for the connection. * <em>This header is generated from the contents of the connection * and may not be exactly the same as the request that will be sent.</em> * @param connection The connection to convert into an HTTP request header. * @return The string that would be sent by the HTTP request. */ public static String getRequestHeader (HttpURLConnection connection) { // dump it StringBuffer buffer; Map map; String key; List items; buffer = new StringBuffer (1024); buffer.append (connection.getRequestMethod ()); buffer.append (" "); buffer.append (connection.getURL ()); buffer.append (" HTTP/1.1\n"); map = connection.getRequestProperties (); for (Iterator iter = map.keySet ().iterator (); iter.hasNext (); ) { key = (String)iter.next (); items = (List)map.get (key); buffer.append (key); buffer.append (": "); for (int i = 0; i < items.size (); i++) { if (0 != i) buffer.append (", "); buffer.append (items.get (i)); } buffer.append ("\n"); } return (buffer.toString ()); } /** * Gets the response header for the connection. * Calling this method on an un-connected connection will * generate an error, as will an attempt to get information * from a connected but invalid connection. * <em>This header is generated from the contents of the connection * and may not be exactly the same as the response that was received.</em> * @param conn The connection to convert into an HTTP response header. * @return The string that was sent as the HTTP response. */ public static String getResponseHeader (HttpURLConnection conn) { // dump it StringBuffer buffer; int code; String message; String key; String value; buffer = new StringBuffer (1024); try { code = conn.getResponseCode (); if (-1 != code) { message = conn.getResponseMessage (); buffer.append ("HTTP/1.1 "); buffer.append (code); buffer.append (" "); buffer.append (message); buffer.append ("\n"); for (int i = 0; null != (value = conn.getHeaderField (i)); i++) { key = conn.getHeaderFieldKey (i); if (null != key) { buffer.append (key); buffer.append (": "); buffer.append (value); buffer.append ("\n"); } } } } catch (IOException ioe) { buffer.append (ioe.toString ()); } return (buffer.toString ()); } } Index: ConnectionManager.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/http/ConnectionManager.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ConnectionManager.java 15 May 2005 11:49:04 -0000 1.4 --- ConnectionManager.java 19 Jun 2005 12:01:13 -0000 1.5 *************** *** 39,45 **** import java.util.Enumeration; import java.util.Hashtable; - import java.util.Iterator; - import java.util.List; - import java.util.Map; import java.util.Properties; import java.util.StringTokenizer; --- 39,42 ---- *************** *** 249,343 **** /** - * Gets the request header for the connection. - * <em>This header is generated from the contents of the connection - * and may not be exactly the same as the request that will be sent.</em> - * @param connection The connection to convert into an HTTP request header. - * @return The string that would be sent by the HTTP request. - */ - public static String getRequestHeader (HttpURLConnection connection) - { - // dump it - StringBuffer buffer; - Map map; - String key; - List items; - - buffer = new StringBuffer (1024); - buffer.append (connection.getRequestMethod ()); - buffer.append (" "); - buffer.append (connection.getURL ()); - buffer.append (" HTTP/1.1\n"); - map = connection.getRequestProperties (); - for (Iterator iter = map.keySet ().iterator (); iter.hasNext (); ) - { - key = (String)iter.next (); - items = (List)map.get (key); - buffer.append (key); - buffer.append (": "); - for (int i = 0; i < items.size (); i++) - { - if (0 != i) - buffer.append (", "); - buffer.append (items.get (i)); - } - buffer.append ("\n"); - } - - return (buffer.toString ()); - } - - /** - * Gets the response header for the connection. - * Calling this method on an un-connected connection will - * generate an error, as will an attempt to get information - * from a connected but invalid connection. - * <em>This header is generated from the contents of the connection - * and may not be exactly the same as the response that was received.</em> - * @param conn The connection to convert into an HTTP response header. - * @return The string that was sent as the HTTP response. - */ - public static String getResponseHeader (HttpURLConnection conn) - { - // dump it - StringBuffer buffer; - int code; - String message; - String key; - String value; - - buffer = new StringBuffer (1024); - try - { - code = conn.getResponseCode (); - if (-1 != code) - { - message = conn.getResponseMessage (); - buffer.append ("HTTP/1.1 "); - buffer.append (code); - buffer.append (" "); - buffer.append (message); - buffer.append ("\n"); - for (int i = 0; null != (value = conn.getHeaderField (i)); i++) - { - key = conn.getHeaderFieldKey (i); - if (null != key) - { - buffer.append (key); - buffer.append (": "); - buffer.append (value); - buffer.append ("\n"); - } - } - } - } - catch (IOException ioe) - { - buffer.append (ioe.toString ()); - } - - return (buffer.toString ()); - } - - /** * Get the current request header properties. * A String-to-String map of header keys and values, --- 246,249 ---- *************** *** 630,634 **** ret.setRequestProperty (key, value); } ! // set the proxy name and password if ((null != getProxyUser ()) --- 536,540 ---- ret.setRequestProperty (key, value); } ! // set the proxy name and password if ((null != getProxyUser ()) |