mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-03 00:37:55 +00:00
Merge pull request #89 from Parabot/feature/nightly-client-support
[FEATURE] Added nightly support for Parabot
This commit is contained in:
@@ -15,7 +15,7 @@ public class Configuration {
|
||||
public static final String GET_SERVER_PROVIDER = "http://bdn.parabot.org/api/get.php?action=server_provider&name=";
|
||||
public static final String GET_SERVER_PROVIDER_INFO = "http://bdn.parabot.org/api/get.php?action=server_information&name=";
|
||||
public static final String GET_SERVER_SETTINGS = "http://bdn.parabot.org/api/get.php?action=get_settings";
|
||||
public static final String GET_BOT_VERSION = "http://v3.bdn.parabot.org/api/bot/list/client?latest=true";
|
||||
public static final String GET_BOT_VERSION = "http://bdn.parabot.org/api/v2/bot/version";
|
||||
public static final String API_DOWNLOAD_BOT = "http://bdn.parabot.org/api/v2/bot/download/client/";
|
||||
public static final String DOWNLOAD_BOT = "http://bdn.parabot.org/versions/";
|
||||
public static final String REGISTRATION_PAGE = "https://www.parabot.org/community/register/";
|
||||
@@ -23,8 +23,13 @@ public class Configuration {
|
||||
public static final String DATA_API = "http://bdn.parabot.org/api/v2/data/";
|
||||
public static final String ITEM_API = DATA_API + "items/";
|
||||
|
||||
public static final String COMPARE_VERSION_URL = "http://v3.bdn.parabot.org/api/bot/compare/%s/%s";
|
||||
public static final String COMPARE_CHECKSUM_URL = "http://v3.bdn.parabot.org/api/bot/checksum/%s/%s";
|
||||
|
||||
public static final Version BOT_VERSION = ProjectProperties.getProjectVersion();
|
||||
|
||||
public static final String BOT_TITLE = "Parabot";
|
||||
public static final String BOT_SLOGAN = "The best RuneScape private server bot";
|
||||
|
||||
public static final String NIGHTLY_APPEND = "nightly=true";
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import java.security.NoSuchAlgorithmException;
|
||||
*/
|
||||
@SuppressWarnings("Duplicates")
|
||||
public class Core {
|
||||
public static boolean mDebug;
|
||||
|
||||
private static boolean debug;
|
||||
private static boolean verbose;
|
||||
private static boolean dump;
|
||||
@@ -75,21 +75,21 @@ public class Core {
|
||||
* @param dump
|
||||
*/
|
||||
public static void setDump(final boolean dump) {
|
||||
Core.dump = dump;
|
||||
Core.dump = dump;
|
||||
}
|
||||
|
||||
public static void disableSec(){
|
||||
UILog.log(
|
||||
"Security Warning",
|
||||
"Disabling the securty manager is ill advised.\n"
|
||||
+ " Only do so if the client fails to load, or functions incorrectly (freezes,crashes, etc.)\n"
|
||||
+ "The security manager protects you from malicous code within the client, without it you are exposed!\n"
|
||||
+ "\nPlease contact Parabot staff to resolve whatever problem you are having!");
|
||||
Core.secure = false;
|
||||
|
||||
public static void disableSec() {
|
||||
UILog.log(
|
||||
"Security Warning",
|
||||
"Disabling the securty manager is ill advised.\n"
|
||||
+ " Only do so if the client fails to load, or functions incorrectly (freezes,crashes, etc.)\n"
|
||||
+ "The security manager protects you from malicous code within the client, without it you are exposed!\n"
|
||||
+ "\nPlease contact Parabot staff to resolve whatever problem you are having!");
|
||||
Core.secure = false;
|
||||
}
|
||||
|
||||
public static boolean isSecure(){
|
||||
return secure;
|
||||
|
||||
public static boolean isSecure() {
|
||||
return secure;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,7 +110,7 @@ public class Core {
|
||||
* @return if parabot should dump injected jar
|
||||
*/
|
||||
public static boolean shouldDump() {
|
||||
return dump;
|
||||
return dump;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,9 +130,10 @@ public class Core {
|
||||
|
||||
/**
|
||||
* Checks the version of the bot using a checksum of the jar comparison against checksum given by the website
|
||||
*
|
||||
* @return <b>true</b> if no new version is found, otherwise <b>false</b>.
|
||||
*/
|
||||
private static boolean checksumValid(){
|
||||
private static boolean checksumValid() {
|
||||
File f = new File(Landing.class.getProtectionDomain().getCodeSource().getLocation().getFile());
|
||||
if (f.isFile()) {
|
||||
try {
|
||||
@@ -151,18 +152,15 @@ public class Core {
|
||||
byte[] mdbytes = md.digest();
|
||||
|
||||
StringBuilder sb = new StringBuilder("");
|
||||
for (int i = 0; i < mdbytes.length; i++) {
|
||||
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
|
||||
for (byte mdbyte : mdbytes) {
|
||||
sb.append(Integer.toString((mdbyte & 0xff) + 0x100, 16).substring(1));
|
||||
}
|
||||
|
||||
String result;
|
||||
if ((result = WebUtil.getContents("http://bdn.parabot.org/api/v2/bot/checksum", "checksum=" + URLEncoder.encode(sb.toString(), "UTF-8"))) != null) {
|
||||
if ((result = WebUtil.getContents(String.format(Configuration.COMPARE_CHECKSUM_URL, "client", currentVersion.get()), "checksum=" + URLEncoder.encode(sb.toString(), "UTF-8"))) != null) {
|
||||
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(result);
|
||||
if (!(boolean) object.get("result")){
|
||||
Core.verbose("Latest checksum: " + sb.toString());
|
||||
Core.verbose("Latest checksum: " + object.get("current"));
|
||||
return false;
|
||||
}
|
||||
System.out.println(object.get("result"));
|
||||
return Boolean.parseBoolean((String) object.get("result"));
|
||||
}
|
||||
}
|
||||
} catch (NoSuchAlgorithmException | ParseException | IOException | URISyntaxException e) {
|
||||
@@ -172,64 +170,25 @@ public class Core {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Deprecated use #validVersion instead
|
||||
*
|
||||
* Checks the version of the bot using a variable comparison from the bot code and the Parabot website
|
||||
*
|
||||
* @return <b>true</b> if no new version is found, otherwise <b>false</b>.
|
||||
*/
|
||||
private static boolean versionValid(){
|
||||
BufferedReader br = WebUtil.getReader(Configuration.GET_BOT_VERSION);
|
||||
try {
|
||||
String version = null;
|
||||
if (br != null) {
|
||||
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(br);
|
||||
version = (String) object.get("result");
|
||||
}
|
||||
if (version != null) {
|
||||
if (!Configuration.BOT_VERSION.equals(version)) {
|
||||
Core.verbose("Our version: " + Configuration.BOT_VERSION);
|
||||
Core.verbose("Latest version: " + version);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (NumberFormatException | IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (br != null) {
|
||||
br.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the latest version from the BDN and the current version
|
||||
*
|
||||
* @return True if the current version is equal or higher than the latest version, false if lower than the latest version
|
||||
*/
|
||||
public static boolean validVersion() {
|
||||
BufferedReader br = WebUtil.getReader(Configuration.GET_BOT_VERSION);
|
||||
String url = String.format(Configuration.COMPARE_VERSION_URL, "client", currentVersion.get());
|
||||
|
||||
BufferedReader br = WebUtil.getReader(url);
|
||||
try {
|
||||
latestVersion = null;
|
||||
if (br != null) {
|
||||
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(br);
|
||||
latestVersion = new Version((String) object.get("version"));
|
||||
}
|
||||
if (latestVersion != null) {
|
||||
if (Configuration.BOT_VERSION.compareTo(latestVersion) < 0) {
|
||||
Core.verbose("Our version: " + Configuration.BOT_VERSION.get());
|
||||
Core.verbose("Latest version: " + latestVersion.get());
|
||||
return false;
|
||||
boolean latest = Boolean.parseBoolean((String) object.get("result"));
|
||||
if (!latest) {
|
||||
Directories.clearCache();
|
||||
}
|
||||
return latest;
|
||||
}
|
||||
} catch (NumberFormatException | IOException | ParseException e) {
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
@@ -247,13 +206,13 @@ public class Core {
|
||||
/**
|
||||
* Validates the cache and removes the cache contents if required
|
||||
*/
|
||||
private static void validateCache(){
|
||||
private static void validateCache() {
|
||||
File[] cache = Directories.getCachePath().listFiles();
|
||||
Integer lowest = null;
|
||||
if (cache != null) {
|
||||
for (File f : cache) {
|
||||
int date = (int) (f.lastModified()/ 1000);
|
||||
if (lowest == null || date < lowest){
|
||||
int date = (int) (f.lastModified() / 1000);
|
||||
if (lowest == null || date < lowest) {
|
||||
lowest = date;
|
||||
}
|
||||
}
|
||||
@@ -261,10 +220,10 @@ public class Core {
|
||||
|
||||
try {
|
||||
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(WebUtil.getContents("http://bdn.parabot.org/api/v2/bot/cache", "date=" + lowest));
|
||||
if ((boolean) object.get("result")){
|
||||
if ((boolean) object.get("result")) {
|
||||
Core.verbose("Making space for the latest cache files");
|
||||
Directories.clearCache();
|
||||
}else{
|
||||
} else {
|
||||
Core.verbose("Cache is up to date");
|
||||
}
|
||||
} catch (MalformedURLException | ParseException e) {
|
||||
@@ -281,18 +240,17 @@ public class Core {
|
||||
Core.verbose("Checking for updates...");
|
||||
validateCache();
|
||||
|
||||
if ((validVersion() && checksumValid()) || (!checksumValid() && currentVersion.compareTo(latestVersion) >= 0)){
|
||||
Core.verbose("No updates available.");
|
||||
return true;
|
||||
if (validate) {
|
||||
if (validVersion() && checksumValid()) {
|
||||
Core.verbose("No updates available.");
|
||||
return true;
|
||||
} else {
|
||||
Core.verbose("Updates available...");
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
Core.verbose("Updates available...");
|
||||
return false;
|
||||
Core.verbose("Validation disabled");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void debug(int i) {
|
||||
if(mDebug) {
|
||||
System.out.println("DEBUG: " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.parabot.core.io;
|
||||
|
||||
/**
|
||||
* @author JKetelaar
|
||||
*/
|
||||
public class NoProgressListener implements ProgressListener {
|
||||
@Override
|
||||
public void onProgressUpdate(double value) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDownloadSpeed(double mbPerSecond) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import org.parabot.core.Configuration;
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.Core;
|
||||
import org.parabot.core.Directories;
|
||||
import org.parabot.core.io.NoProgressListener;
|
||||
import org.parabot.core.io.ProgressListener;
|
||||
import org.parabot.environment.api.utils.WebUtil;
|
||||
|
||||
@@ -51,17 +52,7 @@ public class PublicRandoms extends RandomParser {
|
||||
return;
|
||||
}
|
||||
String downloadLink = Configuration.GET_RANDOMS;
|
||||
WebUtil.downloadFile(new URL(downloadLink), random, new ProgressListener() {
|
||||
@Override
|
||||
public void onProgressUpdate(double v) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDownloadSpeed(double v) {
|
||||
|
||||
}
|
||||
});
|
||||
WebUtil.downloadFile(new URL(downloadLink), random, new NoProgressListener());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -22,6 +22,10 @@ public class Version implements Comparable<Version> {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
public boolean isNightly(){
|
||||
return this.version.contains("RC");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Version that) {
|
||||
if (that == null) {
|
||||
|
||||
@@ -5,307 +5,323 @@ import org.parabot.core.io.ProgressListener;
|
||||
import org.parabot.core.io.SizeInputStream;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* A WebUtil class fetches data from an URL
|
||||
*
|
||||
*
|
||||
* @author Everel
|
||||
*
|
||||
*/
|
||||
public class WebUtil {
|
||||
|
||||
private static JSONParser jsonParser;
|
||||
private static JSONParser jsonParser;
|
||||
|
||||
private static String agent = "Mozilla/5.0 (Wind0ws NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
|
||||
private static String agent = "Mozilla/5.0 (Wind0ws NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
|
||||
|
||||
/**
|
||||
* Agent to set at a URL connection
|
||||
*
|
||||
* @param userAgent
|
||||
*/
|
||||
public static void setUserAgent(final String userAgent) {
|
||||
agent = userAgent;
|
||||
}
|
||||
/**
|
||||
* Agent to set at a URL connection
|
||||
*
|
||||
* @param userAgent
|
||||
*/
|
||||
public static void setUserAgent(final String userAgent) {
|
||||
agent = userAgent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets useragent
|
||||
*
|
||||
* @return useragent
|
||||
*/
|
||||
public static String getUserAgent() {
|
||||
return agent;
|
||||
}
|
||||
/**
|
||||
* Gets useragent
|
||||
*
|
||||
* @return useragent
|
||||
*/
|
||||
public static String getUserAgent() {
|
||||
return agent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches content of a page
|
||||
*
|
||||
* @param location
|
||||
* @return contents of page
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
public static String getContents(final String location)
|
||||
throws MalformedURLException {
|
||||
return getContents(new URL(location));
|
||||
}
|
||||
/**
|
||||
* Fetches content of a page
|
||||
*
|
||||
* @param location
|
||||
* @return contents of page
|
||||
* @throws MalformedURLException
|
||||
*/
|
||||
public static String getContents(final String location)
|
||||
throws MalformedURLException {
|
||||
return getContents(new URL(location));
|
||||
}
|
||||
|
||||
public static String getContents(final String location, String parameters) throws MalformedURLException {
|
||||
return getContents(new URL(location), parameters);
|
||||
}
|
||||
public static String getContents(final String location, String parameters) throws MalformedURLException {
|
||||
return getContents(new URL(location), parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get contents from URL
|
||||
*
|
||||
* @param url
|
||||
* @return page contents
|
||||
*/
|
||||
public static String getContents(final URL url) {
|
||||
return getContents(getConnection(url));
|
||||
}
|
||||
/**
|
||||
* Get contents from URL
|
||||
*
|
||||
* @param url
|
||||
* @return page contents
|
||||
*/
|
||||
public static String getContents(final URL url) {
|
||||
return getContents(getConnection(url));
|
||||
}
|
||||
|
||||
public static String getContents(final URL url, final String parameters) {
|
||||
return getContents(getConnection(url), parameters);
|
||||
}
|
||||
public static String getContents(final URL url, final String parameters) {
|
||||
return getContents(getConnection(url), parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets contents from URLConnection
|
||||
*
|
||||
* @param urlConnection
|
||||
* @return page contents
|
||||
*/
|
||||
public static String getContents(URLConnection urlConnection) {
|
||||
try {
|
||||
final BufferedReader in = getReader(urlConnection);
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
if (in != null) {
|
||||
while ((line = in.readLine()) != null) {
|
||||
/**
|
||||
* Gets contents from URLConnection
|
||||
*
|
||||
* @param urlConnection
|
||||
* @return page contents
|
||||
*/
|
||||
public static String getContents(URLConnection urlConnection) {
|
||||
try {
|
||||
final BufferedReader in = getReader(urlConnection);
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
if (in != null) {
|
||||
while ((line = in.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
return builder.toString();
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
return builder.toString();
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getContents(URLConnection urlConnection, String parameters) {
|
||||
try {
|
||||
urlConnection.setDoOutput(true);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
|
||||
wr.write(parameters);
|
||||
wr.flush();
|
||||
wr.close();
|
||||
public static String getContents(URLConnection urlConnection, String parameters) {
|
||||
try {
|
||||
urlConnection.setDoOutput(true);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
|
||||
wr.write(parameters);
|
||||
wr.flush();
|
||||
wr.close();
|
||||
|
||||
final BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
return builder.toString();
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
final BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
return builder.toString();
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets buffered reader from string url
|
||||
*
|
||||
* @param url
|
||||
* @return bufferedreader
|
||||
*/
|
||||
public static BufferedReader getReader(final String url) {
|
||||
try {
|
||||
return getReader(new URL(url));
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Gets buffered reader from string url
|
||||
*
|
||||
* @param url
|
||||
* @return bufferedreader
|
||||
*/
|
||||
public static BufferedReader getReader(final String url) {
|
||||
try {
|
||||
return getReader(new URL(url));
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets BufferedReader from URL
|
||||
*
|
||||
* @param url
|
||||
* @return BufferedReader from URL
|
||||
*/
|
||||
public static BufferedReader getReader(final URL url) {
|
||||
return getReader(getConnection(url));
|
||||
}
|
||||
/**
|
||||
* Gets BufferedReader from URL
|
||||
*
|
||||
* @param url
|
||||
* @return BufferedReader from URL
|
||||
*/
|
||||
public static BufferedReader getReader(final URL url) {
|
||||
return getReader(getConnection(url));
|
||||
}
|
||||
|
||||
public static BufferedReader getReader(final URLConnection urlConnection) {
|
||||
try {
|
||||
return new BufferedReader(new InputStreamReader(
|
||||
urlConnection.getInputStream()));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static BufferedReader getReader(final URLConnection urlConnection) {
|
||||
try {
|
||||
return new BufferedReader(new InputStreamReader(
|
||||
urlConnection.getInputStream()));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets inputstream from url
|
||||
*
|
||||
* @param url
|
||||
* @return inputstream from url
|
||||
*/
|
||||
public static InputStream getInputStream(final URL url) {
|
||||
final URLConnection con = getConnection(url);
|
||||
try {
|
||||
return con.getInputStream();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Gets inputstream from url
|
||||
*
|
||||
* @param url
|
||||
* @return inputstream from url
|
||||
*/
|
||||
public static InputStream getInputStream(final URL url) {
|
||||
final URLConnection con = getConnection(url);
|
||||
try {
|
||||
return con.getInputStream();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a connection
|
||||
*
|
||||
* @param url
|
||||
* @return URLConnection to URL
|
||||
*/
|
||||
public static URLConnection getConnection(final URL url) {
|
||||
try {
|
||||
final URLConnection con = url.openConnection();
|
||||
con.setRequestProperty("User-Agent", agent);
|
||||
return con;
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Opens a connection
|
||||
*
|
||||
* @param url
|
||||
* @return URLConnection to URL
|
||||
*/
|
||||
public static URLConnection getConnection(final URL url) {
|
||||
try {
|
||||
final URLConnection con = url.openConnection();
|
||||
con.setRequestProperty("User-Agent", agent);
|
||||
return con;
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static BufferedReader getReader(final URL url, String username, String password) {
|
||||
try {
|
||||
String data = URLEncoder.encode("username", "UTF-8") + "=" + username;
|
||||
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + password;
|
||||
|
||||
URLConnection connection = url.openConnection();
|
||||
|
||||
connection.setDoOutput(true);
|
||||
public static BufferedReader getReader(final URL url, String username, String password) {
|
||||
try {
|
||||
String data = URLEncoder.encode("username", "UTF-8") + "=" + username;
|
||||
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + password;
|
||||
|
||||
URLConnection connection = url.openConnection();
|
||||
|
||||
connection.setDoOutput(true);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
||||
wr.write(data);
|
||||
wr.flush();
|
||||
wr.close();
|
||||
|
||||
|
||||
return new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static URLConnection getConnection(final URL url, String username, String password) {
|
||||
try {
|
||||
String data = URLEncoder.encode("username", "UTF-8") + "=" + username;
|
||||
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + password;
|
||||
|
||||
URLConnection connection = url.openConnection();
|
||||
|
||||
connection.setDoOutput(true);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static URLConnection getConnection(final URL url, String username, String password) {
|
||||
try {
|
||||
String data = URLEncoder.encode("username", "UTF-8") + "=" + username;
|
||||
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + password;
|
||||
|
||||
URLConnection connection = url.openConnection();
|
||||
|
||||
connection.setDoOutput(true);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
||||
wr.write(data);
|
||||
wr.flush();
|
||||
wr.close();
|
||||
|
||||
|
||||
return connection;
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a file on the internet
|
||||
* @param url
|
||||
* @param destination
|
||||
* @param listener
|
||||
*/
|
||||
public static void downloadFile(final URL url, final File destination,
|
||||
final ProgressListener listener) {
|
||||
try {
|
||||
final URLConnection connection = getConnection(url);
|
||||
int size = connection.getContentLength();
|
||||
SizeInputStream sizeInputStream = new SizeInputStream(
|
||||
connection.getInputStream(), size, listener);
|
||||
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
|
||||
FileOutputStream fileOut = new FileOutputStream(destination);
|
||||
try {
|
||||
byte data[] = new byte[1024];
|
||||
int count;
|
||||
while ((count = in.read(data, 0, 1024)) != -1) {
|
||||
fileOut.write(data, 0, count);
|
||||
}
|
||||
} finally {
|
||||
if (in != null)
|
||||
in.close();
|
||||
if (fileOut != null)
|
||||
fileOut.close();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a file on the internet
|
||||
* @param url
|
||||
* @param destination
|
||||
* @param listener
|
||||
*/
|
||||
public static void downloadFile(final URL url, final File destination,
|
||||
final ProgressListener listener, String username, String password) {
|
||||
try {
|
||||
final URLConnection connection = getConnection(url, username, password);
|
||||
int size = connection.getContentLength();
|
||||
SizeInputStream sizeInputStream = new SizeInputStream(
|
||||
connection.getInputStream(), size, listener);
|
||||
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
|
||||
FileOutputStream fileOut = new FileOutputStream(destination);
|
||||
try {
|
||||
byte data[] = new byte[1024];
|
||||
int count;
|
||||
while ((count = in.read(data, 0, 1024)) != -1) {
|
||||
fileOut.write(data, 0, count);
|
||||
}
|
||||
} finally {
|
||||
if (in != null)
|
||||
in.close();
|
||||
if (fileOut != null)
|
||||
fileOut.close();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts file format to url format
|
||||
* @param file
|
||||
* @return url to file
|
||||
*/
|
||||
public static URL toURL(File file) {
|
||||
try {
|
||||
return file.toURI().toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Downloads a file on the internet
|
||||
*
|
||||
* @param url
|
||||
* @param destination
|
||||
* @param listener
|
||||
*/
|
||||
public static void downloadFile(final URL url, final File destination,
|
||||
final ProgressListener listener) {
|
||||
try {
|
||||
final URLConnection connection = getConnection(url);
|
||||
int size = connection.getContentLength();
|
||||
SizeInputStream sizeInputStream = new SizeInputStream(
|
||||
connection.getInputStream(), size, listener);
|
||||
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
|
||||
FileOutputStream fileOut = new FileOutputStream(destination);
|
||||
try {
|
||||
byte data[] = new byte[1024];
|
||||
int count;
|
||||
while ((count = in.read(data, 0, 1024)) != -1) {
|
||||
fileOut.write(data, 0, count);
|
||||
}
|
||||
} finally {
|
||||
if (in != null)
|
||||
in.close();
|
||||
if (fileOut != null)
|
||||
fileOut.close();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static JSONParser getJsonParser() {
|
||||
if (jsonParser == null){
|
||||
jsonParser = new JSONParser();
|
||||
}
|
||||
return jsonParser;
|
||||
}
|
||||
/**
|
||||
* Downloads a file on the internet
|
||||
*
|
||||
* @param url
|
||||
* @param destination
|
||||
* @param listener
|
||||
*/
|
||||
public static void downloadFile(final URL url, final File destination,
|
||||
final ProgressListener listener, String username, String password) {
|
||||
try {
|
||||
final URLConnection connection = getConnection(url, username, password);
|
||||
int size = connection.getContentLength();
|
||||
SizeInputStream sizeInputStream = new SizeInputStream(
|
||||
connection.getInputStream(), size, listener);
|
||||
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
|
||||
FileOutputStream fileOut = new FileOutputStream(destination);
|
||||
try {
|
||||
byte data[] = new byte[1024];
|
||||
int count;
|
||||
while ((count = in.read(data, 0, 1024)) != -1) {
|
||||
fileOut.write(data, 0, count);
|
||||
}
|
||||
} finally {
|
||||
if (in != null)
|
||||
in.close();
|
||||
if (fileOut != null)
|
||||
fileOut.close();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts file format to url format
|
||||
*
|
||||
* @param file
|
||||
* @return url to file
|
||||
*/
|
||||
public static URL toURL(File file) {
|
||||
try {
|
||||
return file.toURI().toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static JSONParser getJsonParser() {
|
||||
if (jsonParser == null) {
|
||||
jsonParser = new JSONParser();
|
||||
}
|
||||
return jsonParser;
|
||||
}
|
||||
|
||||
public static URI appendUri(String uri, String appendQuery) {
|
||||
try {
|
||||
URI oldUri = new URI(uri);
|
||||
|
||||
String newQuery = oldUri.getQuery();
|
||||
if (newQuery == null) {
|
||||
newQuery = appendQuery;
|
||||
} else {
|
||||
newQuery += "&" + appendQuery;
|
||||
}
|
||||
return new URI(oldUri.getScheme(), oldUri.getAuthority(),
|
||||
oldUri.getPath(), newQuery, oldUri.getFragment());
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user