rwaldhoff 01/08/09 17:06:19 Modified: httpclient/src/java/org/apache/commons/httpclient Tag: rlwrefactoring HttpClient.java Added: httpclient/src/java/org/apache/commons/httpclient Tag: rlwrefactoring HttpConnection.java Log: Moving most of the Socket/InputStream/OutputStream operations to HttpConnection. (Note that the interceptor stuff is probably broken right now) Revision Changes Path No revision No revision 1.31.2.1 +69 -150 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java Index: HttpClient.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v retrieving revision 1.31 retrieving revision 1.31.2.1 diff -u -r1.31 -r1.31.2.1 --- HttpClient.java 2001/08/08 21:23:40 1.31 +++ HttpClient.java 2001/08/10 00:06:19 1.31.2.1 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v 1.31 2001/08/08 21:23:40 rwaldhoff Exp $ - * $Revision: 1.31 $ - * $Date: 2001/08/08 21:23:40 $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v 1.31.2.1 2001/08/10 00:06:19 rwaldhoff Exp $ + * $Revision: 1.31.2.1 $ + * $Date: 2001/08/10 00:06:19 $ * * ==================================================================== * @@ -150,11 +150,7 @@ // ----------------------------------------------------- Instance Variables - /** - * Client Socket in use. - */ - protected Socket socket; - + protected HttpConnection connection = null; /** * Session state. @@ -169,53 +165,11 @@ /** - * Socket input stream. - */ - protected InputStream input; - - - /** - * Socket output stream. - */ - protected OutputStream output; - - - /** - * The host name specified when the startSession(host, port) method was - * called. - */ - protected String sessionHost = ""; - - - /** - * Proxy port number. - */ - protected int proxyPort = -1; - - - /** - * Proxy hostname. - */ - protected String proxyHost = null; - - - /** - * Port number. - */ - protected int sessionPort = -1; - - - /** * HTTP/1.1 flag. */ protected boolean http11 = false; /** - * HTTPS flag. - */ - protected boolean https = false; - - /** * Stream interceptors. */ protected StreamInterceptor streamInterceptor = null; @@ -241,7 +195,8 @@ * Set the socket to use. */ public void setSocket(Socket socket) { - this.socket = socket; + //this.socket = socket; + throw new UnsupportedOperationException("Is this ever used?"); } @@ -300,24 +255,7 @@ return debug; } - /** - * Get the session host. - */ - public String getHost() { - return sessionHost; - } - - - /** - * Get the session port. - */ - public int getPort() { - return sessionPort; - } - - - /** * Get the state for lock information. */ public State getState() { @@ -431,7 +369,9 @@ String wwwAuthenticateValue = null; - openConnection(); + if(!connection.isOpen()) { + connection.open(); + } while ((retries < 5) && (!methodProcessed)) { log.debug("HttpClient.executeMethod(): trying " + retries + "."); @@ -455,30 +395,19 @@ boolean closeOutput = needToCloseOutput(method); if (closeOutput) { - try { - // Socket.shutdownOutput is a JDK 1.3 - // method. We'll use reflection in case - // we're running in an older VM - Class[] paramsClasses = new Class[0]; - Method shutdownOutput = socket.getClass().getMethod - ("shutdownOutput", paramsClasses); - Object[] params = new Object[0]; - shutdownOutput.invoke(socket, params); - } catch (Exception e) { - // Ignore, and hope everything goes right - } + connection.shutdownOutput(); } // Parsing response // Parse status line - String statusLine = readLine(input); + String statusLine = connection.readLine(); if (statusLine == null) throw new IOException("Couldn't parse status line"); parseStatusLine(statusLine, method); // Parse headers - responseHeaders = parseHeaders(input); + responseHeaders = parseHeaders(); while (method.getStatusCode() < 200) { @@ -497,13 +426,13 @@ } } - statusLine = readLine(input); + statusLine = connection.readLine(); if (statusLine == null) throw new IOException("Couldn't parse status line"); parseStatusLine(statusLine, method); // Parse headers - responseHeaders = parseHeaders(input); + responseHeaders = parseHeaders(); } @@ -576,7 +505,7 @@ // Consume bytes returned (if any) method.processResponseHeaders(responseHeaders); ResponseInputStream responseInputStream = - new ResponseInputStream(input, method, + new ResponseInputStream(connection.getInputStream(), method, responseHeaders); // FIXME : Really set the interceptors here ? // The content is meant to be discarded @@ -586,8 +515,8 @@ if (closeOutput || needToCloseConnection(method, responseHeaders)) { // Disconnect and reconnect if needed - closeConnection(); - openConnection(); + connection.close(); + connection.open(); } } } @@ -599,11 +528,11 @@ log.debug("HttpClient.executeMethod(): IOException while executing method, will close and try again.",e); // If something goes wrong, disconnect, then reconnect try { - closeConnection(); + connection.close(); } catch (IOException ex) { // Silent catch } - openConnection(); + connection.open(); } catch (HttpException e) { log.warn("HttpClient.executeMethod(): caught HTTP Exception: " + e.getMessage() + ", Status Code: " + e.getStatusCode(),e); @@ -615,13 +544,13 @@ method.setStatusCode(e.getStatusCode()); // If something goes wrong, disconnect, then reconnect try { - closeConnection(); + connection.close(); } catch (IOException ex) { // Silent catch } if (e.getStatusCode() == HttpException.NO_CREDENTIALS_GIVEN) throw new HttpException(HttpStatus.SC_UNAUTHORIZED); - openConnection(); + connection.open(); } retries++; @@ -637,7 +566,7 @@ method.setUsed(); // Parse response ResponseInputStream responseInputStream = - new ResponseInputStream(input, method, responseHeaders); + new ResponseInputStream(connection.getInputStream(), method, responseHeaders); responseInputStream.setInterceptor(streamInterceptor); method.parseResponse(responseInputStream); @@ -645,7 +574,7 @@ responseInputStream.close(); if (needToCloseConnection(method, responseHeaders)) { - closeConnection(); + connection.close(); } } @@ -659,13 +588,12 @@ * to make usage more clear. */ public void startSession() { - - if (state == null) + if(null == state) { state = new State(); - this.sessionHost = "localhost"; - this.sessionPort = 80; - this.https = false; - + } + if (null == connection) { + connection = new HttpConnection("localhost",80,false); + } } @@ -683,13 +611,12 @@ public void startSession(String host, int port, boolean https) { log.debug("HttpClient.startSession(String,int,boolean): Host:" + host + " Port:" + port + " HTTPS:" + https); - - if (state == null) + if(null == state) { state = new State(); - this.sessionHost = host; - this.sessionPort = port; - this.https = https; - + } + if (null == connection) { + connection = new HttpConnection(host,port,https); + } } @@ -710,12 +637,12 @@ host + " Port:" + port + " Credentials:" + creds + " HTTPS:" + https); setCredentials(creds); - if (state == null) + if(null == state) { state = new State(); - this.sessionHost = host; - this.sessionPort = port; - this.https = https; - + } + if (null == connection) { + connection = new HttpConnection(host,port,https); + } } @@ -760,9 +687,12 @@ */ public void startSession(String host, int port, String proxyhost, int proxyport) { - this.proxyHost = proxyhost; - this.proxyPort = proxyport; - startSession(host, port); + if(null == state) { + state = new State(); + } + if (null == connection) { + connection = new HttpConnection(proxyhost,proxyport,host,port,false); + } } @@ -771,22 +701,17 @@ */ public void endSession() throws IOException { - log.debug("HttpClient.endSession()"); - - closeConnection(); - + connection.close(); state = null; - this.sessionHost = ""; - this.sessionPort = -1; - this.https = false; + connection = null; } // ------------------------------------------------------ Protected Methods - +/* protected void openConnection() throws IOException, UnknownHostException { log.debug("HttpClient.openConnection()"); try { @@ -831,8 +756,8 @@ } } - - +*/ +/* protected void closeConnection() throws IOException { log.debug("HttpClient.closeConnection()"); @@ -858,8 +783,8 @@ } } +*/ - /** * Send a WebDAV request. * @@ -877,22 +802,21 @@ method.setState(state); String requestLine; - if (proxyHost == null || proxyPort < 0) + if (!connection.isProxied()) requestLine = method.generateRequestLine(); else - requestLine = method.generateRequestLine(getHost(), getPort()); + requestLine = method.generateRequestLine(connection.getHost(), connection.getPort()); - String hostName = sessionHost; - if (sessionPort != 80) - hostName = hostName + ":" + sessionPort; + String hostName = connection.getHost(); + if (connection.getPort() != 80) + hostName = connection.getHost() + ":" + connection.getPort(); method.generateHeaders(hostName, state); Enumeration headersList = method.getHeaders(); // Sending request line - wireLog.info(">> \"" + requestLine + "\""); - output.write(requestLine.getBytes()); + connection.write(requestLine.getBytes()); // Sending headers byte[] query = null; @@ -903,15 +827,13 @@ query = queryStr.getBytes("UTF8"); if (method.needContentLength()) { String contentLengthHeader = "Content-Length: " + query.length + "\r\n"; - wireLog.info(">> \"" + contentLengthHeader + "\""); - output.write(contentLengthHeader.getBytes()); + connection.write(contentLengthHeader.getBytes()); } } else { // Chunking if ((http11) && (method.getHeader("Content-Length") == null)) { String transferEncodingHeader = "Transfer-Encoding: chunked\r\n"; - wireLog.info(">> \"" + transferEncodingHeader + "\""); - output.write(transferEncodingHeader.getBytes()); + connection.write(transferEncodingHeader.getBytes()); } } @@ -928,8 +850,7 @@ String challengeResponse = Authenticator.challengeResponse(state.getAuthenticateToken(),state); if (challengeResponse != null) { String authorizationHeader = "Authorization: " + challengeResponse + "\r\n"; - wireLog.info(">> \"" + authorizationHeader + "\""); - output.write(authorizationHeader.getBytes()); + connection.write(authorizationHeader.getBytes()); if (connectionInterceptor != null) { connectionInterceptor.authenticate(); } @@ -939,20 +860,17 @@ // Send expectation header if (method.needExpectation()) { String expectHeader = "Expect: 100-continue\r\n"; - wireLog.info(">> \"" + expectHeader + "\""); - output.write(expectHeader.getBytes()); + connection.write(expectHeader.getBytes()); } // Writing HTTP headers while (headersList.hasMoreElements()) { Header header = (Header) headersList.nextElement(); - wireLog.info(">> \"" + header.toString() + "\""); - output.write(header.toString().getBytes()); + connection.write(header.toString()); } - wireLog.info(">> \\r\\n (closing head)"); - output.write("\r\n".getBytes()); + connection.writeLine(); // close out head return query; } @@ -969,7 +887,7 @@ // Writing request body RequestOutputStream requestOutputStream = - new RequestOutputStream(output, method); + new RequestOutputStream(connection.getOutputStream(), method); requestOutputStream.setInterceptor(streamInterceptor); if (method.isStreamedQuery()) { @@ -998,6 +916,7 @@ * was encountered * @exception IOException if an input or output exception has occurred */ +/* protected String readLine(InputStream input) throws IOException { @@ -1021,7 +940,7 @@ return (sb.toString()); } - +*/ /** * Parse status line. @@ -1034,7 +953,7 @@ log.debug("HttpClient.parseStatusLine(String,HttpMethod)"); while (statusLine != null && !statusLine.startsWith("HTTP/")) { - statusLine = readLine(input); + statusLine = connection.readLine(); } if (statusLine == null) throw new HttpException @@ -1085,7 +1004,7 @@ * * @param input Input stream on which the bytes are read */ - protected Hashtable parseHeaders(InputStream input) + protected Hashtable parseHeaders() throws IOException, HttpException { log.debug("HttpClient.parseHeaders(InputStream)"); Hashtable result = new Hashtable(); @@ -1093,7 +1012,7 @@ while (true) { // Read the next header line - String line = readLine(input); + String line = connection.readLine(); if ((line == null) || (line.length() < 1)) break; @@ -1122,7 +1041,7 @@ if (header != null) { try { - Cookie[] cookies = Cookie.parse(sessionHost, header); + Cookie[] cookies = Cookie.parse(connection.getHost(), header); state.addCookies(cookies); } catch (Exception e) { e.printStackTrace(); No revision No revision 1.1.2.1 +306 -0 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Attic/HttpConnection.java