Example usage for org.apache.commons.vfs.provider.webdav WebdavClientFactory createConnection

List of usage examples for org.apache.commons.vfs.provider.webdav WebdavClientFactory createConnection

Introduction

In this page you can find the example usage for org.apache.commons.vfs.provider.webdav WebdavClientFactory createConnection.

Prototype

public static HttpClient createConnection(String hostname, int port, char[] username, char[] password,
        FileSystemOptions fileSystemOptions) throws FileSystemException 

Source Link

Document

Creates a new connection to the server.

Usage

From source file:org.docx4all.util.AuthenticationUtil.java

public final static int getAuthorisationStatus(WebdavFileObject fo) throws FileSystemException {
    int status = 401; //unauthorised and retry.

    org.apache.webdav.lib.methods.OptionsMethod optionsMethod = null;
    try {/* w w  w.ja v  a  2s  . c o m*/
        String urlCharset = WebdavFileSystemConfigBuilder.getInstance()
                .getUrlCharset(fo.getFileSystem().getFileSystemOptions());
        URLFileName urlFileName = (URLFileName) fo.getName();
        optionsMethod = new org.apache.webdav.lib.methods.OptionsMethod(
                urlFileName.getPathQueryEncoded(urlCharset));

        optionsMethod.setMethodRetryHandler(WebdavMethodRetryHandler.getInstance());
        optionsMethod.setFollowRedirects(true);

        char[] username = null;
        if (urlFileName.getUserName() != null) {
            username = urlFileName.getUserName().toCharArray();
        }
        char[] password = null;
        if (urlFileName.getPassword() != null) {
            password = urlFileName.getPassword().toCharArray();
        }

        WebdavClientFactory factory = new WebdavClientFactory();
        HttpClient client = factory.createConnection(urlFileName.getHostName(), urlFileName.getPort(), username,
                password, fo.getFileSystem().getFileSystemOptions());
        status = client.executeMethod(optionsMethod);
    } catch (Exception exc) {
        throw new FileSystemException("Cannot get authorisation status", exc);
    } finally {
        if (optionsMethod != null) {
            optionsMethod.releaseConnection();
        }
    }

    return status;
}