harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ghar...@apache.org
Subject svn commit: r396693 - in /incubator/harmony/enhanced/classlib/trunk/modules: luni/src/main/java/java/net/ luni/src/main/java/org/apache/harmony/luni/net/ luni/src/test/java/tests/api/java/net/ nio/src/main/java/org/apache/harmony/nio/internal/
Date Mon, 24 Apr 2006 21:41:00 GMT
Author: gharley
Date: Mon Apr 24 14:40:59 2006
New Revision: 396693

URL: http://svn.apache.org/viewcvs?rev=396693&view=rev
Log:
HARMONY-359 : Java 5 Enhancement: One new constructor Socket(Proxy p) in class java.net.Socket

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socket.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/net/PlainSocketImpl.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl2.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketImplProvider.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socket.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socket.java?rev=396693&r1=396692&r2=396693&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socket.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socket.java
Mon Apr 24 14:40:59 2006
@@ -25,7 +25,6 @@
 import org.apache.harmony.luni.net.NetUtil;
 import org.apache.harmony.luni.net.SocketImplProvider;
 import org.apache.harmony.luni.platform.Platform;
-
 import org.apache.harmony.luni.util.Msg;
 import org.apache.harmony.luni.util.PriviAction;
 
@@ -52,6 +51,8 @@
 	private boolean isOutputShutdown = false;
 
 	private Object connectLock = new Object();
+	
+	private Proxy proxy;
 
 	static final int MULTICAST_IF = 1;
 
@@ -79,6 +80,41 @@
 		impl = factory != null ? factory.createSocketImpl()
 				: SocketImplProvider.getSocketImpl();
 	}
+	/**
+	 * Constructs a connection-oriented Socket with specified 
+	 * <code>proxy</code>. 
+	 * 
+	 * Method <code>checkConnect</code> is called if a security manager exists,
+	 * and the proxy host address and port number are passed as parameters.  
+	 * 
+	 * @param proxy
+	 *            the specified proxy for this Socket.
+	 * @throws IllegalArgumentException
+	 *             if the proxy is null or of an invalid type.
+	 * @throws SecurityException
+	 *             if a security manager exists and it denies the permission
+	 *             to connect to proxy. 
+	 */
+	public Socket(Proxy proxy){
+		if(null == proxy || Proxy.Type.HTTP == proxy.type()){
+			throw new IllegalArgumentException("proxy is null or invalid type");
+		}
+		InetSocketAddress address = (InetSocketAddress)proxy.address();
+		if(null != address){
+			InetAddress addr = address.getAddress();
+			String host;
+			if(null != addr){
+				host = addr.getHostAddress();
+			}else{
+				host = address.getHostName();
+			}
+			int port = address.getPort();
+			checkConnectPermission(host, port);
+		}
+		impl = factory != null ? factory.createSocketImpl()
+				: SocketImplProvider.getSocketImpl(proxy);
+		this.proxy = proxy;
+	}
 
 	/**
 	 * Construct a stream socket connected to the nominated destination
@@ -248,11 +284,25 @@
 	 *            the port on the destination host
 	 */
 	void checkDestination(InetAddress destAddr, int dstPort) {
-		if (dstPort < 0 || dstPort > 65535)
+		if (dstPort < 0 || dstPort > 65535){
 			throw new IllegalArgumentException(Msg.getString("K0032"));
+		}
+		checkConnectPermission(destAddr.getHostName(), dstPort);
+	}
+	
+	/*
+	 * Checks the connection destination satisfies the security policy.
+	 * 
+	 * @param hostname
+	 *            the destination hostname
+	 * @param dstPort
+	 *            the port on the destination host
+	 */
+	private void checkConnectPermission(String hostname, int dstPort) {
 		SecurityManager security = System.getSecurityManager();
-		if (security != null)
-			security.checkConnect(destAddr.getHostName(), dstPort);
+		if (security != null){
+			security.checkConnect(hostname, dstPort);
+		}
 	}
 
 	/**
@@ -646,8 +696,9 @@
 			impl.create(streaming);
 			isCreated = true;
 			try {
-				if (!streaming || !NetUtil.usingSocks())
+				if (!streaming || !NetUtil.usingSocks(proxy)){
 					impl.bind(addr, localPort);
+				}
 				isBound = true;
 				impl.connect(dstAddress, dstPort);
 				isConnected = true;
@@ -820,8 +871,9 @@
 
 		synchronized (this) {
 			try {
-				if (!NetUtil.usingSocks())
+				if (!NetUtil.usingSocks(proxy)){
 					impl.bind(addr, port);
+				}
 				isBound = true;
 			} catch (IOException e) {
 				impl.close();
@@ -891,8 +943,9 @@
 					// checkClosedAndCreate
 					// this caused us to lose socket options on create
 					// impl.create(true);
-					if (!NetUtil.usingSocks())
+					if (!NetUtil.usingSocks(proxy)){
 						impl.bind(InetAddress.ANY, 0);
+					}
 					isBound = true;
 				}
 				impl.connect(remoteAddr, timeout);

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=396693&r1=396692&r2=396693&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
Mon Apr 24 14:40:59 2006
@@ -14,6 +14,7 @@
  */
 package org.apache.harmony.luni.net;
 
