Example usage for org.apache.http.auth AuthScope AuthScope

List of usage examples for org.apache.http.auth AuthScope AuthScope

Introduction

In this page you can find the example usage for org.apache.http.auth AuthScope AuthScope.

Prototype

public AuthScope(final String host, final int port) 

Source Link

Document

Creates a new credentials scope for the given host, port, any realm name, and any authentication scheme.

Usage

From source file:com.clonephpscrapper.utility.FetchPageWithProxy.java

public static String fetchPage(URI newurl) throws IOException, InterruptedException {

    int portNo = generateRandomPort();
    CredentialsProvider credsprovider = new BasicCredentialsProvider();
    credsprovider.setCredentials(new AuthScope("195.154.161.103", portNo),
            new UsernamePasswordCredentials("mongoose", "Fjh30fi"));
    HttpHost proxy = new HttpHost("195.154.161.103", portNo);
    //-----------------------------------------------------------------------
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000)
            .setConnectionRequestTimeout(5000).build();

    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsprovider)
            .setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0")
            .setDefaultRequestConfig(requestConfig).setProxy(proxy).build();
    String responsebody = "";
    String responsestatus = null;
    int count = 0;
    try {//  ww  w .  ja  v  a  2 s. co m
        HttpGet httpget = new HttpGet(newurl);
        httpget.addHeader("GET ", "http://clonephp.com/");
        httpget.addHeader("Host", "clonephp.com");
        httpget.addHeader("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        httpget.addHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
        httpget.addHeader("Accept-Encoding", "gzip, deflate, sdch");
        httpget.addHeader("Accept-Language", "en-US,en;q=0.8");
        httpget.addHeader("Connection", "keep-alive");
        //            httpget.addHeader("Cookie", "_ym_uid=14523228509491377; _gat=1; _ym_isad=0; _ga=GA1.2.438893798.1452322850; _ym_visorc_26745633=b; last-featured-visit-time=1452323357912\n" +
        //"If-None-Match: W/\"10d86-1fKww4Zqud7NjVeuSjZKEw\"");

        System.out.println("Response status " + httpget.getRequestLine());
        CloseableHttpResponse resp = httpclient.execute(httpget);
        responsestatus = resp.getStatusLine().toString();
        if (responsestatus.contains("503") || responsestatus.contains("502") || responsestatus.contains("403")
                || responsestatus.contains("400") || responsestatus.contains("407")
                || responsestatus.contains("401") || responsestatus.contains("402")
                || responsestatus.contains("404") || responsestatus.contains("405")
                || responsestatus.contains("SSLHandshakeException") || responsestatus.contains("999")
                || responsestatus.contains("ClientProtocolException")
                || responsestatus.contains("SocketTimeoutException") || "".equals(responsestatus)) {
            Thread.sleep(10000);
            do {
                count++;
                responsebody = fetchPageSourcefromClientGoogleSecond(newurl);
                if (responsebody == null) {
                    Thread.sleep(10000);
                    System.out.println("PROX FAILURE");
                }
                if (count > 20) {
                    Thread.sleep(1000);
                    break;
                }
            } while (responsebody == null || "".equals(responsebody));
        } else {
            HttpEntity entity = resp.getEntity();
            System.out.println(resp.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
                BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    responsebody = new StringBuilder().append(responsebody).append(inputLine).toString();
                }
                // writeResponseFile(responsebody, pagename);
            }
            EntityUtils.consume(entity);
        }
    } catch (IOException | IllegalStateException e) {
        System.out.println("Exception = " + e);
        do {
            count++;
            responsebody = fetchPageSourcefromClientGoogleSecond(newurl);
            if (responsebody == null) {
                System.out.println("PROX FAILURE");
            }
            if (count > 15) {
                Thread.sleep(50000);
                //                    responsebody = fetchPageSourcefromClientGoogleSecond(newurl);
                break;
            }
        } while (responsebody == null || "".equals(responsebody));
    } finally {
        httpclient.close();
    }
    return responsebody;
}

From source file:com.smartling.api.sdk.util.HttpProxyUtilsTest.java

@Test
public void testGetHttpClientWithProxyConfig() throws Exception {
    ArgumentCaptor<CredentialsProvider> credentialsProviderCaptor = ArgumentCaptor
            .forClass(CredentialsProvider.class);
    PowerMockito.when(httpClientBuilder.setDefaultCredentialsProvider(credentialsProviderCaptor.capture()))
            .thenReturn(httpClientBuilder);

    assertNotNull(httpProxyUtils.getHttpClient(fillProxyConfiguration()));
    Credentials credentials = credentialsProviderCaptor.getValue().getCredentials(new AuthScope(HOST, PORT));

    assertEquals(USERNAME, credentials.getUserPrincipal().getName());
    assertEquals(PASSWORD, credentials.getPassword());

    verify(httpClientBuilder).build();/*from   ww  w.j a  v a2s. c  o  m*/
}

From source file:org.commonjava.maven.galley.transport.htcli.HttpImpl.java

@Override
public HttpClientContext createContext(final HttpLocation location) {
    final HttpClientContext ctx = HttpClientContext.create();

    if (location != null) {
        final CredentialsProvider creds = new BasicCredentialsProvider();
        final AuthScope as = new AuthScope(location.getHost(), location.getPort());

        if (location.getUser() != null) {
            final String password = passwords
                    .getPassword(new PasswordEntry(location, PasswordEntry.USER_PASSWORD));
            creds.setCredentials(as, new UsernamePasswordCredentials(location.getUser(), password));
        }//from w  ww  .  j  a v  a2 s . co m

        if (location.getProxyHost() != null && location.getProxyUser() != null) {
            final String password = passwords
                    .getPassword(new PasswordEntry(location, PasswordEntry.PROXY_PASSWORD));
            creds.setCredentials(new AuthScope(location.getProxyHost(), getProxyPort(location)),
                    new UsernamePasswordCredentials(location.getProxyUser(), password));
        }

        ctx.setCredentialsProvider(creds);
    }

    return ctx;
}

