Skip to content

Commit 4015f0f

Browse files
change webdav cookie spec (#189)
* add Sardine factory to change cookie spec * remove unused functions
1 parent e9bdfb2 commit 4015f0f

File tree

1 file changed

+68
-41
lines changed

1 file changed

+68
-41
lines changed

DriveBackup/src/main/java/ratismal/drivebackup/uploaders/webdav/WebDAVUploader.java

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package ratismal.drivebackup.uploaders.webdav;
22

3-
import org.jetbrains.annotations.Contract;
43
import org.jetbrains.annotations.NotNull;
54
import ratismal.drivebackup.uploaders.Uploader;
6-
import ratismal.drivebackup.uploaders.Authenticator.AuthenticationProvider;
75
import ratismal.drivebackup.UploadThread.UploadLogger;
86
import ratismal.drivebackup.config.ConfigParser;
97
import ratismal.drivebackup.config.configSections.BackupMethods.WebDAVBackupMethod;
@@ -20,9 +18,76 @@
2018
import com.github.sardine.DavResource;
2119
import com.github.sardine.Sardine;
2220
import com.github.sardine.SardineFactory;
21+
import com.github.sardine.impl.SardineImpl;
22+
import org.apache.http.auth.AuthScope;
23+
import org.apache.http.auth.NTCredentials;
24+
import org.apache.http.auth.UsernamePasswordCredentials;
25+
import org.apache.http.client.CredentialsProvider;
26+
import org.apache.http.client.config.AuthSchemes;
27+
import org.apache.http.client.config.CookieSpecs;
28+
import org.apache.http.client.config.RequestConfig;
29+
import org.apache.http.impl.client.BasicCredentialsProvider;
30+
import org.apache.http.impl.client.HttpClientBuilder;
2331

2432
import static ratismal.drivebackup.config.Localization.intl;
2533

34+
/**
35+
* {@link SardineFactory} replacement to customize {@link HttpClientBuilder} while retaining {@link SardineImpl} settings
36+
*/
37+
class SardineCannery {
38+
39+
/**
40+
* Creates a new instance of {@link Sardine} with the given username and password, and standard cookie specs
41+
*
42+
* @param username Use in authentication header credentials
43+
* @param password Use in authentication header credentials
44+
* @return a new {@link Sardine} instance with the specified credentials, and standard cookie specs
45+
*/
46+
static Sardine make(String username, String password) {
47+
// helper class to get a builder with the same settings as SardineImpl, and standard cookie specs
48+
// because builder can't be extracted from SardineImpl due to being private
49+
class SardineCanneryHelper extends SardineImpl {
50+
private final HttpClientBuilder builder;
51+
52+
private SardineCanneryHelper(String username, String password) {
53+
super();
54+
this.builder = this.configure(null, this.createDefaultCredentialsProvider(username, password))
55+
.setDefaultRequestConfig(RequestConfig.custom()
56+
.setExpectContinueEnabled(false)
57+
// replicated settings from createDefaultCredentialsProvider above, new below
58+
.setCookieSpec(CookieSpecs.STANDARD).build());
59+
}
60+
61+
// sadly private in SardineImpl so copied here
62+
private CredentialsProvider createDefaultCredentialsProvider(String username, String password)
63+
{
64+
CredentialsProvider provider = new BasicCredentialsProvider();
65+
if (username != null)
66+
{
67+
provider.setCredentials(
68+
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthSchemes.NTLM),
69+
new NTCredentials(username, password, null, null));
70+
provider.setCredentials(
71+
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthSchemes.BASIC),
72+
new UsernamePasswordCredentials(username, password));
73+
provider.setCredentials(
74+
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthSchemes.DIGEST),
75+
new UsernamePasswordCredentials(username, password));
76+
provider.setCredentials(
77+
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthSchemes.SPNEGO),
78+
new NTCredentials(username, password, null, null));
79+
provider.setCredentials(
80+
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthSchemes.KERBEROS),
81+
new UsernamePasswordCredentials(username, password));
82+
}
83+
return provider;
84+
}
85+
}
86+
SardineCanneryHelper cannery = new SardineCanneryHelper(username, password);
87+
return new SardineImpl(cannery.builder);
88+
}
89+
}
90+
2691
public class WebDAVUploader extends Uploader {
2792

2893
Sardine sardine;
@@ -37,7 +102,7 @@ public WebDAVUploader(UploadLogger logger, WebDAVBackupMethod webdav) {
37102
this.logger = logger;
38103
try {
39104
_remoteBaseFolder = new URL(webdav.hostname + "/" + webdav.remoteDirectory);
40-
sardine = SardineFactory.begin(webdav.username, webdav.password);
105+
sardine = SardineCannery.make(webdav.username, webdav.password);
41106
sardine.enablePreemptiveAuthentication(_remoteBaseFolder.getHost());
42107
createDirectory(_remoteBaseFolder.toString());
43108
} catch (Exception e) {
@@ -108,30 +173,6 @@ public void uploadFile(File file, String type) {
108173
}
109174
}
110175

111-
/**
112-
* Returns a list of the paths of the files inside the specified folder and subfolders.
113-
* @param folderPath the path of the folder
114-
* @return the list of file paths
115-
*/
116-
public ArrayList<String> getFiles(String folderPath) {
117-
ArrayList<String> filePaths = new ArrayList<>();
118-
try {
119-
//TODO path
120-
List<DavResource> resources = sardine.list(new URL(_remoteBaseFolder + "/" + folderPath).toString());
121-
for (DavResource resource : resources) {
122-
if (resource.isDirectory()) {
123-
filePaths.addAll(prependToAll(getFiles(resource.getName()), new File(resource.getName()).getName() + '/'));
124-
} else {
125-
filePaths.add(resource.getName());
126-
}
127-
}
128-
} catch (Exception e) {
129-
MessageUtil.sendConsoleException(e);
130-
setErrorOccurred(true);
131-
}
132-
return filePaths;
133-
}
134-
135176
/**
136177
* Deletes the oldest files past the number to retain from the FTP server inside the specified folder for the file type.
137178
* <p>
@@ -203,18 +244,4 @@ private void createDirectory(String path) {
203244
//Sardine throws an error when the file exists instead of returning a boolean.
204245
}
205246
}
206-
207-
/**
208-
* Prepends the specified String to each element in the specified ArrayList.
209-
* @param list the ArrayList
210-
* @param string the String
211-
* @return the new ArrayList
212-
*/
213-
@Contract ("_, _ -> param1")
214-
private static ArrayList<String> prependToAll(@NotNull ArrayList<String> list, String string) {
215-
for (int i = 0; i < list.size(); i++) {
216-
list.set(i, string + list.get(i));
217-
}
218-
return list;
219-
}
220247
}

0 commit comments

Comments
 (0)