harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r399687 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/net/ main/java/org/apache/harmony/luni/internal/net/www/protocol/file/ main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/ main/java/org/a...
Date Thu, 04 May 2006 12:40:38 GMT
Author: tellison
Date: Thu May  4 05:40:35 2006
New Revision: 399687

URL: http://svn.apache.org/viewcvs?rev=399687&view=rev
Log:
Apply patch HARMONY-425 (J2SE5 Enhancement: Two proxy related new methods in class java.net.URL
and java.net.URLStreamHandler)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URL.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLStreamHandler.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/Handler.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/FtpURLConnection.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/Handler.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Handler.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/NetUtil.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/URLTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URL.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URL.java?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URL.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URL.java
Thu May  4 05:40:35 2006
@@ -22,6 +22,7 @@
 import java.util.Hashtable;
 import java.util.StringTokenizer;
 
+import org.apache.harmony.luni.util.Msg;
 import org.apache.harmony.luni.util.PriviAction;
 
 /**
@@ -637,6 +638,33 @@
 		return new URI(toExternalForm());
 	}
 
+	/**
+	 * The method is the same as <code>openConnection()</code> except that it 
+	 * uses the <code>proxy</code> to establish a connection to this URL using
+	 * appropriate ProtocolHandler. 
+	 * 
+	 * @return The connection to this URL.
+	 * @param proxy
+	 *            the proxy which is used to make the connection
+	 * 
+	 * @exception IOException
+	 *                thrown if an IO error occurs during connection
+	 *                establishment
+	 * @exception SecurityException
+	 * 			      thrown if a security manager is installed and it denies 
+	 *                the permission to connect to the proxy. 	
+	 * @exception IllegalArgumentException
+	 *                thrown if the proxy is null or of an invalid type.
+	 * @exception UnsupportedOperationException
+	 *                thrown if the protocol handler doesn't support this method.
+	 */
+	public URLConnection openConnection(Proxy proxy) throws IOException {
+		if(null == proxy){
+			throw new IllegalArgumentException(Msg.getString("K034c"));
+		}
+		return strmHandler.openConnection(this, proxy);
+	}
+	
 	/**
 	 * Answers a string containing a concise, human-readable description of the
 	 * receiver.

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLStreamHandler.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLStreamHandler.java?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLStreamHandler.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLStreamHandler.java
Thu May  4 05:40:35 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
 
 import java.io.IOException;
 
+import org.apache.harmony.luni.util.Msg;
+
 /**
  * The abstract superclass of all classes that implement Protocol Handler.
  */
@@ -36,6 +38,30 @@
 	 *                establishment
 	 */
 	protected abstract URLConnection openConnection(URL u) throws IOException;
+	
+	/**
+	 * The method is the same as <code>openConnection(URL u)</code> except 
+	 * that it uses the <code>proxy</code> to establish a connection to the 
+	 * <code>URL</code>. Since different protocols may have different ways of
+	 * connecting, it must be overwritten by the subclass.
+	 * 
+	 * @return java.net.URLConnection
+	 * @param u
+	 *            java.net.URL
+	 * @param proxy
+	 *            the proxy which is used to make the connection
+	 * 
+	 * @throws IOException
+	 *                thrown if an IO error occurs during connection
+	 *                establishment
+	 * @throws IllegalArgumentException
+	 *             if any argument is null or the type of proxy is wrong.
+	 * @throws UnsupportedOperationException
+	 *             if the protocol handler doesn't support this method.
+	 */
+	protected URLConnection openConnection(URL u, Proxy proxy) throws IOException{
+		throw new UnsupportedOperationException(Msg.getString("K034d"));
+	}
 
 	/**
 	 * Parse the <code>string</code>str into <code>URL</code> using
u's

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/Handler.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/Handler.java?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/Handler.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/Handler.java
Thu May  4 05:40:35 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,9 +16,13 @@
 package org.apache.harmony.luni.internal.net.www.protocol.file;
 
 
+import java.io.IOException;
+import java.net.Proxy;
 import java.net.URL;
 import java.net.URLConnection;
 
+import org.apache.harmony.luni.util.Msg;
+
 /**
  * This is the handler that is responsible for reading files from the file
  * system.
@@ -37,6 +41,32 @@
 	 * 
 	 */
 	public URLConnection openConnection(URL url) {
+		return new FileURLConnection(url);
+	}
+	
+	/**
+	 * The behaviour of this method is the same as openConnection(URL).
+	 * <code>proxy</code> is not used in FileURLConnection.
+	 * 
+	 * @param u
+	 *            the URL which the connection is pointing to
+	 * @param proxy
+	 *            java.net.Proxy
+	 * @return a connection to the resource pointed by this url.
+	 * 
+	 * @throws IOException
+	 *             if this handler fails to establish a connection.
+	 * @throws IllegalArgumentException
+	 *             if any argument is null or of an invalid type.
+	 * @throws UnsupportedOperationException
+	 *             if the protocol handler doesn't support this method.
+	 */
+
+	public URLConnection openConnection(URL url, Proxy proxy)
+			throws IOException {
+		if (null == url || null == proxy) {
+			throw new IllegalArgumentException(Msg.getString("K034b"));
+		}
 		return new FileURLConnection(url);
 	}
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/FtpURLConnection.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/FtpURLConnection.java?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/FtpURLConnection.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/FtpURLConnection.java
Thu May  4 05:40:35 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,15 +22,24 @@
 import java.io.InputStream;
 import java.io.InterruptedIOException;
 import java.io.OutputStream;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+import java.net.ProxySelector;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketPermission;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
