Search in sources :

Example 76 with Authenticator

use of java.net.Authenticator in project fess by codelibs.

the class FessProp method getHttpProxy.

default Proxy getHttpProxy() {
    Proxy proxy = (Proxy) propMap.get(HTML_PROXY);
    if (proxy == null) {
        if (StringUtil.isNotBlank(getHttpProxyHost()) && getHttpProxyPortAsInteger() != null) {
            final SocketAddress addr = new InetSocketAddress(getHttpProxyHost(), getHttpProxyPortAsInteger());
            proxy = new Proxy(Type.HTTP, addr);
            if (StringUtil.isNotBlank(getHttpProxyUsername())) {
                Authenticator.setDefault(new Authenticator() {

                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(getHttpProxyUsername(), getHttpProxyPassword().toCharArray());
                    }
                });
            }
        } else {
            proxy = Proxy.NO_PROXY;
        }
        propMap.put(HTML_PROXY, proxy);
    }
    return proxy;
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 77 with Authenticator

use of java.net.Authenticator in project apjp by jvansteirteghem.

the class Main method onCreate.

public void onCreate() {
    try {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        APJP.APJP_KEY = sharedPreferences.getString("APJP_KEY", "");
        APJP.APJP_LOGGER_ID = "APJP";
        APJP.APJP_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_PROXY_SERVER_LOGGER_ID = "APJP_LOCAL_PROXY_SERVER";
        APJP.APJP_LOCAL_PROXY_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_HTTP_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTP_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTP_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTP_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTP_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTP_PROXY_SERVER_LOGGER_ID = "APJP_LOCAL_HTTP_PROXY_SERVER";
        APJP.APJP_LOCAL_HTTP_PROXY_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_HTTP_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTP_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTP_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTP_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTP_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTP_SERVER_LOGGER_ID = "APJP_LOCAL_HTTP_SERVER";
        APJP.APJP_LOCAL_HTTP_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_URL = new String[10];
        APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY = new String[10][5];
        APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_VALUE = new String[10][5];
        for (int i = 0; i < APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY.length; i = i + 1) {
            APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_URL[i] = sharedPreferences.getString("APJP_REMOTE_HTTP_SERVER_" + (i + 1) + "_REQUEST_URL", "");
            for (int j = 0; j < APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY[i].length; j = j + 1) {
                APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTP_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_KEY", "");
                APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_VALUE[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTP_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_VALUE", "");
            }
        }
        APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTPS_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTPS_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_LOGGER_ID = "APJP_LOCAL_HTTPS_PROXY_SERVER";
        APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_HTTPS_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTPS_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTPS_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTPS_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTPS_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTPS_SERVER_LOGGER_ID = "APJP_LOCAL_HTTPS_SERVER";
        APJP.APJP_LOCAL_HTTPS_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL = new String[10];
        APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY = new String[10][5];
        APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_VALUE = new String[10][5];
        for (int i = 0; i < APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY.length; i = i + 1) {
            APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL[i] = sharedPreferences.getString("APJP_REMOTE_HTTPS_SERVER_" + (i + 1) + "_REQUEST_URL", "");
            for (int j = 0; j < APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i].length; j = j + 1) {
                APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTPS_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_KEY", "");
                APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_VALUE[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTPS_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_VALUE", "");
            }
        }
        APJP.APJP_HTTP_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_HTTP_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_HTTP_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_HTTP_PROXY_SERVER_USERNAME = sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_USERNAME", "");
        APJP.APJP_HTTP_PROXY_SERVER_PASSWORD = sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_PASSWORD", "");
        APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_HTTPS_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_HTTPS_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_HTTPS_PROXY_SERVER_USERNAME = sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_USERNAME", "");
        APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD = sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_PASSWORD", "");
        APJP.APJP_APPLICATION = getApplication();
        Authenticator.setDefault(new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication passwordAuthentication = null;
                if (this.getRequestorType() == Authenticator.RequestorType.PROXY) {
                    if (this.getRequestingURL().getProtocol().equalsIgnoreCase("HTTP") == true) {
                        passwordAuthentication = new PasswordAuthentication(APJP.APJP_HTTP_PROXY_SERVER_USERNAME, APJP.APJP_HTTP_PROXY_SERVER_PASSWORD.toCharArray());
                    } else {
                        if (this.getRequestingURL().getProtocol().equalsIgnoreCase("HTTPS") == true) {
                            passwordAuthentication = new PasswordAuthentication(APJP.APJP_HTTPS_PROXY_SERVER_USERNAME, APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD.toCharArray());
                        }
                    }
                }
                return passwordAuthentication;
            }
        });
        logger = Logger.getLogger(APJP.APJP_LOGGER_ID);
        proxyServer = new ProxyServer();
    } catch (Exception e) {
        logger.log(1, "EXCEPTION", e);
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 78 with Authenticator

use of java.net.Authenticator in project maven-plugins by apache.

the class ProjectInfoReportUtils method getContent.

/**
 * Get the input stream from a URL.
 *
 * @param url not null
 * @param project could be null
 * @param settings not null to handle proxy settings
 * @param encoding the wanted encoding for the URL input stream. If null, UTF-8 will be used.
 * @return the input stream decoded with the wanted encoding as string
 * @throws IOException if any
 * @since 2.3
 */
public static String getContent(URL url, MavenProject project, Settings settings, String encoding) throws IOException {
    String scheme = url.getProtocol();
    if (StringUtils.isEmpty(encoding)) {
        encoding = DEFAULT_ENCODING;
    }
    if ("file".equals(scheme)) {
        InputStream in = null;
        try {
            URLConnection conn = url.openConnection();
            in = conn.getInputStream();
            final String content = IOUtil.toString(in, encoding);
            in.close();
            in = null;
            return content;
        } finally {
            IOUtil.close(in);
        }
    }
    Proxy proxy = settings.getActiveProxy();
    if (proxy != null) {
        if ("http".equals(scheme) || "https".equals(scheme) || "ftp".equals(scheme)) {
            scheme += ".";
        } else {
            scheme = "";
        }
        String host = proxy.getHost();
        if (!StringUtils.isEmpty(host)) {
            Properties p = System.getProperties();
            p.setProperty(scheme + "proxySet", "true");
            p.setProperty(scheme + "proxyHost", host);
            p.setProperty(scheme + "proxyPort", String.valueOf(proxy.getPort()));
            if (!StringUtils.isEmpty(proxy.getNonProxyHosts())) {
                p.setProperty(scheme + "nonProxyHosts", proxy.getNonProxyHosts());
            }
            final String userName = proxy.getUsername();
            if (!StringUtils.isEmpty(userName)) {
                final String pwd = StringUtils.isEmpty(proxy.getPassword()) ? "" : proxy.getPassword();
                Authenticator.setDefault(new Authenticator() {

                    /**
                     * {@inheritDoc}
                     */
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(userName, pwd.toCharArray());
                    }
                });
            }
        }
    }
    InputStream in = null;
    try {
        URLConnection conn = getURLConnection(url, project, settings);
        in = conn.getInputStream();
        final String string = IOUtil.toString(in, encoding);
        in.close();
        in = null;
        return string;
    } finally {
        IOUtil.close(in);
    }
}
Also used : Proxy(org.apache.maven.settings.Proxy) InputStream(java.io.InputStream) Properties(java.util.Properties) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 79 with Authenticator

