com.github.sardine.DavResource Java Examples

The following examples show how to use com.github.sardine.DavResource. 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: WebdavFileSystem.java    From xenon with Apache License 2.0 6 votes vote down vote up
@Override
protected List<PathAttributes> listDirectory(Path path) throws XenonException {

    List<DavResource> list = null;

    try {
        list = client.list(getDirectoryPath(path), 1);
    } catch (Exception e) {
        throw new XenonException(ADAPTOR_NAME, "Failed to list directory: " + path, e);
    }

    ArrayList<PathAttributes> result = new ArrayList<>(list.size());

    String dirPath = path.toString() + "/";

    for (DavResource d : list) {
        // The list also returns the directory itself, so ensure we don't
        // return it!
        if (!dirPath.equals(d.getPath())) {
            String filename = d.getName();
            result.add(getAttributes(path.resolve(filename), d));
        }
    }

    return result;
}
 
Example #2
Source File: WebdavFileSystem.java    From xenon with Apache License 2.0 6 votes vote down vote up
private PathAttributes getAttributes(Path path, DavResource p) {
    PathAttributesImplementation attributes = new PathAttributesImplementation();

    attributes.setPath(path);
    attributes.setDirectory(p.isDirectory());
    attributes.setRegular(!p.isDirectory());

    attributes.setCreationTime(p.getCreation().getTime());
    attributes.setLastModifiedTime(p.getModified().getTime());
    attributes.setLastAccessTime(attributes.getLastModifiedTime());
    attributes.setSize(p.getContentLength());

    // Not sure if this is right ?
    attributes.setReadable(true);
    attributes.setWritable(false);

    return attributes;
}
 
Example #3
Source File: DAVClient.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected List<DavResource> propfind(final String url, final int depth, final Propfind body) throws IOException {
    HttpPropFind entity = new HttpPropFind(url);
    entity.setDepth(depth < 0 ? "infinity" : Integer.toString(depth));
    entity.setEntity(new StringEntity(SardineUtil.toXml(body), StandardCharsets.UTF_8));
    Multistatus multistatus = this.execute(entity, PreferencesFactory.get().getBoolean("webdav.list.handler.sax") ? new SaxPropFindResponseHandler() : new MultiStatusResponseHandler());
    List<Response> responses = multistatus.getResponse();
    List<DavResource> resources = new ArrayList<DavResource>(responses.size());
    for(Response response : responses) {
        try {
            resources.add(new DavResource(response));
        }
        catch(URISyntaxException e) {
            log.warn(String.format("Ignore resource with invalid URI %s", response.getHref().get(0)));
        }
    }
    return resources;
}
 
Example #4
Source File: DAVAttributesFinderFeatureTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCustomModified_NotSet() {
    final DAVAttributesFinderFeature f = new DAVAttributesFinderFeature(null);
    final DavResource mock = mock(DavResource.class);

    Map<QName, String> map = new HashMap<>();
    final Date modified = new DateTime("2018-11-01T15:31:57Z").toDate();
    when(mock.getModified()).thenReturn(modified);
    when(mock.getCustomPropsNS()).thenReturn(map);

    final PathAttributes attrs = f.toAttributes(mock);
    assertEquals(modified.getTime(), attrs.getModificationDate());
}
 
Example #5
Source File: WebdavFileSystem.java    From xenon with Apache License 2.0 5 votes vote down vote up
@Override
public PathAttributes getAttributes(Path path) throws XenonException {

    Path absPath = toAbsolutePath(path);
    assertPathExists(absPath);

    try {
        List<DavResource> result = client.list(getFilePath(absPath), 0);
        return getAttributes(absPath, result.get(0));
    } catch (Exception e) {
        throw new XenonException(ADAPTOR_NAME, "Failed to get attributes for file: " + absPath, e);
    }
}
 
Example #6
Source File: WebDavServiceImpl.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
protected String getName(DavResource resource) {
    if(StringUtils.isNotEmpty(resource.getDisplayName())) {
        return resource.getDisplayName();
    } else {
        String path = resource.getPath();
        if(resource.isDirectory()) {
            path = StringUtils.removeEnd(path, "/");
        }
        return StringUtils.substringAfterLast(path, "/");
    }
}
 
Example #7
Source File: WebDavServiceImpl.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
protected String getUrl(DavResource resource, String profileId, WebDavProfile profile) {
    String relativePath = StringUtils.removeFirst(resource.getPath(), URI.create(profile.getBaseUrl()).getPath());
    if(resource.isDirectory()) {
        return relativePath;
    } else {
        return getRemoteAssetUrl(profileId, relativePath);
    }
}
 
