Search in sources :

Example 21 with ResourceDownloaderException

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

the class MagnetPlugin method doSecondaryLookup.

protected void doSecondaryLookup(final MagnetPluginProgressListener listener, final Object[] result, byte[] hash, Set<String> networks_enabled, String args) {
    if (listener != null) {
        listener.reportActivity(getMessageText("report.secondarylookup", null));
    }
    PluginProxy plugin_proxy = null;
    try {
        URL original_sl_url = new URL(SECONDARY_LOOKUP + "magnetLookup?hash=" + Base32.encode(hash) + (args.length() == 0 ? "" : ("&args=" + UrlUtils.encode(args))));
        URL sl_url = original_sl_url;
        Proxy proxy = null;
        if (!networks_enabled.contains(AENetworkClassifier.AT_PUBLIC)) {
            plugin_proxy = AEProxyFactory.getPluginProxy("secondary magnet lookup", sl_url);
            if (plugin_proxy == null) {
                throw (new NoRouteToHostException("plugin proxy unavailable"));
            } else {
                proxy = plugin_proxy.getProxy();
                sl_url = plugin_proxy.getURL();
            }
        }
        ResourceDownloaderFactory rdf = plugin_interface.getUtilities().getResourceDownloaderFactory();
        ResourceDownloader rd;
        if (proxy == null) {
            rd = rdf.create(sl_url);
        } else {
            rd = rdf.create(sl_url, proxy);
            rd.setProperty("URL_HOST", original_sl_url.getHost());
        }
        final PluginProxy f_pp = plugin_proxy;
        rd.addListener(new ResourceDownloaderAdapter() {

            @Override
            public boolean completed(ResourceDownloader downloader, InputStream data) {
                try {
                    if (listener != null) {
                        listener.reportActivity(getMessageText("report.secondarylookup.ok", null));
                    }
                    synchronized (result) {
                        result[0] = data;
                    }
                    return (true);
                } finally {
                    complete();
                }
            }

            @Override
            public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
                try {
                    synchronized (result) {
                        result[0] = e;
                    }
                    if (listener != null) {
                        listener.reportActivity(getMessageText("report.secondarylookup.fail"));
                    }
                } finally {
                    complete();
                }
            }

            private void complete() {
                if (f_pp != null) {
                    // outcome doesn't really indicate whether the result was wholesome
                    f_pp.setOK(true);
                }
            }
        });
        rd.asyncDownload();
    } catch (Throwable e) {
        if (plugin_proxy != null) {
            // tidy up, no indication of proxy badness here so say its ok
            plugin_proxy.setOK(true);
        }
        if (listener != null) {
            listener.reportActivity(getMessageText("report.secondarylookup.fail", Debug.getNestedExceptionMessage(e)));
        }
    }
}
Also used : Proxy(java.net.Proxy) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) InputStream(java.io.InputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) NoRouteToHostException(java.net.NoRouteToHostException) URL(java.net.URL)

Example 22 with ResourceDownloaderException

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

the class PlatformManagerUpdateChecker method checkForUpdate.

