SUPPORT-8381: add webdav auth

This commit is contained in:
Alexandr Shalaginov 2024-07-19 14:19:49 +03:00
parent 519a6f4f74
commit 9a79327fea

View file

@ -1,6 +1,8 @@
package ervu.client.fileupload;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
@ -21,13 +23,26 @@ public class EmployeeInfoWebDavClient {
@Value("${file.webdav.upload.url:http://localhost:5757}")
private String url;
@Value("${file.webdav.upload.username:#{null}}")
private String username;
@Value("${file.webdav.upload.password:#{null}}")
private String password;
public boolean webDavUploadFile(String filename, MultipartFile multipartFile) {
try {
HttpClient httpClient = HttpClient.newBuilder().build();
HttpClient.Builder httpClientBuilder = HttpClient.newBuilder();
if (username != null && password != null) {
httpClientBuilder.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
}
HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(this.url + "/" + filename))
.PUT(HttpRequest.BodyPublishers.ofByteArray(multipartFile.getBytes())).build();
HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response = httpClientBuilder.build()
.send(httpRequest, HttpResponse.BodyHandlers.ofString());
return (response.statusCode() >= 200) && (response.statusCode() <= 202);
}
catch (IOException | InterruptedException e) {