Connecting to WebDAV server

To make a real connection to the server, you must implement the connect() method. The following example implementation of the method checks if the authentication information is correct for the provided site, and also checks that the site is a valid WebDAV resource.

private CollectionHandle siteHandle;
public void connect() throws ConnectionException {
    ServerConfig config = server.getServerConfig();
    String siteUrl = config.getConfigParameter(“siteUrl”).
                       getValue().toString();
    String username = config.getConfigParameter(“username”).
                       getValue().toString();
    String password = config.getConfigParameter(“password”).
                       getValue().toString();
    try {
        password = EncryptionUtil.decrypt(password);
    } catch (IllegalArgumentException e) {
        throw new ConnectionException(
              "Connection failed. "
            + "Password has changed in the servers configuration file. "
            + "Please re-enter the server password.", e);
    }
    WebDAVFactory davFactory = new WebDAVFactory();
    HttpClient httpClient = new HttpClient();
    httpClient.setAuthenticator(new SimpleBasicAuthenticator(username,
            password));
    DAVClient davClient = new RemoteDAVClient(davFactory, httpClient);
    ILocator siteLocator = davFactory.newLocator(siteUrl);
    siteHandle = new CollectionHandle(davClient, siteLocator);
    try {
        if (siteHandle.canTalkDAV()) {
            server.setServerState(ServerState.CONNECTED_LITERAL);
        } else {
            throw new ConnectionException(
                     "Cannot connect to WebDAV site.");
        }
    } catch (DAVException e) {
        throw new ConnectionException(e);
    }
}

In this example, the server parameters’ values are read from the server configuration. Then, we create davClient and siteLocator (which is responsible for locating DAV resource). Next we create CollectionHandle (the proxy for a remote resource) for the site and invoke the canTalkDAV() method. This method checks if the connection to the server can be made, and if the corresponding remote resource is a valid WebDAV collection resource.

The WebDAV is a request response protocol, so there is no explicit session associated with a connection. However, the HttpClient used by siteHandle does contain some session information (for example authentication information) that is sent with every request.