Example usage for org.apache.http.impl.client DefaultHttpClient getCredentialsProvider

List of usage examples for org.apache.http.impl.client DefaultHttpClient getCredentialsProvider

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient getCredentialsProvider.

Prototype

public synchronized final CredentialsProvider getCredentialsProvider() 

Source Link

Usage

From source file:org.xwiki.extension.repository.xwiki.internal.httpclient.DefaultHttpClientFactory.java

@Override
public HttpClient createClient(String user, String password) {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, this.configuration.getUserAgent());
    httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
    httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);

    // Setup proxy
    ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
            httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    httpClient.setRoutePlanner(routePlanner);

    // Setup authentication
    if (user != null) {
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(user, password));
    }// ww w . ja v  a 2s .com

    return httpClient;
}

From source file:cm.aptoide.pt.ManageRepo.java

private int checkServer(String uri, String user, String pwd) {

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 5000);

    DefaultHttpClient mHttpClient = new DefaultHttpClient(httpParameters);
    HttpGet mHttpGet = new HttpGet(uri + "/info.xml");
    try {/*from   w  w w . j  av  a 2 s.  c  om*/
        if (user != null && pwd != null) {
            URL mUrl = new URL(uri);
            mHttpClient.getCredentialsProvider().setCredentials(new AuthScope(mUrl.getHost(), mUrl.getPort()),
                    new UsernamePasswordCredentials(user, pwd));
        }
        HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
        return mHttpResponse.getStatusLine().getStatusCode();
    } catch (ClientProtocolException e) {
        return -1;
    } catch (IOException e) {
        return -1;
    } catch (IllegalArgumentException e) {
        return -1;
    }
}

From source file:com.mycompany.kerberosbyip.NewMain.java

private void configureHttpClient(final DefaultHttpClient httpclient) throws GeneralSecurityException {
    AuthSchemeRegistry registry = new AuthSchemeRegistry();
    registry.register(KERBEROS, new WsmanKerberosSchemeFactory(true, "WSMAN", ipAddress, port));
    registry.register(SPNEGO, new WsmanSPNegoSchemeFactory(true, "WSMAN", ipAddress, port));
    httpclient.setAuthSchemes(registry);

    final Credentials jaasCreds = new Credentials() {
        @Override//w  ww.ja v a  2  s.c  om
        public String getPassword() {
            return null;
        }

        @Override
        public Principal getUserPrincipal() {
            return null;
        }
    };

    httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1, null), jaasCreds);
}

From source file:cm.aptoide.pt.RemoteInTab.java

