0

In Sardine how do I change the port number to something different from port 80 (for HTTP) and 443 (for HTTPS)?

The User guide states that I have to "override SardineImpl#createDefaultSchemeRegistry() or provide your own configured HTTP client instance by instantiating SardineImpl#SardineImpl(org.apache.http.impl.client.HttpClientBuilder)" but I can't find how to define the port.

When I instantiate SardineImpl using:

HttpClientBuilder builder = HttpClientBuilder.create();
SardineImpl sardine = new SardineImpl(builder, "user", "password");
byte[] data;
data = FileUtils.readFileToByteArray(new File("test.txt"));
sardine.put("http://webdav-server:8095/projects/", data);

I obtain:

org.apache.http.NoHttpResponseException: webdav-server:8095 failed to respond

The server is accessible via browser so the problem must be with the definition of the port and I could not find an example on how to do this.

Can someone help me on this? Thanks in advance.

ciberg
  • 73
  • 1
  • 7

1 Answers1

2

This is what I figured out after racking my brain trying to find a solution. Hopefully it helps someone else:

HttpClientBuilder builder = new HttpClientBuilder(){
  @Override
  public CloseableHttpClient build() {
    SchemePortResolver spr = new SchemePortResolver() {
      @Override
      public int resolve(HttpHost httpHost) throws UnsupportedSchemeException {
          return 8443; // SSL port to use
      }
  };

  CloseableHttpClient httpclient = HttpClients.custom()
      .useSystemProperties()
      .setSchemePortResolver(spr)
      .build();
  return httpclient;
}
};

Sardine sardine = new SardineImpl(builder, "user", "passwd");

List<DavResource> resources = null;

resources = sardine.list("https://ftp-site.com/path/");
resources.forEach(resource -> {
  System.out.println(resource.getName());
}

Hope that helps somebody.

user3317868
  • 696
  • 2
  • 9
  • 20