+import java.net.Proxy;
 import java.security.AccessController;
 
 import org.apache.harmony.luni.util.PriviAction;
@@ -25,11 +26,23 @@
      */
     
     /**
-     * Answer whether to use a SOCKS proxy.
-     * 
-     * @return boolean
-     */
-    public static boolean usingSocks() {
+	 * Answer whether to use a SOCKS proxy.
+	 * 
+	 * @param proxy
+	 *            java.net.Proxy, may be <code>null</code> in which case the 
+     *            return value is determined from the values of the 
+     *            "socksProxySet" and "socksProxyHost" system properties.
+	 * @return true if SOCKS proxy is set by <code>proxy</code> parameter or
+     *         else "socksProxySet" system property is set and has the value
+     *         of "true" or if "socksProxyHost" is set in system properties.
+	 */
+    public static boolean usingSocks(Proxy proxy) {
+    	if(null != proxy && Proxy.NO_PROXY == proxy){
+    		return false;
+    	}
+    	if(null != proxy && Proxy.Type.SOCKS == proxy.type()){
+    		return true;
+    	}
         String proxySet = (String) AccessController
                 .doPrivileged(new PriviAction("socksProxySet"));
         if (proxySet != null) {

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java?rev=396693&r1=396692&r2=396693&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
Mon Apr 24 14:40:59 2006
@@ -23,6 +23,7 @@
 import java.net.ConnectException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
+import java.net.Proxy;
 import java.net.SocketAddress;
 import java.net.SocketException;
 import java.net.SocketImpl;
@@ -31,14 +32,8 @@
 import java.net.UnknownHostException;
 import java.security.AccessController;
 
-import org.apache.harmony.luni.net.NetUtil;
-import org.apache.harmony.luni.net.PlainSocketImpl;
-import org.apache.harmony.luni.net.SocketInputStream;
-import org.apache.harmony.luni.net.SocketOutputStream;
-import org.apache.harmony.luni.net.Socks4Message;
 import org.apache.harmony.luni.platform.INetworkSystem;
 import org.apache.harmony.luni.platform.Platform;
-
 import org.apache.harmony.luni.util.Msg;
 import org.apache.harmony.luni.util.PriviAction;
 
@@ -79,6 +74,8 @@
 	public boolean streaming = true;
 
 	public boolean shutdownInput = false;
+	
+	Proxy proxy = null;
 
 	/**
 	 * Accepts a connection on the provided socket, by calling the IP stack.
@@ -89,7 +86,7 @@
 	 *                if an error occurs while accepting
 	 */
 	protected void accept(SocketImpl newImpl) throws IOException {
-		if (NetUtil.usingSocks()) {
+		if (NetUtil.usingSocks(proxy)) {
 			((PlainSocketImpl) newImpl).socksBind();
 			((PlainSocketImpl) newImpl).socksAccept();
 			return;
@@ -136,7 +133,7 @@
 	 *                if an error occurs while binding
 	 */
 	protected void bind(InetAddress anAddr, int aPort) throws IOException {
-		if (NetUtil.usingSocks()) {
+		if (NetUtil.usingSocks(proxy)) {
 			socksBind();
 			return;
 		}
@@ -224,7 +221,7 @@
 
 		try {
 			if (streaming) {
-				if (NetUtil.usingSocks()) {
+				if (NetUtil.usingSocks(proxy)) {
 					socksConnect(anAddr, aPort, 0);
 				} else {
 					if (timeout == 0) {
@@ -343,7 +340,7 @@
 	 *                thrown if an error occurs while listening
 	 */
 	protected void listen(int backlog) throws IOException {
-		if (NetUtil.usingSocks()) {
+		if (NetUtil.usingSocks(proxy)) {
 			// Do nothing for a SOCKS connection. The listen occurs on the
 			// server during the bind.
 			return;
@@ -400,7 +397,13 @@
 	 */
 	private int socksGetServerPort() {
 		int portValue = -1;
-
+		
+		if(null != proxy && Proxy.Type.SOCKS == proxy.type()){
+			// get from proxy first
+			InetSocketAddress addr = (InetSocketAddress)proxy.address();
+			return addr.getPort();
+		}
+		
 		String proxyPort = (String) AccessController
 				.doPrivileged(new PriviAction("socksProxyPort"));
 
@@ -418,9 +421,19 @@
 	 * Get the InetAddress of the SOCKS proxy server.
 	 */
 	private InetAddress socksGetServerAddress() throws UnknownHostException {
-		String proxyName = (String) AccessController
+		String proxyName;
+		if(null != proxy && Proxy.Type.SOCKS == proxy.type()){
+			// get from proxy first
+			InetSocketAddress addr = (InetSocketAddress)proxy.address();
+			proxyName = addr.getHostName();
+			if(null == proxyName){
+				proxyName = addr.getAddress().getHostAddress();
+			}
+		}else{
+			// get from system properties
+			proxyName = (String) AccessController
 				.doPrivileged(new PriviAction("socksProxyHost"));
-
+		}
 		InetAddress anAddr = netImpl.getHostByName(proxyName,
                 NetUtil.preferIPv6Addresses());
 		return anAddr;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl2.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl2.java?rev=396693&r1=396692&r2=396693&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl2.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl2.java
Mon Apr 24 14:40:59 2006
@@ -18,10 +18,9 @@
 import java.io.FileDescriptor;
 import java.io.IOException;
 import java.net.InetAddress;
+import java.net.Proxy;
 import java.net.SocketException;
 
-import org.apache.harmony.luni.net.NetUtil;
-
 
 /**
  * This class was added so we can create sockets without options that were
@@ -43,6 +42,14 @@
     
     public PlainSocketImpl2(){
         super();
+    }
+    
+    /*
+     * creates an instance with specified proxy.
+     */
+    public PlainSocketImpl2(Proxy proxy){
+    	super();
+    	this.proxy = proxy;
     }
 
     /**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketImplProvider.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketImplProvider.java?rev=396693&r1=396692&r2=396693&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketImplProvider.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketImplProvider.java
Mon Apr 24 14:40:59 2006
@@ -17,6 +17,7 @@
 import java.io.FileDescriptor;
 import java.net.DatagramSocketImpl;
 import java.net.InetAddress;
+import java.net.Proxy;
 import java.net.SocketImpl;
 
 
@@ -24,6 +25,12 @@
     
     public static SocketImpl getSocketImpl(){
         return new PlainSocketImpl2();
+    }
+    /*
+     * gets a SocketImpl with specified proxy.
+     */
+    public static SocketImpl getSocketImpl(Proxy proxy){
+        return new PlainSocketImpl2(proxy);
     }
     
     public static SocketImpl getSocketImpl(FileDescriptor fd, int localport, InetAddress
addr, int port){

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java?rev=396693&r1=396692&r2=396693&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java
Mon Apr 24 14:40:59 2006
@@ -24,11 +24,13 @@
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
+import java.net.Proxy;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketAddress;
 import java.net.SocketException;
 import java.net.SocketTimeoutException;
+import java.security.Permission;
 
 import tests.support.Support_Configuration;
 import tests.support.Support_PortManager;
@@ -2514,8 +2516,87 @@
 	}
 	
 	/**
-	 * 
+	 * @tests java.net.Socket#Socket(Proxy)
 	 */
+	public void test_ConstructorLjava_net_Proxy_Exception() {
+
+	    SocketAddress addr1 = InetSocketAddress.createUnresolved("127.0.0.1",
+	            80);
+	    SocketAddress addr2 = new InetSocketAddress("localhost", 80);
+
+	    Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1);
+	    // IllegalArgumentException test
+	    try {
+	        new Socket(proxy1);
+	        fail("should throw IllegalArgumentException");
+	    } catch (IllegalArgumentException e) {
+	        // expected
+	    }
+
+	    Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr1);
+	    try {
+	        new Socket(proxy2);
+	    } catch (IllegalArgumentException e) {
+	        fail("should not throw IllegalArgumentException");
+	    }
+
+	    try {
+	        new Socket(Proxy.NO_PROXY);
+	    } catch (IllegalArgumentException e) {
+	        fail("should not throw IllegalArgumentException");
+	    }
+
+	    // SecurityException test
+	    SecurityManager originalSecurityManager = System.getSecurityManager();
+	    try {
+	        System.setSecurityManager(new MockSecurityManager());
+	    } catch (SecurityException e) {
+	        System.err
+	        .println("No permission to setSecurityManager, security related test in test_ConstructorLjava_net_Proxy_Security
is ignored");
+	        return;
+	    }
+
+	    Proxy proxy3 = new Proxy(Proxy.Type.SOCKS, addr1);
+	    Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, addr2);
+	    try {
+	        try {
+	            new Socket(proxy3);
+	            fail("should throw SecurityException");
+	        } catch (SecurityException e) {
+	            // expected
+	        }
+	        try {
+	            new Socket(proxy4);
+	            fail("should throw SecurityException");
+	        } catch (SecurityException e) {
+	            // expected
+	        }
+	    } finally {
+	        System.setSecurityManager(originalSecurityManager);
+	    }
+
+	}
+
+	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);
+	    }
+
+	}
+    
+	/**
+     * 
+     */
 	protected int startServer(String name) {
 		int portNumber = Support_PortManager.getNextPort();
 		try {

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java?rev=396693&r1=396692&r2=396693&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java
Mon Apr 24 14:40:59 2006
@@ -250,7 +250,7 @@
             // close the
             // ServerSocket as well. The ServerSocket cannot be used for a
             // second accept.
-            if (NetUtil.usingSocks()) {
+            if (NetUtil.usingSocks(null)) {
                 return super.accept();
             }
 



Mime
View raw message