Skip to content

Commit

Permalink
Review URL handling for OCS.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Apr 28, 2024
1 parent d50c379 commit 67e6dab
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import ch.cyberduck.core.ListProgressListener;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathAttributes;
import ch.cyberduck.core.URIEncoder;
import ch.cyberduck.core.dav.DAVAttributesFinderFeature;
import ch.cyberduck.core.dav.DAVPathEncoder;
import ch.cyberduck.core.dav.DAVSession;
import ch.cyberduck.core.dav.DAVTimestampFeature;
import ch.cyberduck.core.exception.BackgroundException;
Expand Down Expand Up @@ -92,17 +92,17 @@ protected PathAttributes head(final Path file) {
}

@Override
protected List<DavResource> list(final Path file) throws IOException {
final String url;
protected List<DavResource> list(final Path file) throws IOException, BackgroundException {
final String path;
if(StringUtils.isNotBlank(file.attributes().getVersionId())) {
url = String.format("%sversions/%s/%s",
new DAVPathEncoder().encode(new NextcloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions)),
path = String.format("%s/versions/%s/%s",
new NextcloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions).getAbsolute(),
file.attributes().getFileId(), file.attributes().getVersionId());
}
else {
url = new DAVPathEncoder().encode(file);
path = file.getAbsolute();
}
return session.getClient().list(url, 0,
return session.getClient().list(URIEncoder.encode(path), 0,
Stream.of(OC_FILEID_CUSTOM_NAMESPACE, OC_CHECKSUMS_CUSTOM_NAMESPACE, OC_SIZE_CUSTOM_NAMESPACE,
DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE,
DAVTimestampFeature.LAST_MODIFIED_SERVER_CUSTOM_NAMESPACE).collect(Collectors.toSet()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
*/

import ch.cyberduck.core.Path;
import ch.cyberduck.core.dav.DAVPathEncoder;
import ch.cyberduck.core.URIEncoder;
import ch.cyberduck.core.dav.DAVReadFeature;
import ch.cyberduck.core.dav.DAVSession;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.transfer.TransferStatus;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -36,12 +37,12 @@ public NextcloudReadFeature(final DAVSession session) {
}

@Override
protected HttpRequestBase toRequest(final Path file, final TransferStatus status) {
protected HttpRequestBase toRequest(final Path file, final TransferStatus status) throws BackgroundException {
final HttpRequestBase request = super.toRequest(file, status);
if(StringUtils.isNotBlank(file.attributes().getVersionId())) {
request.setURI(URI.create(String.format("%sversions/%s/%s",
new DAVPathEncoder().encode(new NextcloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions)),
file.attributes().getFileId(), file.attributes().getVersionId())));
request.setURI(URI.create(URIEncoder.encode(String.format("%s/versions/%s/%s",
new NextcloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions).getAbsolute(),
file.attributes().getFileId(), file.attributes().getVersionId()))));
}
return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import ch.cyberduck.core.ocs.OcsCapabilitiesRequest;
import ch.cyberduck.core.ocs.OcsCapabilitiesResponseHandler;
import ch.cyberduck.core.proxy.Proxy;
import ch.cyberduck.core.shared.DefaultPathHomeFeature;
import ch.cyberduck.core.shared.DelegatingHomeFeature;
import ch.cyberduck.core.shared.WorkdirHomeFeature;
import ch.cyberduck.core.ssl.X509KeyManager;
Expand Down Expand Up @@ -89,8 +88,7 @@ public void login(final Proxy proxy, final LoginCallback prompt, final CancelCal
@SuppressWarnings("unchecked")
public <T> T _getFeature(final Class<T> type) {
if(type == Home.class) {
return (T) new DelegatingHomeFeature(new WorkdirHomeFeature(host), new DefaultPathHomeFeature(host),
new NextcloudHomeFeature(host));
return (T) new DelegatingHomeFeature(new WorkdirHomeFeature(host), new NextcloudHomeFeature(host));
}
if(type == ListService.class) {
return (T) new NextcloudListService(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import ch.cyberduck.core.Path;
import ch.cyberduck.core.dav.DAVTimestampFeature;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.NotfoundException;

import java.io.IOException;
Expand All @@ -34,7 +35,7 @@ public NextcloudTimestampFeature(final NextcloudSession session) {
}

@Override
protected DavResource getResource(final Path file) throws NotfoundException, IOException {
protected DavResource getResource(final Path file) throws BackgroundException, IOException {
final Optional<DavResource> optional = new NextcloudAttributesFinderFeature(session).list(file).stream().findFirst();
if(!optional.isPresent()) {
throw new NotfoundException(file.getAbsolute());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathAttributes;
import ch.cyberduck.core.PathNormalizer;
import ch.cyberduck.core.URIEncoder;
import ch.cyberduck.core.VersioningConfiguration;
import ch.cyberduck.core.dav.DAVExceptionMappingService;
import ch.cyberduck.core.dav.DAVPathEncoder;
import ch.cyberduck.core.dav.DAVSession;
import ch.cyberduck.core.dav.DAVTimestampFeature;
import ch.cyberduck.core.exception.BackgroundException;
Expand Down Expand Up @@ -72,10 +72,12 @@ public void setConfiguration(final Path container, final PasswordCallback prompt
public void revert(final Path file) throws BackgroundException {
// To restore a version all that needs to be done is to move a version the special restore folder at /remote.php/dav/versions/USER/restore
try {
session.getClient().move(String.format("%sversions/%s/%s",
new DAVPathEncoder().encode(new NextcloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions)),
file.attributes().getFileId(), file.attributes().getVersionId()),
String.format("%srestore/target", new DAVPathEncoder().encode(new NextcloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions))));
session.getClient().move(URIEncoder.encode(String.format("%s/versions/%s/%s",
new NextcloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions).getAbsolute(),
file.attributes().getFileId(), file.attributes().getVersionId())),
URIEncoder.encode(String.format("%s/restore/target",
new NextcloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions).getAbsolute()))
);
}
catch(SardineException e) {
throw new DAVExceptionMappingService().map("Cannot revert file", e, file);
Expand Down Expand Up @@ -149,9 +151,8 @@ protected boolean filter(final Path file, final DavResource resource) {
return true;
}

protected List<DavResource> propfind(final Path file, final Propfind body) throws IOException {
return session.getClient().propfind(String.format("%sversions/%s",
new DAVPathEncoder().encode(new NextcloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions)),
file.attributes().getFileId()), 1, body);
protected List<DavResource> propfind(final Path file, final Propfind body) throws IOException, BackgroundException {
return session.getClient().propfind(URIEncoder.encode(String.format("%s/versions/%s",
new NextcloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions).getAbsolute(), file.attributes().getFileId())), 1, body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class NextcloudHomeFeatureTest {

@Test
public void testFind() {
public void testFind() throws Exception {
final Host bookmark = new Host(new NextcloudProtocol());
final NextcloudHomeFeature feature = new NextcloudHomeFeature(bookmark);
assertNull(feature.find());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public class NextcloudShareFeatureTest extends AbstractNextcloudTest {

@Test
public void testIsSupported() {
public void testIsSupported() throws Exception {
final Path home = new NextcloudHomeFeature(session.getHost()).find();
assertTrue(new NextcloudShareFeature(session).isSupported(home, Share.Type.download));
assertTrue(new NextcloudShareFeature(session).isSupported(home, Share.Type.upload));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
*/

import ch.cyberduck.core.Path;
import ch.cyberduck.core.dav.DAVPathEncoder;
import ch.cyberduck.core.URIEncoder;
import ch.cyberduck.core.dav.DAVSession;
import ch.cyberduck.core.dav.DAVTimestampFeature;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.nextcloud.NextcloudAttributesFinderFeature;
import ch.cyberduck.core.nextcloud.NextcloudHomeFeature;

Expand All @@ -41,17 +42,17 @@ public OwncloudAttributesFinderFeature(DAVSession session) {
}

@Override
protected List<DavResource> list(final Path file) throws IOException {
final String url;
protected List<DavResource> list(final Path file) throws IOException, BackgroundException {
final String path;
if(StringUtils.isNotBlank(file.attributes().getVersionId())) {
url = String.format("%s/%s/v/%s",
path = String.format("%s/%s/v/%s",
new OwncloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions).getAbsolute(),
file.attributes().getFileId(), file.attributes().getVersionId());
}
else {
url = new DAVPathEncoder().encode(file);
path = file.getAbsolute();
}
return session.getClient().list(url, 0,
return session.getClient().list(URIEncoder.encode(path), 0,
Stream.of(OC_FILEID_CUSTOM_NAMESPACE, OC_CHECKSUMS_CUSTOM_NAMESPACE, OC_SIZE_CUSTOM_NAMESPACE,
DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE,
DAVTimestampFeature.LAST_MODIFIED_SERVER_CUSTOM_NAMESPACE).collect(Collectors.toSet()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,34 @@

import ch.cyberduck.core.Host;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.features.Home;
import ch.cyberduck.core.nextcloud.NextcloudHomeFeature;
import ch.cyberduck.core.preferences.HostPreferences;
import ch.cyberduck.core.shared.DefaultPathHomeFeature;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.EnumSet;

public class OwncloudHomeFeature extends NextcloudHomeFeature {
private static final Logger log = LogManager.getLogger(OwncloudHomeFeature.class);

private final String root;

public OwncloudHomeFeature(final Host bookmark) {
this(bookmark, new HostPreferences(bookmark).getProperty("owncloud.root.default"));
this(new DefaultPathHomeFeature(bookmark), bookmark);
}

public OwncloudHomeFeature(final Home delegate, final Host bookmark) {
this(delegate, bookmark, new HostPreferences(bookmark).getProperty("owncloud.root.default"));
}

public OwncloudHomeFeature(final Host bookmark, final String root) {
super(bookmark, root);
this.root = root;
public OwncloudHomeFeature(final Home delegate, final Host bookmark, final String root) {
super(delegate, bookmark, root);
}

public Path find(final Context context) {
public Path find(final Context context) throws BackgroundException {
switch(context) {
case versions:
final Path workdir = new Path(String.format("%s/meta", root), EnumSet.of(Path.Type.directory));
if(log.isDebugEnabled()) {
log.debug(String.format("Use home directory %s", workdir));
}
return workdir;
return super.find(Context.meta);
}
return super.find(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
*/

import ch.cyberduck.core.Path;
import ch.cyberduck.core.URIEncoder;
import ch.cyberduck.core.dav.DAVReadFeature;
import ch.cyberduck.core.dav.DAVSession;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.nextcloud.NextcloudHomeFeature;
import ch.cyberduck.core.transfer.TransferStatus;

Expand All @@ -36,12 +38,12 @@ public OwncloudReadFeature(final DAVSession session) {
}

@Override
protected HttpRequestBase toRequest(final Path file, final TransferStatus status) {
protected HttpRequestBase toRequest(final Path file, final TransferStatus status) throws BackgroundException {
final HttpRequestBase request = super.toRequest(file, status);
if(StringUtils.isNotBlank(file.attributes().getVersionId())) {
request.setURI(URI.create(String.format("%s/%s/v/%s",
request.setURI(URI.create(URIEncoder.encode(String.format("%s/%s/v/%s",
new OwncloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions).getAbsolute(),
file.attributes().getFileId(), file.attributes().getVersionId())));
file.attributes().getFileId(), file.attributes().getVersionId()))));
}
return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import ch.cyberduck.core.ocs.OcsCapabilitiesRequest;
import ch.cyberduck.core.ocs.OcsCapabilitiesResponseHandler;
import ch.cyberduck.core.proxy.Proxy;
import ch.cyberduck.core.shared.DefaultPathHomeFeature;
import ch.cyberduck.core.shared.DelegatingHomeFeature;
import ch.cyberduck.core.shared.WorkdirHomeFeature;
import ch.cyberduck.core.ssl.X509KeyManager;
Expand Down Expand Up @@ -156,8 +155,7 @@ public void login(final Proxy proxy, final LoginCallback prompt, final CancelCal
@SuppressWarnings("unchecked")
public <T> T _getFeature(final Class<T> type) {
if(type == Home.class) {
return (T) new DelegatingHomeFeature(new WorkdirHomeFeature(host), new DefaultPathHomeFeature(host),
new OwncloudHomeFeature(host));
return (T) new DelegatingHomeFeature(new WorkdirHomeFeature(host), new OwncloudHomeFeature(host));
}
if(type == ListService.class) {
return (T) new NextcloudListService(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import ch.cyberduck.core.Path;
import ch.cyberduck.core.dav.DAVTimestampFeature;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.NotfoundException;

import java.io.IOException;
Expand All @@ -34,7 +35,7 @@ public OwncloudTimestampFeature(final OwncloudSession session) {
}

@Override
protected DavResource getResource(final Path file) throws NotfoundException, IOException {
protected DavResource getResource(final Path file) throws BackgroundException, IOException {
final Optional<DavResource> optional = new OwncloudAttributesFinderFeature(session).list(file).stream().findFirst();
if(!optional.isPresent()) {
throw new NotfoundException(file.getAbsolute());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathNormalizer;
import ch.cyberduck.core.URIEncoder;
import ch.cyberduck.core.dav.DAVExceptionMappingService;
import ch.cyberduck.core.dav.DAVPathEncoder;
import ch.cyberduck.core.dav.DAVSession;
Expand Down Expand Up @@ -68,9 +69,9 @@ protected boolean filter(final Path file, final DavResource resource) {
}

@Override
protected List<DavResource> propfind(final Path file, final Propfind body) throws IOException {
return session.getClient().propfind(String.format("%s/%s/v",
protected List<DavResource> propfind(final Path file, final Propfind body) throws IOException, BackgroundException {
return session.getClient().propfind(URIEncoder.encode(String.format("%s/%s/v",
new OwncloudHomeFeature(session.getHost()).find(NextcloudHomeFeature.Context.versions).getAbsolute(),
file.attributes().getFileId()), 1, body);
file.attributes().getFileId())), 1, body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected PathAttributes head(final Path file) throws IOException {
return attributes;
}

protected List<DavResource> list(final Path file) throws IOException {
protected List<DavResource> list(final Path file) throws IOException, BackgroundException {
return session.getClient().list(new DAVPathEncoder().encode(file), 0,
Stream.of(
DAVTimestampFeature.LAST_MODIFIED_CUSTOM_NAMESPACE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public InputStream read(final Path file, final TransferStatus status, final Conn
}
}

protected HttpRequestBase toRequest(final Path file, final TransferStatus status) {
protected HttpRequestBase toRequest(final Path file, final TransferStatus status) throws BackgroundException {
final StringBuilder resource = new StringBuilder(new DAVPathEncoder().encode(file));
if(!status.getParameters().isEmpty()) {
resource.append("?");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void setTimestamp(final Path file, final TransferStatus status) throws Ba
* @param file File
* @return Latest properties
*/
protected DavResource getResource(final Path file) throws NotfoundException, IOException {
protected DavResource getResource(final Path file) throws BackgroundException, IOException {
final Optional<DavResource> optional = new DAVAttributesFinderFeature(session).list(file).stream().findFirst();
if(!optional.isPresent()) {
throw new NotfoundException(file.getAbsolute());
Expand Down

0 comments on commit 67e6dab

Please sign in to comment.