Search in sources :

Example 16 with ResourceDownloader

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.

the class PlayerInstaller method install.

public boolean install() {
    try {
        installer = CoreFactory.getSingleton().getPluginManager().getPluginInstaller();
        StandardPlugin sp = installer.getStandardPlugin("azemp");
        Map<Integer, Object> properties = new HashMap<>();
        properties.put(UpdateCheckInstance.PT_UI_STYLE, UpdateCheckInstance.PT_UI_STYLE_NONE);
        properties.put(UpdateCheckInstance.PT_UI_DISABLE_ON_SUCCESS_SLIDEY, true);
        final AESemaphore sem = new AESemaphore("emp install");
        final boolean[] result = new boolean[1];
        instance = installer.install(new InstallablePlugin[] { sp }, false, properties, new PluginInstallationListener() {

            @Override
            public void completed() {
                result[0] = true;
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }

            @Override
            public void cancelled() {
                result[0] = false;
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }

            @Override
            public void failed(PluginException e) {
                result[0] = false;
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }
        });
        boolean kill_it;
        synchronized (this) {
            kill_it = cancelled;
        }
        if (kill_it) {
            instance.cancel();
            return (false);
        }
        instance.addListener(new UpdateCheckInstanceListener() {

            @Override
            public void cancelled(UpdateCheckInstance instance) {
            }

            @Override
            public void complete(UpdateCheckInstance instance) {
                Update[] updates = instance.getUpdates();
                for (final Update update : updates) {
                    ResourceDownloader[] rds = update.getDownloaders();
                    for (ResourceDownloader rd : rds) {
                        rd.addListener(new ResourceDownloaderAdapter() {

                            @Override
                            public void reportActivity(ResourceDownloader downloader, String activity) {
                            }

                            @Override
                            public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
                                if (listener != null) {
                                    listener.progress(percentage);
                                }
                            }
                        });
                    }
                }
            }
        });
        sem.reserve();
        return result[0];
    } catch (Throwable e) {
    }
    return false;
}
Also used : UpdateCheckInstance(com.biglybt.pif.update.UpdateCheckInstance) UpdateCheckInstanceListener(com.biglybt.pif.update.UpdateCheckInstanceListener) HashMap(java.util.HashMap) PluginException(com.biglybt.pif.PluginException) StandardPlugin(com.biglybt.pif.installer.StandardPlugin) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) AESemaphore(com.biglybt.core.util.AESemaphore) Update(com.biglybt.pif.update.Update) InstallablePlugin(com.biglybt.pif.installer.InstallablePlugin) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) PluginInstallationListener(com.biglybt.pif.installer.PluginInstallationListener)

Example 17 with ResourceDownloader

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.

the class UpdateAutoDownloader method nextUpdate.

/**
 * @since 3.0.0.7
 */
private boolean nextUpdate() {
    if (iterDownloaders.hasNext()) {
        ResourceDownloader downloader = (ResourceDownloader) iterDownloaders.next();
        downloader.addListener(this);
        downloader.asyncDownload();
        return true;
    }
    return false;
}
Also used : ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)

Example 18 with ResourceDownloader

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.

the class PlatformMessenger method downloadURLSupport.

private static Object[] downloadURLSupport(Proxy proxy, String proxy_host, URL url, String postData) throws Throwable {
    ResourceDownloaderFactory rdf = StaticUtilities.getResourceDownloaderFactory();
    ResourceDownloader rd;
    if (proxy == null) {
        rd = rdf.create(url, postData);
    } else {
        rd = rdf.create(url, postData, proxy);
    }
    if (proxy_host != null) {
        rd.setProperty("URL_HOST", proxy_host);
    }
    rd.setProperty("URL_Connection", "Keep-Alive");
    rd = rdf.getRetryDownloader(rd, 3);
    // We could report percentage to listeners, but there's no need to atm
    // rd.addListener(new ResourceDownloaderListener() {
    // 
    // public void reportPercentComplete(ResourceDownloader downloader,
    // int percentage) {
    // }
    // 
    // public void reportActivity(ResourceDownloader downloader, String activity) {
    // }
    // 
    // public void failed(ResourceDownloader downloader,
    // ResourceDownloaderException e) {
    // }
    // 
    // public boolean completed(ResourceDownloader downloader, InputStream data) {
    // return true;
    // }
    // });
    InputStream is = rd.download();
    byte[] data;
    try {
        int length = is.available();
        data = new byte[length];
        is.read(data);
    } finally {
        is.close();
    }
    String s = new String(data, "UTF8");
    Map mapAllReplies = JSONUtils.decodeJSON(s);
    List listReplies = MapUtils.getMapList(mapAllReplies, "replies", null);
    return (new Object[] { s, listReplies });
}
Also used : InputStream(java.io.InputStream) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)

Example 19 with ResourceDownloader

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.

the class NetworkAdminSpeedTestScheduledTestImpl method getBEncodedMapFromRequest.

