Search in sources :

Example 46 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project samourai-wallet-android by Samourai-Wallet.

the class JSONRPC method doRPC.

private JSONObject doRPC(String id, String method, JSONArray params) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    JSONObject responseJsonObj = null;
    try {
        JSONObject json = new JSONObject();
        json.put("id", id);
        json.put("method", method);
        if (null != params && params.length() > 0) {
            json.put("params", params);
        }
        httpclient.getCredentialsProvider().setCredentials(new AuthScope(node, port), new UsernamePasswordCredentials(user, password.toString()));
        StringEntity myEntity = new StringEntity(json.toString());
        Log.d("JSONRPC", json.toString());
        HttpPost httppost = new HttpPost("http://" + node + ":" + port);
        httppost.setEntity(myEntity);
        Log.d("JSONRPC", "Request:" + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        Log.d("JSONRPC", response.getStatusLine().toString());
        // Log.d("JSONRPC", entity == null ? "entity is null" : "entity is not null, content length:" + entity.getContentLength());
        if (response.getStatusLine().getStatusCode() != 200) {
            return null;
        }
        String inputLine = null;
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        try {
            while ((inputLine = br.readLine()) != null) {
                sb.append(inputLine);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (sb.length() > 0) {
            responseJsonObj = new JSONObject(sb.toString());
        } else {
            return null;
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
    return responseJsonObj;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) ClientProtocolException(org.apache.http.client.ClientProtocolException) StringEntity(org.apache.http.entity.StringEntity) JSONObject(org.json.JSONObject) AuthScope(org.apache.http.auth.AuthScope) BufferedReader(java.io.BufferedReader) ParseException(org.apache.http.ParseException)

Example 47 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project keycloak by keycloak.

the class ProxyMappingsAwareRoutePlanner method determineProxy.

@Override
protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
    String targetHostName = target.getHostName();
    ProxyMapping proxyMapping = proxyMappings.getProxyFor(targetHostName);
    LOG.debugf("Returning proxyMapping=%s for targetHost=%s", proxyMapping, targetHostName);
    UsernamePasswordCredentials proxyCredentials = proxyMapping.getProxyCredentials();
    HttpHost proxyHost = proxyMapping.getProxyHost();
    if (proxyCredentials != null) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), proxyCredentials);
        context.setAttribute(HttpClientContext.CREDS_PROVIDER, credsProvider);
    }
    return proxyHost;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) ProxyMapping(org.keycloak.connections.httpclient.ProxyMappings.ProxyMapping) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 48 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project NoraUi by NoraUi.

the class MailSteps method createRestTemplate.

private static RestTemplate createRestTemplate() {
    String proxyUser = "";
    String proxyPassword = "";
    String proxyUrl = "";
    int proxyPort = 0;
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    if (!"".equals(proxyUrl)) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));
        HttpHost myProxy = new HttpHost(proxyUrl, proxyPort);
        clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();
    }
    HttpClient httpClient = clientBuilder.build();
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    factory.setHttpClient(httpClient);
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
    return restTemplate;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) AuthScope(org.apache.http.auth.AuthScope) RestTemplate(org.springframework.web.client.RestTemplate) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 49 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project archiva by apache.

the class RepositoryModelResolver method connectToRepository.

/**
 * Using wagon, connect to the remote repository.
 *
 * @param wagon the wagon instance to establish the connection on.
 * @return true if the connection was successful. false if not connected.
 */