+import java.net.URLStreamHandler;
 import java.security.Permission;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
 
 import org.apache.harmony.luni.internal.net.www.MimeTable;
-
+import org.apache.harmony.luni.net.NetUtil;
 import org.apache.harmony.luni.util.Msg;
 
 public class FtpURLConnection extends URLConnection {
@@ -77,6 +86,12 @@
 	//private static final int FTP_ERROR = 500;
 
 	private static final int FTP_NOTFOUND = 550;
+	
+	private Proxy proxy = null;
+	
+	private Proxy currentProxy = null;
+	
+	private URI uri = null;
 
 	/**
 	 * FtpURLConnection constructor comment.
@@ -96,8 +111,27 @@
 			} else
 				USERNAME = parse;
 		}
+		uri = null;
+		try {
+			uri = url.toURI();
+		} catch (URISyntaxException e) {
+			// do nothing.
+		}
 	}
 
+	/**
+	 * FtpURLConnection constructor.
+	 * 
+	 * @param url
+	 *            java.net.URL
+	 * @param proxy
+	 *            java.net.Proxy
+	 */
+	protected FtpURLConnection(URL url, Proxy proxy) {
+		this(url);
+		this.proxy = proxy;
+	}
+	
 	/* Change the server directory to that specfied in the URL */
 	private void cd() throws IOException {
 		int idx = url.getFile().lastIndexOf('/');
@@ -124,10 +158,52 @@
 	 * @see URLStreamHandler
 	 */
 	public void connect() throws IOException {
+		// Use system-wide ProxySelect to select proxy list,
+		// then try to connect via elements in the proxy list.
+		List proxyList = null;
+		if(null != proxy){
+			proxyList = new ArrayList(1);
+			proxyList.add(proxy);
+		}else{
+			proxyList = NetUtil.getProxyList(uri);
+		}
+		if(null == proxyList){
+			currentProxy = null;
+			connectInternal();
+		}else{
+			ProxySelector selector = ProxySelector.getDefault();
+			Iterator iter = proxyList.iterator();
+			boolean connectOK = false;
+			while(iter.hasNext() && !connectOK){
+				currentProxy = (Proxy)iter.next();
+				try{
+					connectInternal();
+					connectOK = true;
+				}catch(IOException ioe){
+					// If connect failed, callback "connectFailed" 
+					// should be invoked.
+					if(null != selector && Proxy.NO_PROXY != currentProxy){
+						selector.connectFailed(uri, currentProxy.address(), ioe);
+					}
+				}
+			}
+			if(!connectOK){
+				throw new IOException(Msg.getString("K0097"));
+			}
+		}
+	}
+	private void connectInternal() throws IOException {
 		int port = url.getPort();
 		if (port <= 0)
 			port = FTP_PORT;
-		controlSocket = new Socket(hostName, port);
+		if(null == currentProxy || Proxy.Type.HTTP == currentProxy.type()){
+			controlSocket = new Socket(hostName,port);
+		}else{
+			controlSocket = new Socket(currentProxy);
+			InetSocketAddress addr = new InetSocketAddress(hostName,port);
+			controlSocket.connect(addr);
+		}
+		
 		connected = true;
 		ctrlOutput = controlSocket.getOutputStream();
 		ctrlInput = controlSocket.getInputStream();

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/Handler.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/Handler.java?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/Handler.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/Handler.java
Thu May  4 05:40:35 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2002 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,9 +16,12 @@
 package org.apache.harmony.luni.internal.net.www.protocol.ftp;
 
 import java.io.IOException;
+import java.net.Proxy;
 import java.net.URL;
 import java.net.URLConnection;
 
+import org.apache.harmony.luni.util.Msg;
+
 
 public class Handler extends java.net.URLStreamHandler {
 	/**
@@ -28,6 +31,33 @@
 		return new FtpURLConnection(u);
 	}
 
+	/**
+	 * Answers a connection, which is established via the <code>proxy</code>,
+	 * to the FTP server specified by this <code>URL</code>. If
+	 * <code>proxy</code> is DIRECT type, the connection is made in normal
+	 * way.
+	 * 
+	 * @param u
+	 *            the URL which the connection is pointing to
+	 * @param proxy
+	 *            the proxy which is used to make the connection
+	 * @return a connection to the resource pointed by this url.
+	 * 
+	 * @throws IOException
+	 *             if this handler fails to establish a connection.
+	 * @throws IllegalArgumentException
+	 *             if any argument is null or the type of proxy is wrong.
+	 * @throws UnsupportedOperationException
+	 *             if the protocol handler doesn't support this method.
+	 */
+	protected URLConnection openConnection(URL u, Proxy proxy)
+			throws IOException {
+		if (null == u || null == proxy) {
+			throw new IllegalArgumentException(Msg.getString("K034b"));
+		}
+		return new FtpURLConnection(u, proxy);
+	}
+	
 	/**
 	 * Return the default port.
 	 */

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Handler.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Handler.java?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Handler.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Handler.java
Thu May  4 05:40:35 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,11 +16,14 @@
 package org.apache.harmony.luni.internal.net.www.protocol.http;
 
 
+import java.io.IOException;
+import java.net.Proxy;
 import java.net.URL;
 import java.net.URLConnection;
-import java.io.IOException;
 import java.net.URLStreamHandler;
 
+import org.apache.harmony.luni.util.Msg;
+
 /**
  * This is the handler that manages all transactions between the client and a HTTP remote
server.
  *
@@ -37,6 +40,29 @@
  */
 protected URLConnection openConnection(URL u) throws IOException {
 	return new HttpURLConnection(u, getDefaultPort());
+}
+
+/**
+ * Answers a connection, which is established via the <code>proxy</code>,
+ * to the HTTP server specified by this <code>URL</code>. If the 
+ * <code>proxy</code> is DIRECT type, the connection is made in normal way.
+ *
+ * @param 		u 		the URL which the connection is pointing to
+ * @param		proxy	the proxy which is used to make the connection
+ * @return 		a connection to the resource pointed by this url.
+ *
+ * @throws		IOException
+ *                  if this handler fails to establish a connection.
+ * @throws 		IllegalArgumentException	
+ *                  if any argument is null or the type of proxy is wrong.
+ * @throws 		UnsupportedOperationException	
+ *                  if the protocol handler doesn't support this method.
+ */
+protected URLConnection openConnection(URL u, Proxy proxy) throws IOException {
+	if(null == u || null == proxy){
+		throw new IllegalArgumentException(Msg.getString("K034b"));
+	}
+	return new HttpURLConnection(u, getDefaultPort(), proxy);
 }
 
 /**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
Thu May  4 05:40:35 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,17 +16,38 @@
 package org.apache.harmony.luni.internal.net.www.protocol.http;
 
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.ByteArrayOutputStream;
-import java.net.*;
-import java.util.*;
+import java.net.Authenticator;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.PasswordAuthentication;
+import java.net.ProtocolException;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketPermission;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.net.UnknownServiceException;
 import java.security.AccessController;
-import org.apache.harmony.luni.util.PriviAction;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.TimeZone;
-import java.text.SimpleDateFormat;
+
+import org.apache.harmony.luni.net.NetUtil;
+import org.apache.harmony.luni.util.Msg;
+import org.apache.harmony.luni.util.PriviAction;
 
 /**
  * This subclass extends <code>HttpURLConnection</code> which in turns extends
<code>URLConnection</code>
@@ -47,7 +68,19 @@
 	private String proxyName;
 	private int hostPort = -1;
 	private int readTimeout = -1;
-
+	
+	// proxy which is used to make the connection.
+	private Proxy proxy = null;
+	
+	// the proxy list which is used to make the connection.
+	private List proxyList = null;
+	
+	// the current proxy which is used to make the connection.
+	private Proxy currentProxy;
+	
+	// the destination uri
+	private URI uri = null;
+	
 	// default request header
 	private static Header defaultReqHeader = new Header();
 
@@ -247,7 +280,7 @@
 	}
 
 /**
- * Creates a instance of the <code>HttpURLConnection</code>
+ * Creates an instance of the <code>HttpURLConnection</code>
  * using default port 80.
  * @param url URL	The URL this connection is connecting
  */
@@ -256,7 +289,7 @@
 }
 
 /**
- * Creates a instance of the <code>HttpURLConnection</code>
+ * Creates an instance of the <code>HttpURLConnection</code>
  * @param url URL	The URL this connection is connecting
  * @param port int	The default connection port
  */
@@ -265,8 +298,28 @@
 	defaultPort = port;
 
 	reqHeader = (Header)defaultReqHeader.clone();
-
+	try {
+		uri = url.toURI();
+	} catch (URISyntaxException e) {
+		// do nothing.
+	}
 }
+
+	/**
+	 * Creates an instance of the <code>HttpURLConnection</code>
+	 * 
+	 * @param url
+	 *            URL The URL this connection is connecting
+	 * @param port
+	 *            int The default connection port
+	 * @param proxy
+	 *            Proxy The proxy which is used to make the connection
+	 */
+	protected HttpURLConnection(URL url, int port, Proxy proxy) {
+		this(url, port);
+		this.proxy = proxy;
+	}
+
 /**
  * Establishes the connection to the remote HTTP server
  *
@@ -280,7 +333,18 @@
  */
 public void connect() throws java.io.IOException {
 	if (connected) return;
-	Socket socket = new Socket(getHostAddress(), getHostPort());
+	Socket socket;
+	
+	InetAddress host = getHostAddress();
+	int port = getHostPort();
+	if(null == currentProxy || Proxy.Type.HTTP == currentProxy.type()){
+		socket = new Socket(host, port);
+	}else{
+		socket = new Socket(currentProxy);
+		SocketAddress sa = new InetSocketAddress(host, port);
+		socket.connect(sa);
+	}
+	
 	if (readTimeout >= 0) socket.setSoTimeout(readTimeout);
 	connected = true;
 	socketOut = socket.getOutputStream();
@@ -824,15 +888,24 @@
  * proxy port if a proxy port has been set.
  */
 private int getHostPort() {
-	if (hostPort != -1) return hostPort;
-	String portString = getSystemPropertyOrAlternative("http.proxyPort", "proxyPort");
-	if (portString != null && usingProxy()) {
-		hostPort = Integer.parseInt(portString);
-	} else {
+	if(usingProxy()){
+		if(null == currentProxy){
+			// get from system property
+			String portString = getSystemPropertyOrAlternative("http.proxyPort", "proxyPort");
+			if (portString != null) {
+				hostPort = Integer.parseInt(portString);
+			}
+		}else{
+			// get from proxy
+			InetSocketAddress addr = (InetSocketAddress) currentProxy.address();
+			hostPort = addr.getPort();
+		}
+	}else{
 		hostPort = url.getPort();
 	}
-
-	if (hostPort < 0) hostPort = defaultPort;
+	if (hostPort < 0){ 
+		hostPort = defaultPort;
+	}
 	return hostPort;
 }
 
@@ -855,7 +928,12 @@
 		return proxyName;
 	}
 	if (usingProxy()) {
-	    proxyName = getSystemPropertyOrAlternative("http.proxyHost", "proxyHost");
+		if(null == currentProxy){
+			proxyName = getSystemPropertyOrAlternative("http.proxyHost", "proxyHost");
+		}else{
+			InetSocketAddress addr = (InetSocketAddress)currentProxy.address();
+			proxyName = addr.getHostName();
+		}
 		return proxyName;
 	}
 	return url.getHost();
@@ -879,6 +957,13 @@
  * Need to check both proxy* and http.proxy* because of change between JDK 1.0 and JDK 1.1
  */
 public boolean usingProxy() {
+	if(Proxy.NO_PROXY == currentProxy){
+		return false;
+	}
+	// using proxy if http proxy is set by caller.
+	if(null != currentProxy && Proxy.Type.HTTP == currentProxy.type()){
+		return true;
+	}
 	// First check whether the user explicitly set whether to use a proxy.
     String proxySet = getSystemProperty("http.proxySet");
 	if (proxySet != null) return proxySet.toLowerCase().equals("true");
@@ -891,6 +976,7 @@
 	return getSystemProperty("proxyHost") != null;
 }
 
+
 /**
  * Handles an HTTP request along with its redirects and authentication
  *
@@ -908,7 +994,43 @@
 		}
 		return;
 	}
+	// Use system-wide ProxySelect to select proxy list,
+	// then try to connect via elements in the proxy list.
+	if(null != proxy){
+		proxyList = new ArrayList(1);
+		proxyList.add(proxy);
+	}else{
+		proxyList = NetUtil.getProxyList(uri);
+	}
+	if(null == proxyList){
+		currentProxy = null;
+		doRequestInternal();
+	}else{
+		// try the proxy list until one of them establish
+		// the connection successfully.
+		ProxySelector selector = ProxySelector.getDefault();
+		Iterator iter = proxyList.iterator();
+		boolean doRequestOK = false;
+		while(iter.hasNext() && !doRequestOK){
+			currentProxy = (Proxy)iter.next();
+			try{
+				doRequestInternal();
+				doRequestOK = true;
+			}catch(IOException ioe){
+				// if connect failed, callback method "connectFailed" 
+				// should be invoked.
+				if(null != selector && Proxy.NO_PROXY != currentProxy){
+					selector.connectFailed(uri, currentProxy.address(), ioe);
+				}
+			}
+		}
+		if(!doRequestOK){
+			throw new IOException(Msg.getString("K0097"));
+		}
+	}
+}
 
+void doRequestInternal() throws java.io.IOException {
 	int redirect = 0;
 	while(true) {
 		// send the request and process the results

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/NetUtil.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/NetUtil.java?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/NetUtil.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/NetUtil.java
Thu May  4 05:40:35 2006
@@ -15,7 +15,10 @@
 package org.apache.harmony.luni.net;
 
 import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.URI;
 import java.security.AccessController;
+import java.util.List;
 
 import org.apache.harmony.luni.util.PriviAction;
 
@@ -71,6 +74,22 @@
     	String result = (String) AccessController.doPrivileged(new PriviAction(
     			"java.net.preferIPv4Stack"));
         return "true".equals(result);
+    }
+
+    /**
+     * Gets proxy list according to the uri by system ProxySelector.
+     * @param uri
+     * @return a list of proxy for the uri. Returns null if no proxy 
+     * 		   is available.
+     */
+    public static List getProxyList(URI uri){
+		// use system default selector to get proxy list
+		ProxySelector selector = ProxySelector.getDefault();
+		if(null == selector){
+			return null;
+		}
+		List proxyList = selector.select(uri);
+    	return proxyList;
     }
 
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
Thu May  4 05:40:35 2006
@@ -281,3 +281,6 @@
 K0348=Format specifier '{0}'
 K0349=Conversion is '{0}'
 K034a=The flags are {0}
+K034b=url and proxy can not be null
+K034c=proxy should not be be null
+K034d=method has not been implemented yet

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java
Thu May  4 05:40:35 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -145,9 +145,7 @@
 						+ Support_Configuration.InetTestAddress);
 				URLConnection conn = u.openConnection();
 				conn.getInputStream();
-			} catch (ConnectException e) {
-				exception = true;
-			} catch (UnknownHostException e) {
+			} catch (IOException e) {
 				exception = true;
 			} catch (Exception e) {
 				exception = false;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/URLTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/URLTest.java?rev=399687&r1=399686&r2=399687&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/URLTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/URLTest.java
Thu May  4 05:40:35 2006
@@ -19,12 +19,20 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
 import java.net.MalformedURLException;
+import java.net.Proxy;
+import java.net.ProxySelector;
+import java.net.SocketAddress;
 import java.net.URI;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLStreamHandler;
 import java.net.URLStreamHandlerFactory;
+import java.security.Permission;
+import java.util.ArrayList;
+import java.util.List;
 
 import tests.support.Support_Configuration;
 import tests.support.resource.Support_Resources;
@@ -53,6 +61,8 @@
 	URL u6;
 
 	boolean caught = false;
+	
+	static boolean isSelectCalled;
 
 	/**
 	 * @tests java.net.URL#setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory)
@@ -1057,6 +1067,99 @@
         URI uri = u.toURI();
         assertTrue(u.equals(uri.toURL()));
     }
+    
+    /**
+     * @tests java.net.URL#openConnection(Proxy)
+     */
+    public void test_openConnectionLjava_net_Proxy() throws IOException {
+		SocketAddress addr1 = new InetSocketAddress(
+				Support_Configuration.ProxyServerTestHost, 808);
+		SocketAddress addr2 = new InetSocketAddress(
+				Support_Configuration.ProxyServerTestHost, 1080);
+		Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1);
+		Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr2);
+		Proxy proxyList[] = { proxy1, proxy2 };
+		for (int i = 0; i < proxyList.length; ++i) {
+			try {
+				String posted = "just a test";
+				URL u = new URL("http://"
+						+ Support_Configuration.ProxyServerTestHost
+						+ "/cgi-bin/test.pl");
+				java.net.HttpURLConnection conn = (java.net.HttpURLConnection) u
+						.openConnection(proxyList[i]);
+				conn.setDoOutput(true);
+				conn.setRequestMethod("POST");
+				conn.setRequestProperty("Content-length", String.valueOf(posted
+						.length()));
+				OutputStream out = conn.getOutputStream();
+				out.write(posted.getBytes());
+				out.close();
+				conn.getResponseCode();
+				InputStream is = conn.getInputStream();
+				String response = "";
+				byte[] b = new byte[1024];
+				int count = 0;
+				while ((count = is.read(b)) > 0){
+					response += new String(b, 0, count);
+				}
+				assertTrue("Response to POST method invalid", response
+						.equals(posted));
+			}  catch (Exception e) {
+				fail("Unexpected exception connecting to proxy : "
+						+ proxyList[i] + e.getMessage());
+			}
+		}
+		
+		URL httpUrl = new URL("http://abc.com");
+		URL jarUrl = new URL("jar:"
+				+ Support_Resources.getResourceURL("/JUC/lf.jar!/plus.bmp"));
+		URL ftpUrl = new URL("ftp://" + Support_Configuration.FTPTestAddress
+				+ "/nettest.txt");
+		URL fileUrl = new URL("file://abc");
+		URL[] urlList = { httpUrl, jarUrl, ftpUrl, fileUrl };
+		for (int i = 0; i < urlList.length; ++i) {
+			try {
+				urlList[i].openConnection(null);
+			} catch (IllegalArgumentException iae) {
+				// expected
+			}
+		}
+		// should not throw exception
+		fileUrl.openConnection(Proxy.NO_PROXY);
+	}
+    
+ 
+    /**
+     * @tests java.net.URL#openConnection()
+     */
+	public void test_openConnection_SelectorCalled()
+			throws MalformedURLException {
+		URL httpUrl = new URL("http://"
+				+ Support_Configuration.ProxyServerTestHost
+				+ "/cgi-bin/test.pl");
+		URL ftpUrl = new URL("ftp://" + Support_Configuration.FTPTestAddress
+				+ "/nettest.txt");
+		URL[] urlList = { httpUrl, ftpUrl };
+		ProxySelector orignalSelector = ProxySelector.getDefault();
+		ProxySelector.setDefault(new MockProxySelector());
+		try {
+			for (int i = 0; i < urlList.length; ++i) {
+				try {
+					isSelectCalled = false;
+					URLConnection conn = urlList[i].openConnection();
+					conn.getInputStream();
+				} catch (Exception e) {
+					// ignore
+				}
+				assertTrue(
+						"openConnection should call ProxySelector.select(), url = "
+								+ urlList[i], isSelectCalled);
+			}
+		} finally {
+			ProxySelector.setDefault(orignalSelector);
+
+		}
+	}
 
 	/**
 	 * Sets up the fixture, for example, open a network connection. This method
@@ -1070,5 +1173,36 @@
 	 * method is called after a test is executed.
 	 */
 	protected void tearDown() {
+	}
+	
+	static class MockProxySelector extends ProxySelector {
+
+		public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+			System.out.println("connection failed");
+		}
+
+		public List select(URI uri) {
+			isSelectCalled = true;
+			ArrayList proxyList = new ArrayList(1);
+			proxyList.add(Proxy.NO_PROXY);
+			return proxyList;
+		}
+	}
+
+	static class MockSecurityManager extends SecurityManager {
+
+		public void checkConnect(String host, int port) {
+			if ("127.0.0.1".equals(host)) {
+				throw new SecurityException("permission is not allowed");
+			}
+		}
+
+		public void checkPermission(Permission permission) {
+			if ("setSecurityManager".equals(permission.getName())) {
+				return;
+			}
+			super.checkPermission(permission);
+		}
+
 	}
 }



Mime
View raw message