use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.
the class WelcomeWindow method getWhatsNew.
private void getWhatsNew(final int phase) {
String helpFile = null;
if (phase == 1) {
helpFile = MessageText.getString("window.welcome.file");
if (!helpFile.toLowerCase().startsWith(Constants.PLUGINS_WEB_SITE)) {
getWhatsNew(2);
return;
}
} else {
helpFile = MessageText.getString("window.welcome.file");
InputStream stream;
stream = getClass().getResourceAsStream(helpFile);
if (stream == null) {
String helpFullPath = "/com/biglybt/internat/whatsnew/" + helpFile;
stream = getClass().getResourceAsStream(helpFullPath);
}
if (stream == null) {
stream = getClass().getResourceAsStream("/ChangeLog.txt");
}
if (stream == null) {
sWhatsNew = "Welcome Window: Error loading resource: " + helpFile;
} else {
try {
sWhatsNew = FileUtil.readInputStreamAsString(stream, 65535, "utf8");
stream.close();
} catch (IOException e) {
Debug.out(e);
}
}
setWhatsNew();
return;
}
final String url = helpFile;
new AEThread2("getWhatsNew", true) {
@Override
public void run() {
String s;
ResourceDownloaderFactory rdf = ResourceDownloaderFactoryImpl.getSingleton();
try {
ResourceDownloader rd = rdf.create(new URL(url));
InputStream is = rd.download();
int length = is.available();
byte[] data = new byte[length];
is.read(data);
is.close();
s = new String(data);
} catch (ResourceDownloaderException rde) {
// We don't need a stack trace - it's arguable that we even need any
// errors at all - the below line is better, but suppressed output might
// be better.
// Debug.outNoStack("Error downloading from " + url + ", " + rde, true);
s = "";
} catch (Exception e) {
Debug.out(e);
s = "";
}
sWhatsNew = s;
if (sWhatsNew == null || sWhatsNew.length() == 0) {
getWhatsNew(phase + 1);
return;
}
Utils.execSWTThread(new AERunnable() {
@Override
public void runSupport() {
if (cWhatsNew != null && !cWhatsNew.isDisposed()) {
setWhatsNew();
}
}
});
}
}.start();
}
use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.
the class TrackerListURLConnection method readList.
private static synchronized String readList(String url_str) throws IOException {
if (url_str.contains("info_hash=")) {
throw (new IOException("Tracker list URLs can't be directly used as announce URLs"));
}
String key = "tl_" + Base32.encode(url_str.getBytes("UTF-8")) + ".txt";
long now = SystemTime.getMonotonousTime();
URL url = new URL(url_str);
boolean do_cache = !url.getProtocol().equals("file");
File cache_dir = new File(SystemProperties.getUserPath(), "cache");
if (!cache_dir.exists()) {
cache_dir.mkdirs();
}
File cache_file = new File(cache_dir, key);
if (do_cache) {
long cache_time = cache_file.exists() ? 60 * 60 * 1000 : 5 * 60 * 1000;
Long last = last_downloads.get(key);
if (last != null && now - last < cache_time) {
if (cache_file.exists()) {
try {
String result = FileUtil.readFileAsString(cache_file, 32 * 1024, "UTF-8");
return (result);
} catch (Throwable e) {
cache_file.delete();
}
} else {
return ("");
}
}
last_downloads.put(key, now);
}
try {
ResourceDownloader rd = ResourceDownloaderFactoryImpl.getSingleton().create(url);
rd.setProperty("URL_Connect_Timeout", 20 * 1000);
rd.setProperty("URL_Read_Timeout", 10 * 1000);
InputStream is = rd.download();
try {
String result = FileUtil.readInputStreamAsString(is, 32 * 1024, "UTF-8");
if (do_cache) {
FileUtil.writeStringAsFile(cache_file, result);
}
return (result);
} finally {
is.close();
}
} catch (Throwable e) {
Logger.log(new LogAlert(true, LogAlert.AT_ERROR, "Failed to load Tracker List from '" + url_str + "'", e));
if (do_cache && cache_file.exists()) {
try {
String result = FileUtil.readFileAsString(cache_file, 32 * 1024, "UTF-8");
return (result);
} catch (Throwable f) {
cache_file.delete();
}
}
if (e instanceof IOException) {
throw ((IOException) e);
} else {
throw (new IOException(Debug.getNestedExceptionMessage(e)));
}
}
}
use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.
the class UPnPImpl method downloadXMLSupport.
protected SimpleXMLParserDocument downloadXMLSupport(String friendly_name, URL url) throws UPnPException {
String url_str = url.toExternalForm();
boolean record_failure = true;
try {
TorrentUtils.setTLSDescription("UPnP Device" + (friendly_name == null ? "" : (": " + friendly_name)));
ResourceDownloaderFactory rdf = adapter.getResourceDownloaderFactory();
int retries;
synchronized (failed_urls) {
long[] fails = failed_urls.get(url_str);
if (fails == null) {
retries = 3;
} else {
long consec_fails = fails[0];
long last_fail = fails[1];
long max_period = 10 * 60 * 1000;
long period = 60 * 1000;
for (int i = 0; i < consec_fails; i++) {
period <<= 1;
if (period >= max_period) {
period = max_period;
break;
}
}
if (SystemTime.getMonotonousTime() - last_fail < period) {
record_failure = false;
throw (new UPnPException("Download failed too recently, ignoring"));
}
retries = 1;
}
}
ResourceDownloader rd = rdf.getRetryDownloader(rdf.create(url, true), retries);
rd.addListener(this);
InputStream data = rd.download();
try {
SimpleXMLParserDocument res = parseXML(data);
synchronized (failed_urls) {
failed_urls.remove(url_str);
}
return (res);
} finally {
data.close();
}
} catch (Throwable e) {
if (record_failure) {
synchronized (failed_urls) {
if (failed_urls.size() >= 64) {
failed_urls.clear();
}
long[] fails = failed_urls.get(url_str);
if (fails == null) {
fails = new long[2];
failed_urls.put(url_str, fails);
}
fails[0]++;
fails[1] = SystemTime.getMonotonousTime();
}
adapter.log("Failed to parse XML from :" + url_str + ": " + Debug.getNestedExceptionMessageAndStack(e));
}
if (e instanceof UPnPException) {
throw ((UPnPException) e);
}
throw (new UPnPException("Root device location '" + url + "' - data read failed", e));
} finally {
TorrentUtils.setTLSDescription(null);
}
}
use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader 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:OSX 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:OSX 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("OSX: current = " + current_version + ", latest = " + sf_comp_version);
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "PlatformManager:OSX 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();
}
}
use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.
the class PluginUpdatePlugin method checkForUpdateSupport.
protected int checkForUpdateSupport(UpdateChecker checker, // explicit ids or null for all
String[] ids_to_check, boolean mandatory) {
int num_updates_found = 0;
try {
if ((!mandatory) && // allow custom actions through
(ids_to_check == null) && (!plugin_interface.getPluginconfig().getPluginBooleanParameter("enable.update", true))) {
return (num_updates_found);
}
PluginInterface[] plugins = plugin_interface.getPluginManager().getPlugins();
List plugins_to_check = new ArrayList();
List plugins_to_check_ids = new ArrayList();
Map plugins_to_check_names = new HashMap();
for (int i = 0; i < plugins.length; i++) {
PluginInterface pi = plugins[i];
if (pi.getPluginState().isDisabled()) {
if (!pi.getPluginState().hasFailed()) {
continue;
}
}
String mand = pi.getPluginProperties().getProperty("plugin.mandatory");
boolean pi_mandatory = mand != null && mand.trim().toLowerCase().equals("true");
if (pi_mandatory != mandatory) {
continue;
}
String id = pi.getPluginID();
String version = pi.getPluginVersion();
String name = pi.getPluginName();
if (ids_to_check != null) {
boolean id_selected = false;
for (int j = 0; j < ids_to_check.length; j++) {
if (ids_to_check[j].equals(id)) {
id_selected = true;
break;
}
}
if (!id_selected) {
continue;
}
}
if (version != null) {
if (plugins_to_check_ids.contains(id)) {
String s = (String) plugins_to_check_names.get(id);
if (!name.equals(id)) {
plugins_to_check_names.put(id, s + "," + name);
}
} else {
plugins_to_check_ids.add(id);
plugins_to_check.add(pi);
plugins_to_check_names.put(id, name.equals(id) ? "" : name);
}
}
String location = pi.getPluginDirectoryName();
log.log(LoggerChannel.LT_INFORMATION, (mandatory ? "*" : "-") + pi.getPluginName() + ", id = " + id + (version == null ? "" : (", version = " + pi.getPluginVersion())) + (location == null ? "" : (", loc = " + location)));
}
SFPluginDetailsLoader loader = SFPluginDetailsLoaderFactory.getSingleton();
if (!loader_listener_added) {
loader_listener_added = true;
loader.addListener(new SFPluginDetailsLoaderListener() {
@Override
public void log(String str) {
log.log(LoggerChannel.LT_INFORMATION, "[" + str + "]");
}
});
}
String[] ids = loader.getPluginIDs();
String id_info = "";
for (int i = 0; i < ids.length; i++) {
String id = ids[i];
SFPluginDetails details = loader.getPluginDetails(id);
id_info += (i == 0 ? "" : ",") + ids[i] + "=" + details.getVersion() + "/" + details.getCVSVersion();
}
if (!id_info.equals(last_id_info)) {
last_id_info = id_info;
log.log(LoggerChannel.LT_INFORMATION, "Downloaded plugin info = " + id_info);
}
for (int i = 0; i < plugins_to_check.size(); i++) {
if (checker.getCheckInstance().isCancelled()) {
// XXX: Uh?
throw (new Exception("Update check cancelled"));
}
final PluginInterface pi_being_checked = (PluginInterface) plugins_to_check.get(i);
final String plugin_id = pi_being_checked.getPluginID();
boolean found = false;
for (int j = 0; j < ids.length; j++) {
if (ids[j].equalsIgnoreCase(plugin_id)) {
found = true;
break;
}
}
if (!found) {
if (!pi_being_checked.getPluginState().isBuiltIn()) {
log.log(LoggerChannel.LT_INFORMATION, "Skipping " + plugin_id + " as not listed on web site");
}
continue;
}
String plugin_names = (String) plugins_to_check_names.get(plugin_id);
// final boolean plugin_unloadable = ((Boolean)plugins_to_check_unloadable.get( plugin_id )).booleanValue();
log.log(LoggerChannel.LT_INFORMATION, "Checking " + plugin_id);
try {
checker.reportProgress("Loading details for " + plugin_id + "/" + pi_being_checked.getPluginName());
SFPluginDetails details = loader.getPluginDetails(plugin_id);
if (plugin_names.length() == 0) {
plugin_names = details.getName();
}
boolean az_cvs = plugin_interface.getUtilities().isCVSVersion();
String pi_version_info = pi_being_checked.getPluginProperties().getProperty("plugin.version.info");
String az_plugin_version = pi_being_checked.getPluginVersion();
String sf_plugin_version = details.getVersion();
String sf_comp_version = sf_plugin_version;
if (az_cvs) {
String sf_cvs_version = details.getCVSVersion();
if (sf_cvs_version.length() > 0) {
// sf cvs version ALWAYS ends in _CVS
sf_plugin_version = sf_cvs_version;
sf_comp_version = sf_plugin_version.substring(0, sf_plugin_version.length() - 4);
}
}
if (sf_comp_version.length() == 0 || !Character.isDigit(sf_comp_version.charAt(0))) {
log.log(LoggerChannel.LT_INFORMATION, "Skipping " + plugin_id + " as no valid version to check");
continue;
}
// System.out.println("comp version = " + sf_comp_version );
int comp = PluginUtils.comparePluginVersions(az_plugin_version, sf_comp_version);
// if they're the same version and latest is CVS then stick a _CVS on
// the end of current to avoid confusion
log.log(LoggerChannel.LT_INFORMATION, " Current: " + az_plugin_version + (comp == 0 && sf_plugin_version.endsWith("_CVS") ? "_CVS" : "") + ", Latest: " + sf_plugin_version + (pi_version_info == null ? "" : " [" + pi_version_info + "]"));
checker.reportProgress(" current=" + az_plugin_version + (comp == 0 && sf_plugin_version.endsWith("_CVS") ? "_CVS" : "") + ", latest=" + sf_plugin_version);
if (comp < 0 && !(pi_being_checked.getPlugin() instanceof UpdatableComponent)) {
// only update if newer verison + plugin itself doesn't handle
// the update
String sf_plugin_download = details.getDownloadURL();
if (az_cvs) {
String sf_cvs_version = details.getCVSVersion();
if (sf_cvs_version.length() > 0) {
sf_plugin_download = details.getCVSDownloadURL();
}
}
log.log(LoggerChannel.LT_INFORMATION, " Description:");
List update_desc = new ArrayList();
List desc_lines = HTMLUtils.convertHTMLToText("", details.getDescription());
logMultiLine(" ", desc_lines);
update_desc.addAll(desc_lines);
log.log(LoggerChannel.LT_INFORMATION, " Comment:");
List comment_lines = HTMLUtils.convertHTMLToText(" ", details.getComment());
logMultiLine(" ", comment_lines);
update_desc.addAll(comment_lines);
String msg = "A newer version (version " + sf_plugin_version + ") of plugin '" + plugin_id + "' " + (plugin_names.length() == 0 ? "" : "(" + plugin_names + ") ") + "is available. ";
log.log(LoggerChannel.LT_INFORMATION, "");
log.log(LoggerChannel.LT_INFORMATION, " " + msg + "Download from " + sf_plugin_download);
ResourceDownloaderFactory rdf = plugin_interface.getUtilities().getResourceDownloaderFactory();
ResourceDownloader direct_rdl = rdf.create(new URL(sf_plugin_download));
ResourceDownloader direct_ap_rdl = rdf.createWithAutoPluginProxy(new URL(sf_plugin_download));
// work out what the torrent download will be, if it exists
// sf_plugin_download will be something like ../plugins/safepeer_2.4.zip
// torrent is safepeer_2.4.zip.torrent
String torrent_download = Constants.URL_PLUGINS_TORRENT_BASE;
int slash_pos = sf_plugin_download.lastIndexOf("/");
if (slash_pos == -1) {
torrent_download += sf_plugin_download;
} else {
torrent_download += sf_plugin_download.substring(slash_pos + 1);
}
torrent_download += ".torrent";
if (I2PHelpers.isI2PInstalled()) {
torrent_download += "?i2p=1";
}
ResourceDownloader torrent_rdl = rdf.create(new URL(torrent_download));
// ResourceDownloader torrent_ap_rdl = rdf.createWithAutoPluginProxy( 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, direct_ap_rdl });
// get size so it is cached
rdf.getTimeoutDownloader(rdf.getRetryDownloader(alternate_rdl, RD_SIZE_RETRIES), RD_SIZE_TIMEOUT).getSize();
String[] update_d = new String[update_desc.size()];
update_desc.toArray(update_d);
num_updates_found++;
// see if unloadable
boolean plugin_unloadable = true;
for (int j = 0; j < plugins.length; j++) {
PluginInterface pi = plugins[j];
if (pi.getPluginID().equals(plugin_id)) {
plugin_unloadable &= pi.getPluginState().isUnloadable();
}
}
if (plugin_unloadable) {
checker.reportProgress("Plugin is unloadable");
}
Update update = addUpdate(pi_being_checked, checker, plugin_id + "/" + plugin_names, update_d, az_plugin_version, sf_plugin_version, alternate_rdl, sf_plugin_download.toLowerCase().endsWith(".jar"), plugin_unloadable ? Update.RESTART_REQUIRED_NO : Update.RESTART_REQUIRED_YES, true);
update.setRelativeURLBase(details.getRelativeURLBase());
update.setDescriptionURL(details.getInfoURL());
}
} catch (Throwable e) {
checker.reportProgress("Failed to load details for plugin '" + plugin_id + "': " + Debug.getNestedExceptionMessage(e));
log.log(" Plugin check failed", e);
}
}
} catch (Throwable e) {
if (!"Update check cancelled".equals(e.getMessage())) {
log.log("Failed to load plugin details", e);
}
checker.reportProgress("Failed to load plugin details: " + Debug.getNestedExceptionMessage(e));
checker.failed();
} finally {
// any prior failure will take precedence
checker.completed();
}
return (num_updates_found);
}
Aggregations