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

Update to the latest Vaadin 24.2 jmix-framework/jmix#2411 #2419

Merged
merged 2 commits into from
Oct 31, 2023
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
4 changes: 2 additions & 2 deletions jmix-bom/bom.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ publishing {

group = 'io.jmix.bom'

def vaadinFlowVersion = '24.1.10'
def vaadinFlowVersion = '24.2.1'

javaPlatform {
allowDependencies()
}

dependencies {
api platform("org.springframework.boot:spring-boot-dependencies:3.1.4")
api platform("org.springframework.boot:spring-boot-dependencies:3.1.5")
api platform('software.amazon.awssdk:bom:2.16.69')

def freeVersion = version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.validation.constraints.NotNull;
import org.apache.http.util.Asserts;
import org.springframework.scripting.ScriptEvaluator;
import org.springframework.scripting.support.StaticScriptSource;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,20 @@
*/
package io.jmix.flowui.devserver.frontend.installer;

import io.jmix.flowui.devserver.frontend.FrontendUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Redirect;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.file.Path;

/**
* Default file downloader implementation.
Expand All @@ -54,6 +40,7 @@
* @since
*/
public final class DefaultFileDownloader implements FileDownloader {

public static final String HTTPS_PROTOCOLS = "https.protocols";

private final ProxyConfig proxyConfig;
Expand All @@ -64,16 +51,15 @@ public final class DefaultFileDownloader implements FileDownloader {
/**
* Construct file downloader with given proxy configuration.
*
* @param proxyConfig
* proxy configuration to use for file download
* @param proxyConfig proxy configuration to use for file download
*/
public DefaultFileDownloader(ProxyConfig proxyConfig) {
this.proxyConfig = proxyConfig;
}

@Override
public void download(URI downloadURI, File destination, String userName,
String password) throws DownloadException {
String password) throws DownloadException {
this.userName = userName;
this.password = password;

Expand All @@ -100,114 +86,69 @@ public void download(URI downloadURI, File destination, String userName,

private void downloadFile(File destination, URI downloadUri)
throws IOException, DownloadException {
CloseableHttpResponse response = execute(downloadUri);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new DownloadException(
"Got error code " + statusCode + " from the server.");
}
new File(
FilenameUtils.getFullPathNoEndSeparator(destination.toString()))
.mkdirs();

HttpEntity responseEntity = response.getEntity();
long expected = responseEntity.getContentLength();
try (ReadableByteChannel rbc = Channels
.newChannel(responseEntity.getContent());
FileOutputStream fos = new FileOutputStream(destination)) {
long transferred = fos.getChannel().transferFrom(rbc, 0,
Long.MAX_VALUE);
if (expected > 0 && transferred != expected) {
// Download failed and channel.transferFrom does not rethrow the
// exception
throw new DownloadException(
"Error downloading from " + downloadUri + ". Expected "
+ expected + " bytes but got " + transferred);
}
}

}
HttpClient.Builder clientBuilder = HttpClient.newBuilder()
.version(Version.HTTP_1_1).followRedirects(Redirect.NORMAL);

private CloseableHttpResponse execute(URI requestUri) throws IOException {
CloseableHttpResponse response;
ProxyConfig.Proxy proxy = proxyConfig
.getProxyForUrl(requestUri.toString());
.getProxyForUrl(downloadUri.toString());

if (proxy != null) {
String message = String.format("Downloading via proxy %s", proxy);
getLogger().info(message);
FrontendUtils.logInFile(message);
return executeViaProxy(proxy, requestUri);
getLogger().debug("Downloading via proxy {}", proxy.toString());
clientBuilder = clientBuilder.proxy(ProxySelector
.of(new InetSocketAddress(proxy.host, proxy.port)));
clientBuilder = clientBuilder.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == RequestorType.PROXY) {
return new PasswordAuthentication(proxy.username,
proxy.password.toCharArray());
}
return new PasswordAuthentication(userName,
password.toCharArray());
}
});
} else {
String message = "No proxy was configured, downloading directly";
getLogger().info(message);
FrontendUtils.logInFile(message);
getLogger().debug("No proxy was configured, downloading directly");
if (userName != null && !userName.isEmpty() && password != null
&& !password.isEmpty()) {
getLogger().info("Using credentials ({})", userName);
// Auth target host
URL aURL = requestUri.toURL();
HttpClientContext localContext = makeLocalContext(aURL);
CredentialsProvider credentialsProvider = makeCredentialsProvider(
aURL.getHost(), aURL.getPort(), userName, password);
response = buildHttpClient(credentialsProvider)
.execute(new HttpGet(requestUri), localContext);
} else {
response = buildHttpClient(null)
.execute(new HttpGet(requestUri));
}
}
return response;
}
clientBuilder = clientBuilder
.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName,
password.toCharArray());
}
});

private CloseableHttpResponse executeViaProxy(ProxyConfig.Proxy proxy,
URI requestUri) throws IOException {
final CloseableHttpClient proxyClient;
if (proxy.useAuthentication()) {
proxyClient = buildHttpClient(makeCredentialsProvider(proxy.host,
proxy.port, proxy.username, proxy.password));
} else {
proxyClient = buildHttpClient(null);
}
}

final HttpHost proxyHttpHost = new HttpHost(proxy.host, proxy.port);

final RequestConfig requestConfig = RequestConfig.custom()
.setProxy(proxyHttpHost).build();

final HttpGet request = new HttpGet(requestUri);
request.setConfig(requestConfig);
HttpClient client = clientBuilder.build();
HttpRequest request = HttpRequest.newBuilder().uri(downloadUri).GET()
.build();

return proxyClient.execute(request);
}

private CloseableHttpClient buildHttpClient(
CredentialsProvider credentialsProvider) {
return HttpClients.custom().disableContentCompression()
.useSystemProperties()
.setDefaultCredentialsProvider(credentialsProvider).build();
}
try {
HttpResponse<Path> response = client.send(request,
BodyHandlers.ofFile(destination.toPath()));
if (response.statusCode() != 200) {
throw new DownloadException("Got error code "
+ response.statusCode() + " from the server.");
}
long expected = response.headers()
.firstValueAsLong("Content-Length").getAsLong();
if (destination.length() != expected) {
throw new DownloadException("Error downloading from "
+ downloadUri + ". Expected " + expected
+ " bytes but got " + destination.length());
}

private CredentialsProvider makeCredentialsProvider(String host, int port,
String username, String password) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(host, port),
new UsernamePasswordCredentials(username, password));
return credentialsProvider;
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
}

private HttpClientContext makeLocalContext(URL requestUrl) {
// Auth target host
HttpHost target = new HttpHost(requestUrl.getHost(),
requestUrl.getPort(), requestUrl.getProtocol());
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
// Add AuthCache to the execution context
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
return localContext;
}

private Logger getLogger() {
Expand Down
Loading