@Override
public void checkForUpdate(final UpdateChecker checker) {
    try {
        SFPluginDetails sf_details = SFPluginDetailsLoaderFactory.getSingleton().getPluginDetails(plugin_interface.getPluginID());
        String current_version = plugin_interface.getPluginVersion();
        if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, "PlatformManager:Win32 update check starts: current = " + current_version));
        boolean current_az_is_cvs = Constants.isCVSVersion();
        String sf_plugin_version = sf_details.getVersion();
        String sf_comp_version = sf_plugin_version;
        if (current_az_is_cvs) {
            String sf_cvs_version = sf_details.getCVSVersion();
            if (sf_cvs_version.length() > 0) {
                // sf cvs version ALWAYS entry in _CVS
                sf_plugin_version = sf_cvs_version;
                sf_comp_version = sf_plugin_version.substring(0, sf_plugin_version.length() - 4);
            }
        }
        String target_version = null;
        if (sf_comp_version.length() == 0 || !Character.isDigit(sf_comp_version.charAt(0))) {
            if (Logger.isEnabled())
                Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "PlatformManager:Win32 no valid version to check against (" + sf_comp_version + ")"));
        } else if (Constants.compareVersions(current_version, sf_comp_version) < 0) {
            target_version = sf_comp_version;
        }
        checker.reportProgress("Win32: current = " + current_version + ", latest = " + sf_comp_version);
        if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, "PlatformManager:Win32 update required = " + (target_version != null)));
        if (target_version != null) {
            String target_download = sf_details.getDownloadURL();
            if (current_az_is_cvs) {
                String sf_cvs_version = sf_details.getCVSVersion();
                if (sf_cvs_version.length() > 0) {
                    target_download = sf_details.getCVSDownloadURL();
                }
            }
            ResourceDownloaderFactory rdf = ResourceDownloaderFactoryImpl.getSingleton();
            ResourceDownloader direct_rdl = rdf.create(new URL(target_download));
            String torrent_download = Constants.URL_PLUGINS_TORRENT_BASE;
            int slash_pos = target_download.lastIndexOf("/");
            if (slash_pos == -1) {
                torrent_download += target_download;
            } else {
                torrent_download += target_download.substring(slash_pos + 1);
            }
            torrent_download += ".torrent";
            if (I2PHelpers.isI2PInstalled()) {
                torrent_download += "?i2p=1";
            }
            ResourceDownloader torrent_rdl = rdf.create(new URL(torrent_download));
            torrent_rdl = rdf.getSuffixBasedDownloader(torrent_rdl);
            // create an alternate downloader with torrent attempt first
            ResourceDownloader alternate_rdl = rdf.getAlternateDownloader(new ResourceDownloader[] { torrent_rdl, direct_rdl });
            // get size here so it is cached
            rdf.getTimeoutDownloader(rdf.getRetryDownloader(alternate_rdl, RD_SIZE_RETRIES), RD_SIZE_TIMEOUT).getSize();
            List update_desc = new ArrayList();
            List desc_lines = HTMLUtils.convertHTMLToText("", sf_details.getDescription());
            update_desc.addAll(desc_lines);
            List comment_lines = HTMLUtils.convertHTMLToText("    ", sf_details.getComment());
            update_desc.addAll(comment_lines);
            String[] update_d = new String[update_desc.size()];
            update_desc.toArray(update_d);
            final Update update = checker.addUpdate(UPDATE_NAME, update_d, current_version, target_version, alternate_rdl, Update.RESTART_REQUIRED_YES);
            update.setDescriptionURL(sf_details.getInfoURL());
            alternate_rdl.addListener(new ResourceDownloaderAdapter() {

                @Override
                public boolean completed(final ResourceDownloader downloader, InputStream data) {
                    installUpdate(checker, update, downloader, data);
                    return (true);
                }

                @Override
                public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
                    // Debug.out( downloader.getName() + " failed", e );
                    update.complete(false);
                }
            });
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        checker.reportProgress("Failed to load plugin details for the platform manager: " + Debug.getNestedExceptionMessage(e));
        checker.failed();
    } finally {
        checker.completed();
    }
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) ArrayList(java.util.ArrayList) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) Update(com.biglybt.pif.update.Update) URL(java.net.URL) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) ArrayList(java.util.ArrayList) List(java.util.List) SFPluginDetails(com.biglybt.pifimpl.update.sf.SFPluginDetails)

Example 23 with ResourceDownloaderException

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

the class PluginUpdatePlugin method addUpdate.

public Update addUpdate(final PluginInterface pi_for_update, final UpdateChecker checker, final String update_name, final String[] update_details, final String old_version, final String new_version, final ResourceDownloader resource_downloader, final boolean is_jar, final int restart_type, final boolean verify) {
    final Update update = checker.addUpdate(update_name, update_details, old_version, new_version, resource_downloader, restart_type);
    update.setUserObject(pi_for_update);
    resource_downloader.addListener(new ResourceDownloaderAdapter() {

        @Override
        public boolean completed(final ResourceDownloader downloader, InputStream data) {
            // during the update phase report any messages
            // to the downloader
            LoggerChannelListener list = new LoggerChannelListener() {

                @Override
                public void messageLogged(int type, String content) {
                    downloader.reportActivity(content);
                }

                @Override
                public void messageLogged(String str, Throwable error) {
                    downloader.reportActivity(str);
                }
            };
            try {
                log.addListener(list);
                installUpdate(checker, update, pi_for_update, restart_type == Update.RESTART_REQUIRED_NO, is_jar, old_version, new_version, data, verify);
                return (true);
            } finally {
                log.removeListener(list);
            }
        }

        @Override
        public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
            if (!downloader.isCancelled()) {
                Debug.out(downloader.getName() + " failed", e);
            }
            update.complete(false);
        }
    });
    return (update);
}
Also used : ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) ZipInputStream(java.util.zip.ZipInputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)

Example 24 with ResourceDownloaderException

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

the class BiglyBTURLConnection method connect.