Example #8
Source File: WebDavServiceImpl.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
protected String getName(DavResource resource) {
    if(StringUtils.isNotEmpty(resource.getDisplayName())) {
        return resource.getDisplayName();
    } else {
        String path = resource.getPath();
        if(resource.isDirectory()) {
            path = StringUtils.removeEnd(path, "/");
        }
        return StringUtils.substringAfterLast(path, "/");
    }
}
 
Example #9
Source File: WebDavServiceImpl.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
protected String getUrl(DavResource resource, String baseUrl, String deliveryUrl, String basePath) {
    String relativePath = StringUtils.removeFirst(resource.getPath(), basePath);
    if(resource.isDirectory()) {
        return baseUrl + relativePath;
    } else {
        return (StringUtils.isNotEmpty(deliveryUrl)? deliveryUrl : baseUrl) + relativePath;
    }
}
 
Example #10
Source File: WebdavClientImpl.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Execute PROPFIND with depth 0.
 * 
 * @param url
 * @return
 * @throws IOException
 */
// not used now, but may be useful for file operations
@Override
public DavResource info(String url) throws IOException {
	HttpPropFind entity = new HttpPropFind(url);
	entity.setDepth("0");
	Propfind body = new Propfind();
	body.setAllprop(new Allprop());
	entity.setEntity(new StringEntity(SardineUtil.toXml(body), UTF_8));
	
	Multistatus multistatus = execute(entity, new MultiStatusResponseHandler() {

		@Override
		public Multistatus handleResponse(HttpResponse response) throws SardineException, IOException {
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == HttpStatus.SC_NOT_FOUND)
			{
				return null; // expected response, do not throw an exception
			}
			return super.handleResponse(response);
		}
		
	});
	
	if (multistatus == null) {
		return null;
	}
	
	List<Response> responses = multistatus.getResponse();
	List<DavResource> resources = new ArrayList<DavResource>(responses.size());
	for (Response resp : responses) {
		try {
			resources.add(new DavResource(resp));
		} catch (URISyntaxException e) {
			// Ignore resource with invalid URI
		}
	}
	return !resources.isEmpty() ? resources.get(0) : null;
}
 
Example #11
Source File: CSVParser.java    From substitution-schedule-parser with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public SubstitutionSchedule getSubstitutionSchedule() throws IOException, JSONException,
        CredentialInvalidException {
    String url = data.getString(PARAM_URL);
    SubstitutionSchedule schedule = SubstitutionSchedule.fromData(scheduleData);

    if (url.startsWith("webdav")) {
        UserPasswordCredential credential = (UserPasswordCredential) this.credential;
        try {
            Sardine client = getWebdavClient(credential);
            String httpUrl = url.replaceFirst("webdav", "http");
            List<DavResource> files = client.list(httpUrl);
            for (DavResource file : files) {
                if (!file.isDirectory() && !file.getName().startsWith(".")) {
                    LocalDateTime modified = new LocalDateTime(file.getModified());
                    if (schedule.getLastChange() == null || schedule.getLastChange().isBefore(modified)) {
                        schedule.setLastChange(modified);
                    }
                    InputStream stream = client.get(new URI(httpUrl).resolve(file.getHref()).toString());
                    parseCSV(IOUtils.toString(stream, "UTF-8"), schedule);
                }
            }
            return schedule;
        } catch (GeneralSecurityException | URISyntaxException e) {
            throw new IOException(e);
        }
    } else {
        new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
        String response = httpGet(url);
        return parseCSV(response, schedule);
    }
}
 
Example #12
Source File: BrickTimestampFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
protected List<Element> getCustomProperties(final DavResource resource, final Long modified) {
    final List<Element> props = new ArrayList<>();
    final Element element = SardineUtil.createElement(LAST_MODIFIED_WIN32_CUSTOM_NAMESPACE);
    element.setTextContent(new RFC1123DateFormatter().format(modified, TimeZone.getTimeZone("GMT")));
    props.add(element);
    return props;
}
 
Example #13
Source File: DAVAttributesFinderFeatureTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCustomModified_Modified() {
    final DAVAttributesFinderFeature f = new DAVAttributesFinderFeature(null);
    final DavResource mock = mock(DavResource.class);

    Map<QName, String> map = new HashMap<>();
    map.put(DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE, "Mon, 29 Oct 2018 21:14:06 UTC");
    map.put(DAVTimestampFeature.LAST_MODIFIED_SERVER_CUSTOM_NAMESPACE, "Thu, 01 Nov 2018 15:31:57 UTC");
    final Date modified = new DateTime("2018-11-02T15:31:57Z").toDate();
    when(mock.getModified()).thenReturn(modified);
    when(mock.getCustomPropsNS()).thenReturn(map);

    final PathAttributes attrs = f.toAttributes(mock);
    assertEquals(modified.getTime(), attrs.getModificationDate());
}
 