private boolean connectToRepository(Wagon wagon, RemoteRepository remoteRepository) {
    boolean connected;
    final NetworkProxy proxyConnector = this.networkProxyMap.get(remoteRepository.getId());
    ProxyInfo networkProxy = null;
    if (proxyConnector != null) {
        networkProxy = new ProxyInfo();
        networkProxy.setType(proxyConnector.getProtocol());
        networkProxy.setHost(proxyConnector.getHost());
        networkProxy.setPort(proxyConnector.getPort());
        networkProxy.setUserName(proxyConnector.getUsername());
        networkProxy.setPassword(proxyConnector.getPassword());
        String msg = "Using network proxy " + networkProxy.getHost() + ":" + networkProxy.getPort() + " to connect to remote repository " + remoteRepository.getLocation();
        if (networkProxy.getNonProxyHosts() != null) {
            msg += "; excluding hosts: " + networkProxy.getNonProxyHosts();
        }
        if (StringUtils.isNotBlank(networkProxy.getUserName())) {
            msg += "; as user: " + networkProxy.getUserName();
        }
        log.debug(msg);
    }
    AuthenticationInfo authInfo = null;
    RepositoryCredentials creds = remoteRepository.getLoginCredentials();
    String username = "";
    String password = "";
    if (creds instanceof UsernamePasswordCredentials) {
        UsernamePasswordCredentials c = (UsernamePasswordCredentials) creds;
        username = c.getUserName();
        password = c.getPassword();
    }
    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
        log.debug("Using username {} to connect to remote repository {}", username, remoteRepository.getLocation());
        authInfo = new AuthenticationInfo();
        authInfo.setUserName(username);
        authInfo.setPassword(password);
    }
    int timeoutInMilliseconds = ((int) remoteRepository.getTimeout().getSeconds()) * 1000;
    // FIXME olamy having 2 config values
    // Set timeout
    wagon.setReadTimeout(timeoutInMilliseconds);
    wagon.setTimeout(timeoutInMilliseconds);
    try {
        org.apache.maven.wagon.repository.Repository wagonRepository = new org.apache.maven.wagon.repository.Repository(remoteRepository.getId(), remoteRepository.getLocation().toString());
        if (networkProxy != null) {
            wagon.connect(wagonRepository, authInfo, networkProxy);
        } else {
            wagon.connect(wagonRepository, authInfo);
        }
        connected = true;
    } catch (ConnectionException | AuthenticationException e) {
        log.error("Could not connect to {}:{} ", remoteRepository.getName(), e.getMessage());
        connected = false;
    }
    return connected;
}
Also used : RepositoryCredentials(org.apache.archiva.repository.RepositoryCredentials) AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Repository(org.apache.maven.model.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) ManagedRepository(org.apache.archiva.repository.ManagedRepository) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 50 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project ANNIS by korpling.

the class Helper method createRESTClient.

/**
 * Creates an authentificiated REST client
 *
 * @param userName
 * @param password
 * @return A newly created client.
 */
public static Client createRESTClient(String userName, String password) {
    DefaultApacheHttpClient4Config rc = new DefaultApacheHttpClient4Config();
    rc.getClasses().add(SaltProjectProvider.class);
    ThreadSafeClientConnManager clientConnMgr = new ThreadSafeClientConnManager();
    clientConnMgr.setDefaultMaxPerRoute(10);
    rc.getProperties().put(ApacheHttpClient4Config.PROPERTY_CONNECTION_MANAGER, clientConnMgr);
    if (userName != null && password != null) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
        rc.getProperties().put(ApacheHttpClient4Config.PROPERTY_CREDENTIALS_PROVIDER, credentialsProvider);
        rc.getProperties().put(ApacheHttpClient4Config.PROPERTY_PREEMPTIVE_BASIC_AUTHENTICATION, true);
    }
    Client c = ApacheHttpClient4.create(rc);
    return c;
}
Also used : DefaultApacheHttpClient4Config(com.sun.jersey.client.apache4.config.DefaultApacheHttpClient4Config) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Client(com.sun.jersey.api.client.Client) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)337 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)212 CredentialsProvider (org.apache.http.client.CredentialsProvider)189 AuthScope (org.apache.http.auth.AuthScope)163 HttpHost (org.apache.http.HttpHost)95 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)78 HttpGet (org.apache.http.client.methods.HttpGet)73 IOException (java.io.IOException)54 BasicScheme (org.apache.http.impl.auth.BasicScheme)53 Test (org.junit.Test)53 HttpResponse (org.apache.http.HttpResponse)52 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)46 Credentials (org.apache.http.auth.Credentials)44 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)43 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)37 URI (java.net.URI)34 AuthCache (org.apache.http.client.AuthCache)32 HttpClient (org.apache.http.client.HttpClient)31 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)31 HttpPost (org.apache.http.client.methods.HttpPost)29