@Override
public void connect() throws IOException {
    String str = url.toExternalForm();
    int pos = str.indexOf("body=");
    if (pos >= 0) {
        str = str.substring(pos + 5);
        byte[] bytes = str.getBytes(Constants.BYTE_ENCODING);
        VuzeFile vf = VuzeFileHandler.getSingleton().loadVuzeFile(bytes);
        if (vf == null) {
            throw (new IOException("Invalid biglybt url"));
        }
        input_stream = new ByteArrayInputStream(bytes);
    } else {
        String host = url.getHost();
        String httpsURL;
        if (host.equalsIgnoreCase("install-plugin") && url.getPath().length() > 1) {
            String plugin_id = url.getPath().substring(1);
            httpsURL = Constants.PLUGINS_WEB_SITE + "getplugin.php?plugin=" + plugin_id + "&" + SFPluginDetailsLoaderImpl.getBaseUrlParams();
        } else if (host.contains(".")) {
            httpsURL = "https" + str.substring("biglybt".length());
        } else {
            throw (new IOException("Invalid biglybt url"));
        }
        ResourceDownloader rd = StaticUtilities.getResourceDownloaderFactory().create(new URL(httpsURL));
        try {
            if (ONLY_VUZE_FILE) {
                VuzeFile vf = VuzeFileHandler.getSingleton().loadVuzeFile(rd.download());
                if (vf == null) {
                    throw (new IOException("Invalid biglybt url"));
                }
                boolean hasPlugin = false;
                VuzeFileComponent[] components = vf.getComponents();
                for (VuzeFileComponent component : components) {
                    if (component.getType() == VuzeFileComponent.COMP_TYPE_PLUGIN) {
                        hasPlugin = true;
                        break;
                    }
                }
                if (!hasPlugin) {
                    throw (new IOException("Biglybt url does not contain plugin"));
                }
                input_stream = new ByteArrayInputStream(vf.exportToBytes());
            } else {
                input_stream = rd.download();
            }
        } catch (ResourceDownloaderException e) {
            throw new IOException(e);
        }
    }
}
Also used : VuzeFileComponent(com.biglybt.core.vuzefile.VuzeFileComponent) ByteArrayInputStream(java.io.ByteArrayInputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) IOException(java.io.IOException) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) URL(java.net.URL)

Example 25 with ResourceDownloaderException

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

the class PluginInstallerImpl method uninstall.