Example #14
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 #15
Source File: DAVAttributesFinderFeatureTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCustomModified_NotModified() throws Exception {
    final DAVAttributesFinderFeature f = new DAVAttributesFinderFeature(null);
    final DavResource mock = mock(DavResource.class);

    Map<QName, String> map = new HashMap<>();
    final String ts = "Mon, 29 Oct 2018 21:14:06 UTC";
    map.put(DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE, ts);
    map.put(DAVTimestampFeature.LAST_MODIFIED_SERVER_CUSTOM_NAMESPACE, "Thu, 01 Nov 2018 15:31:57 UTC");
    when(mock.getModified()).thenReturn(new DateTime("2018-11-01T15:31:57Z").toDate());
    when(mock.getCustomPropsNS()).thenReturn(map);

    final PathAttributes attrs = f.toAttributes(mock);
    assertEquals(new RFC1123DateFormatter().parse(ts).getTime(), attrs.getModificationDate());
}
 
Example #16
Source File: MicrosoftIISDAVAttributesFinderFeatureTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCustomModified_PropertyNotAvailable() {
    final MicrosoftIISDAVAttributesFinderFeature f = new MicrosoftIISDAVAttributesFinderFeature(null);
    final DavResource mock = mock(DavResource.class);

    Map<QName, String> map = new HashMap<>();
    final Date modified = new DateTime("2018-11-01T15:31:57Z").toDate();
    when(mock.getModified()).thenReturn(modified);
    when(mock.getCustomPropsNS()).thenReturn(map);

    final PathAttributes attrs = f.toAttributes(mock);
    assertEquals(modified.getTime(), attrs.getModificationDate());
}
 
Example #17
Source File: MicrosoftIISDAVAttributesFinderFeatureTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCustomModified_PropertyAvailable() throws Exception {
    final MicrosoftIISDAVAttributesFinderFeature f = new MicrosoftIISDAVAttributesFinderFeature(null);
    final DavResource mock = mock(DavResource.class);

    Map<QName, String> map = new HashMap<>();
    final String ts = "Mon, 29 Oct 2018 21:14:06 GMT";
    map.put(MicrosoftIISDAVTimestampFeature.LAST_MODIFIED_WIN32_CUSTOM_NAMESPACE, ts);
    when(mock.getModified()).thenReturn(new DateTime("2018-11-01T15:31:57Z").toDate());
    when(mock.getCustomPropsNS()).thenReturn(map);

    final PathAttributes attrs = f.toAttributes(mock);
    assertEquals(new RFC1123DateFormatter().parse(ts).getTime(), attrs.getModificationDate());
}
 
Example #18
Source File: DAVListService.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
protected List<DavResource> list(final Path directory) throws IOException {
    return session.getClient().list(new DAVPathEncoder().encode(directory), 1,
        Stream.of(
            DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE,
            DAVTimestampFeature.LAST_MODIFIED_SERVER_CUSTOM_NAMESPACE).
            collect(Collectors.toSet()));
}
 
Example #19
Source File: DAVAttributesFinderFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
protected List<DavResource> list(final Path file) throws IOException {
    return session.getClient().list(new DAVPathEncoder().encode(file), 0,
        Stream.of(
            DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE,
            DAVTimestampFeature.LAST_MODIFIED_SERVER_CUSTOM_NAMESPACE).
            collect(Collectors.toSet())
    );
}
 
Example #20
Source File: MicrosoftIISDAVTimestampFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
protected List<Element> getCustomProperties(final DavResource resource, final Long modified) {
    final List<Element> props = new ArrayList<>();
    final Element element = SardineUtil.createElement(LAST_MODIFIED_WIN32_CUSTOM_NAMESPACE);
    element.setTextContent(new RFC1123DateFormatter().format(modified, TimeZone.getTimeZone("GMT")));
    props.add(element);
    return props;
}
 
