use of com.github.kevinsawicki.http.HttpRequest in project PowerNukkitX by BlocklyNukkit.
the class ComponentsHelper method listRemoteComponents.
public static List<ComponentEntry> listRemoteComponents() {
if (cache.get() != null) {
return cache.get();
} else {
final HttpRequest request = HttpRequest.get(OSS_LIST);
try {
final Map<String, String> data = INIUtils.parseINI(request.bufferedReader());
final List<ComponentEntry> out = new ArrayList<>(1);
for (String name : data.get("components").split(";")) {
String description = "Unknown";
final String languageId = LanguageUtils.locale.toLanguageTag().toLowerCase();
if (data.containsKey(name + ".description." + languageId)) {
description = data.get(name + ".description." + languageId);
} else {
description = data.get(name + ".description.en-us");
}
final ComponentEntry componentEntry = new ComponentEntry().setName(name).setDescription(description).setVersion(data.get(name + ".version"));
final String[] downloadURLs = data.get(name + ".remote-paths").split(";");
final ComponentFile[] componentFiles = new ComponentFile[downloadURLs.length];
String[] split = data.get(name + ".files").split(";");
for (int i = 0, splitLength = split.length; i < splitLength; i++) {
componentFiles[i] = new ComponentFile(split[i], downloadURLs[i]);
}
componentEntry.setComponentFiles(componentFiles);
cache = new WeakReference<>(out);
out.add(componentEntry);
}
return out;
} catch (IOException e) {
return Collections.emptyList();
}
}
}
use of com.github.kevinsawicki.http.HttpRequest in project PowerNukkitX by BlocklyNukkit.
the class URLUtils method downloadWithBar.
public static void downloadWithBar(URL downloadURL, File target, String displayName, Timer timer) {
try {
HttpRequest request = HttpRequest.get(downloadURL);
Logger.trInfo("display.connecting", downloadURL.toString());
Logger.newLine();
final AtomicLong atomicLong = new AtomicLong(0);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
final long finished = target.length();
final long total = request.getConnection().getContentLength();
final long speed = finished - atomicLong.get();
atomicLong.set(finished);
Logger.bar((float) (finished * 1.0 / total), displayableBytes(finished) + "/" + displayableBytes(total) + " (" + displayableBytes(speed) + "/s)");
if (finished == total) {
this.cancel();
}
}
}, 500, 500);
request.receive(target);
Logger.clearUpLine();
Logger.trInfo("display.success.download", displayName);
} catch (Exception e) {
Logger.trWarn("display.fail.download", displayName);
}
}
use of com.github.kevinsawicki.http.HttpRequest in project PowerNukkitX by BlocklyNukkit.
the class AdoptOpenJDKInstall method downloadAdoptOpenJDK17.
@SuppressWarnings("ResultOfMethodCallIgnored")
private void downloadAdoptOpenJDK17() {
final File localJavaDir = new File("./java");
if (!localJavaDir.exists()) {
localJavaDir.mkdirs();
}
final URL downloadURL = URLUtils.adopt17URL();
if (downloadURL == null) {
Logger.trWarn("display.fail.install-java17");
return;
}
try {
suffix = StringUtils.uriSuffix(downloadURL);
final File targetZip = new File("tmp." + suffix);
HttpRequest request = HttpRequest.get(downloadURL);
Logger.trInfo("display.connecting", downloadURL.toString());
final AtomicLong atomicLong = new AtomicLong(0);
cli.timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
final long total = request.getConnection().getContentLength();
final long finished = targetZip.length();
final long speed = finished - atomicLong.get();
atomicLong.set(finished);
Logger.bar((float) (finished * 1.0 / total), displayableBytes(finished) + "/" + displayableBytes(total) + " (" + displayableBytes(speed) + "/s)");
if (finished == total) {
Logger.trInfo("display.success.download", "Java17");
this.cancel();
}
}
}, 500, 500);
request.receive(targetZip);
} catch (Exception e) {
Logger.trWarn("display.fail.install-java17");
}
}
use of com.github.kevinsawicki.http.HttpRequest in project PowerNukkitX by BlocklyNukkit.
the class GraalVMInstall method downloadGraalVM17.
@SuppressWarnings("ResultOfMethodCallIgnored")
private void downloadGraalVM17() {
final File localJavaDir = new File("./java");
if (!localJavaDir.exists()) {
localJavaDir.mkdirs();
}
final URL downloadURL = URLUtils.graal17URL();
if (downloadURL == null) {
Logger.trWarn("display.fail.install-java17");
return;
}
try {
final File targetZip = new File("tmp.zip");
HttpRequest request = HttpRequest.get(downloadURL);
Logger.trInfo("display.connecting", downloadURL.toString());
final AtomicLong atomicLong = new AtomicLong(0);
cli.timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
final long total = request.getConnection().getContentLength();
final long finished = targetZip.length();
final long speed = finished - atomicLong.get();
atomicLong.set(finished);
Logger.bar((float) (finished * 1.0 / total), displayableBytes(finished) + "/" + displayableBytes(total) + " (" + displayableBytes(speed) + "/s)");
if (finished == total) {
Logger.trInfo("display.success.download", "Java17");
this.cancel();
}
}
}, 500, 500);
request.receive(targetZip);
} catch (Exception e) {
Logger.trWarn("display.fail.install-java17");
}
}
use of com.github.kevinsawicki.http.HttpRequest in project pancm_project by xuwujing.
the class HttpUtil method doGet.
/**
* Do get string.
*
* @param url the url
* @param json the json
* @return string
*/
public static String doGet(String url, JSONObject json) {
HttpRequest hr = new HttpRequest(url, HttpRequest.METHOD_GET);
if ("https".equalsIgnoreCase(url.substring(0, 5))) {
hr.trustAllCerts().trustAllHosts();
}
String contentType = "application/json";
if (contentType != null) {
hr.contentType(contentType);
}
return hr.send(json.toString()).body();
}
Aggregations