/**
 * Read from URL and return byte array.
 * @param url -
 * @return byte[] of the results. Max size currently 100k.
 * @throws java.io.IOException -
 */
private Map getBEncodedMapFromRequest(URL url) throws IOException {
    ResourceDownloader rd = ResourceDownloaderFactoryImpl.getSingleton().create(url);
    InputStream is = null;
    Map reply = new HashMap();
    try {
        is = rd.download();
        reply = BDecoder.decode(new BufferedInputStream(is));
        // all replys of this type contains a "result"
        Long res = (Long) reply.get("result");
        if (res == null)
            throw new IllegalStateException("No result parameter in the response!! reply=" + reply);
        if (res.intValue() == 0) {
            StringBuilder msg = new StringBuilder("Server failed. ");
            String error = new String((byte[]) reply.get("error"));
            String errDetail = new String((byte[]) reply.get("error_detail"));
            msg.append("Error: ").append(error);
            // detail is of no interest to the user
            // msg.append(" ,error detail: ").append(errDetail);
            Debug.outNoStack("SpeedCheck server returned an error: " + error + ", details=" + errDetail);
            throw new IOException(msg.toString());
        }
    } catch (IOException ise) {
        // rethrow this type of exception.
        throw ise;
    } catch (Throwable t) {
        Debug.out(t);
        Debug.printStackTrace(t);
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (Throwable e) {
            Debug.printStackTrace(e);
        }
    }
    return reply;
}
Also used : HashMap(java.util.HashMap) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with ResourceDownloader

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.

the class SubscriptionSchedulerImpl method download.

