Setting CONNECTION_TIMEOUT, and SO_TIMEOUT time paramters

291 views
Skip to first unread message

Ian McCoy

unread,
Apr 9, 2013, 1:10:22 PM4/9/13
to sardi...@googlegroups.com
Hi,

I wrote a post the other day about using Sardine to upload files to a WebDav server using an embedded device.  Everything is working great out of the box.  I would like to get some information about the defaults values for the following parameters:

And, if I wanted to set these values manually, how would I go about doing it?

Little Background:

There was a networking issue the other day and it appeared that sardine was hanging (it could have been on my end).  I would like to protect against a connection hanging and also extend the Connection_Timeout and SO_Timeout parameters.  The project I'm working has a cellular modem built into it and per the wireless service provider networking latencies can be on the order of 500ms or greater.

Here is how I am going about my business:

I followed the following procedure to setup my keystore and trustmanager: http://www.techbrainwave.com/?p=953

When I run the program I added this VM options to the command line: KEYSTORE="/opt/mystore/mykey.jks"
VM_OPTION1="-Djavax.net.ssl.trustStore=$KEYSTORE"

From there, I initialized sardine as described in the usage guide: Sardine sardine = SardineFactory.begin(username, password), then use the sardine.get and sardine.put methods without any problem.

My background on the SSL stuff is pretty week.  So, I would appreciate the help.

Regards,

Ian




Sasan Yavari

unread,
Nov 11, 2013, 8:15:46 AM11/11/13
to sardi...@googlegroups.com
Hi dear Ian

Use this code:

import com.github.sardine.Version;
import com.github.sardine.impl.SardineImpl;
import com.github.sardine.impl.handler.VoidResponseHandler;
import com.github.sardine.impl.methods.HttpMkCol;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.VersionInfo;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.ProxySelector;

public class CSardine extends SardineImpl {
    public CSardine(String username, String password) {
        super(username, password);
    }

    @Override
    protected HttpParams createDefaultHttpParams() {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        String version = Version.getSpecification();
        if (version == null) {
            version = VersionInfo.UNAVAILABLE;
        }
        HttpProtocolParams.setUserAgent(params, "Sardine/" + version);
        HttpProtocolParams.setUseExpectContinue(params, false);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.DEF_CONTENT_CHARSET.name());

        HttpConnectionParams.setTcpNoDelay(params, true);
        HttpConnectionParams.setSocketBufferSize(params, 8192);
        HttpConnectionParams.setConnectionTimeout(params, 10000);
        return params;
    }

    @Override
    protected AbstractHttpClient createDefaultClient(ProxySelector selector) {
        SchemeRegistry schemeRegistry = this.createDefaultSchemeRegistry();
        ClientConnectionManager cm = this.createDefaultConnectionManager(schemeRegistry);
        HttpParams params = this.createDefaultHttpParams();
        AbstractHttpClient c = new DefaultHttpClient(cm, params);
        c.setRoutePlanner(this.createDefaultRoutePlanner(schemeRegistry, selector));
        return c;
    }

    public HttpResponse head(String url) throws IOException {
        return execute(new HttpHead(url));
    }

    public HttpResponse put(String url, FileInputStream fis, int available, String md5) throws Exception {
        HttpResponse response;
        HttpPut put = null;
        InputStreamEntity entity;

        try {
            entity = new InputStreamEntity(fis, available);

            put = new HttpPut(url);
            put.setEntity(entity);
            put.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
            put.addHeader(HttpHeaders.CONTENT_MD5, md5);
            put.addHeader(HttpHeaders.CONTENT_TYPE, HTTP.DEF_CONTENT_CHARSET.name());

            response = execute(put);
        } catch (Exception e) {
            abort(put);
            throw e;
        } finally {
            releaseConnection(put);
        }

        return response;
    }

    private void abort(HttpRequestBase requestBase) {
        if (requestBase != null) {
            try {
                requestBase.abort();
            } catch (Exception ignored) {}
        }
    }

    private void releaseConnection(HttpRequestBase requestBase) {
        if (requestBase != null) {
            try {
                requestBase.releaseConnection();
            } catch (Exception ignored) {}
Reply all
Reply to author
Forward
0 new messages