Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add webdav path resolver composite #52

Merged
merged 4 commits into from
Oct 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,483 changes: 2,309 additions & 1,174 deletions src/main/java/org/aarboard/nextcloud/api/NextcloudConnector.java

Large diffs are not rendered by default.

137 changes: 116 additions & 21 deletions src/main/java/org/aarboard/nextcloud/api/webdav/AWebdavHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@

import com.github.sardine.Sardine;
import com.github.sardine.SardineFactory;
import java.io.BufferedReader;
import com.github.sardine.impl.SardineImpl;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.aarboard.nextcloud.api.ServerConfig;
import org.aarboard.nextcloud.api.exception.NextcloudApiException;
import org.aarboard.nextcloud.api.webdav.pathresolver.NextcloudVersion;
import org.aarboard.nextcloud.api.webdav.pathresolver.WebDavPathResolver;
import org.aarboard.nextcloud.api.webdav.pathresolver.WebDavPathResolverBuilder;
import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,35 +39,116 @@
*
* @author a.schild
*/
public abstract class AWebdavHandler {
public abstract class AWebdavHandler
{

private static final Logger LOG = LoggerFactory.getLogger(AWebdavHandler.class);

public static final int FILE_BUFFER_SIZE= 4096;
public static String WEB_DAV_BASE_PATH = "remote.php/webdav/";

private final ServerConfig _serverConfig;

public AWebdavHandler(ServerConfig serverConfig) {
private WebDavPathResolver resolver;

private String nextcloudServerVersion;

public AWebdavHandler(ServerConfig serverConfig)
{
_serverConfig = serverConfig;
}


public void setWebDavPathResolver(final WebDavPathResolver resolver)
{
this.resolver = resolver;
}

/**
* @return the nextcloud server instance version
*/
public String getServerVersion()
{
if (null == nextcloudServerVersion)
{
resolveNextcloudServerVersion();
}

return nextcloudServerVersion;
}

private void resolveNextcloudServerVersion()
{
final WebDavPathResolver versionResolver = WebDavPathResolverBuilder.get(WebDavPathResolverBuilder.TYPE.VERSION).withBasePathPrefix(_serverConfig.getSubPathPrefix()).build();

final String url = buildWebdavPath(versionResolver, "");
final Sardine sardine = buildAuthSardine();

try (final InputStream inputStream = sardine.get(url))
{
final String json = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
.lines().collect(Collectors.joining("\n"));

//TODO parse with proper json api
nextcloudServerVersion = Arrays.asList(json.split(",")).stream().filter(x -> x.contains("version")).map(x -> x.split(":")[1]).findAny().orElse("20.0").replaceAll("\"", "");

}
catch (IOException ex)
{
throw new NextcloudApiException(ex);
}
finally
{
try
{
sardine.shutdown();
}
catch (IOException ex)
{
LOG.warn("error in closing sardine connector", ex);
}
}

}

/**
* Defaults to FILES resolver
*
* @return the resolver
* @since 11.5
*/
protected WebDavPathResolver getWebDavPathResolver()
{
if (null == this.resolver)
{
this.resolver = WebDavPathResolverBuilder.get(WebDavPathResolverBuilder.TYPE.FILES)//
.ofVersion(NextcloudVersion.get(getServerVersion()))
.withUserName(_serverConfig.getUserName())
.withBasePathSuffix("files")
.withBasePathPrefix(_serverConfig.getSubPathPrefix()).build();

}

return this.resolver;
}

/**
* Build the full URL for the webdav access to a resource
*
*
* @param remotePath remote path for file (Not including remote.php/webdav/)
* @return Full URL including http....
*/
protected String buildWebdavPath(String remotePath)
{
URIBuilder uB= new URIBuilder()
.setScheme(_serverConfig.isUseHTTPS() ? "https" : "http")
.setHost(_serverConfig.getServerName())
.setPort(_serverConfig.getPort())
.setPath(
_serverConfig.getSubPathPrefix() == null ?
WEB_DAV_BASE_PATH + (remotePath != null ? remotePath : "") :
_serverConfig.getSubPathPrefix()+ "/" + WEB_DAV_BASE_PATH + (remotePath != null ? remotePath : "")
);
return buildWebdavPath(getWebDavPathResolver(), remotePath);
}

protected String buildWebdavPath(WebDavPathResolver resolver, String remotePath)
{
URIBuilder uB = new URIBuilder()
.setScheme(_serverConfig.isUseHTTPS() ? "https" : "http")
.setHost(_serverConfig.getServerName())
.setPort(_serverConfig.getPort())
.setPath(resolver.getWebDavPath(remotePath));
return uB.toString();
}

Expand All @@ -71,7 +161,7 @@ protected String getWebdavPathPrefix()

/**
* Create a authenticate sardine connector
*
*
* @return sardine connector to server including authentication
*/
protected Sardine buildAuthSardine()
Expand All @@ -85,21 +175,23 @@ protected Sardine buildAuthSardine()
Sardine sardine = new SardineImpl(_serverConfig.getAuthenticationConfig().getBearerToken());
return sardine;
}

/**
* 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) {
public boolean pathExists(String remotePath)
{
String path = buildWebdavPath(remotePath);
Sardine sardine = buildAuthSardine();

try
{
return sardine.exists(path);
} catch (IOException e)
}
catch (IOException e)
{
throw new NextcloudApiException(e);
}
Expand All @@ -115,20 +207,23 @@ public boolean pathExists(String remotePath) {
}
}
}

/**
* Deletes the file/folder at the specified path
*
* @param remotePath path of the file/folder
*/
public void deletePath(String remotePath)
{
String path= buildWebdavPath( remotePath );
String path = buildWebdavPath(remotePath);

Sardine sardine = buildAuthSardine();
try {
try
{
sardine.delete(path);
} catch (IOException e) {
}
catch (IOException e)
{
throw new NextcloudApiException(e);
}
finally
Expand Down
Loading