mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-06 16:50:39 +00:00
[FEATURE] Added nightly build support
This commit is contained in:
@@ -15,16 +15,19 @@ 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 = "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_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_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 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 DOWNLOAD_BOT = "http://bdn.parabot.org/versions/";
|
||||||
public static final String REGISTRATION_PAGE = "https://www.parabot.org/community/register/";
|
public static final String REGISTRATION_PAGE = "https://www.parabot.org/community/register/";
|
||||||
public static final String GET_RANDOMS = "http://v3.bdn.parabot.org/api/bot/download/randoms";
|
public static final String GET_RANDOMS = "http://v3.bdn.parabot.org/api/bot/download/randoms";
|
||||||
public static final String DATA_API = "http://bdn.parabot.org/api/v2/data/";
|
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 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 Version BOT_VERSION = ProjectProperties.getProjectVersion();
|
public static final Version BOT_VERSION = ProjectProperties.getProjectVersion();
|
||||||
|
|
||||||
public static final String BOT_TITLE = "Parabot";
|
public static final String BOT_TITLE = "Parabot";
|
||||||
public static final String BOT_SLOGAN = "The best RuneScape private server bot";
|
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")
|
@SuppressWarnings("Duplicates")
|
||||||
public class Core {
|
public class Core {
|
||||||
public static boolean mDebug;
|
|
||||||
private static boolean debug;
|
private static boolean debug;
|
||||||
private static boolean verbose;
|
private static boolean verbose;
|
||||||
private static boolean dump;
|
private static boolean dump;
|
||||||
@@ -75,21 +75,21 @@ public class Core {
|
|||||||
* @param dump
|
* @param dump
|
||||||
*/
|
*/
|
||||||
public static void setDump(final boolean dump) {
|
public static void setDump(final boolean dump) {
|
||||||
Core.dump = dump;
|
Core.dump = dump;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void disableSec(){
|
public static void disableSec() {
|
||||||
UILog.log(
|
UILog.log(
|
||||||
"Security Warning",
|
"Security Warning",
|
||||||
"Disabling the securty manager is ill advised.\n"
|
"Disabling the securty manager is ill advised.\n"
|
||||||
+ " Only do so if the client fails to load, or functions incorrectly (freezes,crashes, etc.)\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"
|
+ "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!");
|
+ "\nPlease contact Parabot staff to resolve whatever problem you are having!");
|
||||||
Core.secure = false;
|
Core.secure = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSecure(){
|
public static boolean isSecure() {
|
||||||
return secure;
|
return secure;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -110,7 +110,7 @@ public class Core {
|
|||||||
* @return if parabot should dump injected jar
|
* @return if parabot should dump injected jar
|
||||||
*/
|
*/
|
||||||
public static boolean shouldDump() {
|
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
|
* 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>.
|
* @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());
|
File f = new File(Landing.class.getProtectionDomain().getCodeSource().getLocation().getFile());
|
||||||
if (f.isFile()) {
|
if (f.isFile()) {
|
||||||
try {
|
try {
|
||||||
@@ -158,7 +159,7 @@ public class Core {
|
|||||||
String result;
|
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("http://bdn.parabot.org/api/v2/bot/checksum", "checksum=" + URLEncoder.encode(sb.toString(), "UTF-8"))) != null) {
|
||||||
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(result);
|
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(result);
|
||||||
if (!(boolean) object.get("result")){
|
if (!(boolean) object.get("result")) {
|
||||||
Core.verbose("Latest checksum: " + sb.toString());
|
Core.verbose("Latest checksum: " + sb.toString());
|
||||||
Core.verbose("Latest checksum: " + object.get("current"));
|
Core.verbose("Latest checksum: " + object.get("current"));
|
||||||
return false;
|
return false;
|
||||||
@@ -173,13 +174,12 @@ public class Core {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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>.
|
* @return <b>true</b> if no new version is found, otherwise <b>false</b>.
|
||||||
|
* @Deprecated use #validVersion instead
|
||||||
|
* <p>
|
||||||
|
* Checks the version of the bot using a variable comparison from the bot code and the Parabot website
|
||||||
*/
|
*/
|
||||||
private static boolean versionValid(){
|
private static boolean versionValid() {
|
||||||
BufferedReader br = WebUtil.getReader(Configuration.GET_BOT_VERSION);
|
BufferedReader br = WebUtil.getReader(Configuration.GET_BOT_VERSION);
|
||||||
try {
|
try {
|
||||||
String version = null;
|
String version = null;
|
||||||
@@ -215,21 +215,20 @@ public class Core {
|
|||||||
* @return True if the current version is equal or higher than the latest version, false if lower than the latest 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() {
|
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 {
|
try {
|
||||||
latestVersion = null;
|
latestVersion = null;
|
||||||
if (br != null) {
|
if (br != null) {
|
||||||
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(br);
|
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(br);
|
||||||
latestVersion = new Version((String) object.get("version"));
|
boolean latest = Boolean.parseBoolean((String) object.get("result"));
|
||||||
}
|
if (!latest){
|
||||||
if (latestVersion != null) {
|
Directories.clearCache();
|
||||||
if (Configuration.BOT_VERSION.compareTo(latestVersion) < 0) {
|
|
||||||
Core.verbose("Our version: " + Configuration.BOT_VERSION.get());
|
|
||||||
Core.verbose("Latest version: " + latestVersion.get());
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return latest;
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException | IOException | ParseException e) {
|
} catch (IOException | ParseException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
@@ -247,13 +246,13 @@ public class Core {
|
|||||||
/**
|
/**
|
||||||
* Validates the cache and removes the cache contents if required
|
* Validates the cache and removes the cache contents if required
|
||||||
*/
|
*/
|
||||||
private static void validateCache(){
|
private static void validateCache() {
|
||||||
File[] cache = Directories.getCachePath().listFiles();
|
File[] cache = Directories.getCachePath().listFiles();
|
||||||
Integer lowest = null;
|
Integer lowest = null;
|
||||||
if (cache != null) {
|
if (cache != null) {
|
||||||
for (File f : cache) {
|
for (File f : cache) {
|
||||||
int date = (int) (f.lastModified()/ 1000);
|
int date = (int) (f.lastModified() / 1000);
|
||||||
if (lowest == null || date < lowest){
|
if (lowest == null || date < lowest) {
|
||||||
lowest = date;
|
lowest = date;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -261,10 +260,10 @@ public class Core {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(WebUtil.getContents("http://bdn.parabot.org/api/v2/bot/cache", "date=" + lowest));
|
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");
|
Core.verbose("Making space for the latest cache files");
|
||||||
Directories.clearCache();
|
Directories.clearCache();
|
||||||
}else{
|
} else {
|
||||||
Core.verbose("Cache is up to date");
|
Core.verbose("Cache is up to date");
|
||||||
}
|
}
|
||||||
} catch (MalformedURLException | ParseException e) {
|
} catch (MalformedURLException | ParseException e) {
|
||||||
@@ -281,18 +280,12 @@ public class Core {
|
|||||||
Core.verbose("Checking for updates...");
|
Core.verbose("Checking for updates...");
|
||||||
validateCache();
|
validateCache();
|
||||||
|
|
||||||
if ((validVersion() && checksumValid()) || (!checksumValid() && currentVersion.compareTo(latestVersion) >= 0)){
|
if (validVersion() && checksumValid()) {
|
||||||
Core.verbose("No updates available.");
|
Core.verbose("No updates available.");
|
||||||
return true;
|
return true;
|
||||||
}else{
|
} else {
|
||||||
Core.verbose("Updates available...");
|
Core.verbose("Updates available...");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void debug(int i) {
|
|
||||||
if(mDebug) {
|
|
||||||
System.out.println("DEBUG: " + i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -22,6 +22,10 @@ public class Version implements Comparable<Version> {
|
|||||||
return this.version;
|
return this.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isNightly(){
|
||||||
|
return this.version.contains("RC");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Version that) {
|
public int compareTo(Version that) {
|
||||||
if (that == null) {
|
if (that == null) {
|
||||||
|
|||||||
@@ -8,315 +8,320 @@ import java.io.*;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* A WebUtil class fetches data from an URL
|
* A WebUtil class fetches data from an URL
|
||||||
*
|
*
|
||||||
* @author Everel
|
* @author Everel
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class WebUtil {
|
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
|
* Agent to set at a URL connection
|
||||||
*
|
*
|
||||||
* @param userAgent
|
* @param userAgent
|
||||||
*/
|
*/
|
||||||
public static void setUserAgent(final String userAgent) {
|
public static void setUserAgent(final String userAgent) {
|
||||||
agent = userAgent;
|
agent = userAgent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets useragent
|
* Gets useragent
|
||||||
*
|
*
|
||||||
* @return useragent
|
* @return useragent
|
||||||
*/
|
*/
|
||||||
public static String getUserAgent() {
|
public static String getUserAgent() {
|
||||||
return agent;
|
return agent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches content of a page
|
* Fetches content of a page
|
||||||
*
|
*
|
||||||
* @param location
|
* @param location
|
||||||
* @return contents of page
|
* @return contents of page
|
||||||
* @throws MalformedURLException
|
* @throws MalformedURLException
|
||||||
*/
|
*/
|
||||||
public static String getContents(final String location)
|
public static String getContents(final String location)
|
||||||
throws MalformedURLException {
|
throws MalformedURLException {
|
||||||
return getContents(new URL(location));
|
return getContents(new URL(location));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getContents(final String location, String parameters) throws MalformedURLException {
|
public static String getContents(final String location, String parameters) throws MalformedURLException {
|
||||||
return getContents(new URL(location), parameters);
|
return getContents(new URL(location), parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get contents from URL
|
* Get contents from URL
|
||||||
*
|
*
|
||||||
* @param url
|
* @param url
|
||||||
* @return page contents
|
* @return page contents
|
||||||
*/
|
*/
|
||||||
public static String getContents(final URL url) {
|
public static String getContents(final URL url) {
|
||||||
return getContents(getConnection(url));
|
return getContents(getConnection(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getContents(final URL url, final String parameters) {
|
public static String getContents(final URL url, final String parameters) {
|
||||||
return getContents(getConnection(url), parameters);
|
return getContents(getConnection(url), parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets contents from URLConnection
|
* Gets contents from URLConnection
|
||||||
*
|
*
|
||||||
* @param urlConnection
|
* @param urlConnection
|
||||||
* @return page contents
|
* @return page contents
|
||||||
*/
|
*/
|
||||||
public static String getContents(URLConnection urlConnection) {
|
public static String getContents(URLConnection urlConnection) {
|
||||||
try {
|
try {
|
||||||
final BufferedReader in = getReader(urlConnection);
|
final BufferedReader in = getReader(urlConnection);
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
String line;
|
String line;
|
||||||
if (in != null) {
|
if (in != null) {
|
||||||
while ((line = in.readLine()) != null) {
|
while ((line = in.readLine()) != null) {
|
||||||
builder.append(line);
|
builder.append(line);
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getContents(URLConnection urlConnection, String parameters) {
|
public static String getContents(URLConnection urlConnection, String parameters) {
|
||||||
try {
|
try {
|
||||||
urlConnection.setDoOutput(true);
|
urlConnection.setDoOutput(true);
|
||||||
OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
|
OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
|
||||||
wr.write(parameters);
|
wr.write(parameters);
|
||||||
wr.flush();
|
wr.flush();
|
||||||
wr.close();
|
wr.close();
|
||||||
|
|
||||||
final BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
|
final BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
String line;
|
String line;
|
||||||
while ((line = in.readLine()) != null) {
|
while ((line = in.readLine()) != null) {
|
||||||
builder.append(line);
|
builder.append(line);
|
||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets buffered reader from string url
|
* Gets buffered reader from string url
|
||||||
*
|
*
|
||||||
* @param url
|
* @param url
|
||||||
* @return bufferedreader
|
* @return bufferedreader
|
||||||
*/
|
*/
|
||||||
public static BufferedReader getReader(final String url) {
|
public static BufferedReader getReader(final String url) {
|
||||||
try {
|
try {
|
||||||
return getReader(new URL(url));
|
return getReader(new URL(url));
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets BufferedReader from URL
|
* Gets BufferedReader from URL
|
||||||
*
|
*
|
||||||
* @param url
|
* @param url
|
||||||
* @return BufferedReader from URL
|
* @return BufferedReader from URL
|
||||||
*/
|
*/
|
||||||
public static BufferedReader getReader(final URL url) {
|
public static BufferedReader getReader(final URL url) {
|
||||||
return getReader(getConnection(url));
|
return getReader(getConnection(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BufferedReader getReader(final URLConnection urlConnection) {
|
public static BufferedReader getReader(final URLConnection urlConnection) {
|
||||||
try {
|
try {
|
||||||
return new BufferedReader(new InputStreamReader(
|
return new BufferedReader(new InputStreamReader(
|
||||||
urlConnection.getInputStream()));
|
urlConnection.getInputStream()));
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets inputstream from url
|
* Gets inputstream from url
|
||||||
*
|
*
|
||||||
* @param url
|
* @param url
|
||||||
* @return inputstream from url
|
* @return inputstream from url
|
||||||
*/
|
*/
|
||||||
public static InputStream getInputStream(final URL url) {
|
public static InputStream getInputStream(final URL url) {
|
||||||
final URLConnection con = getConnection(url);
|
final URLConnection con = getConnection(url);
|
||||||
try {
|
try {
|
||||||
return con.getInputStream();
|
return con.getInputStream();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens a connection
|
* Opens a connection
|
||||||
*
|
*
|
||||||
* @param url
|
* @param url
|
||||||
* @return URLConnection to URL
|
* @return URLConnection to URL
|
||||||
*/
|
*/
|
||||||
public static URLConnection getConnection(final URL url) {
|
public static URLConnection getConnection(final URL url) {
|
||||||
try {
|
try {
|
||||||
final URLConnection con = url.openConnection();
|
final URLConnection con = url.openConnection();
|
||||||
con.setRequestProperty("User-Agent", agent);
|
con.setRequestProperty("User-Agent", agent);
|
||||||
return con;
|
return con;
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BufferedReader getReader(final URL url, String username, String password) {
|
public static BufferedReader getReader(final URL url, String username, String password) {
|
||||||
try {
|
try {
|
||||||
String data = URLEncoder.encode("username", "UTF-8") + "=" + username;
|
String data = URLEncoder.encode("username", "UTF-8") + "=" + username;
|
||||||
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + password;
|
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + password;
|
||||||
|
|
||||||
URLConnection connection = url.openConnection();
|
URLConnection connection = url.openConnection();
|
||||||
|
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
||||||
wr.write(data);
|
wr.write(data);
|
||||||
wr.flush();
|
wr.flush();
|
||||||
wr.close();
|
wr.close();
|
||||||
|
|
||||||
return new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
return new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static URLConnection getConnection(final URL url, String username, String password) {
|
public static URLConnection getConnection(final URL url, String username, String password) {
|
||||||
try {
|
try {
|
||||||
String data = URLEncoder.encode("username", "UTF-8") + "=" + username;
|
String data = URLEncoder.encode("username", "UTF-8") + "=" + username;
|
||||||
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + password;
|
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + password;
|
||||||
|
|
||||||
URLConnection connection = url.openConnection();
|
URLConnection connection = url.openConnection();
|
||||||
|
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
||||||
wr.write(data);
|
wr.write(data);
|
||||||
wr.flush();
|
wr.flush();
|
||||||
wr.close();
|
wr.close();
|
||||||
|
|
||||||
return connection;
|
return connection;
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads a file on the internet
|
* Downloads a file on the internet
|
||||||
* @param url
|
*
|
||||||
* @param destination
|
* @param url
|
||||||
* @param listener
|
* @param destination
|
||||||
*/
|
* @param listener
|
||||||
public static void downloadFile(final URL url, final File destination,
|
*/
|
||||||
final ProgressListener listener) {
|
public static void downloadFile(final URL url, final File destination,
|
||||||
try {
|
final ProgressListener listener) {
|
||||||
final URLConnection connection = getConnection(url);
|
try {
|
||||||
int size = connection.getContentLength();
|
final URLConnection connection = getConnection(url);
|
||||||
SizeInputStream sizeInputStream = new SizeInputStream(
|
int size = connection.getContentLength();
|
||||||
connection.getInputStream(), size, listener);
|
SizeInputStream sizeInputStream = new SizeInputStream(
|
||||||
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
|
connection.getInputStream(), size, listener);
|
||||||
FileOutputStream fileOut = new FileOutputStream(destination);
|
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
|
||||||
try {
|
FileOutputStream fileOut = new FileOutputStream(destination);
|
||||||
byte data[] = new byte[1024];
|
try {
|
||||||
int count;
|
byte data[] = new byte[1024];
|
||||||
while ((count = in.read(data, 0, 1024)) != -1) {
|
int count;
|
||||||
fileOut.write(data, 0, count);
|
while ((count = in.read(data, 0, 1024)) != -1) {
|
||||||
}
|
fileOut.write(data, 0, count);
|
||||||
} finally {
|
}
|
||||||
if (in != null)
|
} finally {
|
||||||
in.close();
|
if (in != null)
|
||||||
if (fileOut != null)
|
in.close();
|
||||||
fileOut.close();
|
if (fileOut != null)
|
||||||
}
|
fileOut.close();
|
||||||
} catch (Throwable t) {
|
}
|
||||||
t.printStackTrace();
|
} catch (Throwable t) {
|
||||||
}
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads a file on the internet
|
* Downloads a file on the internet
|
||||||
* @param url
|
*
|
||||||
* @param destination
|
* @param url
|
||||||
* @param listener
|
* @param destination
|
||||||
*/
|
* @param listener
|
||||||
public static void downloadFile(final URL url, final File destination,
|
*/
|
||||||
final ProgressListener listener, String username, String password) {
|
public static void downloadFile(final URL url, final File destination,
|
||||||
try {
|
final ProgressListener listener, String username, String password) {
|
||||||
final URLConnection connection = getConnection(url, username, password);
|
try {
|
||||||
int size = connection.getContentLength();
|
final URLConnection connection = getConnection(url, username, password);
|
||||||
SizeInputStream sizeInputStream = new SizeInputStream(
|
int size = connection.getContentLength();
|
||||||
connection.getInputStream(), size, listener);
|
SizeInputStream sizeInputStream = new SizeInputStream(
|
||||||
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
|
connection.getInputStream(), size, listener);
|
||||||
FileOutputStream fileOut = new FileOutputStream(destination);
|
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
|
||||||
try {
|
FileOutputStream fileOut = new FileOutputStream(destination);
|
||||||
byte data[] = new byte[1024];
|
try {
|
||||||
int count;
|
byte data[] = new byte[1024];
|
||||||
while ((count = in.read(data, 0, 1024)) != -1) {
|
int count;
|
||||||
fileOut.write(data, 0, count);
|
while ((count = in.read(data, 0, 1024)) != -1) {
|
||||||
}
|
fileOut.write(data, 0, count);
|
||||||
} finally {
|
}
|
||||||
if (in != null)
|
} finally {
|
||||||
in.close();
|
if (in != null)
|
||||||
if (fileOut != null)
|
in.close();
|
||||||
fileOut.close();
|
if (fileOut != null)
|
||||||
}
|
fileOut.close();
|
||||||
} catch (Throwable t) {
|
}
|
||||||
t.printStackTrace();
|
} catch (Throwable t) {
|
||||||
}
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts file format to url format
|
* Converts file format to url format
|
||||||
* @param file
|
*
|
||||||
* @return url to file
|
* @param file
|
||||||
*/
|
* @return url to file
|
||||||
public static URL toURL(File file) {
|
*/
|
||||||
try {
|
public static URL toURL(File file) {
|
||||||
return file.toURI().toURL();
|
try {
|
||||||
} catch (MalformedURLException e) {
|
return file.toURI().toURL();
|
||||||
e.printStackTrace();
|
} catch (MalformedURLException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
return null;
|
}
|
||||||
}
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static JSONParser getJsonParser() {
|
public static JSONParser getJsonParser() {
|
||||||
if (jsonParser == null){
|
if (jsonParser == null) {
|
||||||
jsonParser = new JSONParser();
|
jsonParser = new JSONParser();
|
||||||
}
|
}
|
||||||
return jsonParser;
|
return jsonParser;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static URI appendUri(String uri, String appendQuery) throws URISyntaxException {
|
public static URI appendUri(String uri, String appendQuery) {
|
||||||
URI oldUri = new URI(uri);
|
try {
|
||||||
|
URI oldUri = new URI(uri);
|
||||||
|
|
||||||
String newQuery = oldUri.getQuery();
|
String newQuery = oldUri.getQuery();
|
||||||
if (newQuery == null) {
|
if (newQuery == null) {
|
||||||
newQuery = appendQuery;
|
newQuery = appendQuery;
|
||||||
} else {
|
} else {
|
||||||
newQuery += "&" + appendQuery;
|
newQuery += "&" + appendQuery;
|
||||||
}
|
}
|
||||||
|
return new URI(oldUri.getScheme(), oldUri.getAuthority(),
|
||||||
return new URI(oldUri.getScheme(), oldUri.getAuthority(),
|
oldUri.getPath(), newQuery, oldUri.getFragment());
|
||||||
oldUri.getPath(), newQuery, oldUri.getFragment());
|
} catch (URISyntaxException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user