use of java.net.Authenticator in project i2p.i2p-bote by i2p.

the class ProxyRequest method doURLRequest.

/**
 * This function makes an HTTP GET request of the specified URL using a proxy if provided.
 * If successful, the HTTPURLConnection is returned.
 *
 * @param strURL A string representing the URL to request, eg, "http://sponge.i2p/"
 * @param header the X-Seedless: header to add to the request.
 * @param strProxy A string representing either the IP address or host name of the proxy server.
 * @param iProxyPort  An integer that indicates the proxy port or -1 to indicate the default port for the protocol.
 * @return HTTPURLConnection or null
 */
HttpURLConnection doURLRequest(String strURL, String header, String strProxy, int iProxyPort, String user, String pass) {
    try {
        URL url = null;
        // System.out.println("HTTP Request: " + strURL);
        URL urlOriginal = new URL(strURL);
        if ((null != strProxy) && (0 < strProxy.length())) {
            // The original URL is passed as "the file on the host".
            URL urlProxy = new URL(urlOriginal.getProtocol(), strProxy, iProxyPort, strURL);
            url = urlProxy;
        } else {
            url = urlOriginal;
        }
        if (pass != null) {
            final String login = user;
            final String password = pass;
            Authenticator.setDefault(new Authenticator() {

                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(login, password.toCharArray());
                }
            });
        }
        c = url.openConnection();
        c.setUseCaches(false);
        // Eepproxy will time out in 1 minute
        c.setConnectTimeout(1000 * 45);
        if (header != null) {
            c.setRequestProperty("X-Seedless", header);
        }
        if (c instanceof HttpURLConnection) {
            // instanceof returns true only if the object is not null.
            h = (HttpURLConnection) c;
            h.connect();
            return h;
        }
        return null;
    } catch (MalformedURLException ex) {
        log.debug("Can't get URL \"" + strURL + "\"", ex);
    } catch (IOException ex) {
        log.debug("Can't get URL \"" + strURL + "\"", ex);
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException) URL(java.net.URL) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 80 with Authenticator