From source file:password.pwm.http.client.PwmHttpClient.java

public static HttpClient getHttpClient(final Configuration configuration,
        final PwmHttpClientConfiguration pwmHttpClientConfiguration) throws PwmUnrecoverableException {
    final DefaultHttpClient httpClient;
    try {/*from   w  w w.  ja v  a2s  .  c o  m*/
        if (Boolean.parseBoolean(configuration.readAppProperty(AppProperty.SECURITY_HTTP_PROMISCUOUS_ENABLE))) {
            httpClient = new DefaultHttpClient(makeConnectionManager(new X509Utils.PromiscuousTrustManager()));
        } else if (pwmHttpClientConfiguration != null && pwmHttpClientConfiguration.getCertificates() != null) {
            final TrustManager trustManager = new X509Utils.CertMatchingTrustManager(configuration,
                    pwmHttpClientConfiguration.getCertificates());
            httpClient = new DefaultHttpClient(makeConnectionManager(trustManager));
        } else {
            httpClient = new DefaultHttpClient();
        }
    } catch (Exception e) {
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN,
                "unexpected error creating promiscuous https client: " + e.getMessage()));
    }
    final String strValue = configuration.readSettingAsString(PwmSetting.HTTP_PROXY_URL);
    if (strValue != null && strValue.length() > 0) {
        final URI proxyURI = URI.create(strValue);

        final String host = proxyURI.getHost();
        final int port = proxyURI.getPort();
        final HttpHost proxy = new HttpHost(host, port);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        final String username = proxyURI.getUserInfo();
        if (username != null && username.length() > 0) {
            final String password = (username.contains(":")) ? username.split(":")[1] : "";
            final UsernamePasswordCredentials passwordCredentials = new UsernamePasswordCredentials(username,
                    password);
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port), passwordCredentials);
        }
    }
    final String userAgent = PwmConstants.PWM_APP_NAME + " " + PwmConstants.SERVLET_VERSION;
    httpClient.getParams().setParameter(HttpProtocolParams.USER_AGENT, userAgent);
    return httpClient;
}

From source file:org.neo4j.jdbc.http.driver.CypherExecutor.java

private CredentialsProvider getCredentialsProvider(String host, Integer port, Properties properties) {
    if (properties.containsKey("password")) {
        String user = properties.getProperty("user", properties.getProperty("username", "neo4j"));
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user,
                properties.getProperty("password"));
        credsProvider.setCredentials(new AuthScope(host, port), credentials);
        return credsProvider;
    }/* w w w  .j  a v  a  2 s  . co m*/
    return null;
}

From source file:com.hp.mqm.clt.RestClient.java

public RestClient(Settings settings) {
    this.settings = settings;

    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    cookieStore = new BasicCookieStore();
    httpClientBuilder.setDefaultCookieStore(cookieStore);
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD)
            .setSocketTimeout(DEFAULT_SO_TIMEOUT).setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);

    // proxy setting
    if (StringUtils.isNotEmpty(settings.getProxyHost())) {
        HttpHost proxy = new HttpHost(settings.getProxyHost(), settings.getProxyPort());
        requestConfigBuilder.setProxy(proxy);
        if (settings.getProxyUser() != null) {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(settings.getProxyHost(), settings.getProxyPort()),
                    new UsernamePasswordCredentials(settings.getProxyUser(), settings.getProxyPassword()));
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }/*from  w ww. j av a  2 s.  com*/
    }
    httpClient = httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()).build();

}

From source file:org.openml.knime.OpenMLWebservice.java

/**
 * Upload an implementation.//  w w w  .j  a v a2s .  c  o m
 * 
 * 
 * @param description Implementation description file
 * @param workflow Workflow file
 * @param user Name of the user
 * @param password Password of the user
 * @return Response from the server
 * @throws Exception If communication failed
 */
public static String sendImplementation(final File description, final File workflow, final String user,
        final String password) throws Exception {
    String result = "";
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        Credentials credentials = new UsernamePasswordCredentials(user, password);
        AuthScope scope = new AuthScope(new URI(WEBSERVICEURL).getHost(), 80);
        httpclient.getCredentialsProvider().setCredentials(scope, credentials);
        String url = WEBSERVICEURL + "?f=openml.implementation.upload";
        HttpPost httppost = new HttpPost(url);
        MultipartEntity reqEntity = new MultipartEntity();
        FileBody descBin = new FileBody(description);
        reqEntity.addPart("description", descBin);
        FileBody workflowBin = new FileBody(workflow);
        reqEntity.addPart("source", workflowBin);
        httppost.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        if (response.getStatusLine().getStatusCode() < 200) {
            throw new Exception(response.getStatusLine().getReasonPhrase());
        }
        if (resEntity != null) {
            result = convertStreamToString(resEntity.getContent());
        }
        ErrorDocument errorDoc = null;
        try {
            errorDoc = ErrorDocument.Factory.parse(result);
        } catch (Exception e) {
            // no error XML should mean no error
        }
        if (errorDoc != null && errorDoc.validate()) {
            ErrorDocument.Error error = errorDoc.getError();
            String errorMessage = error.getCode() + " : " + error.getMessage();
            if (error.isSetAdditionalInformation()) {
                errorMessage += " : " + error.getAdditionalInformation();
            }
            throw new Exception(errorMessage);
        }
        EntityUtils.consume(resEntity);
    } finally {
        try {
            httpclient.getConnectionManager().shutdown();
        } catch (Exception ignore) {
            // ignore
        }
    }
    return result;
}