@Override
public UpdateCheckInstance uninstall(final PluginInterface[] pis, final PluginInstallationListener listener_maybe_null, final Map<Integer, Object> properties) throws PluginException {
    properties.put(UpdateCheckInstance.PT_UNINSTALL_RESTART_REQUIRED, false);
    for (int i = 0; i < pis.length; i++) {
        PluginInterface pi = pis[i];
        if (pi.getPluginState().isMandatory()) {
            throw (new PluginException("Plugin '" + pi.getPluginID() + "' is mandatory, can't uninstall"));
        }
        if (pi.getPluginState().isBuiltIn()) {
            throw (new PluginException("Plugin '" + pi.getPluginID() + "' is built-in, can't uninstall"));
        }
        String plugin_dir = pi.getPluginDirectoryName();
        if (plugin_dir == null || !new File(plugin_dir).exists()) {
            throw (new PluginException("Plugin '" + pi.getPluginID() + "' is not loaded from the file system, can't uninstall"));
        }
    }
    try {
        UpdateManager uman = manager.getDefaultPluginInterface().getUpdateManager();
        UpdateCheckInstance inst = uman.createEmptyUpdateCheckInstance(UpdateCheckInstance.UCI_UNINSTALL, "update.instance.uninstall");
        final int[] rds_added = { 0 };
        final AESemaphore rd_waiter_sem = new AESemaphore("uninst:rd:wait");
        for (int i = 0; i < pis.length; i++) {
            final PluginInterface pi = pis[i];
            final String plugin_dir = pi.getPluginDirectoryName();
            inst.addUpdatableComponent(new UpdatableComponent() {

                @Override
                public String getName() {
                    return (pi.getPluginName());
                }

                @Override
                public int getMaximumCheckTime() {
                    return (0);
                }

                @Override
                public void checkForUpdate(final UpdateChecker checker) {
                    try {
                        ResourceDownloader rd = manager.getDefaultPluginInterface().getUtilities().getResourceDownloaderFactory().create(new File(plugin_dir));
                        // the plugin may have > 1 plugin interfaces, make the name up appropriately
                        String update_name = "";
                        PluginInterface[] ifs = manager.getPluginInterfaces();
                        Arrays.sort(ifs, new Comparator() {

                            @Override
                            public int compare(Object o1, Object o2) {
                                return (((PluginInterface) o1).getPluginName().compareTo(((PluginInterface) o2).getPluginName()));
                            }
                        });
                        for (int i = 0; i < ifs.length; i++) {
                            if (ifs[i].getPluginID().equals(pi.getPluginID())) {
                                update_name += (update_name.length() == 0 ? "" : ",") + ifs[i].getPluginName();
                            }
                        }
                        boolean unloadable = pi.getPluginState().isUnloadable();
                        if (!unloadable) {
                            properties.put(UpdateCheckInstance.PT_UNINSTALL_RESTART_REQUIRED, true);
                        }
                        final Update update = checker.addUpdate(update_name, new String[] { "Uninstall: " + plugin_dir }, pi.getPluginVersion(), pi.getPluginVersion(), rd, unloadable ? Update.RESTART_REQUIRED_NO : Update.RESTART_REQUIRED_YES);
                        synchronized (rds_added) {
                            rds_added[0]++;
                        }
                        rd.addListener(new ResourceDownloaderAdapter() {

                            @Override
                            public boolean completed(ResourceDownloader downloader, InputStream data) {
                                try {
                                    try {
                                        if (pi.getPluginState().isUnloadable()) {
                                            pi.getPluginState().unload();
                                            if (!FileUtil.recursiveDelete(new File(plugin_dir))) {
                                                update.setRestartRequired(Update.RESTART_REQUIRED_YES);
                                                properties.put(UpdateCheckInstance.PT_UNINSTALL_RESTART_REQUIRED, true);
                                                checker.reportProgress("Failed to remove plugin, restart will be required");
                                            }
                                        }
                                        UpdateInstaller installer = checker.createInstaller();
                                        installer.addRemoveAction(new File(plugin_dir).getCanonicalPath());
                                        update.complete(true);
                                        try {
                                            PluginInitializer.fireEvent(PluginEvent.PEV_PLUGIN_UNINSTALLED, pi.getPluginID());
                                        } catch (Throwable e) {
                                            Debug.out(e);
                                        }
                                    } catch (Throwable e) {
                                        update.complete(false);
                                        Debug.printStackTrace(e);
                                        Logger.log(new LogAlert(LogAlert.REPEATABLE, "Plugin uninstall failed", e));
                                    }
                                    return (true);
                                } finally {
                                    rd_waiter_sem.release();
                                }
                            }

                            @Override
                            public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
                                try {
                                    update.complete(false);
                                    if (!downloader.isCancelled()) {
                                        Logger.log(new LogAlert(LogAlert.REPEATABLE, "Plugin uninstall failed", e));
                                    }
                                } finally {
                                    rd_waiter_sem.release();
                                }
                            }
                        });
                    } finally {
                        checker.completed();
                    }
                }
            }, false);
        }
        if (listener_maybe_null != null) {
            inst.addListener(new UpdateCheckInstanceListener() {

                @Override
                public void cancelled(UpdateCheckInstance instance) {
                    listener_maybe_null.cancelled();
                }

                @Override
                public void complete(UpdateCheckInstance instance) {
                    // needs to be async in the case of the caller of the uninstall needing access to the
                    // updatecheckinstance and this is a sync callback
                    new AEThread2("Uninstall:async") {

                        @Override
                        public void run() {
                            int wait_count;
                            synchronized (rds_added) {
                                wait_count = rds_added[0];
                            }
                            for (int i = 0; i < wait_count; i++) {
                                rd_waiter_sem.reserve();
                            }
                            listener_maybe_null.completed();
                        }
                    }.start();
                }
            });
        }
        inst.start();
        return (inst);
    } catch (Throwable e) {
        PluginException pe;
        if (e instanceof PluginException) {
            pe = (PluginException) e;
        } else {
            pe = new PluginException("Uninstall failed", e);
        }
        if (listener_maybe_null != null) {
            listener_maybe_null.failed(pe);
        }
        throw (pe);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) LogAlert(com.biglybt.core.logging.LogAlert) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) File(java.io.File)

Aggregations

ResourceDownloaderException (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException)26 ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)13 InputStream (java.io.InputStream)11 URL (java.net.URL)11 ResourceDownloaderAdapter (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter)6 ResourceDownloaderFactory (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory)6 IOException (java.io.IOException)5 ZipInputStream (java.util.zip.ZipInputStream)5 PluginProxy (com.biglybt.core.proxy.AEProxyFactory.PluginProxy)4 TOTorrent (com.biglybt.core.torrent.TOTorrent)4 File (java.io.File)4 ArrayList (java.util.ArrayList)4 LogEvent (com.biglybt.core.logging.LogEvent)3 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)3 Update (com.biglybt.pif.update.Update)3 FileInputStream (java.io.FileInputStream)3 HTMLPage (com.biglybt.core.html.HTMLPage)2 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)2 AEThread (com.biglybt.core.util.AEThread)2 ClientIDGenerator (com.biglybt.pif.clientid.ClientIDGenerator)2