use of java.net.Authenticator in project proxyee-down by monkeyWie.

the class AppUtil method download.

/**
 * 下载http资源
 */
public static void download(String url, String path) throws IOException {
    URL u = new URL(url);
    HttpURLConnection connection;
    ProxyConfig proxyConfig = PDownConfigContent.getInstance().get().getProxyConfig();
    if (proxyConfig != null) {
        Type type;
        if (proxyConfig.getProxyType() == ProxyType.HTTP) {
            type = Type.HTTP;
        } else {
            type = Type.SOCKS;
        }
        if (!StringUtils.isEmpty(proxyConfig.getUser())) {
            Authenticator authenticator = new Authenticator() {

                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(proxyConfig.getUser(), proxyConfig.getPwd() == null ? null : proxyConfig.getPwd().toCharArray());
                }
            };
            Authenticator.setDefault(authenticator);
        }
        Proxy proxy = new Proxy(type, new InetSocketAddress(proxyConfig.getHost(), proxyConfig.getPort()));
        connection = (HttpURLConnection) u.openConnection(proxy);
    } else {
        connection = (HttpURLConnection) u.openConnection();
    }
    connection.setConnectTimeout(30000);
    connection.setReadTimeout(0);
    File file = new File(path);
    if (!file.exists() || file.isDirectory()) {
        FileUtil.createFileSmart(file.getPath());
    }
    try (InputStream input = connection.getInputStream();
        FileOutputStream output = new FileOutputStream(file)) {
        byte[] bts = new byte[8192];
        int len;
        while ((len = input.read(bts)) != -1) {
            output.write(bts, 0, len);
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) ProxyConfig(org.pdown.core.proxy.ProxyConfig) URL(java.net.URL) ProxyType(org.pdown.core.proxy.ProxyType) Type(java.net.Proxy.Type) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Authenticator (java.net.Authenticator)80 PasswordAuthentication (java.net.PasswordAuthentication)50 URL (java.net.URL)18 Proxy (java.net.Proxy)14 InetSocketAddress (java.net.InetSocketAddress)12 HttpClient (java.net.http.HttpClient)11 Field (java.lang.reflect.Field)10 HttpURLConnection (java.net.HttpURLConnection)10 IOException (java.io.IOException)9 Test (org.junit.Test)7 Method (java.lang.reflect.Method)6 File (java.io.File)5 SocketAddress (java.net.SocketAddress)5 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3 Debug (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug)3 JCommander (com.beust.jcommander.JCommander)2