@Override
public void download(final Subscription subs, final SubscriptionResult original_result) {
    String download_link = original_result.getDownloadLink();
    if (download_link == null) {
        log(subs.getName() + ": can't download " + original_result.getID() + " as no direct download link available");
        return;
    }
    final String key = subs.getID() + ":" + original_result.getID();
    final String dl = download_link;
    synchronized (active_result_downloaders) {
        if (active_result_downloaders.contains(key)) {
            return;
        }
        log(subs.getName() + ": queued result for download - " + original_result.getID() + "/" + download_link);
        active_result_downloaders.add(key);
        result_downloader.run(new AERunnable() {

            @Override
            public void runSupport() {
                // need to fix up to the latest history due to the lazy nature of things :(
                SubscriptionResult result = subs.getHistory().getResult(original_result.getID());
                boolean success = false;
                try {
                    if (result == null) {
                        log(subs.getName() + ": result has been deleted - " + original_result.getID());
                        success = true;
                    } else if (result.getRead()) {
                        log(subs.getName() + ": result already marked as read, skipping - " + result.getID());
                        success = true;
                    } else {
                        boolean retry = true;
                        boolean use_ref = subs.getHistory().getDownloadWithReferer();
                        boolean tried_ref_switch = false;
                        while (retry) {
                            retry = false;
                            try {
                                TorrentUtils.setTLSDescription("Subscription: " + subs.getName());
                                URL original_url = new URL(dl);
                                PluginProxy plugin_proxy = null;
                                if (dl.startsWith("tor:")) {
                                    String target_resource = dl.substring(4);
                                    original_url = new URL(target_resource);
                                    Map<String, Object> options = new HashMap<>();
                                    options.put(AEProxyFactory.PO_PEER_NETWORKS, new String[] { AENetworkClassifier.AT_TOR });
                                    plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download of '" + target_resource + "'", original_url, options, true);
                                    if (plugin_proxy == null) {
                                        throw (new Exception("No Tor plugin proxy available for '" + dl + "'"));
                                    }
                                }
                                URL current_url = plugin_proxy == null ? original_url : plugin_proxy.getURL();
                                Torrent torrent = null;
                                try {
                                    while (true) {
                                        try {
                                            ResourceDownloaderFactory rdf = StaticUtilities.getResourceDownloaderFactory();
                                            ResourceDownloader url_rd = rdf.create(current_url, plugin_proxy == null ? null : plugin_proxy.getProxy());
                                            if (plugin_proxy != null) {
                                                url_rd.setProperty("URL_HOST", plugin_proxy.getURLHostRewrite() + (current_url.getPort() == -1 ? "" : (":" + current_url.getPort())));
                                            }
                                            String referer = use_ref ? subs.getReferer() : null;
                                            UrlUtils.setBrowserHeaders(url_rd, referer);
                                            Engine engine = subs.getEngine();
                                            if (engine instanceof WebEngine) {
                                                WebEngine we = (WebEngine) engine;
                                                if (we.isNeedsAuth()) {
                                                    String cookies = we.getCookies();
                                                    if (cookies != null && cookies.length() > 0) {
                                                        url_rd.setProperty("URL_Cookie", cookies);
                                                    }
                                                }
                                            }
                                            ResourceDownloader mr_rd = rdf.getMetaRefreshDownloader(url_rd);
                                            InputStream is = mr_rd.download();
                                            torrent = new TorrentImpl(TOTorrentFactory.deserialiseFromBEncodedInputStream(is));
                                            break;
                                        } catch (Throwable e) {
                                            if (plugin_proxy == null) {
                                                plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download", original_url);
                                                if (plugin_proxy != null) {
                                                    current_url = plugin_proxy.getURL();
                                                    continue;
                                                }
                                            }
                                            throw (e);
                                        }
                                    }
                                } finally {
                                    if (plugin_proxy != null) {
                                        plugin_proxy.setOK(torrent != null);
                                    }
                                }
                                byte[] hash = torrent.getHash();
                                // PlatformTorrentUtils.setContentTitle(torrent, torr );
                                DownloadManager dm = PluginInitializer.getDefaultInterface().getDownloadManager();
                                Download download;
                                // if we're assigning a tag/networks then we need to add it stopped in case the tag has any pre-start actions (e.g. set initial save location)
                                // this is because the assignments are done in SubscriptionManagerImpl on the download(willbe)added event
                                boolean stop_override = subs.getTagID() >= 0 || subs.getHistory().getDownloadNetworks() != null;
                                boolean auto_start = manager.shouldAutoStart(torrent);
                                manager.addPrepareTrigger(hash, new Subscription[] { subs }, new SubscriptionResult[] { result });
                                try {
                                    if (auto_start && !stop_override) {
                                        download = dm.addDownload(torrent);
                                    } else {
                                        download = dm.addDownloadStopped(torrent, null, null);
                                    }
                                } finally {
                                    manager.removePrepareTrigger(hash);
                                }
                                log(subs.getName() + ": added download " + download.getName() + ": auto-start=" + auto_start);
                                // maybe remove this as should be actioned in the trigger?
                                manager.prepareDownload(download, new Subscription[] { subs }, new SubscriptionResult[] { result });
                                subs.addAssociation(hash);
                                if (auto_start && stop_override) {
                                    download.restart();
                                }
                                result.setRead(true);
                                success = true;
                                if (tried_ref_switch) {
                                    subs.getHistory().setDownloadWithReferer(use_ref);
                                }
                            } catch (Throwable e) {
                                log(subs.getName() + ": Failed to download result " + dl, e);
                                if (e instanceof TOTorrentException && !tried_ref_switch) {
                                    use_ref = !use_ref;
                                    tried_ref_switch = true;
                                    retry = true;
                                    log(subs.getName() + ": Retrying " + (use_ref ? "with referer" : "without referer"));
                                }
                            } finally {
                                TorrentUtils.setTLSDescription(null);
                            }
                        }
                    }
                } finally {
                    try {
                        if (!success) {
                            if (dl.startsWith("azplug:") || dl.startsWith("chat:")) {
                                // whatever the outcome these have been handled async
                                result.setRead(true);
                            } else {
                                int rad = manager.getAutoDownloadMarkReadAfterDays();
                                if (rad > 0) {
                                    long rad_millis = rad * 24 * 60 * 60 * 1000L;
                                    long time_found = result.getTimeFound();
                                    if (time_found > 0 && time_found + rad_millis < SystemTime.getCurrentTime()) {
                                        log(subs.getName() + ": result expired, marking as read - " + result.getID());
                                        result.setRead(true);
                                    }
                                }
                            }
                        }
                    } catch (Throwable e) {
                        Debug.out(e);
                    }
                    synchronized (active_result_downloaders) {
                        active_result_downloaders.remove(key);
                    }
                    calculateSchedule();
                }
            }
        });
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TorrentImpl(com.biglybt.pifimpl.local.torrent.TorrentImpl) InputStream(java.io.InputStream) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) DownloadManager(com.biglybt.pif.download.DownloadManager) URL(java.net.URL) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) Download(com.biglybt.pif.download.Download) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) Engine(com.biglybt.core.metasearch.Engine)

Aggregations

ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)37 URL (java.net.URL)18 ResourceDownloaderException (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException)16 InputStream (java.io.InputStream)14 ResourceDownloaderAdapter (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter)12 ResourceDownloaderFactory (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory)11 Update (com.biglybt.pif.update.Update)9 LogEvent (com.biglybt.core.logging.LogEvent)7 PluginProxy (com.biglybt.core.proxy.AEProxyFactory.PluginProxy)6 ZipInputStream (java.util.zip.ZipInputStream)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)4 SFPluginDetails (com.biglybt.pifimpl.update.sf.SFPluginDetails)4 LogAlert (com.biglybt.core.logging.LogAlert)3 AESemaphore (com.biglybt.core.util.AESemaphore)3 PluginException (com.biglybt.pif.PluginException)3 InstallablePlugin (com.biglybt.pif.installer.InstallablePlugin)3 PluginInstallationListener (com.biglybt.pif.installer.PluginInstallationListener)3 StandardPlugin (com.biglybt.pif.installer.StandardPlugin)3