Java Code Examples for com.github.sardine.Sardine#shutdown()

The following examples show how to use com.github.sardine.Sardine#shutdown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AWebdavHandler.java    From nextcloud-java-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * method to check if a remote object already exists
 *
 * @param remotePath path of the file/folder
 * @return boolean value if the given file/folder exists or not
 */
public boolean pathExists(String remotePath) {
    String path = buildWebdavPath(remotePath);
    Sardine sardine = buildAuthSardine();

    try
    {
        return sardine.exists(path);
    } catch (IOException e)
    {
        throw new NextcloudApiException(e);
    }
    finally
    {
        try
        {
            sardine.shutdown();
        }
        catch (IOException ex)
        {
            LOG.warn("error in closing sardine connector", ex);
        }
    }
}
 
Example 2
Source File: AWebdavHandler.java    From nextcloud-java-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Deletes the file/folder at the specified path
 *
 * @param remotePath path of the file/folder
 */
public void deletePath(String remotePath)
{
    String path=  buildWebdavPath( remotePath );

    Sardine sardine = buildAuthSardine();
    try {
        sardine.delete(path);
    } catch (IOException e) {
        throw new NextcloudApiException(e);
    }
    finally
    {
        try
        {
            sardine.shutdown();
        }
        catch (IOException ex)
        {
            LOG.warn("error in closing sardine connector", ex);
        }
    }
}
 
Example 3
Source File: Folders.java    From nextcloud-java-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a folder at the specified path
 *
 * @param remotePath path of the folder
 */
public void createFolder(String remotePath)
{
    String path=  buildWebdavPath(remotePath );
    Sardine sardine = buildAuthSardine();

    try {
        sardine.createDirectory(path);
    } catch (IOException e) {
        throw new NextcloudApiException(e);
    }
    finally
    {
        try
        {
            sardine.shutdown();
        }
        catch (IOException ex)
        {
            LOG.warn("error in closing sardine connector", ex);
        }
    }
}
 
Example 4
Source File: Files.java    From nextcloud-java-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Downloads the file at the specified remotepath as an InputStream,
 *
 * @param remotePath Remotepath where the file is saved in the nextcloud
 * server
 * @return InputStream
 * @throws IOException  In case of IO errors
 */
public InputStream downloadFile(String remotePath) throws IOException {
    String path = buildWebdavPath(remotePath);
    Sardine sardine = buildAuthSardine();

    WebdavInputStream in = null;
    try
    {
        in = new WebdavInputStream(sardine, sardine.get(path));
    } catch (IOException e)
    {
        sardine.shutdown();
        throw new NextcloudApiException(e);
    }
    return in;
}
 
Example 5
Source File: Folders.java    From nextcloud-java-api with GNU General Public License v3.0 5 votes vote down vote up
/**
 * List all file names and subfolders of the specified path traversing 
 * into subfolders to the given depth.
 *
 * @param remotePath path of the folder
 * @param depth depth of recursion while listing folder contents
 * @param excludeFolderNames excludes the folder names from the list
 * @return found file names and subfolders
 */
public List<String> listFolderContent(String remotePath, int depth, boolean excludeFolderNames, boolean returnFullPath)
{
    String path = buildWebdavPath(remotePath);

    List<String> retVal = new LinkedList<>();
    Sardine sardine = buildAuthSardine();
    List<DavResource> resources;
    try {
        resources = sardine.list(path, depth);
    } catch (IOException e) {
        throw new NextcloudApiException(e);
    }
    finally
    {
        try
        {
            sardine.shutdown();
        }
        catch (IOException ex)
        {
            LOG.warn("error in closing sardine connector", ex);
        }
    }
    for (DavResource res : resources)
    {
        if (excludeFolderNames) {
            if (!res.isDirectory()) {
                retVal.add(returnFullPath ? res.getPath().replaceFirst("/remote.php/webdav/", "") : res.getName());
            }
        }
        else {
            retVal.add(returnFullPath ? res.getPath().replaceFirst("/remote.php/webdav/", "") : res.getName());
        }
    }
    return retVal;
}
 
Example 6
Source File: Files.java    From nextcloud-java-api with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Downloads the file at the specified remotepath to the download directory,
 * and returns true if the download is successful
 *
 * @param remotePath Remotepath where the file is saved in the nextcloud
 * server
 * @param downloadDirPath Path where the file is downloaded, it would be
 * created if it doesn't exist.
 * @return boolean
 * @throws IOException  In case of IO errors
 */
public boolean downloadFile(String remotePath, String downloadDirPath) throws IOException {
    boolean status = false;
    String path = buildWebdavPath(remotePath);
    Sardine sardine = buildAuthSardine();

    File downloadFilepath = new File(downloadDirPath);
    if (!downloadFilepath.exists())
    {
        downloadFilepath.mkdir();
    }

    if (fileExists(remotePath))
    {
        //Extract the Filename from the path
        String[] segments = path.split("/");
        String filename = segments[segments.length - 1];
        downloadDirPath = downloadDirPath + "/" + filename;
    }
    InputStream in = null;
    try
    {
        in = sardine.get(path);
        byte[] buffer = new byte[AWebdavHandler.FILE_BUFFER_SIZE];
        int bytesRead;
        File targetFile = new File(downloadDirPath);
        try (OutputStream outStream = new FileOutputStream(targetFile))
        {
            while ((bytesRead = in.read(buffer)) != -1)
            {
                outStream.write(buffer, 0, bytesRead);
            }
            outStream.flush();
            outStream.close();
        }
        status = true;
    } catch (IOException e)
    {
        throw new NextcloudApiException(e);
    } finally
    {
        sardine.shutdown();
        if (in != null)
        {
            in.close();
        }
    }
    return status;
}