Example #21
Source File: MicrosoftIISDAVAttributesFinderFeature.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
protected PathAttributes toAttributes(final DavResource resource) {
    final PathAttributes attributes = super.toAttributes(resource);
    final Map<QName, String> properties = resource.getCustomPropsNS();
    if(null != properties && properties.containsKey(MicrosoftIISDAVTimestampFeature.LAST_MODIFIED_WIN32_CUSTOM_NAMESPACE)) {
        final String value = properties.get(MicrosoftIISDAVTimestampFeature.LAST_MODIFIED_WIN32_CUSTOM_NAMESPACE);
        if(StringUtils.isNotBlank(value)) {
            try {
                attributes.setModificationDate(rfc1123.parse(value).getTime());
            }
            catch(InvalidDateException e) {
                log.warn(String.format("Failure parsing property %s with value %s", MicrosoftIISDAVTimestampFeature.LAST_MODIFIED_WIN32_CUSTOM_NAMESPACE, value));
                if(resource.getModified() != null) {
                    attributes.setModificationDate(resource.getModified().getTime());
                }
            }
        }
        else {
            if(log.isDebugEnabled()) {
                log.debug(String.format("Missing value for property %s", MicrosoftIISDAVTimestampFeature.LAST_MODIFIED_WIN32_CUSTOM_NAMESPACE));
            }
            if(resource.getModified() != null) {
                attributes.setModificationDate(resource.getModified().getTime());
            }
        }
    }
    return attributes;
}
 
Example #22
Source File: DAVAttributesFinderFeature.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
protected PathAttributes toAttributes(final DavResource resource) {
    final PathAttributes attributes = new PathAttributes();
    final Map<QName, String> properties = resource.getCustomPropsNS();
    if(null != properties && properties.containsKey(DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE)) {
        final String value = properties.get(DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE);
        if(StringUtils.isNotBlank(value)) {
            try {
                if(properties.containsKey(DAVTimestampFeature.LAST_MODIFIED_SERVER_CUSTOM_NAMESPACE)) {
                    final String svalue = properties.get(DAVTimestampFeature.LAST_MODIFIED_SERVER_CUSTOM_NAMESPACE);
                    if(StringUtils.isNotBlank(svalue)) {
                        final Date server = rfc1123.parse(svalue);
                        if(server.equals(resource.getModified())) {
                            // file not touched with a different client
                            attributes.setModificationDate(
                                rfc1123.parse(value).getTime());
                        }
                        else {
                            // file touched with a different client, use default modified date from server
                            if(resource.getModified() != null) {
                                attributes.setModificationDate(resource.getModified().getTime());
                            }
                        }
                    }
                    else {
                        if(log.isDebugEnabled()) {
                            log.debug(String.format("Missing value for property %s", DAVTimestampFeature.LAST_MODIFIED_SERVER_CUSTOM_NAMESPACE));
                        }
                        if(resource.getModified() != null) {
                            attributes.setModificationDate(resource.getModified().getTime());
                        }
                    }
                }
                else {
                    attributes.setModificationDate(
                        rfc1123.parse(value).getTime());
                }
            }
            catch(InvalidDateException e) {
                log.warn(String.format("Failure parsing property %s with value %s", DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE, value));
                if(resource.getModified() != null) {
                    attributes.setModificationDate(resource.getModified().getTime());
                }
            }
        }
        else {
            if(log.isDebugEnabled()) {
                log.debug(String.format("Missing value for property %s", DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE));
            }
            if(resource.getModified() != null) {
                attributes.setModificationDate(resource.getModified().getTime());
            }
        }
    }
    else if(resource.getModified() != null) {
        attributes.setModificationDate(resource.getModified().getTime());
    }
    if(resource.getCreation() != null) {
        attributes.setCreationDate(resource.getCreation().getTime());
    }
    if(resource.getContentLength() != null) {
        attributes.setSize(resource.getContentLength());
    }
    if(StringUtils.isNotBlank(resource.getEtag())) {
        attributes.setETag(resource.getEtag());
    }
    if(StringUtils.isNotBlank(resource.getDisplayName())) {
        attributes.setDisplayname(resource.getDisplayName());
    }
    if(StringUtils.isNotBlank(resource.getDisplayName())) {
        attributes.setDisplayname(resource.getDisplayName());
    }
    attributes.setLockId(resource.getLockToken());
    return attributes;
}
 
Example #23
Source File: MicrosoftIISDAVAttributesFinderFeature.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
protected List<DavResource> list(final Path file) throws IOException {
    return session.getClient().list(new DAVPathEncoder().encode(file), 0, true);
}
 
Example #24
Source File: MicrosoftIISDAVListService.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected List<DavResource> list(final Path directory) throws IOException {
    return session.getClient().list(new DAVPathEncoder().encode(directory), 1, true);
}
 
Example #25
Source File: WebdavClient.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Execute PROPFIND with depth 0.
 * 
 * @param url
 * @return
 * @throws IOException
 */
public DavResource info(String url) throws IOException;