[jcifs] Timeout still failing on v1.3.18

ron190 ron190 at ymail.com
Sat Jul 16 21:31:45 UTC 2016


For me the problem is that jcifs wraps a new HttpURLConnection so it loses
every settings defined on the original connection, like the timeout
settings. To prove this either I use reflection or I modify the library and
change jcifs internal connection, then the timeout works fine.

(For information setting jcifs.smb.client.responseTimeout and
jcifs.smb.client.soTimeout does not work)


First, I validate that jcifs is the problem : my timeout of 15000ms does not
work at all when I use jcifs.Config.registerSmbURLHandler(), connection
breaks after 30000ms. My 15000ms timeout works only if I remove the call to
registerSmbURLHandler().


Regarding the problem, I open a connection (with jcifs previously registered) :

    URLConnection myConnection = new URL(url).openConnection();

Then the URLStreamHandler creates a wrapping NtlmHttpURLConnection and hides
the real HttpURLConnection :

    protected URLConnection openConnection(URL url) throws IOException {
        url = new URL(url, url.toExternalForm(),
                getDefaultStreamHandler(url.getProtocol()));
        return new NtlmHttpURLConnection((HttpURLConnection)
                url.openConnection());
    }


So my timeout settings are applied to the wrapper NtlmHttpURLConnection,
it's not applied to the true opened URLConnection. So my timeout are useless :

    myConnection.setReadTimeout(15000);    // applied to the new
NtlmHttpURLConnection(wrapped), not the real wrapped one
    myConnection.setConnectTimeout(15000); // applied to the new
NtlmHttpURLConnection(wrapped), not the real wrapped one


There are two solutions I can use to change the timeout on the wrapped
connection : with reflection or with a fixed library.


With reflection, I access the private wrapped connection and change the
private fields connectTimeout and readTimeout :

    Class<?> classConnection = myConnection.getClass();

    Field privateFieldURLConnection =
classConnection.getDeclaredField("connection");
    privateFieldURLConnection.setAccessible(true);
    
    URLConnection privateURLConnection = (URLConnection)
privateFieldURLConnection.get(myConnection);
    Class<?> classURLConnectionPrivate = privateURLConnection.getClass();
    
    Field privateFieldConnectTimeout =
classURLConnectionPrivate.getDeclaredField("connectTimeout");
    privateFieldConnectTimeout.setAccessible(true);
    privateFieldConnectTimeout.setInt(privateURLConnection, 15000);
    
    Field privateFieldReadTimeout =
classURLConnectionPrivate.getDeclaredField("readTimeout");
    privateFieldReadTimeout.setAccessible(true);
    privateFieldReadTimeout.setInt(privateURLConnection, 15000);
    

Or I modify the jcifs library and the constructor NtlmHttpURLConnection() :

    public NtlmHttpURLConnection(HttpURLConnection connection) {
        super(connection.getURL());
        this.connection = connection;
        
        this.connection.setReadTimeout(15000);
        this.connection.setConnectTimeout(15000);
        
        requestProperties = new HashMap();
    }


These solutions are working but it's only a workaround, please can someone
take this problem into account ?




More information about the jCIFS mailing list