Search in sources :

Example 6 with DavResource

use of com.github.sardine.DavResource in project cyberduck by iterate-ch.

the class DAVAttributesFinderFeatureTest method testCustomModified_NotModified.

@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());
}
Also used : DavResource(com.github.sardine.DavResource) HashMap(java.util.HashMap) RFC1123DateFormatter(ch.cyberduck.core.date.RFC1123DateFormatter) QName(javax.xml.namespace.QName) PathAttributes(ch.cyberduck.core.PathAttributes) DateTime(org.joda.time.DateTime) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 7 with DavResource

use of com.github.sardine.DavResource in project cyberduck by iterate-ch.

the class DAVAttributesFinderFeatureTest method testCustomModified_Epoch.

@Test
public void testCustomModified_Epoch() {
    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, "Thu, 01 Jan 1970 00:00:00 UTC");
    map.put(DAVTimestampFeature.LAST_MODIFIED_SERVER_CUSTOM_NAMESPACE, "Thu, 02 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());
}
Also used : DavResource(com.github.sardine.DavResource) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) PathAttributes(ch.cyberduck.core.PathAttributes) Date(java.util.Date) DateTime(org.joda.time.DateTime) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 8 with DavResource

use of com.github.sardine.DavResource in project cyberduck by iterate-ch.

the class DAVAttributesFinderFeatureTest method testCustomModified_Modified.

@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());
}
Also used : DavResource(com.github.sardine.DavResource) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) PathAttributes(ch.cyberduck.core.PathAttributes) Date(java.util.Date) DateTime(org.joda.time.DateTime) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 9 with DavResource

use of com.github.sardine.DavResource in project cyberduck by iterate-ch.

the class DAVAttributesFinderFeature method find.

@Override
public PathAttributes find(final Path file, final ListProgressListener listener) throws BackgroundException {
    if (file.isRoot()) {
        return PathAttributes.EMPTY;
    }
    try {
        try {
            for (final DavResource resource : this.list(file)) {
                if (resource.isDirectory()) {
                    if (!file.getType().contains(Path.Type.directory)) {
                        throw new NotfoundException(String.format("Path %s is directory", file.getAbsolute()));
                    }
                } else {
                    if (!file.getType().contains(Path.Type.file)) {
                        throw new NotfoundException(String.format("Path %s is file", file.getAbsolute()));
                    }
                }
                return this.toAttributes(resource);
            }
            throw new NotfoundException(file.getAbsolute());
        } catch (SardineException e) {
            try {
                throw new DAVExceptionMappingService().map("Failure to read attributes of {0}", e, file);
            } catch (InteroperabilityException i) {
                // PROPFIND Method not allowed
                log.warn(String.format("Failure with PROPFIND request for %s. %s", file, i.getMessage()));
                final Map<String, String> headers = session.getClient().execute(new HttpHead(new DAVPathEncoder().encode(file)), new HeadersResponseHandler());
                final PathAttributes attributes = new PathAttributes();
                try {
                    attributes.setModificationDate(rfc1123.parse(headers.get(HttpHeaders.LAST_MODIFIED)).getTime());
                } catch (InvalidDateException p) {
                    log.warn(String.format("%s is not RFC 1123 format %s", headers.get(HttpHeaders.LAST_MODIFIED), p.getMessage()));
                }
                if (!headers.containsKey(HttpHeaders.CONTENT_ENCODING)) {
                    // Set size unless response is compressed
                    attributes.setSize(NumberUtils.toLong(headers.get(HttpHeaders.CONTENT_LENGTH), -1));
                }
                if (headers.containsKey(HttpHeaders.ETAG)) {
                    attributes.setETag(headers.get(HttpHeaders.ETAG));
                // Setting checksum is disabled. See #8798
                // attributes.setChecksum(Checksum.parse(headers.get(HttpHeaders.ETAG)));
                }
                if (headers.containsKey(HttpHeaders.CONTENT_MD5)) {
                    attributes.setChecksum(Checksum.parse(headers.get(HttpHeaders.CONTENT_MD5)));
                }
                return attributes;
            }
        }
    } catch (SardineException e) {
        throw new DAVExceptionMappingService().map("Failure to read attributes of {0}", e, file);
    } catch (IOException e) {
        throw new HttpExceptionMappingService().map(e, file);
    }
}
Also used : NotfoundException(ch.cyberduck.core.exception.NotfoundException) DavResource(com.github.sardine.DavResource) InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) PathAttributes(ch.cyberduck.core.PathAttributes) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead) SardineException(com.github.sardine.impl.SardineException) HttpExceptionMappingService(ch.cyberduck.core.http.HttpExceptionMappingService) InvalidDateException(ch.cyberduck.core.date.InvalidDateException) HeadersResponseHandler(com.github.sardine.impl.handler.HeadersResponseHandler)

Example 10 with DavResource

use of com.github.sardine.DavResource in project cyberduck by iterate-ch.

the class DAVListService method list.

@Override
public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
    try {
        final AttributedList<Path> children = new AttributedList<Path>();
        for (final DavResource resource : this.list(directory)) {
            // Try to parse as RFC 2396
            final String href = PathNormalizer.normalize(resource.getHref().getPath(), true);
            if (href.equals(directory.getAbsolute())) {
                log.warn(String.format("Ignore resource %s", href));
                // Do not include self
                if (resource.isDirectory()) {
                    continue;
                }
                throw new NotfoundException(directory.getAbsolute());
            }
            final PathAttributes attr = attributes.toAttributes(resource);
            final Path file = new Path(directory, PathNormalizer.name(href), resource.isDirectory() ? EnumSet.of(Path.Type.directory) : EnumSet.of(Path.Type.file), attr);
            children.add(file);
            listener.chunk(directory, children);
        }
        return children;
    } catch (SardineException e) {
        throw new DAVExceptionMappingService().map("Listing directory {0} failed", e, directory);
    } catch (IOException e) {
        throw new HttpExceptionMappingService().map(e, directory);
    }
}
Also used : Path(ch.cyberduck.core.Path) NotfoundException(ch.cyberduck.core.exception.NotfoundException) SardineException(com.github.sardine.impl.SardineException) HttpExceptionMappingService(ch.cyberduck.core.http.HttpExceptionMappingService) DavResource(com.github.sardine.DavResource) AttributedList(ch.cyberduck.core.AttributedList) PathAttributes(ch.cyberduck.core.PathAttributes) IOException(java.io.IOException)

Aggregations

DavResource (com.github.sardine.DavResource)22 Test (org.junit.Test)11 PathAttributes (ch.cyberduck.core.PathAttributes)8 Date (java.util.Date)8 QName (javax.xml.namespace.QName)7 HashMap (java.util.HashMap)6 DateTime (org.joda.time.DateTime)6 IntegrationTest (ch.cyberduck.test.IntegrationTest)4 Sardine (com.github.sardine.Sardine)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 URL (java.net.URL)4 SardineException (com.github.sardine.impl.SardineException)3 RFC1123DateFormatter (ch.cyberduck.core.date.RFC1123DateFormatter)2 NotfoundException (ch.cyberduck.core.exception.NotfoundException)2 HttpExceptionMappingService (ch.cyberduck.core.http.HttpExceptionMappingService)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Charset (java.nio.charset.Charset)2 ArrayList (java.util.ArrayList)2