Search in sources :

Example 56 with HttpRequest

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();
        }
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) IOException(java.io.IOException)

Example 57 with HttpRequest

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);
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) TimerTask(java.util.TimerTask) MalformedURLException(java.net.MalformedURLException)

Example 58 with HttpRequest

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");
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) TimerTask(java.util.TimerTask) File(java.io.File) ZipFile(net.lingala.zip4j.ZipFile) URL(java.net.URL) IOException(java.io.IOException)

Example 59 with HttpRequest

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");
    }
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) TimerTask(java.util.TimerTask) ZipFile(net.lingala.zip4j.ZipFile) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException)

Example 60 with HttpRequest

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();
}
Also used : HttpRequest(com.github.kevinsawicki.http.HttpRequest)

Aggregations

HttpRequest (com.github.kevinsawicki.http.HttpRequest)86 HttpRequestException (com.github.kevinsawicki.http.HttpRequest.HttpRequestException)29 IOException (java.io.IOException)25 JSONObject (org.json.JSONObject)19 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 File (java.io.File)8 TimerTask (java.util.TimerTask)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 Pair (android.util.Pair)6 URL (java.net.URL)5 JSONException (org.json.JSONException)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)4 JSONObject (com.alibaba.fastjson.JSONObject)4 ZipFile (net.lingala.zip4j.ZipFile)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)3 IPEntity (com.example.li.springboot_crawler_demo.utils.img.entity.IPEntity)2 HttpURLConnection (java.net.HttpURLConnection)2 MalformedURLException (java.net.MalformedURLException)2 Matcher (java.util.regex.Matcher)2 JSONArray (org.json.JSONArray)2