private void downloadExtras(String srv) {
    String url = srv + REMOTE_EXTRAS_FILE;

    try {//www  .j a va 2 s  . co  m
        FileOutputStream saveit = new FileOutputStream(LOCAL_PATH + REMOTE_EXTRAS_FILE);
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
        HttpConnectionParams.setSoTimeout(httpParameters, 5000);
        DefaultHttpClient mHttpClient = new DefaultHttpClient(httpParameters);
        HttpGet mHttpGet = new HttpGet(url);

        String[] logins = null;
        logins = db.getLogin(srv);
        if (logins != null) {
            URL mUrl = new URL(url);
            mHttpClient.getCredentialsProvider().setCredentials(new AuthScope(mUrl.getHost(), mUrl.getPort()),
                    new UsernamePasswordCredentials(logins[0], logins[1]));
        }

        HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
        if (mHttpResponse.getStatusLine().getStatusCode() == 401) {
            /*Message msg = new Message();
            msg.obj = new String(srv);
            secure_error_handler.sendMessage(msg);*/
        } else {
            byte[] buffer = EntityUtils.toByteArray(mHttpResponse.getEntity());
            saveit.write(buffer);
        }
    } catch (UnknownHostException e) {
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
}

From source file:cm.aptoide.pt.RemoteInTab.java

private void downloadList(String srv) {
    String url = srv + REMOTE_FILE;

    try {//from  w ww  .  jav  a 2 s.  c o  m
        FileOutputStream saveit = new FileOutputStream(XML_PATH);
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
        HttpConnectionParams.setSoTimeout(httpParameters, 5000);
        DefaultHttpClient mHttpClient = new DefaultHttpClient(httpParameters);
        HttpGet mHttpGet = new HttpGet(url);

        String[] logins = null;
        logins = db.getLogin(srv);
        if (logins != null) {
            URL mUrl = new URL(url);
            mHttpClient.getCredentialsProvider().setCredentials(new AuthScope(mUrl.getHost(), mUrl.getPort()),
                    new UsernamePasswordCredentials(logins[0], logins[1]));
        }

        HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
        if (mHttpResponse.getStatusLine().getStatusCode() == 401) {
            Message msg = new Message();
            msg.obj = new String(srv);
            secure_error_handler.sendMessage(msg);
        } else {
            byte[] buffer = EntityUtils.toByteArray(mHttpResponse.getEntity());
            saveit.write(buffer);
        }
    } catch (UnknownHostException e) {
        Message msg = new Message();
        msg.obj = new String(srv);
        error_handler.sendMessage(msg);
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
}

From source file:com.nubits.nubot.RPC.NuRPCClient.java

private JSONObject invokeRPC(String id, String method, List params) {
    DefaultHttpClient httpclient = new DefaultHttpClient();

    JSONObject json = new JSONObject();
    json.put("id", id);
    json.put("method", method);
    if (null != params) {
        JSONArray array = new JSONArray();
        array.addAll(params);//from   w w w  .j  av  a 2  s .c o m
        json.put("params", params);
    }
    JSONObject responseJsonObj = null;
    try {
        httpclient.getCredentialsProvider().setCredentials(new AuthScope(this.ip, this.port),
                new UsernamePasswordCredentials(this.rpcUsername, this.rpcPassword));
        StringEntity myEntity = new StringEntity(json.toJSONString());
        if (Global.options.verbose) {
            LOG.info("RPC : " + json.toString());
        }
        HttpPost httppost = new HttpPost("http://" + this.ip + ":" + this.port);
        httppost.setEntity(myEntity);

        if (Global.options.verbose) {
            LOG.info("RPC executing request :" + httppost.getRequestLine());
        }
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        if (Global.options.verbose) {
            LOG.info("RPC----------------------------------------");
            LOG.info("" + response.getStatusLine());

            if (entity != null) {
                LOG.info("RPC : Response content length: " + entity.getContentLength());
            }
        }
        JSONParser parser = new JSONParser();
        String entityString = EntityUtils.toString(entity);
        LOG.debug("Entity = " + entityString);
        /* TODO In case of wrong username/pass the response would be the following .Consider parsing it.
        Entity = <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
                <HTML>
                <HEAD>
                <TITLE>Error</TITLE>
                <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
                </HEAD>
                <BODY><H1>401 Unauthorized.</H1></BODY>
                </HTML>
         */
        responseJsonObj = (JSONObject) parser.parse(entityString);
    } catch (ClientProtocolException e) {
        LOG.error("Nud RPC Connection problem:" + e.toString());
        this.connected = false;
    } catch (IOException e) {
        LOG.error("Nud RPC Connection problem:" + e.toString());
        this.connected = false;
    } catch (ParseException ex) {
        LOG.error("Nud RPC Connection problem:" + ex.toString());
        this.connected = false;
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
    return responseJsonObj;
}

From source file:io.cloudsoft.marklogic.nodes.MarkLogicNodeSshDriver.java

@Override
public String getForestStatus(String forestName) {
    LOG.trace("Getting status for forest {}", forestName);

    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {//w w w.  jav a 2 s .  c  o  m
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(getEntity().getUser(), getEntity().getPassword()));

        String adminUrl = getEntity().getAdminConnectUrl();
        String uri = adminUrl + format("/forest_detailed_status.xqy?forest=%s", forestName);
        HttpGet httpget = new HttpGet(uri);

        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String status = IOUtils.toString(entity.getContent());
        EntityUtils.consume(entity);
        return status;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:io.cloudsoft.marklogic.nodes.MarkLogicNodeSshDriver.java

@Override
public Set<String> scanDatabases() {
    LOG.debug("Scanning databases");

    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {/*from  ww w.  jav a  2  s  . c om*/
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(getEntity().getUser(), getEntity().getPassword()));

        String adminUrl = getEntity().getAdminConnectUrl();
        String uri = adminUrl + "/database_list.xqy";
        HttpGet httpget = new HttpGet(uri);

        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String result = IOUtils.toString(entity.getContent());
        EntityUtils.consume(entity);

        Set<String> forests = Sets.newHashSet();
        String[] split = result.split("\n");
        Collections.addAll(forests, split);
        return forests;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:io.cloudsoft.marklogic.nodes.MarkLogicNodeSshDriver.java

@Override
public Set<String> scanForests() {
    LOG.debug("Scanning forests");

    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {/*from   w  ww .ja v a2 s  .c  om*/
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(getEntity().getUser(), getEntity().getPassword()));

        String adminUrl = getEntity().getAdminConnectUrl();
        String uri = adminUrl + "/forest_list.xqy";
        HttpGet httpget = new HttpGet(uri);

        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String result = IOUtils.toString(entity.getContent());
        EntityUtils.consume(entity);

        Set<String> forests = new HashSet<String>();
        String[] split = result.split("\n");
        Collections.addAll(forests, split);
        return forests;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}