Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public boolean installPlugin(ToolPluginDescriptor plugin, final Step step, Proce
boolean customRepo = plugin.url() != null;
List<String> args = new ArrayList<>();
args.add("installPlugins");
args.add(plugin.id());
args.add(plugin.id().replace("+", " "));
if (customRepo) {
args.add(plugin.url());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Redirect;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.io.FileAccess;
Expand All @@ -23,6 +28,8 @@ public class IdeaPluginDownloader {
private static final String BUILD_FILE = "build.txt";
private final IdeContext context;
private final IdeaBasedIdeToolCommandlet commandlet;
//TODO: clarify whether .followRedirects() is needed here
protected final HttpClient client = HttpClient.newBuilder().followRedirects(Redirect.ALWAYS).build();

/**
* the constructor
Expand Down Expand Up @@ -131,24 +138,34 @@ private void extractDownloadedPlugin(FileAccess fileAccess, Path downloadedFile,
}

private String getFileExtensionFromUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();

int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new IOException("Failed to fetch file headers: HTTP " + responseCode);
}

String contentType = connection.getContentType();
if (contentType == null) {
return "";
URI uri = null;
HttpRequest request;
try {
uri = URI.create(urlString);
request = HttpRequest.newBuilder().uri(uri)
.method("HEAD", HttpRequest.BodyPublishers.noBody()).timeout(Duration.ofSeconds(5)).build();

HttpResponse<?> res = this.client.send(request, HttpResponse.BodyHandlers.ofString());

int responseCode = res.statusCode();
// TODO: brauchen wir die if-abfrage überhaupt noch, oder wird der Fehler durch catch-block behandelt
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Failed to fetch file headers: HTTP " + responseCode);
}

String contentType = res.headers().firstValue("content-type").orElse("undefined");
if (contentType.equals("undefined")) {
return "";
}
return switch (contentType) {
case "application/zip" -> ".zip";
case "application/java-archive" -> ".jar";
default -> "";
};
} catch (Exception e) {
// logger.error("Failed to perform HEAD request of URL {}", uri, e);
throw new RuntimeException("Failed to perform HEAD request of URL " + uri, e);
}
return switch (contentType) {
case "application/zip" -> ".zip";
case "application/java-archive" -> ".jar";
default -> "";
};
}
}