Hi Paulo,
I must admit that making a URLConnection request from within select
looks a little funny, but I don't think it should cause a problem. I've
run the test you posted and I don't see the default proxy selector being
invoke. I only see the user defined selector. Can you provide more
information about what you are seeing? JDK version, any debugging
output/traces?
It may also be worth enabling HTTP logging,
-Djava.util.logging.config.file=logging.properties (see attached
logging.properties)
-Chris.
On 02/12/2009 00:56, Paulo Levi wrote:
Test case:
package util.net;
import java.io.IOException;
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.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author i30817
*/
public class ProxySelectorBugTest {
public ProxySelectorBugTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
*
*/
@Test
public void testURLConnectionDoesntBypassProxySelector(){
ProxySelector proxySelector = ProxySelector.getDefault();
ProxySelector.setDefault(new UserProxySelector());
try {
//This calls the installed proxy selector.
URL u = new URL("http://www.yahoo.com");
URLConnection conn = u.openConnection();
conn.connect();
} catch (Exception ex) {
Logger.getLogger(ProxySelectorBugTest.class.getName()).log(Level.SEVERE,
null, ex);
}
ProxySelector.setDefault(proxySelector);
}
class UserProxySelector extends ProxySelector{
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
@Override
public List<Proxy> select(URI uri) {
try {
//bug here, the java doc say that this will bypass the installed
//proxyselector but it doesn't.
URL u = new URL("http://www.google.com");
URLConnection conn = u.openConnection(Proxy.NO_PROXY);
conn.connect();
} catch (Exception ex) {
Logger.getLogger(UserProxySelector.class.getName()).log(Level.SEVERE,
null, ex);
}
return Collections.singletonList(Proxy.NO_PROXY);
}
}
}
############################################################
# Default Logging Configuration File
#
# You can use a different file by specifying a filename
# with the java.util.logging.config.file system property.
# For example java -Djava.util.logging.config.file=myfile
############################################################
############################################################
# Global properties
############################################################
# "handlers" specifies a comma separated list of log Handler
# classes. These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers. For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= SEVERE
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter =
sun.net.www.protocol.http.HttpLogFormatter
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
sun.net.www.protocol.http.HttpURLConnection.level = ALL