Search in sources :

Example 1 with ProxyType

use of mage.remote.Connection.ProxyType in project mage by magefree.

the class CardImageUtils method getProxyFromPreferences.

public static Proxy getProxyFromPreferences() {
    Preferences prefs = MageFrame.getPreferences();
    Connection.ProxyType proxyType = Connection.ProxyType.valueByText(prefs.get("proxyType", "None"));
    if (proxyType != ProxyType.NONE) {
        String proxyServer = prefs.get("proxyAddress", "");
        int proxyPort = Integer.parseInt(prefs.get("proxyPort", "0"));
        return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyServer, proxyPort));
    }
    return null;
}
Also used : Proxy(java.net.Proxy) ProxyType(mage.remote.Connection.ProxyType) InetSocketAddress(java.net.InetSocketAddress) HttpURLConnection(java.net.HttpURLConnection) Connection(mage.remote.Connection) Preferences(java.util.prefs.Preferences)

Example 2 with ProxyType

use of mage.remote.Connection.ProxyType in project mage by magefree.

the class MagePaneMenuItem method performConnect.

private boolean performConnect(boolean reconnect) {
    if (currentConnection == null || !reconnect) {
        String server = MagePreferences.getLastServerAddress();
        int port = MagePreferences.getLastServerPort();
        String userName = MagePreferences.getLastServerUser();
        String password = MagePreferences.getLastServerPassword();
        String proxyServer = PREFS.get("proxyAddress", "");
        int proxyPort = Integer.parseInt(PREFS.get("proxyPort", "0"));
        ProxyType proxyType = ProxyType.valueByText(PREFS.get("proxyType", "None"));
        String proxyUsername = PREFS.get("proxyUsername", "");
        String proxyPassword = PREFS.get("proxyPassword", "");
        setCursor(new Cursor(Cursor.WAIT_CURSOR));
        currentConnection = new Connection();
        currentConnection.setUsername(userName);
        currentConnection.setPassword(password);
        currentConnection.setHost(server);
        currentConnection.setPort(port);
        // force to redownload db on updates
        boolean redownloadDatabase = (ExpansionRepository.instance.getSetByCode("GRN") == null || CardRepository.instance.findCard("Island") == null);
        currentConnection.setForceDBComparison(redownloadDatabase);
        String allMAC = "";
        try {
            allMAC = Connection.getMAC();
        } catch (SocketException ex) {
        }
        currentConnection.setUserIdStr(System.getProperty("user.name") + ":" + System.getProperty("os.name") + ":" + MagePreferences.getUserNames() + ":" + allMAC);
        currentConnection.setProxyType(proxyType);
        currentConnection.setProxyHost(proxyServer);
        currentConnection.setProxyPort(proxyPort);
        currentConnection.setProxyUsername(proxyUsername);
        currentConnection.setProxyPassword(proxyPassword);
        setUserPrefsToConnection(currentConnection);
    }
    try {
        LOGGER.debug("connecting (auto): " + currentConnection.getProxyType().toString() + ' ' + currentConnection.getProxyHost() + ' ' + currentConnection.getProxyPort() + ' ' + currentConnection.getProxyUsername());
        if (MageFrame.connect(currentConnection)) {
            prepareAndShowTablesPane();
            return true;
        } else {
            showMessage("Unable connect to server: " + SessionHandler.getLastConnectError());
        }
    } finally {
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }
    return false;
}
Also used : SocketException(java.net.SocketException) Connection(mage.remote.Connection) ProxyType(mage.remote.Connection.ProxyType)

Example 3 with ProxyType

use of mage.remote.Connection.ProxyType in project mage by magefree.

the class CardImageUtils method downloadHtmlDocument.

public static Document downloadHtmlDocument(String urlString) throws NumberFormatException, IOException {
    Preferences prefs = MageFrame.getPreferences();
    Connection.ProxyType proxyType = Connection.ProxyType.valueByText(prefs.get("proxyType", "None"));
    Document doc;
    if (proxyType == ProxyType.NONE) {
        doc = Jsoup.connect(urlString).timeout(60 * 1000).get();
    } else {
        String proxyServer = prefs.get("proxyAddress", "");
        int proxyPort = Integer.parseInt(prefs.get("proxyPort", "0"));
        URL url = new URL(urlString);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyServer, proxyPort));
        HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
        uc.setConnectTimeout(10000);
        uc.setReadTimeout(60000);
        uc.connect();
        String line;
        StringBuffer tmp = new StringBuffer();
        BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        while ((line = in.readLine()) != null) {
            tmp.append(line);
        }
        doc = Jsoup.parse(String.valueOf(tmp));
    }
    return doc;
}
Also used : ProxyType(mage.remote.Connection.ProxyType) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) HttpURLConnection(java.net.HttpURLConnection) Connection(mage.remote.Connection) Document(org.jsoup.nodes.Document) URL(java.net.URL) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) Preferences(java.util.prefs.Preferences)

Example 4 with ProxyType

use of mage.remote.Connection.ProxyType in project mage by magefree.

the class PreferencesDialog method showProxySettings.

// GEN-LAST:event_cbUseSameSettingsForReplacementEffectActionPerformed
private void showProxySettings() {
    Connection.ProxyType proxyType = (Connection.ProxyType) cbProxyType.getSelectedItem();
    switch(proxyType) {
        case SOCKS:
            this.pnlProxy.setVisible(true);
            this.pnlProxySettings.setVisible(true);
            break;
        case HTTP:
            this.pnlProxy.setVisible(true);
            this.pnlProxySettings.setVisible(true);
            break;
        case NONE:
            this.pnlProxy.setVisible(false);
            this.pnlProxySettings.setVisible(false);
            break;
        default:
            break;
    }
    this.pack();
    this.repaint();
}
Also used : ProxyType(mage.remote.Connection.ProxyType) Connection(mage.remote.Connection) ProxyType(mage.remote.Connection.ProxyType)

Example 5 with ProxyType

use of mage.remote.Connection.ProxyType in project mage by magefree.

the class PreferencesDialog method setProxyInformation.

public static void setProxyInformation(Connection connection) {
    ProxyType configProxyType = Connection.ProxyType.valueByText(getCachedValue(KEY_PROXY_TYPE, "None"));
    if (configProxyType == null) {
        return;
    }
    connection.setProxyType(configProxyType);
    if (configProxyType != ProxyType.NONE) {
        String host = getCachedValue(KEY_PROXY_ADDRESS, "");
        String port = getCachedValue(KEY_PROXY_PORT, "");
        if (!host.isEmpty() && !port.isEmpty()) {
            connection.setProxyHost(host);
            connection.setProxyPort(Integer.valueOf(port));
            String username = getCachedValue(KEY_PROXY_USERNAME, "");
            connection.setProxyUsername(username);
            if (getCachedValue(KEY_PROXY_REMEMBER, "false").equals("true")) {
                String password = getCachedValue(KEY_PROXY_PSWD, "");
                connection.setProxyPassword(password);
            }
        } else {
            logger.warn("host or\\and port are empty: host=" + host + ", port=" + port);
        }
    }
}
Also used : ProxyType(mage.remote.Connection.ProxyType)

Aggregations

ProxyType (mage.remote.Connection.ProxyType)5 Connection (mage.remote.Connection)4 HttpURLConnection (java.net.HttpURLConnection)2 InetSocketAddress (java.net.InetSocketAddress)2 Proxy (java.net.Proxy)2 Preferences (java.util.prefs.Preferences)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 SocketException (java.net.SocketException)1 URL (java.net.URL)1 Document (org.jsoup.nodes.Document)1