You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2003/02/28 13:48:59 UTC
cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods EntityEnclosingMethod.java MultipartPostMethod.java
olegk 2003/02/28 04:48:59
Modified: httpclient/src/java/org/apache/commons/httpclient
ChunkedOutputStream.java HttpConnection.java
HttpMethodBase.java HttpParser.java
WireLogInputStream.java
httpclient/src/java/org/apache/commons/httpclient/methods
EntityEnclosingMethod.java MultipartPostMethod.java
Added: httpclient/src/java/org/apache/commons/httpclient Wire.java
WireLogOutputStream.java
Log:
Wire logging refactored
Contributed by Oleg Kalnichevski
Revision Changes Path
1.9 +8 -17 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java
Index: ChunkedOutputStream.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ChunkedOutputStream.java 8 Feb 2003 19:22:49 -0000 1.8
+++ ChunkedOutputStream.java 28 Feb 2003 12:48:58 -0000 1.9
@@ -103,9 +103,6 @@
/** Log object for this class. */
private static final Log LOG = LogFactory.getLog(ChunkedOutputStream.class);
- /** Log for any wire messages. */
- private static final Log WIRE_LOG = LogFactory.getLog("httpclient.wire");
-
// ----------------------------------------------------- Instance Variables
/** Has this stream been closed? */
@@ -190,10 +187,7 @@
stream.write(CRLF, 0, CRLF.length);
stream.write(b);
stream.write(ENDCHUNK, 0, ENDCHUNK.length);
- if (WIRE_LOG.isDebugEnabled()) {
- WIRE_LOG.debug(">> byte 1 \\r\\n (chunk length \"header\")");
- WIRE_LOG.debug(">> byte " + b + "\\r\\n (chunked byte)");
- }
+ LOG.debug("Writing chunk (length: 1)");
}
/**
@@ -215,11 +209,8 @@
stream.write(chunkHeader, 0, chunkHeader.length);
stream.write(b, off, len);
stream.write(ENDCHUNK, 0, ENDCHUNK.length);
- if (WIRE_LOG.isDebugEnabled()) {
- WIRE_LOG.debug(">> byte(s)" + len + " \\r\\n (chunk length "
- + "\"header\")");
- WIRE_LOG.debug(">> \"" + new String(b, off, len)
- + "\"\\r\\n (chunked bytes)");
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Writing chunk (length: " + len + ")");
}
}
@@ -239,7 +230,7 @@
stream.write(ZERO, 0, ZERO.length);
stream.write(CRLF, 0, CRLF.length);
stream.write(ENDCHUNK, 0, ENDCHUNK.length);
- WIRE_LOG.debug(">> byte 0 \\r\\n\\r\\n (final chunk)");
+ LOG.debug("Writing closing chunk");
} catch (IOException e) {
LOG.debug("Unexpected exception caught when closing "
+ "output stream", e);
1.49 +10 -17 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java
Index: HttpConnection.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v
retrieving revision 1.48
retrieving revision 1.49
diff -u -r1.48 -r1.49
--- HttpConnection.java 27 Feb 2003 19:56:46 -0000 1.48
+++ HttpConnection.java 28 Feb 2003 12:48:58 -0000 1.49
@@ -596,7 +596,11 @@
throws IOException, IllegalStateException {
LOG.trace("enter HttpConnection.getRequestOutputStream()");
assertOpen();
- return outputStream;
+ OutputStream out = this.outputStream;
+ if (Wire.enabled()) {
+ out = new WireLogOutputStream(out);
+ }
+ return out;
}
/**
@@ -751,10 +755,6 @@
assertOpen();
- if (WIRE_LOG.isDebugEnabled()) {
- String dataString = new String(data, offset, length, "ISO-8859-1");
- WIRE_LOG.debug(">> \"" + dataString + "\" [\\r\\n]");
- }
try {
outputStream.write(data, offset, length);
} catch (SocketException se) {
@@ -782,10 +782,7 @@
LOG.trace("enter HttpConnection.writeLine(byte[])");
assertOpen();
- if (WIRE_LOG.isDebugEnabled() && (data.length > 0)) {
- String dataString = HttpConstants.getContentString(data);
- WIRE_LOG.debug(">> \"" + dataString.trim() + "\" [\\r\\n]");
- }
+
try {
outputStream.write(data);
writeLine();
@@ -810,7 +807,6 @@
throws IOException, IllegalStateException, HttpRecoverableException {
LOG.trace("enter HttpConnection.writeLine()");
- WIRE_LOG.debug(">> [\\r\\n]");
try {
outputStream.write(CRLF);
} catch (SocketException se) {
@@ -1066,9 +1062,6 @@
/** Log object for this class. */
private static final Log LOG = LogFactory.getLog(HttpConnection.class);
-
- /** Log for any wire messages. */
- private static final Log WIRE_LOG = LogFactory.getLog("httpclient.wire");
// ----------------------------------------------------- Instance Variables
/** My host. */
1.119 +25 -11 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java
Index: HttpMethodBase.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
retrieving revision 1.118
retrieving revision 1.119
diff -u -r1.118 -r1.119
--- HttpMethodBase.java 27 Feb 2003 23:41:38 -0000 1.118
+++ HttpMethodBase.java 28 Feb 2003 12:48:58 -0000 1.119
@@ -147,9 +147,6 @@
/** Log object for this class. */
private static final Log LOG = LogFactory.getLog(HttpMethod.class);
- /** Log for any wire messages. */
- private static final Log WIRE_LOG = LogFactory.getLog("httpclient.wire");
-
/** The User-Agent header sent on every request. */
protected static final Header USER_AGENT;
@@ -665,7 +662,7 @@
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream is = getResponseBodyAsStream();
- byte[] buffer = new byte[10000];
+ byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
os.write(buffer, 0, len);
@@ -1803,7 +1800,7 @@
Header lengthHeader = getResponseHeader("Content-Length");
Header transferEncodingHeader = getResponseHeader("Transfer-Encoding");
InputStream is = conn.getResponseInputStream();
- if (WIRE_LOG.isDebugEnabled()) {
+ if (Wire.enabled()) {
is = new WireLogInputStream(is);
}
InputStream result = null;
@@ -1897,6 +1894,11 @@
getResponseHeaderGroup().clear();
Header[] headers = HttpParser.parseHeaders(conn.getResponseInputStream());
+ if (Wire.enabled()) {
+ for (int i = 0; i < headers.length; i++) {
+ Wire.input(headers[i].toExternalForm());
+ }
+ }
getResponseHeaderGroup().setHeaders(headers);
}
@@ -1938,7 +1940,9 @@
+ " line from the response: unable to find line starting with"
+ " \"HTTP/\"");
}
-
+ if (Wire.enabled()) {
+ Wire.input(statusString);
+ }
//create the status line from the status string
statusLine = new StatusLine(statusString);
@@ -2002,6 +2006,9 @@
writeRequestLine(state, conn);
writeRequestHeaders(state, conn);
conn.writeLine(); // close head
+ if (Wire.enabled()) {
+ Wire.output("\r\n");
+ }
bodySent = writeRequestBody(state, conn);
}
@@ -2065,7 +2072,11 @@
Header[] headers = getRequestHeaders();
for (int i = 0; i < headers.length; i++) {
- conn.print(headers[i].toExternalForm());
+ String s = headers[i].toExternalForm();
+ if (Wire.enabled()) {
+ Wire.output(s);
+ }
+ conn.print(s);
}
}
@@ -2089,6 +2100,9 @@
LOG.trace(
"enter HttpMethodBase.writeRequestLine(HttpState, HttpConnection)");
String requestLine = getRequestLine(conn);
+ if (Wire.enabled()) {
+ Wire.output(requestLine);
+ }
conn.print(requestLine);
}
1.3 +0 -5 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpParser.java
Index: HttpParser.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpParser.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HttpParser.java 20 Feb 2003 16:36:20 -0000 1.2
+++ HttpParser.java 28 Feb 2003 12:48:58 -0000 1.3
@@ -21,8 +21,6 @@
/** Log object for this class. */
private static final Log LOG = LogFactory.getLog(HttpParser.class);
- /** Log for any wire messages. */
- private static final Log WIRE_LOG = LogFactory.getLog("httpclient.wire");
/**
* Constructor for HttpParser.
*/
@@ -57,9 +55,6 @@
break;
}
}
- }
- if (WIRE_LOG.isDebugEnabled()) {
- WIRE_LOG.debug("<< \"" + buf.toString() + (ch>0 ? "\" [\\r\\n]" : ""));
}
if (buf.size() == 0) {
return null;
1.9 +22 -10 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/WireLogInputStream.java
Index: WireLogInputStream.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/WireLogInputStream.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- WireLogInputStream.java 31 Jan 2003 00:33:36 -0000 1.8
+++ WireLogInputStream.java 28 Feb 2003 12:48:58 -0000 1.9
@@ -74,29 +74,39 @@
*
* @author Ortwin Gl�ck
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
+ * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
*
* @since 2.0
*/
class WireLogInputStream extends FilterInputStream {
+
/** Log for any wire messages. */
private static final Log WIRE_LOG = LogFactory.getLog("httpclient.wire");
+ /** Content encoding. */
+ private String charset = "US-ASCII";
+
+ /** Original input stream. */
+ private InputStream in;
+
/**
* Create an instance that wraps the specified input stream.
* @param in The input stream.
*/
public WireLogInputStream(InputStream in) {
super(in);
+ this.in = in;
}
-
/**
*
* @see java.io.InputStream#read(byte[], int, int)
*/
public int read(byte[] b, int off, int len) throws IOException {
- int l = super.read(b, off, len);
- WIRE_LOG.debug("<< " + new String(b, off, len));
+ int l = this.in.read(b, off, len);
+ if (l > 0) {
+ Wire.input(b, off, l);
+ }
return l;
}
@@ -105,9 +115,9 @@
* @see java.io.InputStream#read()
*/
public int read() throws IOException {
- int l = super.read();
+ int l = this.in.read();
if (l > 0) {
- WIRE_LOG.debug("<< " + (char) l);
+ Wire.input(l);
}
return l;
}
@@ -117,8 +127,10 @@
* @see java.io.InputStream#read(byte[])
*/
public int read(byte[] b) throws IOException {
- int l = super.read(b);
- WIRE_LOG.debug("<< " + HttpConstants.getString(b));
+ int l = this.in.read(b);
+ if (l > 0) {
+ Wire.input(b);
+ }
return l;
}
}
1.1 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Wire.java
Index: Wire.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* 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/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.httpclient;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Logs data to the wire LOG.
*
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
*
* @since 2.0beta1
*/
class Wire {
/** Log for any wire messages. */
private static final Log WIRE_LOG = LogFactory.getLog("httpclient.wire");
private static void wire(String header, InputStream instream)
throws IOException {
Reader reader = null;
try {
reader = new InputStreamReader(instream, "US-ASCII");
} catch (UnsupportedEncodingException e) {
reader = new InputStreamReader(instream);
}
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = reader.read()) != -1) {
if (ch == 13) {
buffer.append("[\\r]");
} else if (ch == 10){
buffer.append("[\\n]\"");
buffer.insert(0, "\"");
buffer.insert(0, header);
WIRE_LOG.debug(buffer.toString());
buffer.setLength(0);
} else if ((ch < 32) || (ch > 127)) {
buffer.append("[0x");
buffer.append(Integer.toHexString(ch));
buffer.append("]");
} else {
buffer.append((char)ch);
}
}
if (buffer.length() > 0) {
buffer.append("\"");
buffer.insert(0, "\"");
buffer.insert(0, header);
WIRE_LOG.debug(buffer.toString());
}
}
public static final boolean enabled() {
return WIRE_LOG.isDebugEnabled();
}
public static final void output(InputStream outstream)
throws IOException {
if (outstream == null) {
throw new IllegalArgumentException("Output may not be null");
}
wire(">> ", outstream);
}
public static final void input(InputStream instream)
throws IOException {
if (instream == null) {
throw new IllegalArgumentException("Input may not be null");
}
wire("<< ", instream);
}
public static final void output(byte[] b, int off, int len)
throws IOException {
if (b == null) {
throw new IllegalArgumentException("Output may not be null");
}
wire(">> ", new ByteArrayInputStream(b, off, len));
}
public static final void input(byte[] b, int off, int len)
throws IOException {
if (b == null) {
throw new IllegalArgumentException("Input may not be null");
}
wire("<< ", new ByteArrayInputStream(b, off, len));
}
public static final void output(byte[] b)
throws IOException {
if (b == null) {
throw new IllegalArgumentException("Output may not be null");
}
wire(">> ", new ByteArrayInputStream(b));
}
public static final void input(byte[] b)
throws IOException {
if (b == null) {
throw new IllegalArgumentException("Input may not be null");
}
wire("<< ", new ByteArrayInputStream(b));
}
public static final void output(int b)
throws IOException {
output(new byte[] {(byte)b});
}
public static final void input(int b)
throws IOException {
input(new byte[] {(byte)b});
}
public static final void output(final String s)
throws IOException {
if (s == null) {
throw new IllegalArgumentException("Output may noy be null");
}
output(s.getBytes());
}
public static final void input(final String s)
throws IOException {
if (s == null) {
throw new IllegalArgumentException("Input may noy be null");
}
input(s.getBytes());
}
}
1.1 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/WireLogOutputStream.java
Index: WireLogOutputStream.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* 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/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.httpclient;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Logs all data written to the wire LOG.
*
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
*
* @since 2.0beta1
*/
class WireLogOutputStream extends FilterOutputStream {
/** Log for any wire messages. */
private static final Log WIRE_LOG = LogFactory.getLog("httpclient.wire");
/** Original input stream. */
private OutputStream out;
/**
* Create an instance that wraps the specified output stream.
* @param out The output stream.
*/
public WireLogOutputStream(OutputStream out) {
super(out);
this.out = out;
}
/**
*
* @see java.io.OutputStream#write(byte[], int, int)
*/
public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
Wire.output(b, off, len);
}
/**
*
* @see java.io.OutputStream#write()
*/
public void write(int b) throws IOException {
this.out.write(b);
Wire.output(b);
}
/**
*
* @see java.io.OutputStream#write(byte[])
*/
public void write(byte[] b) throws IOException {
this.out.write(b);
Wire.output(b);
}
}
1.11 +5 -4 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java
Index: EntityEnclosingMethod.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- EntityEnclosingMethod.java 25 Feb 2003 23:33:48 -0000 1.10
+++ EntityEnclosingMethod.java 28 Feb 2003 12:48:58 -0000 1.11
@@ -423,6 +423,7 @@
this.repeatCount++;
OutputStream outstream = conn.getRequestOutputStream();
+
if (contentLength == CONTENT_LENGTH_CHUNKED) {
outstream = new ChunkedOutputStream(outstream);
}
1.12 +6 -5 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/MultipartPostMethod.java
Index: MultipartPostMethod.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/MultipartPostMethod.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- MultipartPostMethod.java 25 Feb 2003 23:33:48 -0000 1.11
+++ MultipartPostMethod.java 28 Feb 2003 12:48:58 -0000 1.12
@@ -286,8 +286,9 @@
return false;
}
}
- OutputStream out = conn.getRequestOutputStream();
- Part.sendParts(out, getParts());
+ OutputStream outstream = conn.getRequestOutputStream();
+
+ Part.sendParts(outstream, getParts());
return true;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org