Skip to content

Commit

Permalink
Merge pull request #32 from col-panic/master
Browse files Browse the repository at this point in the history
#31 clean app shutdown / support for subdirectory installations
  • Loading branch information
a-schild authored Feb 24, 2020
2 parents 036c668 + a25c072 commit 5920cbd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
46 changes: 46 additions & 0 deletions src/main/java/org/aarboard/nextcloud/api/NextcloudConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand All @@ -35,6 +37,7 @@
import org.aarboard.nextcloud.api.provisioning.UserData;
import org.aarboard.nextcloud.api.provisioning.UserXMLAnswer;
import org.aarboard.nextcloud.api.provisioning.UsersXMLAnswer;
import org.aarboard.nextcloud.api.utils.ConnectorCommon;
import org.aarboard.nextcloud.api.utils.ListXMLAnswer;
import org.aarboard.nextcloud.api.utils.XMLAnswer;
import org.aarboard.nextcloud.api.webdav.Files;
Expand Down Expand Up @@ -65,6 +68,37 @@ public NextcloudConnector(String serverName, boolean useHTTPS, int port, String
fl= new Files(_serverConfig);
}

/**
* @param serviceUrl url of the nextcloud instance, e.g. https://nextcloud.instance.com:8443/cloud
* @param userName User for login
* @param password Password for login
*/
public NextcloudConnector(String serviceUrl, String userName, String password){
try {
URL _serviceUrl = new URL(serviceUrl);
boolean useHTTPS = serviceUrl.startsWith("https");
_serverConfig = new ServerConfig(_serviceUrl.getHost(), useHTTPS, _serviceUrl.getPort(),
userName, password);
if(!_serviceUrl.getPath().isEmpty()) {
_serverConfig.setSubpathPrefix(_serviceUrl.getPath());
}
pc = new ProvisionConnector(_serverConfig);
fc = new FilesharingConnector(_serverConfig);
fd = new Folders(_serverConfig);
fl = new Files(_serverConfig);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}

/**
* Close the HTTP client. Perform this to cleanly shut down this application.
* @throws IOException
*/
public void shutdown() throws IOException{
ConnectorCommon.shutdown();
}

/**
* Trust all HTTPS certificates presented by the server. This is e.g. used to work against a
* Nextcloud instance with a self-signed certificate.
Expand All @@ -74,6 +108,16 @@ public NextcloudConnector(String serverName, boolean useHTTPS, int port, String
public void trustAllCertificates(boolean trustAllCertificates){
_serverConfig.setTrustAllCertificates(trustAllCertificates);
}

/**
* Subpath prefix to the Nextcloud service (if applicable). This is the case if the Nextcloud
* installation is hosted within a subdirectory.
*
* @param subpathPrefix
*/
public void setSubpathPrefix(String subpathPrefix){
_serverConfig.setSubpathPrefix(subpathPrefix);
}

/**
* Creates a user
Expand Down Expand Up @@ -882,4 +926,6 @@ public void downloadFolder(String remotepath, String downloadpath) throws IOExce
fd.downloadFolder(remotepath, downloadpath);
}



}
29 changes: 23 additions & 6 deletions src/main/java/org/aarboard/nextcloud/api/ServerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ServerConfig {
private String userName;
private String password;
private String serverName;
private String subpathPrefix;
private boolean useHTTPS;
private int port;
private boolean trustAllCertificates;
Expand All @@ -34,6 +35,7 @@ public ServerConfig(String serverName, boolean useHTTPS, int port,
this.userName = userName;
this.password = password;
this.serverName = serverName;
this.subpathPrefix = null;
this.useHTTPS = useHTTPS;
this.port = port;
this.trustAllCertificates = false;
Expand Down Expand Up @@ -74,12 +76,27 @@ public String getServerName() {
return serverName;
}

/**
* @param serverName the serverName to set
*/
public void setServerName(String serverName) {
this.serverName = serverName;
}
/**
* @param serverName
* the serverName to set, defaults to <code>null</code>
*/
public void setServerName(String serverName){
this.serverName = serverName;
}

/**
* @return the configured subpath prefix
*/
public String getSubpathPrefix(){
return subpathPrefix;
}

/**
* @param subpathPrefix to apply
*/
public void setSubpathPrefix(String subpathPrefix){
this.subpathPrefix = subpathPrefix;
}

/**
* @return the useHTTPS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public <R> CompletableFuture<R> executeDelete(String part1, String part2, List<N

private URI buildUrl(String subPath, List<NameValuePair> queryParams)
{
if(serverConfig.getSubpathPrefix()!=null) {
subPath = serverConfig.getSubpathPrefix()+"/"+subPath;
}

URIBuilder uB= new URIBuilder()
.setScheme(serverConfig.isUseHTTPS() ? "https" : "http")
.setHost(serverConfig.getServerName())
Expand Down Expand Up @@ -242,4 +246,14 @@ public interface ResponseParser<R>
{
public R parseResponse(Reader reader);
}

/**
* Close the http client. Required for clean shutdown.
* @throws IOException
*/
public static void shutdown() throws IOException{
if(HttpAsyncClientSingleton.HTTPC_CLIENT != null) {
HttpAsyncClientSingleton.getInstance(null).close();
}
}
}

0 comments on commit 5920cbd

Please sign in to comment.