Example usage for org.apache.commons.httpclient HttpURL setUserinfo

List of usage examples for org.apache.commons.httpclient HttpURL setUserinfo

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpURL setUserinfo.

Prototype

public void setUserinfo(String paramString1, String paramString2) throws URIException, NullPointerException 

Source Link

Usage

From source file:com.mirth.connect.connectors.file.filesystems.WebDavConnection.java

public WebDavConnection(String host, boolean secure, FileSystemConnectionOptions fileSystemOptions)
        throws Exception {
    this.secure = secure;
    username = fileSystemOptions.getUsername();
    password = fileSystemOptions.getPassword();

    HttpURL url = null;

    if (secure) {
        url = new HttpsURL("https://" + host);
    } else {//from   ww  w.  j av  a2s  .  c o  m
        url = new HttpURL("http://" + host);
    }

    if (!username.equals("null")) {
        url.setUserinfo(username, password);
    }

    client = new WebdavResource(url);
}

From source file:net.sf.jftp.net.WebdavConnection.java

private HttpURL getURL(String u) {
    try {//from  w ww  .java2  s.co m
        HttpURL url = new HttpURL(u);

        //System.out.println(user + ":"+ pass);
        url.setUserinfo(user, pass);

        return url;
    } catch (Exception ex) {
        ex.printStackTrace();
        Log.debug("ERROR: " + ex);

        return null;
    }
}

From source file:com.cladonia.xngreditor.URLUtilities.java

private static WebdavResource createWebdavResource(String uri, String username, String password)
        throws IOException {
    WebdavResource webdavResource = null;

    if (!uri.endsWith("/")) {
        int index = uri.lastIndexOf('/');

        if (index != -1) {
            uri = uri.substring(0, index + 1);
        }/*from w  ww  .j  a  v  a2  s  . co  m*/
    }

    HttpURL httpURL = null;

    if (uri.startsWith("https")) {
        httpURL = new HttpsURL(uri.toCharArray());
    } else {
        httpURL = new HttpURL(uri.toCharArray());
    }

    try {
        // Set up for processing WebDAV resources
        webdavResource = new WebdavResource(httpURL);
    } catch (HttpException we) {
        // try with authorization ...
        we.printStackTrace();
        //         System.out.println( we.getReasonCode()+": "+we.getReason());
        if ((username == null) || (username.length() == 0)) {
            if (webdavResource != null) {
                webdavResource.close();
            }

            return null;
        }

        if (webdavResource != null) {
            webdavResource.close();
        }

        httpURL.setUserinfo(username, password);

        webdavResource = new WebdavResource(httpURL);
    }

    return webdavResource;
}

From source file:com.sos.VirtualFileSystem.WebDAV.SOSVfsWebDAV.java

private HttpURL getWebdavRessourceURL(String path) throws Exception {
    HttpURL url = null;

    if (rootUrl != null) {
        path = path.startsWith("/") ? path.substring(1) : path;
        if (rootUrl.getScheme().equalsIgnoreCase("https")) {
            url = new HttpsURL(rootUrl.getEscapedURI() + path);
        } else {/*from w  ww.  j  av a 2s .  c  o m*/
            url = new HttpURL(rootUrl.getEscapedURI() + path);
        }
    }
    url.setUserinfo(userName, password);

    return url;
}

From source file:com.idega.slide.business.IWSlideServiceBean.java

/**
 * Gets the root url for the webdav server with authentication
 *
 * @return//from www .java 2s . c om
 */
