Index: httpclient/src/java/org/apache/commons/httpclient/Authenticator.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java,v retrieving revision 1.37 diff -u -r1.37 Authenticator.java --- httpclient/src/java/org/apache/commons/httpclient/Authenticator.java 23 Jan 2003 22:47:43 -0000 1.37 +++ httpclient/src/java/org/apache/commons/httpclient/Authenticator.java 27 Jan 2003 01:56:46 -0000 @@ -98,6 +98,7 @@ * @author Ortwin Glück * @author Sean C. Sullivan * @author Adrian Sutton + * @author Authorization header for the given * Challenge and state to the given method. * + * @param challenge The challenge. * @param method the {@link HttpMethod request} requiring the ntlm * @param state a {@link HttpState} object providing * {@link Credentials} @@ -343,7 +347,7 @@ private static Header ntlm(String challenge, HttpMethod method, HttpState state, String responseHeader) throws HttpException { - log.trace("enter Authenticator.ntlm(String, HttpMethod, HttpState, " + LOG.trace("enter Authenticator.ntlm(String, HttpMethod, HttpState, " + "String)"); boolean proxy = PROXY_AUTH_RESP.equals(responseHeader); @@ -353,9 +357,9 @@ if (method.getRequestHeader("Host") != null) { String host = method.getRequestHeader("Host").getValue(); try { - credentials = (NTCredentials) (proxy ? - state.getProxyCredentials(host) : - state.getCredentials(host)); + credentials = (NTCredentials) (proxy + ? state.getProxyCredentials(host) + : state.getCredentials(host)); } catch (ClassCastException e) { throw new HttpException("NTCredentials required " + "for NTLM authentication."); @@ -363,12 +367,12 @@ } if (credentials == null) { - log.info("No credentials for specific host, " + - "attempting to use default credentials."); + LOG.info("No credentials for specific host, " + + "attempting to use default credentials."); try { - credentials = (NTCredentials) (proxy ? - state.getProxyCredentials(null) : - state.getCredentials(null)); + credentials = (NTCredentials) (proxy + ? state.getProxyCredentials(null) + : state.getCredentials(null)); } catch (ClassCastException e) { throw new HttpException( "NTCredentials required for NTLM authentication."); @@ -377,8 +381,8 @@ try { challenge = - challenge.substring(challenge.toLowerCase().indexOf("ntlm") + - "ntlm".length()).trim(); + challenge.substring(challenge.toLowerCase().indexOf("ntlm") + + "ntlm".length()).trim(); } catch (IndexOutOfBoundsException e) { throw new HttpException("Invalid NTLM challenge."); } @@ -391,8 +395,8 @@ String response = "NTLM " + ntlm.getResponseFor(challenge, credentials.getUserName(), credentials.getPassword(), credentials.getHost(), credentials.getDomain()); - if (log.isDebugEnabled()) { - log.debug("Replying to challenge with: " + response); + if (LOG.isDebugEnabled()) { + LOG.debug("Replying to challenge with: " + response); } return new Header(responseHeader, response); } @@ -415,31 +419,31 @@ private static Header digest(String realm, HttpMethod method, HttpState state, String responseHeader) throws HttpException { - log.trace("enter Authenticator.digest(String, HttpMethod, HttpState, " + LOG.trace("enter Authenticator.digest(String, HttpMethod, HttpState, " + "String)"); boolean proxy = PROXY_AUTH_RESP.equals(responseHeader); UsernamePasswordCredentials credentials = null; try { - credentials = (UsernamePasswordCredentials) (proxy ? - state.getProxyCredentials(realm) : - state.getCredentials(realm)); + credentials = (UsernamePasswordCredentials) (proxy + ? state.getProxyCredentials(realm) + : state.getCredentials(realm)); } catch (ClassCastException e) { throw new HttpException("UsernamePasswordCredentials required for " + "Digest authentication."); } if (credentials == null) { - if (log.isInfoEnabled()) { - log.info("No credentials found for realm \"" + realm + "\", " + if (LOG.isInfoEnabled()) { + LOG.info("No credentials found for realm \"" + realm + "\", " + "attempting to use default credentials."); } try { - credentials = (UsernamePasswordCredentials) (proxy ? - state.getProxyCredentials(null) : - state.getCredentials(null)); + credentials = (UsernamePasswordCredentials) (proxy + ? state.getProxyCredentials(null) + : state.getCredentials(null)); } catch (ClassCastException e) { throw new HttpException("UsernamePasswordCredentials required " + "for Digest authentication."); @@ -467,14 +471,14 @@ * UsernamePasswordCredentials}. * * @param credentials Credentials to create the digest with - * @param headers The headers for the current request + * @param mapHeaders The headers for the current request * @return a string containing the authorization header for digest * @throws HttpException When a recoverable error occurs */ private static String digest(UsernamePasswordCredentials credentials, Map mapHeaders) throws HttpException { - log.trace("enter Authenticator.digest(UsernamePasswordCredentials, " + LOG.trace("enter Authenticator.digest(UsernamePasswordCredentials, " + "Map)"); String digest = createDigest(credentials.getUserName(), @@ -503,7 +507,7 @@ */ private static Map getHTTPDigestCredentials(HttpMethod method, boolean proxy) { - log.trace("enter Authenticator.getHTTPDigestCredentials(HttpMethod, " + LOG.trace("enter Authenticator.getHTTPDigestCredentials(HttpMethod, " + "boolean)"); //Determine wether to use proxy or www header @@ -549,10 +553,10 @@ */ private static Map parseAuthenticateHeader(Header authHeader) { - log.trace("enter parseAuthenticateHeader(Header)"); + LOG.trace("enter parseAuthenticateHeader(Header)"); - if (log.isDebugEnabled()) { - log.debug("Attempting to parse authenticate header: '" + if (LOG.isDebugEnabled()) { + LOG.debug("Attempting to parse authenticate header: '" + authHeader + "'"); } if (authHeader == null || authHeader.getValue() == null) { @@ -579,8 +583,8 @@ atComma = authValue.indexOf(',', atStart); // skip any commas in quotes - while (atComma > atQuote1 && atComma < atQuote2 && - atComma > 0) { + while (atComma > atQuote1 && atComma < atQuote2 + && atComma > 0) { atComma = authValue.indexOf(',', atComma + 1); } @@ -589,10 +593,10 @@ atComma = authValueLength; } - if (log.isDebugEnabled()) { - log.debug("atStart =" + atStart + ", atQuote1 =" + - atQuote1 + ", atQuote2=" + atQuote2 + ", atComma =" - + atComma); + if (LOG.isDebugEnabled()) { + LOG.debug("atStart =" + atStart + ", atQuote1 =" + + atQuote1 + ", atQuote2=" + atQuote2 + ", atComma =" + + atComma); } try { @@ -602,24 +606,24 @@ //find the blank and parse out the scheme atSpace = challenge.indexOf(' '); - scheme = (atSpace > 0) ? - challenge.substring(0, atSpace).trim() : challenge; + scheme = (atSpace > 0) + ? challenge.substring(0, atSpace).trim() : challenge; //store the challenge keyed on the scheme challengeMap.put(scheme.toLowerCase(), challenge); - if (log.isDebugEnabled()) { - log.debug(scheme.toLowerCase() + "=>" + challenge); + if (LOG.isDebugEnabled()) { + LOG.debug(scheme.toLowerCase() + "=>" + challenge); } } catch (StringIndexOutOfBoundsException e) { - log.warn("Parsing authorization challenge'" + challenge + - "' failed", e); + LOG.warn("Parsing authorization challenge'" + challenge + + "' failed", e); } } // end of while } catch (StringIndexOutOfBoundsException e) { - log.warn("Parsing authorization header value'" + authValue + - "' failed", e); + LOG.warn("Parsing authorization header value'" + authValue + + "' failed", e); } return challengeMap; @@ -649,7 +653,7 @@ Header authenticateHeader, String responseHeader) throws HttpException, UnsupportedOperationException { - log.trace("enter Authenticator.authenticate(HttpMethod, HttpState, " + LOG.trace("enter Authenticator.authenticate(HttpMethod, HttpState, " + "Header, String)"); // check the preemptive policy @@ -661,7 +665,7 @@ if (!(preemptiveDefault.equals("true") || preemptiveDefault.equals("false"))) { // property problem - log.warn("Configuration property " + PREEMPTIVE_PROPERTY + LOG.warn("Configuration property " + PREEMPTIVE_PROPERTY + " must be either true or false. Using default: " + PREEMPTIVE_DEFAULT); preemptiveDefault = PREEMPTIVE_DEFAULT; @@ -672,7 +676,7 @@ //if there is no challenge, attempt to use preemptive authorization if (authenticateHeader == null) { if (preemptive) { - log.debug("Preemptively sending default basic credentials"); + LOG.debug("Preemptively sending default basic credentials"); try { Header requestHeader = Authenticator.basic(null, state, @@ -680,8 +684,8 @@ method.addRequestHeader(requestHeader); return true; } catch (HttpException httpe) { - if (log.isDebugEnabled()) { - log.debug( + if (LOG.isDebugEnabled()) { + LOG.debug( "No default credentials to preemptively send"); } return false; @@ -736,7 +740,7 @@ * TODO: + Add createCnonce() method */ private static String createCnonce() throws HttpException { - log.trace("enter Authenticator.createCnonce()"); + LOG.trace("enter Authenticator.createCnonce()"); String cnonce; final String digAlg = "MD5"; @@ -745,7 +749,7 @@ try { md5Helper = MessageDigest.getInstance(digAlg); } catch (Exception e) { - log.error("ERROR! Unsupported algorithm in HTTP Digest " + LOG.error("ERROR! Unsupported algorithm in HTTP Digest " + "authentication: " + digAlg); throw new HttpException("Unsupported algorithm in HTTP Digest " + "authentication: " + digAlg); @@ -772,7 +776,7 @@ private static String createDigestHeader(String uname, Map mapCredentials, String digest) { - log.trace("enter Authenticator.createDigestHeader(String, Map, " + LOG.trace("enter Authenticator.createDigestHeader(String, Map, " + "String)"); StringBuffer sb = new StringBuffer(); @@ -814,9 +818,11 @@ * TODO: + Add encode() method */ private static String encode(byte[] binaryData) { - log.trace("enter Authenticator.encode(byte[])"); + LOG.trace("enter Authenticator.encode(byte[])"); - if (binaryData.length != 16) return null; + if (binaryData.length != 16) { + return null; + } char[] buffer = new char[32]; for (int i = 0; i < 16; i++) { @@ -853,8 +859,8 @@ realm = realm.substring(atFirst + 1, atLast); } - if (log.isDebugEnabled()) { - log.debug("Parsed realm '" + realm + "' from challenge '" + if (LOG.isDebugEnabled()) { + LOG.debug("Parsed realm '" + realm + "' from challenge '" + challenge + "'"); } @@ -877,7 +883,7 @@ * TODO: + Add processDigestToken() method */ private static void processDigestToken(String token, Map tokens) { - log.trace("enter Authenticator.processDigestToken(String, Map)"); + LOG.trace("enter Authenticator.processDigestToken(String, Map)"); int atEqual = token.indexOf("="); @@ -900,15 +906,17 @@ * is same as str */ private static String removeQuotes(String str) { - log.trace("enter Authenticator.removeQuotes(String)"); + LOG.trace("enter Authenticator.removeQuotes(String)"); - if (str == null) return null; + if (str == null) { + return null; + } int atFirst = str.indexOf("\"") + 1; int atLast = str.lastIndexOf("\""); - return (atFirst > 0 && atLast > atFirst) ? - str.substring(atFirst, atLast) : str; + return (atFirst > 0 && atLast > atFirst) + ? str.substring(atFirst, atLast) : str; } } Index: httpclient/src/java/org/apache/commons/httpclient/AutoCloseInputStream.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/AutoCloseInputStream.java,v retrieving revision 1.5 diff -u -r1.5 AutoCloseInputStream.java --- httpclient/src/java/org/apache/commons/httpclient/AutoCloseInputStream.java 23 Jan 2003 22:47:43 -0000 1.5 +++ httpclient/src/java/org/apache/commons/httpclient/AutoCloseInputStream.java 27 Jan 2003 01:56:46 -0000 @@ -72,18 +72,26 @@ * * @author Ortwin Glück * @author Eric Johnson + * @author + *

Note that this class NEVER closes the underlying stream, even when close + * gets called. Instead, it will read until the "end" of its chunking on close, + * which allows for the seamless invocation of subsequent HTTP 1.1 calls, while + * not requiring the client to remember to read the entire contents of the + * response.

* * @see ResponseInputStream * @@ -79,16 +82,29 @@ * @author Sean C. Sullivan * @author Martin Elwin * @author Eric Johnson + * @author
Returns all the data in a chunked stream in coalesced form. A chunk + * is followed by a CRLF. The method returns -1 as soon as a chunksize of 0 + * is detected.

+ * + *

Footers are read automcatically at the end of the stream and can be + * obtained with the getFooters() method.

+ * + * @return -1 of the end of the stream has been reached or the next data + * byte + * @throws IOException If an IO problem occurs */ public int read() throws IOException { - if (closed) + if (closed) { throw new IOException("Attempted read from closed stream."); - if (eof) return -1; + } + if (eof) { + return -1; + } if (pos >= chunkSize) { nextChunk(); - if (eof) return -1; + if (eof) { + return -1; + } } pos++; return in.read(); } - public int read(byte[] b, int off, int len) throws java.io.IOException { + /** + * Read some bytes from the stream. + * @param b The byte array that will hold the contents from the stream. + * @param off The offset into the byte array at which bytes will start to be + * placed. + * @param len the maximum number of bytes that can be returned. + * @return The number of bytes returned or -1 if the end of stream has been + * reached. + * @see java.io.InputStream#read(byte[], int, int) + * @throws IOException if an IO problem occurs. + */ + public int read (byte[] b, int off, int len) throws IOException { - if (closed) + if (closed) { throw new IOException("Attempted read from closed stream."); + } - if (eof) return -1; + if (eof) { + return -1; + } if (pos >= chunkSize) { nextChunk(); - if (eof) return -1; + if (eof) { + return -1; + } } len = Math.min(len, chunkSize - pos); int count = in.read(b, off, len); @@ -157,15 +196,29 @@ return count; } - public int read(byte[] b) throws java.io.IOException { + /** + * Read some bytes from the stream. + * @param b The byte array that will hold the contents from the stream. + * @return The number of bytes returned or -1 if the end of stream has been + * reached. + * @see java.io.InputStream#read(byte[]) + * @throws IOException if an IO problem occurs. + */ + public int read (byte[] b) throws IOException { return read(b, 0, b.length); } + /** + * Read the next chunk. + * @throws IOException If an IO error occurs. + */ private void nextChunk() throws IOException { int cr = in.read(); int lf = in.read(); - if ((cr != '\r') || - (lf != '\n')) throw new IOException("CRLF expected at end of chunk: "+cr+"/"+lf); + if ((cr != '\r') || (lf != '\n')) { + throw new IOException( + "CRLF expected at end of chunk: " + cr + "/" + lf); + } chunkSize = getChunkSizeFromInputStream(in); pos = 0; if (chunkSize == 0) { @@ -175,30 +228,31 @@ } /** - * Expects the stream to start with a chunksize in hex with optional comments - * after a semicolon. The line must end with a CRLF: - * "a3; some comment\r\n" - * Positions the stream at the start of the next line. + * Expects the stream to start with a chunksize in hex with optional + * comments after a semicolon. The line must end with a CRLF: "a3; some + * comment\r\n" Positions the stream at the start of the next line. * + * @param in The new input stream. * @return the chunk size as integer - * * @throws IOException when the chunk size could not be parsed - * @throws java.lang.RuntimeException - * - * */ - private static int getChunkSizeFromInputStream(final InputStream in) throws IOException { + private static int getChunkSizeFromInputStream( + final InputStream in) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int state = 0; //0: normal, 1: \r was scanned, 2: inside quoted string, -1: end + // States: 0=normal, 1=\r was scanned, 2=inside quoted string, -1=end + int state = 0; while (state != -1) { int b = in.read(); - if (b == -1) throw new IOException("chunked stream ended unexpectedly"); + if (b == -1) { + throw new IOException("chunked stream ended unexpectedly"); + } switch (state) { - case 0: + case 0: switch (b) { case '\r': state = 1; - break; + break; case '\"': state = 2; /* fall through */ @@ -212,7 +266,8 @@ state = -1; } else { // this was not CRLF - throw new IOException("Protocol violation: Unexpected single newline character in chunk size"); + throw new IOException("Protocol violation: Unexpected" + + " single newline character in chunk size"); } break; @@ -243,14 +298,15 @@ int result; try { result = Integer.parseInt(dataString.trim(), 16); - } catch(NumberFormatException e) { - throw new IOException("Bad chunk size: "+dataString); + } catch (NumberFormatException e) { + throw new IOException ("Bad chunk size: " + dataString); } return result; } /** * Stores the footers into map of Headers + * @throws IOException If an IO problem occurs */ private void parseFooters() throws IOException { String line = readLine(); @@ -258,7 +314,7 @@ int colonPos = line.indexOf(':'); if (colonPos != -1) { String key = line.substring(0, colonPos).trim(); - String val = line.substring(colonPos+1).trim(); + String val = line.substring(colonPos + 1).trim(); Header footer = new Header(key, val); method.addResponseFooter(footer); } @@ -266,12 +322,17 @@ } } + /** + * Read the next line from {@link #in}. + * @return String The next line. + * @throws IOException If an IO problem occurs. + */ private String readLine() throws IOException { StringBuffer buf = new StringBuffer(); - for(;;) { + while (true) { int ch = in.read(); - if(ch < 0) { - if(buf.length() == 0) { + if (ch < 0) { + if (buf.length() == 0) { return null; } else { break; @@ -281,7 +342,7 @@ } else if (ch == '\n') { break; } - buf.append((char)ch); + buf.append((char) ch); } return (buf.toString()); } @@ -290,6 +351,7 @@ * Upon close, this reads the remainder of the chunked message, * leaving the underlying socket at a position to start reading the * next response without scanning. + * @throws IOException If an IO problem occurs. */ public void close() throws IOException { if (!closed) { @@ -297,8 +359,7 @@ if (!eof) { exhaustInputStream(this); } - } - finally { + } finally { eof = true; closed = true; } @@ -314,11 +375,12 @@ * shared that way.

* * @param inStream The {@link InputStream} to exhaust. + * @throws IOException If an IO problem occurs */ static void exhaustInputStream(InputStream inStream) throws IOException { // read and discard the remainder of the message byte buffer[] = new byte[1024]; - while ( inStream.read(buffer) >= 0) { + while (inStream.read(buffer) >= 0) { ; } } Index: httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java,v retrieving revision 1.4 diff -u -r1.4 ChunkedOutputStream.java --- httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java 23 Jan 2003 22:09:33 -0000 1.4 +++ httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java 27 Jan 2003 01:56:45 -0000 @@ -63,9 +63,10 @@ package org.apache.commons.httpclient; +import java.io.IOException; +import java.io.OutputStream; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import java.io.*; @@ -78,6 +79,7 @@ * @author Sean C. Sullivan * @author
dIon Gillard * @author Oleg Kalnichevski + * @author > byte 1 \\r\\n (chunk length \"header\")"); - wireLog.debug(">> byte " + b + "\\r\\n (chunked byte)"); + if (WIRE_LOG.isDebugEnabled()) { + WIRE_LOG.debug(">> byte 1 \\r\\n (chunk length \"header\")"); + WIRE_LOG.debug(">> byte " + b + "\\r\\n (chunked byte)"); } } @@ -204,20 +206,21 @@ * @param len the length of data within b to write * @throws IOException when errors occur writing output */ - public void write(byte[] b, int off, int len) throws IOException { - log.trace("enter ChunckedOutputStream.write(byte[], int, int)"); + public void write (byte[] b, int off, int len) throws IOException { + LOG.trace("enter ChunckedOutputStream.write(byte[], int, int)"); - if (closed){ + if (closed) { throw new IllegalStateException("Output stream already closed"); } - byte chunkHeader[] = HttpConstants.getBytes(Integer.toHexString(len) + "\r\n"); + byte chunkHeader[] = HttpConstants.getBytes ( + Integer.toHexString(len) + "\r\n"); stream.write(chunkHeader, 0, chunkHeader.length); stream.write(b, off, len); stream.write(ENDCHUNK, 0, ENDCHUNK.length); - if (wireLog.isDebugEnabled()) { - wireLog.debug(">> byte(s)" + len + " \\r\\n (chunk length " + if (WIRE_LOG.isDebugEnabled()) { + WIRE_LOG.debug(">> byte(s)" + len + " \\r\\n (chunk length " + "\"header\")"); - wireLog.debug(">> \"" + new String(b, off, len) + WIRE_LOG.debug(">> \"" + new String(b, off, len) + "\"\\r\\n (chunked bytes)"); } } @@ -230,7 +233,7 @@ * @throws IOException if an error occurs closing the stream */ public void writeClosingChunk() throws IOException { - log.trace("enter ChunkedOutputStream.writeClosingChunk()"); + LOG.trace("enter ChunkedOutputStream.writeClosingChunk()"); if (!closed) { try { @@ -238,9 +241,10 @@ stream.write(ZERO, 0, ZERO.length); stream.write(CRLF, 0, CRLF.length); stream.write(ENDCHUNK, 0, ENDCHUNK.length); - wireLog.debug(">> byte 0 \\r\\n\\r\\n (final chunk)"); + WIRE_LOG.debug(">> byte 0 \\r\\n\\r\\n (final chunk)"); } catch (IOException e) { - log.debug("Unexpected exception caught when closing output stream", e); + LOG.debug("Unexpected exception caught when closing " + + "output stream", e); throw e; } finally { // regardless of what happens, mark the stream as closed. @@ -253,8 +257,9 @@ /** * Flushes the underlying stream. + * @throws IOException If an IO problem occurs. */ - public void flush() throws java.io.IOException { + public void flush() throws IOException { stream.flush(); } @@ -266,7 +271,7 @@ * @throws IOException if an error occurs closing the stream */ public void close() throws IOException { - log.trace("enter ChunkedOutputStream.close()"); + LOG.trace("enter ChunkedOutputStream.close()"); writeClosingChunk(); super.close(); } Index: httpclient/src/java/org/apache/commons/httpclient/ConnectMethod.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ConnectMethod.java,v retrieving revision 1.5 diff -u -r1.5 ConnectMethod.java --- httpclient/src/java/org/apache/commons/httpclient/ConnectMethod.java 23 Jan 2003 22:47:45 -0000 1.5 +++ httpclient/src/java/org/apache/commons/httpclient/ConnectMethod.java 27 Jan 2003 01:56:45 -0000 @@ -63,7 +63,6 @@ package org.apache.commons.httpclient; import java.io.IOException; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -72,6 +71,7 @@ * * @author Ortwin Glück * @author dIon Gillard + * @author = 200) && (code < 300)) { conn.tunnelCreated(); code = method.execute(state, conn); @@ -131,7 +131,7 @@ */ protected void writeRequestHeaders(HttpState state, HttpConnection conn) throws HttpException, IOException { - log.trace("enter ConnectMethod.writeRequestHeaders(HttpState, " + LOG.trace("enter ConnectMethod.writeRequestHeaders(HttpState, " + "HttpConnection)"); if (method instanceof HttpMethodBase) { @@ -168,7 +168,7 @@ /** Log object for this class. */ - private static final Log log = LogFactory.getLog(ConnectMethod.class); + private static final Log LOG = LogFactory.getLog(ConnectMethod.class); /** The wrapped method */ private HttpMethod method; Index: httpclient/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java,v retrieving revision 1.4 diff -u -r1.4 ContentLengthInputStream.java --- httpclient/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java 23 Jan 2003 22:47:45 -0000 1.4 +++ httpclient/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java 27 Jan 2003 01:56:46 -0000 @@ -62,20 +62,31 @@ package org.apache.commons.httpclient; -import java.io.*; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; /** * Cuts the wrapped InputStream off after a specified number of bytes. * * @author Ortwin Glück * @author Eric Johnson + * @author = contentLength) + if (pos >= contentLength) { return -1; + } pos++; return super.read(); } @@ -130,12 +151,14 @@ * * @throws java.io.IOException Should an error occur on the wrapped stream. */ - public int read(byte[] b, int off, int len) throws java.io.IOException { - if (closed) + public int read (byte[] b, int off, int len) throws java.io.IOException { + if (closed) { throw new IOException("Attempted read from closed stream."); + } - if (pos >= contentLength) + if (pos >= contentLength) { return -1; + } if (pos + len > contentLength) { len = contentLength - pos; @@ -146,7 +169,14 @@ } - public int read(byte[] b) throws java.io.IOException { + /** + * Read more bytes from the stream. + * @param b The byte array to put the new data in. + * @return The number of bytes read into the buffer. + * @throws IOException If an IO problem occurs + * @see java.io.InputStream#read(byte[]) + */ + public int read(byte[] b) throws IOException { return read(b, 0, b.length); } Index: httpclient/src/java/org/apache/commons/httpclient/Cookie.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Cookie.java,v retrieving revision 1.33 diff -u -r1.33 Cookie.java --- httpclient/src/java/org/apache/commons/httpclient/Cookie.java 23 Jan 2003 22:47:45 -0000 1.33 +++ httpclient/src/java/org/apache/commons/httpclient/Cookie.java 27 Jan 2003 01:56:47 -0000 @@ -86,6 +86,7 @@ * @author John Evans * @author Marc A. Saegesser * @author Oleg Kalnichevski + * @author Cookie header containing - * all cookies in cookies, - * associated with the given domain, port, - * path and https setting, and which are - * not expired according to the given date. + * Create a Cookie header containing all cookies in cookies, + * associated with the given domain, port, path and + * https setting, and which are not expired according to the given + * date. *

* If no cookies match, returns null. - * @exception java.lang.IllegalArgumentException if domain or path is null + * + * @param domain The domain + * @param port The port + * @param path The path + * @param secure True if this cookie has the secure flag set + * @param now The date to check for expiry + * @param cookies The cookies to use. + * @return The new header + * @exception IllegalArgumentException if domain or path is null * * @deprecated use {@link CookieSpec} interface */ - public static Header createCookieHeader(String domain, int port, String path, boolean secure, Date now, Cookie[] cookies) - throws IllegalArgumentException { - log.trace("enter Cookie.createCookieHeader(String, int, String, boolean, Date, Cookie[])"); + public static Header createCookieHeader( + String domain, int port, String path, boolean secure, + Date now, Cookie[] cookies) + throws IllegalArgumentException { + + LOG.trace("enter Cookie.createCookieHeader(String, int, String, boolean, Date, Cookie[])"); CookieSpec matcher = CookiePolicy.getDefaultSpec(); cookies = matcher.match(domain, port, path, secure, cookies); - if ((cookies != null) && (cookies.length > 0)) - { + if ((cookies != null) && (cookies.length > 0)) { return matcher.formatCookieHeader(cookies); } - else - { + else { return null; } } @@ -580,9 +589,12 @@ * createCookieHeader method.

*

The compare only compares the path of the cookie, see section 4.3.4 * of RFC2109

+ * @param o1 The first object to be compared + * @param o2 The second object to be compared + * @return See {@link java.util.Comparator#compare(Object,Object)} */ public int compare(Object o1, Object o2) { - log.trace("enter Cookie.compare(Object, Object)"); + LOG.trace("enter Cookie.compare(Object, Object)"); if (!(o1 instanceof Cookie)) { throw new ClassCastException(o1.getClass().getName()); @@ -590,8 +602,8 @@ if (!(o2 instanceof Cookie)) { throw new ClassCastException(o2.getClass().getName()); } - Cookie c1 = (Cookie)o1; - Cookie c2 = (Cookie)o2; + Cookie c1 = (Cookie) o1; + Cookie c2 = (Cookie) o2; if (c1.getPath() == null && c2.getPath() == null) { return 0; } else if (c1.getPath() == null) { @@ -609,7 +621,7 @@ return 1; } } else { - return stringCollator.compare(c1.getPath(), c2.getPath()); + return STRING_COLLATOR.compare(c1.getPath(), c2.getPath()); } } @@ -627,16 +639,21 @@ * on an insecure channel. * * @param domain the domain from which the {@link Header} was received - * @param port the port from which the {@link Header} was received (currently ignored) + * @param port the port from which the {@link Header} was received + * (currently ignored) * @param path the path from which the {@link Header} was received - * @param setCookie the Set-Cookie {@link Header} received from the server - * @return an array of Cookies parsed from the Set-Cookie {@link Header} + * @param setCookie the Set-Cookie {@link Header} received from the + * server + * @return an array of Cookies parsed from the Set-Cookie {@link + * Header} * @throws HttpException if an exception occurs during parsing - * @throws java.lang.IllegalArgumentException if domain or path are null + * @throws IllegalArgumentException if domain or path are null */ - public static Cookie[] parse(String domain, int port, String path, Header setCookie) - throws HttpException, IllegalArgumentException { - log.trace("enter Cookie.parse(String, int, String, Header)"); + public static Cookie[] parse( + String domain, int port, String path, Header setCookie) + throws HttpException, IllegalArgumentException { + + LOG.trace("enter Cookie.parse(String, int, String, Header)"); return Cookie.parse(domain,port,path,false,setCookie); } @@ -647,17 +664,19 @@ * * @param domain the domain from which the {@link Header} was received * @param path the path from which the {@link Header} was received - * @param setCookie the Set-Cookie {@link Header} received from the server - * @return an array of Cookies parsed from the Set-Cookie {@link Header} + * @param setCookie the Set-Cookie {@link Header} received from the + * server + * @return an array of Cookies parsed from the Set-Cookie {@link + * Header} * @throws HttpException if an exception occurs during parsing - * @throws java.lang.IllegalArgumentException if domain or path are null + * @throws IllegalArgumentException if domain or path are null * * @deprecated use {@link CookieSpec} interface */ public static Cookie[] parse(String domain, String path, Header setCookie) throws HttpException, IllegalArgumentException { - log.trace("enter Cookie.parse(String, String, Header)"); - return Cookie.parse(domain,80,path,false,setCookie); + LOG.trace("enter Cookie.parse(String, String, Header)"); + return Cookie.parse (domain, 80, path, false, setCookie); } /** @@ -666,18 +685,24 @@ * * @param domain the domain from which the {@link Header} was received * @param path the path from which the {@link Header} was received - * @param secure true when the header was recieved over a secure channel - * @param setCookie the Set-Cookie {@link Header} received from the server - * @return an array of Cookies parsed from the Set-Cookie {@link Header} + * @param secure true when the header was recieved over a secure + * channel + * @param setCookie the Set-Cookie {@link Header} received from the + * server + * @return an array of Cookies parsed from the Set-Cookie {@link + * Header} * @throws HttpException if an exception occurs during parsing - * @throws java.lang.IllegalArgumentException if domain or path are null + * @throws IllegalArgumentException if domain or path are null * * @deprecated use {@link CookieSpec} interface */ - public static Cookie[] parse(String domain, String path, boolean secure, Header setCookie) - throws HttpException { - log.trace("enter Cookie.parse(String, String, boolean, Header)"); - return Cookie.parse(domain,(secure ? 443 : 80),path,secure,setCookie); + public static Cookie[] parse(String domain, String path, + boolean secure, Header setCookie) + throws HttpException, IllegalArgumentException { + + LOG.trace ("enter Cookie.parse(String, String, boolean, Header)"); + return Cookie.parse ( + domain, (secure ? 443 : 80), path, secure, setCookie); } /** @@ -701,26 +726,31 @@ * * * @param domain the domain from which the {@link Header} was received + * @param port The port from which the {@link Header} was received. * @param path the path from which the {@link Header} was received - * @param secure true when the {@link Header} was received over HTTPS - * @param setCookie the Set-Cookie {@link Header} received from the server - * @return an array of Cookies parsed from the Set-Cookie {@link Header} + * @param secure true when the {@link Header} was received over + * HTTPS + * @param setCookie the Set-Cookie {@link Header} received from + * the server + * @return an array of Cookies parsed from the Set-Cookie {@link + * Header} * @throws HttpException if an exception occurs during parsing - * @throws java.lang.IllegalArgumentException if domain or path are null * * @deprecated use {@link CookieSpec} interface */ - public static Cookie[] parse(String domain, int port, String path, boolean secure, Header setCookie) - throws HttpException, IllegalArgumentException { - log.trace("enter Cookie.parse(String, int, String, boolean, Header)"); + public static Cookie[] parse(String domain, int port, String path, + boolean secure, Header setCookie) + throws HttpException { + + LOG.trace("enter Cookie.parse(String, int, String, boolean, Header)"); CookieSpec parser = CookiePolicy.getDefaultSpec(); Cookie[] cookies = parser.parse(domain, port, path, secure, setCookie); - for (int i = 0; i < cookies.length; i++) - { - Cookie cookie = cookies[i]; - CookieSpec validator = CookiePolicy.getSpecByVersion(cookie.getVersion()); + for (int i = 0; i < cookies.length; i++) { + final Cookie cookie = cookies[i]; + final CookieSpec validator + = CookiePolicy.getSpecByVersion(cookie.getVersion()); validator.validate(domain, port, path, secure, cookie); } return cookies; @@ -743,10 +773,16 @@ /** My secure flag. */ private boolean _secure; - /** Specifies if the set-cookie header included a Path attribute for this cookie */ + /** + * Specifies if the set-cookie header included a Path attribute for this + * cookie + */ private boolean _hasPathAttribute = false; - /** Specifies if the set-cookie header included a Domain attribute for this cookie */ + /** + * Specifies if the set-cookie header included a Domain attribute for this + * cookie + */ private boolean _hasDomainAttribute = false; /** The version of the cookie specification I was created from. */ @@ -754,13 +790,16 @@ // -------------------------------------------------------------- Constants - /** Collator for Cookie comparisons. Could be replaced with references to specific Locales. */ - private static final RuleBasedCollator stringCollator = - (RuleBasedCollator)RuleBasedCollator.getInstance( + /** + * Collator for Cookie comparisons. Could be replaced with references to + * specific Locales. + */ + private static final RuleBasedCollator STRING_COLLATOR = + (RuleBasedCollator) RuleBasedCollator.getInstance( new Locale("en", "US", "")); /** Log object for this class */ - private static final Log log = LogFactory.getLog(Cookie.class); + private static final Log LOG = LogFactory.getLog(Cookie.class); } Index: httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v retrieving revision 1.36 diff -u -r1.36 HttpConnection.java --- httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java 25 Jan 2003 04:46:39 -0000 1.36 +++ httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java 27 Jan 2003 01:56:47 -0000 @@ -824,10 +824,10 @@ break; } } else if (ch == '\r') { -// log.debug("HttpConnection.readLine() found \\r, continuing"); +// LOG.debug("HttpConnection.readLine() found \\r, continuing"); continue; } else if (ch == '\n') { -// log.debug("HttpConnection.readLine() found \\n, breaking"); +// LOG.debug("HttpConnection.readLine() found \\n, breaking"); break; } buf.append((char)ch); Index: httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.99 diff -u -r1.99 HttpMethodBase.java --- httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java 25 Jan 2003 22:52:16 -0000 1.99 +++ httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java 27 Jan 2003 01:56:46 -0000 @@ -928,7 +928,7 @@ //check to see if we have visited this url before if (visited.contains(generateVisitedKey(conn))) { - log.error("Link " + generateVisitedKey(conn) + "' revisited"); + LOG.error("Link " + generateVisitedKey(conn) + "' revisited"); return statusCode; } visited.add(generateVisitedKey(conn)); @@ -1015,7 +1015,7 @@ try{ checkValidRedirect(currentUrl, redirectUrl); } catch (HttpException ex) { - //log the error and let the client handle the redirect + //LOG the error and let the client handle the redirect log.warn(ex.getMessage()); return false; } Index: httpclient/src/java/org/apache/commons/httpclient/WireLogInputStream.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/WireLogInputStream.java,v retrieving revision 1.5 diff -u -r1.5 WireLogInputStream.java --- httpclient/src/java/org/apache/commons/httpclient/WireLogInputStream.java 23 Jan 2003 22:47:49 -0000 1.5 +++ httpclient/src/java/org/apache/commons/httpclient/WireLogInputStream.java 27 Jan 2003 01:56:45 -0000 @@ -66,7 +66,7 @@ import org.apache.commons.logging.*; /** - * Logs all data read to the wire log. + * Logs all data read to the wire LOG. * * @author Ortwin Glück * Index: httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java,v retrieving revision 1.4 diff -u -r1.4 CookiePolicy.java --- httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java 26 Jan 2003 06:59:20 -0000 1.4 +++ httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java 27 Jan 2003 01:56:47 -0000 @@ -82,6 +82,7 @@ * COMPATIBILITY, NETSCAPE_DRAFT, RFC2109. * * @author
Oleg Kalnichevski + * @author John Evans * @author Marc A. Saegesser * @author Oleg Kalnichevski + * @author 0) - { + if (lastSlashIndex > 0) { defaultPath = defaultPath.substring(0, lastSlashIndex); } @@ -218,8 +213,7 @@ // could be null. In case only a header element and no parameters. if (parameters != null) { - for (int j = 0; j < parameters.length; j++) - { + for (int j = 0; j < parameters.length; j++) { parseAttribute(parameters[j], cookie); } } @@ -230,7 +224,8 @@ /** - * Parse the "Set-Cookie" {@link Header} into an array of {@link Cookie}s. + * Parse the "Set-Cookie" {@link Header} into an array of {@link + * Cookie}s. * *

The syntax for the Set-Cookie response header is: * @@ -248,22 +243,26 @@ * | "Version" "=" 1*DIGIT * * - * @param host the host from which the Set-Cookie header was received - * @param port the port from which the Set-Cookie header was received - * @param path the path from which the Set-Cookie header was received - * @param secure true when the Set-Cookie header was received over secure conection + * @param host the host from which the Set-Cookie header was + * received + * @param port the port from which the Set-Cookie header was + * received + * @param path the path from which the Set-Cookie header was + * received + * @param secure true when the Set-Cookie header was + * received over secure conection * @param header the Set-Cookie received from the server - * @return an array of Cookies parsed from the "Set-Cookie" header + * @return an array of Cookies parsed from the "Set-Cookie" + * header * @throws MalformedCookieException if an exception occurs during parsing - * @throws java.lang.IllegalArgumentException if an input parameter is illegal */ - - public Cookie[] parse(String host, int port, String path, boolean secure, final Header header) - throws MalformedCookieException - { - log.trace("enter CookieSpecBase.parse(String, port, path, boolean, String)"); - if(header == null) - { + public Cookie[] parse( + String host, int port, String path, boolean secure, final Header header) + throws MalformedCookieException { + + LOG.trace("enter CookieSpecBase.parse(" + + "String, port, path, boolean, String)"); + if (header == null) { throw new IllegalArgumentException("Header may not be null."); } return parse(host, port, path, secure, header.getValue()); @@ -271,95 +270,98 @@ /** - * Parse the cookie attribute and update the corresponsing {@link Cookie} properties. + * Parse the cookie attribute and update the corresponsing {@link Cookie} + * properties. * - * @param attribute {@link HeaderElement} cookie attribute from the Set-Cookie + * @param attribute {@link HeaderElement} cookie attribute from the + * Set- Cookie * @param cookie {@link Cookie} to be updated * @throws MalformedCookieException if an exception occurs during parsing - * @throws java.lang.IllegalArgumentException if an input parameter is illegal */ - public void parseAttribute(final NameValuePair attribute, final Cookie cookie) - throws MalformedCookieException - { - if(attribute == null) - { + public void parseAttribute( + final NameValuePair attribute, final Cookie cookie) + throws MalformedCookieException { + + if (attribute == null) { throw new IllegalArgumentException("Attribute may not be null."); } - if(cookie == null) - { + if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null."); } - String param_name = attribute.getName().toLowerCase(); - String param_value = attribute.getValue(); + final String paramName = attribute.getName().toLowerCase(); + String paramValue = attribute.getValue(); - if (param_name.equals("path")) { + if (paramName.equals("path")) { - if (param_value == null) { - throw new MalformedCookieException("Missing value for path attribute"); + if (paramValue == null) { + throw new MalformedCookieException( + "Missing value for path attribute"); } - if (param_value.trim().equals("")) { - throw new MalformedCookieException("Blank value for path attribute"); + if (paramValue.trim().equals("")) { + throw new MalformedCookieException( + "Blank value for path attribute"); } - cookie.setPath(param_value); + cookie.setPath(paramValue); cookie.setPathAttributeSpecified(true); - } - else if (param_name.equals("domain")) { + } else if (paramName.equals("domain")) { - if (param_value == null) { - throw new MalformedCookieException("Missing value for domain attribute"); + if (paramValue == null) { + throw new MalformedCookieException( + "Missing value for domain attribute"); } - if (param_value.trim().equals("")) { - throw new MalformedCookieException("Blank value for domain attribute"); + if (paramValue.trim().equals("")) { + throw new MalformedCookieException( + "Blank value for domain attribute"); } - cookie.setDomain(param_value); + cookie.setDomain(paramValue); cookie.setDomainAttributeSpecified(true); - } - else if (param_name.equals("max-age")) { + } else if (paramName.equals("max-age")) { - if (param_value == null) { - throw new MalformedCookieException("Missing value for max-age attribute"); + if (paramValue == null) { + throw new MalformedCookieException( + "Missing value for max-age attribute"); } int age; try { - age = Integer.parseInt(param_value); + age = Integer.parseInt(paramValue); } catch (NumberFormatException e) { - throw new MalformedCookieException( "Invalid max-age attribute: " + e.getMessage()); + throw new MalformedCookieException ("Invalid max-age " + + "attribute: " + e.getMessage()); } - cookie.setExpiryDate(new Date(System.currentTimeMillis() + - age * 1000L)); + cookie.setExpiryDate( + new Date(System.currentTimeMillis() + age * 1000L)); - } - else if (param_name.equals("secure")) { + } else if (paramName.equals("secure")) { cookie.setSecure(true); - } - else if (param_name.equals("comment")) { + } else if (paramName.equals("comment")) { - cookie.setComment(param_value); + cookie.setComment(paramValue); - } - else if (param_name.equals("expires")) { + } else if (paramName.equals("expires")) { - if (param_value == null) { - throw new MalformedCookieException("Missing value for expires attribute"); + if (paramValue == null) { + throw new MalformedCookieException( + "Missing value for expires attribute"); } boolean set = false; // trim single quotes around expiry if present // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279 - if(param_value.length() > 1 && - param_value.startsWith("'") && - param_value.endsWith("'")) { - param_value = param_value.substring(1,param_value.length()-1); + if (paramValue.length() > 1 + && paramValue.startsWith("'") + && paramValue.endsWith("'")) { + paramValue + = paramValue.substring (1, paramValue.length() - 1); } - for(int k=0;ktrue when the {@link Cookie} was received using a secure connection - * @throws MalformedCookieException if an exception occurs during validation - * @throws java.lang.IllegalArgumentException if an input parameter is illegal + * @param secure true when the {@link Cookie} was received using a + * secure connection + * @param cookie The cookie to validate. + * @throws MalformedCookieException if an exception occurs during + * validation */ - public void validate(String host, int port, String path, boolean secure, final Cookie cookie) throws MalformedCookieException - { - log.trace("enter CookieSpecBase.validate(String, port, path, boolean, Cookie)"); - if(host == null) - { - throw new IllegalArgumentException("Host of origin may not be null"); - } - if(host.trim().equals("")) - { - throw new IllegalArgumentException("Host of origin may not be blank"); + public void validate(String host, int port, String path, + boolean secure, final Cookie cookie) + throws MalformedCookieException { + + LOG.trace("enter CookieSpecBase.validate(" + + "String, port, path, boolean, Cookie)"); + if (host == null) { + throw new IllegalArgumentException( + "Host of origin may not be null"); + } + if (host.trim().equals("")) { + throw new IllegalArgumentException( + "Host of origin may not be blank"); } - if(port < 0) - { + if (port < 0) { throw new IllegalArgumentException("Invalid port: " + port); } - if(path == null) - { - throw new IllegalArgumentException("Path of origin may not be null."); + if (path == null) { + throw new IllegalArgumentException( + "Path of origin may not be null."); } if (path.trim().equals("")) { path = PATH_DELIM; } host = host.toLowerCase(); // check version - if (cookie.getVersion() < 0) - { - throw new MalformedCookieException( "Illegal version number " + cookie.getValue()); + if (cookie.getVersion() < 0) { + throw new MalformedCookieException ("Illegal version number " + + cookie.getValue()); } // security check... we musn't allow the server to give us an // invalid domain scope - // Validate the cookies domain attribute. NOTE: Domains without any dots are - // allowed to support hosts on private LANs that don't have DNS names. Since - // they have no dots, to domain-match the request-host and domain must be identical - // for the cookie to sent back to the origin-server. - if (host.indexOf(".") >= 0) - { + // Validate the cookies domain attribute. NOTE: Domains without + // any dots are allowed to support hosts on private LANs that don't + // have DNS names. Since they have no dots, to domain-match the + // request-host and domain must be identical for the cookie to sent + // back to the origin-server. + if (host.indexOf(".") >= 0) { // Not required to have at least two dots. RFC 2965. // A Set-Cookie2 with Domain=ajax.com will be accepted. // domain must match host - if (!host.endsWith(cookie.getDomain())) - { + if (!host.endsWith(cookie.getDomain())) { throw new MalformedCookieException( - "Illegal domain attribute \"" + cookie.getDomain() + "\". Domain of origin: \"" + host + "\""); + "Illegal domain attribute \"" + cookie.getDomain() + + "\". Domain of origin: \"" + host + "\""); } } // another security check... we musn't allow the server to give us a // cookie that doesn't match this path - if(!path.startsWith(cookie.getPath())) - { + if (!path.startsWith(cookie.getPath())) { throw new MalformedCookieException( - "Illegal path attribute \"" + cookie.getPath() + "\". Path of origin: \"" + path + "\""); + "Illegal path attribute \"" + cookie.getPath() + + "\". Path of origin: \"" + path + "\""); } } /** - * Return true if the cookie should be submitted with a request with - * given attributes, false otherwise. + * Return true if the cookie should be submitted with a request + * with given attributes, false otherwise. * @param host the host to which the request is being submitted * @param port the port to which the request is being submitted (ignored) * @param path the path to which the request is being submitted * @param secure true if the request is using a secure connection - * @param {@link Cookie} to be matched + * @param cookie {@link Cookie} to be matched * @return true if the cookie matches the criterium */ - public boolean match(String host, int port, String path, boolean secure, final Cookie cookie) - { - log.trace("enter CookieSpecBase.match(String, int, String, boolean, Cookie"); - if(host == null) - { - throw new IllegalArgumentException("Host of origin may not be null"); - } - if(host.trim().equals("")) - { - throw new IllegalArgumentException("Host of origin may not be blank"); + public boolean match(String host, int port, String path, + boolean secure, final Cookie cookie) { + + LOG.trace("enter CookieSpecBase.match(" + + "String, int, String, boolean, Cookie"); + + if (host == null) { + throw new IllegalArgumentException( + "Host of origin may not be null"); + } + if (host.trim().equals("")) { + throw new IllegalArgumentException( + "Host of origin may not be blank"); } - if(port < 0) - { + if (port < 0) { throw new IllegalArgumentException("Invalid port: " + port); } - if(path == null) - { - throw new IllegalArgumentException("Path of origin may not be null."); + if (path == null) { + throw new IllegalArgumentException( + "Path of origin may not be null."); } - if(cookie == null) - { + if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null"); } if (path.trim().equals("")) { @@ -490,44 +498,55 @@ } host = host.toLowerCase(); if (cookie.getDomain() == null) { - log.warn("Invalid cookie state: domain not specified"); + LOG.warn("Invalid cookie state: domain not specified"); return false; } if (cookie.getPath() == null) { - log.warn("Invalid cookie state: path not specified"); + LOG.warn("Invalid cookie state: path not specified"); return false; } - return ( - (cookie.getExpiryDate() == null || - cookie.getExpiryDate().after(new Date())) && // only add the cookie if it hasn't yet expired - (domainMatch(host, cookie.getDomain())) && // and the domain pattern matches - (pathMatch(path, cookie.getPath())) && // and the path is null or matching - (cookie.getSecure() ? secure : true) // and if the secure flag is set, only if the request is actually secure - ); + return + // only add the cookie if it hasn't yet expired + (cookie.getExpiryDate() == null + || cookie.getExpiryDate().after(new Date())) + // and the domain pattern matches + && (domainMatch(host, cookie.getDomain())) + // and the path is null or matching + && (pathMatch(path, cookie.getPath())) + // and if the secure flag is set, only if the request is + // actually secure + && (cookie.getSecure() ? secure : true); } /** * Performs a domain-match as described in RFC2109. + * @param host The host to check. + * @param domain The domain. + * @return true if the specified host matches the given domain. */ - private static boolean domainMatch(String host, String domain) - { - boolean match = host.equals(domain) || - (domain.startsWith(".") && host.endsWith(domain)); + private static boolean domainMatch(String host, String domain) { + boolean match = host.equals(domain) + || (domain.startsWith(".") && host.endsWith(domain)); return match; } /** - * Performs a path-match slightly smarter than a straight-forward startsWith check. + * Performs a path-match slightly smarter than a straight-forward startsWith + * check. + * @param path The path to check. + * @param topmostPath The path to check against. + * @return true if the paths match */ - private static boolean pathMatch(final String path, final String topmostPath) - { - boolean match = path.startsWith( topmostPath ); + private static boolean pathMatch( + final String path, final String topmostPath) { + + boolean match = path.startsWith (topmostPath); // if there is a match and these values are not exactly the same we have // to make sure we're not matcing "/foobar" and "/foo" - if ( match && path.length() != topmostPath.length() ) { + if (match && path.length() != topmostPath.length()) { if (!topmostPath.endsWith(PATH_DELIM)) { match = (path.charAt(topmostPath.length()) == PATH_DELIM_CHAR); } @@ -536,38 +555,39 @@ } /** - * Return an array of {@link Cookie}s that should be submitted with a request with - * given attributes, false otherwise. + * Return an array of {@link Cookie}s that should be submitted with a + * request with given attributes, false otherwise. * @param host the host to which the request is being submitted - * @param port the port to which the request is being submitted (currenlty ignored) + * @param port the port to which the request is being submitted (currently + * ignored) * @param path the path to which the request is being submitted * @param secure true if the request is using a secure protocol - * @param an array of Cookies to be matched + * @param cookies an array of Cookies to be matched * @return an array of Cookies matching the criterium */ - public Cookie[] match(String host, int port, String path, boolean secure, final Cookie cookies[]) - { - log.trace("enter CookieSpecBase.match(String, int, String, boolean, Cookie[])"); - - if(host == null) - { - throw new IllegalArgumentException("Host of origin may not be null"); - } - if(host.trim().equals("")) - { - throw new IllegalArgumentException("Host of origin may not be blank"); + public Cookie[] match(String host, int port, String path, + boolean secure, final Cookie cookies[]) { + + LOG.trace("enter CookieSpecBase.match(" + + "String, int, String, boolean, Cookie[])"); + + if (host == null) { + throw new IllegalArgumentException( + "Host of origin may not be null"); + } + if (host.trim().equals("")) { + throw new IllegalArgumentException( + "Host of origin may not be blank"); } - if(port < 0) - { + if (port < 0) { throw new IllegalArgumentException("Invalid port: " + port); } - if(path == null) - { - throw new IllegalArgumentException("Path of origin may not be null."); + if (path == null) { + throw new IllegalArgumentException( + "Path of origin may not be null."); } - if(cookies == null) - { + if (cookies == null) { throw new IllegalArgumentException("Cookie array may not be null"); } if (path.trim().equals("")) { @@ -581,33 +601,30 @@ return null; } List matching = new LinkedList(); - for(int i=0;i 0){ + for (i = 0; i < list.size(); i++) { + Cookie c = (Cookie) list.get(i); + if (addCookie.compare(addCookie, c) > 0) { break; } } @@ -616,15 +633,12 @@ /** * Return a string suitable for sending in a "Cookie" header - * @param a {@link Cookie} to be formatted as string + * @param cookie a {@link Cookie} to be formatted as string * @return a string suitable for sending in a "Cookie" header. */ - - public String formatCookie(Cookie cookie) - { - log.trace("enter CookieSpecBase.formatCookie(Cookie)"); - if(cookie == null) - { + public String formatCookie(Cookie cookie) { + LOG.trace("enter CookieSpecBase.formatCookie(Cookie)"); + if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null"); } StringBuffer buf = new StringBuffer(); @@ -633,31 +647,26 @@ } /** - * Create a "Cookie" header value containing all {@link Cookie}s in cookies - * suitable for sending in a "Cookie" header - * @param an array of {@link Cookie}s to be formatted + * Create a "Cookie" header value containing all {@link Cookie}s in + * cookies suitable for sending in a "Cookie" header + * @param cookies an array of {@link Cookie}s to be formatted * @return a string suitable for sending in a Cookie header. - * @throws java.lang.IllegalArgumentException if an input parameter is illegal + * @throws IllegalArgumentException if an input parameter is illegal */ public String formatCookies(Cookie[] cookies) - throws IllegalArgumentException - { - log.trace("enter CookieSpecBase.formatCookies(Cookie[])"); - if(cookies == null) - { + throws IllegalArgumentException { + LOG.trace("enter CookieSpecBase.formatCookies(Cookie[])"); + if (cookies == null) { throw new IllegalArgumentException("Cookie array may not be null"); } - if(cookies.length == 0) - { + if (cookies.length == 0) { throw new IllegalArgumentException("Cookie array may not be empty"); } StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < cookies.length; i++) - { - if (i > 0) - { + for (int i = 0; i < cookies.length; i++) { + if (i > 0) { buffer.append("; "); } buffer.append(formatCookie(cookies[i])); @@ -667,29 +676,26 @@ /** - * Create a "Cookie" {@link Header} containing all {@link Cookie}s in cookies. - * @param an array of {@link Cookie}s to be formatted as a "Cookie" header + * Create a "Cookie" {@link Header} containing all {@link Cookie}s + * in cookies. + * @param cookies an array of {@link Cookie}s to be formatted as a " + * Cookie" header * @return a "Cookie" {@link Header}. - * @throws java.lang.IllegalArgumentException if an input parameter is illegal */ - - public Header formatCookieHeader(Cookie[] cookies) - { - log.trace("enter CookieSpecBase.formatCookieHeader(Cookie[])"); + public Header formatCookieHeader(Cookie[] cookies) { + LOG.trace("enter CookieSpecBase.formatCookieHeader(Cookie[])"); return new Header("Cookie", formatCookies(cookies)); } /** * Create a "Cookie" {@link Header} containing the {@link Cookie}. - * @param Cookies to be formatted as a Cookie header + * @param cookie Cookies to be formatted as a Cookie + * header * @return a Cookie header. - * @throws java.lang.IllegalArgumentException if an input parameter is illegal */ - - public Header formatCookieHeader(Cookie cookie) - { - log.trace("enter CookieSpecBase.formatCookieHeader(Cookie)"); + public Header formatCookieHeader(Cookie cookie) { + LOG.trace("enter CookieSpecBase.formatCookieHeader(Cookie)"); return new Header("Cookie", formatCookie(cookie)); } Index: httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java,v retrieving revision 1.5 diff -u -r1.5 NetscapeDraftSpec.java --- httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java 23 Jan 2003 22:48:06 -0000 1.5 +++ httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java 27 Jan 2003 01:56:48 -0000 @@ -83,61 +83,57 @@ * @author John Evans * @author Marc A. Saegesser * @author Oleg Kalnichevski + * @author true when the {@link Cookie} was received using a secure connection - * @throws MalformedCookieException if an exception occurs during validation - * @throws java.lang.IllegalArgumentException if an input parameter is illegal + * @param secure true when the {@link Cookie} was received + * using a secure connection + * @param cookie The cookie to validate. + * @throws MalformedCookieException if an exception occurs during + * validation */ - - public void validate(String host, int port, String path, boolean secure, final Cookie cookie) throws MalformedCookieException - { - log.trace("enterNetscapeDraftCookieProcessor RCF2109CookieProcessor.validate(Cookie)"); + public void validate(String host, int port, String path, + boolean secure, final Cookie cookie) + throws MalformedCookieException { + + LOG.trace("enterNetscapeDraftCookieProcessor " + + "RCF2109CookieProcessor.validate(Cookie)"); // Perform generic validation super.validate(host, port, path, secure, cookie); // Perform Netscape Cookie draft specific validation - if (host.indexOf(".") >= 0) - { - int domainParts = new StringTokenizer(cookie.getDomain(), ".").countTokens(); - - if (isSpecialDomain(cookie.getDomain())) - { - if(domainParts < 2) - { - throw new MalformedCookieException("Domain attribute \""+ cookie.getDomain() + "\" violates the Netscape cookie specification for special domains"); + if (host.indexOf(".") >= 0) { + int domainParts = new StringTokenizer(cookie.getDomain(), ".") + .countTokens(); + + if (isSpecialDomain(cookie.getDomain())) { + if (domainParts < 2) { + throw new MalformedCookieException("Domain attribute \"" + + cookie.getDomain() + + "\" violates the Netscape cookie specification for " + + "special domains"); } + } else { + if (domainParts < 3) { + throw new MalformedCookieException("Domain attribute \"" + + cookie.getDomain() + + "\" violates the Netscape cookie specification"); + } } - else - { - if(domainParts < 3) - { - throw new MalformedCookieException("Domain attribute \""+ cookie.getDomain() + "\" violates the Netscape cookie specification"); - } - } } } /** * Checks if the given domain is in one of the seven special * top level domains defined by the Netscape cookie specification. + * @param domain The domain. + * @return True if the specified domain is "special" */ - private static boolean isSpecialDomain(String domain) - { - String ucDomain = domain.toUpperCase(); - if(ucDomain.endsWith(".COM") || - ucDomain.endsWith(".EDU") || - ucDomain.endsWith(".NET") || - ucDomain.endsWith(".GOV") || - ucDomain.endsWith(".MIL") || - ucDomain.endsWith(".ORG") || - ucDomain.endsWith(".INT")){ + private static boolean isSpecialDomain(final String domain) { + final String ucDomain = domain.toUpperCase(); + if (ucDomain.endsWith(".COM") + || ucDomain.endsWith(".EDU") + || ucDomain.endsWith(".NET") + || ucDomain.endsWith(".GOV") + || ucDomain.endsWith(".MIL") + || ucDomain.endsWith(".ORG") + || ucDomain.endsWith(".INT")) { return true; } return false; } - } Index: httpclient/src/java/org/apache/commons/httpclient/cookie/RFC2109Spec.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/RFC2109Spec.java,v retrieving revision 1.5 diff -u -r1.5 RFC2109Spec.java --- httpclient/src/java/org/apache/commons/httpclient/cookie/RFC2109Spec.java 23 Jan 2003 22:48:06 -0000 1.5 +++ httpclient/src/java/org/apache/commons/httpclient/cookie/RFC2109Spec.java 27 Jan 2003 01:56:48 -0000 @@ -78,58 +78,55 @@ * @author John Evans * @author Marc A. Saegesser * @author Oleg Kalnichevski + * @author true when the {@link Cookie} was received using a secure connection - * @throws MalformedCookieException if an exception occurs during validation - * @throws java.lang.IllegalArgumentException if an input parameter is illegal + * @param secure true when the {@link Cookie} was received using a + * secure connection + * @param cookie The cookie to validate + * @throws MalformedCookieException if an exception occurs during + * validation */ - - public void validate(String host, int port, String path, boolean secure, final Cookie cookie) throws MalformedCookieException - { - log.trace("enter RFC2109Spec.validate(String, int, String, boolean, Cookie)"); + public void validate(String host, int port, String path, + boolean secure, final Cookie cookie) throws MalformedCookieException { + + LOG.trace("enter RFC2109Spec.validate(String, int, String, " + + "boolean, Cookie)"); + // Perform generic validation super.validate(host, port, path, secure, cookie); // Perform RFC 2109 specific validation - if (cookie.isDomainAttributeSpecified() && (!cookie.getDomain().equals(host))) - { + if (cookie.isDomainAttributeSpecified() + && (!cookie.getDomain().equals(host))) { + // domain must start with dot - if (!cookie.getDomain().startsWith(".")) - { - throw new MalformedCookieException("Domain attribute \"" + cookie.getDomain() + "\" violates RFC 2109: domain must start with a dot"); + if (!cookie.getDomain().startsWith(".")) { + throw new MalformedCookieException("Domain attribute \"" + + cookie.getDomain() + + "\" violates RFC 2109: domain must start with a dot"); } // domain must have at least one embedded dot int dotIndex = cookie.getDomain().indexOf('.', 1); - if(dotIndex < 0 || dotIndex == cookie.getDomain().length()-1) - { - throw new MalformedCookieException("Domain attribute \"" + cookie.getDomain() + "\" violates RFC 2109: domain must contain an embedded dot"); + if(dotIndex < 0 || dotIndex == cookie.getDomain().length() - 1) { + throw new MalformedCookieException("Domain attribute \"" + + cookie.getDomain() + + "\" violates RFC 2109: domain must contain an " + + "embedded dot"); } // host minus domain may not contain any dots if (host.substring(0, - host.length() - - cookie.getDomain().length()).indexOf('.') != -1) - { - throw new MalformedCookieException("Domain attribute \"" + cookie.getDomain() + "\" violates RFC 2109: host minus domain may not contain any dots"); + host.length() + - cookie.getDomain().length()).indexOf('.') != -1) { + throw new MalformedCookieException("Domain attribute \"" + + cookie.getDomain() + + "\" violates RFC 2109: host minus domain may not " + + "contain any dots"); } } } /** - * Return a name/value string suitable for sending in a "Cookie" header - * as defined in RFC 2109 for backward compatibility with cookie version 0 - * @param a {@link Cookie} to be formatted as string + * Return a name/value string suitable for sending in a "Cookie" + * header as defined in RFC 2109 for backward compatibility with cookie + * version 0 + * @param name The name. + * @param value The value + * @param version The cookie version * @return a string suitable for sending in a "Cookie" header. */ - private String formatNameValuePair(final String name, final String value, int version) - { - StringBuffer buffer = new StringBuffer(); - if (version < 1) - { + private String formatNameValuePair( + final String name, final String value, int version) { + + final StringBuffer buffer = new StringBuffer(); + if (version < 1) { buffer.append(name).append("=").append(value); - } - else - { + } else { buffer.append(name).append("=\"").append(value).append("\""); } return buffer.toString(); - } /** * Return a string suitable for sending in a "Cookie" header * as defined in RFC 2109 for backward compatibility with cookie version 0 - * @param a {@link Cookie} to be formatted as string + * @param cookie a {@link Cookie} to be formatted as string + * @param version The version to use. * @return a string suitable for sending in a "Cookie" header. */ - - private String formatCookieAsVer(Cookie cookie, int version) - { - log.trace("enter RFC2109Spec.formatCookieAsVer(Cookie)"); - if(cookie == null) - { + private String formatCookieAsVer(Cookie cookie, int version) { + LOG.trace("enter RFC2109Spec.formatCookieAsVer(Cookie)"); + if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null"); } StringBuffer buf = new StringBuffer(); - buf.append(formatNameValuePair(cookie.getName(), cookie.getValue(), version)); - if (cookie.getDomain() != null && cookie.isDomainAttributeSpecified()) { + buf.append(formatNameValuePair(cookie.getName(), + cookie.getValue(), version)); + if (cookie.getDomain() != null + && cookie.isDomainAttributeSpecified()) { + buf.append("; "); - buf.append(formatNameValuePair("$Domain", cookie.getDomain(), version)); + buf.append(formatNameValuePair("$Domain", + cookie.getDomain(), version)); } if (cookie.getPath() != null && cookie.isPathAttributeSpecified()) { buf.append("; "); @@ -226,46 +235,40 @@ /** - * Return a string suitable for sending in a "Cookie" header as defined in RFC 2109 - * @param a {@link Cookie} to be formatted as string + * Return a string suitable for sending in a "Cookie" header as + * defined in RFC 2109 + * @param cookie a {@link Cookie} to be formatted as string * @return a string suitable for sending in a "Cookie" header. */ - - public String formatCookie(Cookie cookie) - { - log.trace("enter RFC2109Spec.formatCookie(Cookie)"); - if(cookie == null) - { + public String formatCookie(Cookie cookie) { + LOG.trace("enter RFC2109Spec.formatCookie(Cookie)"); + if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null"); } return formatCookieAsVer(cookie, cookie.getVersion()); } /** - * Create a RFC 2109 compliant "Cookie" header value containing all - * {@link Cookie}s in cookies suitable for sending in a "Cookie" header - * @param an array of {@link Cookie}s to be formatted + * Create a RFC 2109 compliant "Cookie" header value containing all + * {@link Cookie}s in cookies suitable for sending in a "Cookie" + * header + * @param cookies an array of {@link Cookie}s to be formatted * @return a string suitable for sending in a Cookie header. - * @throws java.lang.IllegalArgumentException if an input parameter is illegal */ - - public String formatCookies(Cookie[] cookies) - { - log.trace("enter RFC2109Spec.formatCookieHeader(Cookie[])"); + public String formatCookies(Cookie[] cookies) { + LOG.trace("enter RFC2109Spec.formatCookieHeader(Cookie[])"); int version = Integer.MAX_VALUE; // Pick the lowerest common denominator - for (int i = 0; i < cookies.length; i++) - { + for (int i = 0; i < cookies.length; i++) { Cookie cookie = cookies[i]; - if (cookie.getVersion() < version) - { + if (cookie.getVersion() < version) { version = cookie.getVersion(); } } - StringBuffer buffer = new StringBuffer(); - buffer.append(formatNameValuePair("$Version", Integer.toString(version), version)); - for (int i = 0; i < cookies.length; i++) - { + final StringBuffer buffer = new StringBuffer(); + buffer.append(formatNameValuePair("$Version", + Integer.toString(version), version)); + for (int i = 0; i < cookies.length; i++) { buffer.append("; "); buffer.append(formatCookieAsVer(cookies[i], version)); } @@ -274,17 +277,17 @@ /** - * Create a RFC 2109 compliant "Cookie" {@link Header} containing the {@link Cookie}. - * @param Cookies to be formatted as a Cookie header + * Create a RFC 2109 compliant "Cookie" {@link Header} containing + * the {@link Cookie}. + * @param cookie Cookies to be formatted as a Cookie + * header * @return a Cookie header. - * @throws java.lang.IllegalArgumentException if an input parameter is illegal */ - - public Header formatCookieHeader(Cookie cookie) - { - log.trace("enter RFC2109Spec.formatCookieHeader(Cookie)"); + public Header formatCookieHeader(Cookie cookie) { + LOG.trace("enter RFC2109Spec.formatCookieHeader(Cookie)"); StringBuffer buffer = new StringBuffer(); - buffer.append(formatNameValuePair("$Version", Integer.toString(cookie.getVersion()), cookie.getVersion())); + buffer.append(formatNameValuePair("$Version", + Integer.toString(cookie.getVersion()), cookie.getVersion())); buffer.append("; "); buffer.append(formatCookie(cookie)); return new Header("Cookie", buffer.toString()); Index: lang/src/java/org/apache/commons/lang/reflect/MethodUtils.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/lang/src/java/org/apache/commons/lang/reflect/MethodUtils.java,v retrieving revision 1.11 diff -u -r1.11 MethodUtils.java --- lang/src/java/org/apache/commons/lang/reflect/MethodUtils.java 25 Jan 2003 13:01:38 -0000 1.11 +++ lang/src/java/org/apache/commons/lang/reflect/MethodUtils.java 27 Jan 2003 01:56:39 -0000 @@ -247,7 +247,7 @@ } } catch (SecurityException se) { - // log but continue just in case the method.invoke works anyway + // LOG but continue just in case the method.invoke works anyway log( "Cannot setAccessible on method. Therefore cannot use jvm access bug workaround.", se); @@ -522,7 +522,7 @@ log("Checking: " + methods[i]); } if (methods[i].getName().equals(methodName)) { - // log some trace information + // LOG some trace information if (debug) { log("Found matching name:" + methods[i]); }