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

The following examples show how to use com.github.sardine.Sardine#exists() . 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: WebDavServiceImpl.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String upload(@ValidateStringParam(name = "site_id") final String site,
                     @ValidateStringParam(name = "profile") final String profileId,
                     @ValidateStringParam(name = "path") final String path,
                     @ValidateStringParam(name = "filename") final String filename,
                     final InputStream content)
    throws WebDavException {
    WebDavProfile profile = getProfile(site, profileId);
    String uploadUrl = StringUtils.appendIfMissing(profile.getBaseUrl(), "/");
    try {
        Sardine sardine = SardineFactory.begin(profile.getUsername(), profile.getPassword());

        if(StringUtils.isNotEmpty(path)) {
            String[] folders = StringUtils.split(path, "/");

            for(String folder : folders) {
                uploadUrl += StringUtils.appendIfMissing(folder, "/");

                logger.debug("Checking folder {0}", uploadUrl);
                if(!sardine.exists(uploadUrl)) {
                    logger.debug("Creating folder {0}", uploadUrl);
                    sardine.createDirectory(uploadUrl);
                    logger.debug("Folder {0} created", uploadUrl);
                } else {
                    logger.debug("Folder {0} already exists", uploadUrl);
                }
            }
        }

        uploadUrl =  StringUtils.appendIfMissing(uploadUrl, "/");
        String fileUrl = uploadUrl + UriUtils.encode(filename, charset.name());

        logger.debug("Starting upload of file {0}", filename);
        logger.debug("Uploading file to {0}", fileUrl);

        sardine.put(fileUrl, content);
        logger.debug("Upload complete for file {0}", fileUrl);
        if(StringUtils.isNotEmpty(profile.getDeliveryBaseUrl())) {
            fileUrl = StringUtils.replaceFirst(fileUrl, profile.getBaseUrl(), profile.getDeliveryBaseUrl());
        }
        return fileUrl;
    } catch (Exception e ) {
        throw new WebDavException("Error uploading file", e);
    }
}
 
Example 3
Source File: WebDavServiceImpl.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@ValidateParams
@HasPermission(type = DefaultPermission.class, action = "webdav_write")
public WebDavItem upload(@ValidateStringParam(name = "siteId") @ProtectedResourceId("siteId") final String siteId,
                         @ValidateStringParam(name = "profileId") final String profileId,
                         @ValidateStringParam(name = "path") final String path,
                         @ValidateStringParam(name = "filename") final String filename,
                         final InputStream content)
    throws WebDavException {
    WebDavProfile profile = getProfile(siteId, profileId);
    String uploadUrl = StringUtils.appendIfMissing(profile.getBaseUrl(), "/");
    try {
        Sardine sardine = createClient(profile);

        if(StringUtils.isNotEmpty(path)) {
            String[] folders = StringUtils.split(path, "/");

            for(String folder : folders) {
                uploadUrl += StringUtils.appendIfMissing(folder, "/");

                logger.debug("Checking folder {0}", uploadUrl);
                if(!sardine.exists(uploadUrl)) {
                    logger.debug("Creating folder {0}", uploadUrl);
                    sardine.createDirectory(uploadUrl);
                    logger.debug("Folder {0} created", uploadUrl);
                } else {
                    logger.debug("Folder {0} already exists", uploadUrl);
                }
            }
        }

        uploadUrl =  StringUtils.appendIfMissing(uploadUrl, "/");
        String fileUrl = uploadUrl + UriUtils.encode(filename, charset.name());

        logger.debug("Starting upload of file {0}", filename);
        logger.debug("Uploading file to {0}", fileUrl);

        sardine.put(fileUrl, content);
        logger.debug("Upload complete for file {0}", fileUrl);

        return new WebDavItem(filename, String.format(urlPattern, profileId, path, filename), false);
    } catch (Exception e ) {
        throw new WebDavException("Error uploading file", e);
    }
}