private HttpURL getWebdavServerURL(UsernamePasswordCredentials credential, String path, String servletPath,
        boolean addSessionId) {
    try {
        String server = getIWApplicationContext().getDomain().getURL();
        if (server == null) {
            return null;
        }

        int port = 80;
        boolean https = false;
        if (server.endsWith(CoreConstants.SLASH)) {
            server = server.substring(0, server.lastIndexOf(CoreConstants.SLASH));
        }
        if (server.startsWith("http://")) {
            server = server.substring(7, server.length());
        }
        if (server.startsWith("https://")) {
            if (getIWMainApplication().getSettings().getBoolean("slide.allow.local.https")) {
                // https protocol when to slide is only enabled when this property is set
                https = true;
            }
            server = server.substring(8, server.length());
        }
        if (server.indexOf(CoreConstants.COLON) != -1) {
            String sPort = server.substring(server.indexOf(CoreConstants.COLON) + 1, server.length());
            port = Integer.parseInt(sPort);
            server = server.substring(0, server.indexOf(CoreConstants.COLON));
        }

        String rootPath = servletPath;
        String realPath = rootPath;
        if (path != null) {
            realPath = rootPath + path;
        }

        HttpURL hrl = https ? new HttpsURL(server, port, realPath) : new HttpURL(server, port, realPath);
        if (credential != null) {
            hrl.setUserinfo(credential.getUserName(), credential.getPassword());
        }

        return hrl;
    } catch (URIException e) {
        throw new IBORuntimeException(e);
    }
}

From source file:com.sos.VirtualFileSystem.WebDAV.SOSVfsWebDAV.java

/**
 *
 * \brief setRootHttpURL//  w  w w . j a  v  a  2 s  .  co m
 *
 * \details
 *
 * \return void
 *
 * @param puser
 * @param ppassword
 * @param phost
 * @param pport
 * @return HttpURL
 * @throws Exception
 */
private HttpURL setRootHttpURL(final String puser, final String ppassword, final String phost, final int pport)
        throws Exception {

    rootUrl = null;
    HttpURL httpUrl = null;
    String path = "/";
    String normalizedHost = normalizeRootHttpURL(phost, pport);

    if (connection2OptionsAlternate.auth_method.isURL()) {
        if (phost.toLowerCase().startsWith("https://")) {
            httpUrl = new HttpsURL(normalizedHost);
        } else {
            httpUrl = new HttpURL(normalizedHost);
        }

        String phostRootUrl = httpUrl.getScheme() + "://" + httpUrl.getAuthority() + "/";
        if (httpUrl.getScheme().equalsIgnoreCase("https")) {
            rootUrl = new HttpsURL(phostRootUrl);

            if (pport > 0) {
                Protocol.registerProtocol("https", new Protocol("https",
                        (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), pport));
            }
        } else {
            rootUrl = new HttpURL(phostRootUrl);
        }
    } else {
        httpUrl = new HttpURL(phost, pport, path);
        rootUrl = new HttpURL(phost, pport, path);
    }
    httpUrl.setUserinfo(puser, ppassword);
    return httpUrl;
}

From source file:org.apache.webdav.ui.WebdavSystemView.java

/**
 * Creates a new folder with a default folder name.
 *//*from ww  w .  java2 s.com*/
public File createNewFolder(File containingDir) throws IOException {
    try {
        if (containingDir == null) {
            throw new IOException("Containing directory is null:");
        }
        WebdavFile newFolder = null;
        HttpURL url = null;

        url = uriToHttpURL(containingDir.getPath() + WebdavFile.davSeparator + newFolderString);
        // Need to add user info so has access for queries
        url.setUserinfo(username, password);
        newFolder = new WebdavFile(url, this.rootURL);
        //System.out.println("new folder : " + newFolder.toString());

        this.connect();
        if (this.webdavResource.mkcolMethod(newFolder.getAbsolutePath())) {
            //System.out.println("succeeded.");
            return newFolder;
        } else {
            System.err.println("failed.");
            System.err.println(this.webdavResource.getStatusMessage());
            throw new IOException(this.webdavResource.getStatusMessage());
        }
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        System.err.println(e.toString());
        e.printStackTrace();
        return null;
    } finally {
        this.disconnect();
    }
}

From source file:org.apache.webdav.ui.WebdavSystemView.java

/**
 * Gets the list of shown (i.e. not hidden) files.
 *///w w  w .ja  v a 2 s  .  c  om
