Login post added

This commit is contained in:
Parnassian
2014-03-04 19:34:14 +01:00
parent d7209aa151
commit f559e8aeb5
5 changed files with 115 additions and 7 deletions
@@ -15,11 +15,11 @@ import org.parabot.environment.api.utils.WebUtil;
public class ServerProviderInfo {
private Properties properties;
public ServerProviderInfo(URL providerInfo) {
public ServerProviderInfo(URL providerInfo, String username, String password) {
this.properties = new Properties();
try {
String line;
BufferedReader br = WebUtil.getReader(providerInfo);
BufferedReader br = WebUtil.getReader(providerInfo, username, password);
while ((line = br.readLine()) != null) {
if(line.contains(": ")) {
properties.put(line.substring(0, line.indexOf(": ")), line.substring(line.indexOf(": ") + 2, line.length()));
@@ -3,10 +3,13 @@ package org.parabot.core.forum;
import org.parabot.core.Configuration;
import org.parabot.core.Core;
import org.parabot.core.parsers.scripts.SDNScripts;
import org.parabot.core.parsers.servers.PublicServers;
import org.parabot.core.ui.LoginUI;
import org.parabot.environment.api.utils.WebUtil;
import org.parabot.environment.scripts.SDNScriptExecuter;
import org.parabot.environment.servers.PublicServerExecuter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
@@ -38,6 +41,8 @@ public final class AccountManager {
accessors.add(SDNScripts.MANAGER_FETCHER);
accessors.add(LoginUI.MANAGER_FETCHER);
accessors.add(SDNScriptExecuter.MANAGER_FETCHER);
accessors.add(PublicServers.MANAGER_FETCHER);
accessors.add(PublicServerExecuter.MANAGER_FETCHER);
for (final AccountManagerAccess accessor : accessors) {
accessor.setManager(instance);
@@ -69,7 +74,11 @@ public final class AccountManager {
}
if (contents.equals("correct")) {
account = new Account(user, pass);
try {
account = new Account(URLEncoder.encode(user, "UTF-8"), URLEncoder.encode(pass, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return true;
}
return false;
@@ -2,6 +2,8 @@ package org.parabot.core.parsers.servers;
import org.parabot.core.Configuration;
import org.parabot.core.desc.ServerDescription;
import org.parabot.core.forum.AccountManager;
import org.parabot.core.forum.AccountManagerAccess;
import org.parabot.environment.api.utils.WebUtil;
import org.parabot.environment.servers.PublicServerExecuter;
@@ -14,12 +16,23 @@ import java.net.URL;
* @author Everel
*/
public class PublicServers extends ServerParser {
private static AccountManager manager;
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
@Override
public final void setManager(AccountManager manager) {
PublicServers.manager = manager;
}
};
@Override
public void execute() {
try {
BufferedReader br = WebUtil.getReader(new URL(
Configuration.GET_SERVER_PROVIDERS));
Configuration.GET_SERVER_PROVIDERS), manager.getAccount().getUsername(), manager.getAccount().getPassword());
int count = 0;
String line;
@@ -56,5 +69,4 @@ public class PublicServers extends ServerParser {
e.printStackTrace();
}
}
}
@@ -7,9 +7,11 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.parabot.core.io.ProgressListener;
import org.parabot.core.io.SizeInputStream;
@@ -153,6 +155,46 @@ public class WebUtil {
}
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);
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);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
return connection;
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
/**
* Downloads a file on the internet
@@ -186,6 +228,38 @@ public class WebUtil {
}
}
/**
* 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
@@ -7,6 +7,8 @@ import org.parabot.core.Directories;
import org.parabot.core.build.BuildPath;
import org.parabot.core.classpath.ClassPath;
import org.parabot.core.desc.ServerProviderInfo;
import org.parabot.core.forum.AccountManager;
import org.parabot.core.forum.AccountManagerAccess;
import org.parabot.core.ui.components.VerboseLoader;
import org.parabot.core.ui.utils.UILog;
import org.parabot.environment.api.utils.WebUtil;
@@ -28,6 +30,17 @@ import java.net.URL;
public class PublicServerExecuter extends ServerExecuter {
private String serverName;
private String serverID;
private static AccountManager manager;
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
@Override
public final void setManager(AccountManager manager) {
PublicServerExecuter.manager = manager;
}
};
public PublicServerExecuter(final String serverName, final String serverID) {
this.serverName = serverName;
@@ -48,7 +61,7 @@ public class PublicServerExecuter extends ServerExecuter {
}
ServerProviderInfo serverProviderInfo = new ServerProviderInfo(new URL(Configuration.GET_SERVER_PROVIDER_INFO
+ this.serverID));
+ this.serverID), manager.getAccount().getUsername(), manager.getAccount().getPassword());
final File destination = new File(Directories.getCachePath(),
serverProviderInfo.getCRC32() + ".jar");
@@ -62,7 +75,7 @@ public class PublicServerExecuter extends ServerExecuter {
Core.verbose("Found cached server provider [CRC32: " + serverProviderInfo.getCRC32() + "]");
} else {
WebUtil.downloadFile(new URL(jarUrl), destination,
VerboseLoader.get());
VerboseLoader.get(), manager.getAccount().getUsername(), manager.getAccount().getPassword());
Core.verbose("Server provider downloaded...");
}