public File[] getFiles(File dir, boolean useFileHiding) {
    try {

        String filenames[] = null;
        WebdavFile files[] = null;
        HttpURL url = null;
        String path = null;
        String localDir = null;

        this.connect();
        // Now we try to list files

        path = dir.getPath();
        //System.out.println("getFiles : RAW PATH : '" + path + "'");

        // If path contains the server preamble, we need to extract that
        // and have the path only
        if (path.startsWith("http")) {
            //System.out.println("getFiles : preample : " + this.uri);
            path = path.replaceAll(this.uri, "");
        }
        if (!path.endsWith("/")) {
            path = path + "/";
        }

        //System.out.println("getFiles : path : " + path);

        this.webdavResource.setPath(path);
        filenames = this.webdavResource.list();
        files = new WebdavFile[filenames.length];
        for (int i = 0; i < filenames.length; i++) {
            //System.out.println("file : " + filenames[i]);
            // Lets try to construct a uri from the dir
            // given and the current file

            localDir = dir.getPath();
            if (!localDir.endsWith("/"))
                localDir = localDir + "/";

            String filepath = localDir + filenames[i];
            //System.out.println("getFiles : file fullpath : " + filepath);
            url = uriToHttpURL(filepath);
            // Need to add user info so has access for queries
            url.setUserinfo(username, password);
            files[i] = new WebdavFile(url, this.rootURL);
        }
        return files;
    } catch (Exception e) {
        System.err.println(e.toString());
        e.printStackTrace();
        return null;
    } finally {
        this.disconnect();
    }
}

From source file:org.fao.geonet.kernel.harvest.harvester.webdav.WebDavRetriever.java

private WebdavResource createResource(String url) throws Exception {
    if (log.isDebugEnabled())
        log.debug("Creating WebdavResource");

    HttpURL http = url.startsWith("https") ? new HttpsURL(url) : new HttpURL(url);

    if (params.useAccount) {
        if (log.isDebugEnabled())
            log.debug("using account, username: " + params.username + " password: " + params.password);
        http.setUserinfo(params.username, params.password);
    } else {//from   w w  w.jav a2 s.com
        if (log.isDebugEnabled())
            log.debug("not using account");
    }

    //--- setup proxy, if the case

    GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
    SettingManager sm = gc.getSettingManager();

    boolean enabled = sm.getValueAsBool("system/proxy/use", false);
    String host = sm.getValue("system/proxy/host");
    String port = sm.getValue("system/proxy/port");

    if (!enabled) {
        if (log.isDebugEnabled()) {
            log.debug("local proxy not enabled");
            log.debug("returning a new WebdavResource");
            log.debug("using http port: " + http.getPort() + " http uri: " + http.getURI());
        }
        return new WebdavResource(http, 1);
    } else {
        if (log.isDebugEnabled())
            log.debug("local proxy enabled");
        if (!Lib.type.isInteger(port)) {
            throw new Exception("Proxy port is not an integer : " + port);
        }
        if (log.isDebugEnabled()) {
            log.debug("returning a new WebdavResource");
            log.debug(
                    "using http proxy port: " + port + " proxy host: " + host + " http uri: " + http.getURI());
        }
        return new WebdavResource(http, host, Integer.parseInt(port));
    }
}

From source file:org.globus.cog.abstraction.impl.file.webdav.FileResourceImpl.java

/**
 * Create the davClient and authenticate with the resource. serviceContact
 * should be in the form of a url/*from  www . jav a2 s  .c o  m*/
 */
public void start() throws IllegalHostException, InvalidSecurityContextException, FileResourceException {

    ServiceContact serviceContact = getAndCheckServiceContact();

    try {

        SecurityContext securityContext = getOrCreateSecurityContext("WebDAV", serviceContact);

        String contact = getServiceContact().getContact().toString();
        if (!contact.startsWith("http")) {
            contact = "http://" + contact;
        }
        HttpURL hrl = new HttpURL(contact);
        PasswordAuthentication credentials = getCredentialsAsPasswordAuthentication(securityContext);

        String username = credentials.getUserName();
        String password = String.valueOf(credentials.getPassword());
        hrl.setUserinfo(username, password);

        davClient = new WebdavResource(hrl);
        setStarted(true);
    } catch (URIException ue) {
        throw new IllegalHostException("Error connecting to the WebDAV server at " + serviceContact, ue);
    } catch (Exception e) {
        throw new IrrecoverableResourceException(e);
    }
}