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:com.marklogic.client.functionaltest.ConnectedRESTQA.java

public static void assocRESTServer(String restServerName, String dbName, int restPort) {
    try {/*from   w w w .java2 s. c  o m*/
        DefaultHttpClient client = new DefaultHttpClient();

        client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8002),
                new UsernamePasswordCredentials("admin", "admin"));

        HttpPost post = new HttpPost("http://localhost:8002" + "/v1/rest-apis?format=json");
        //         
        String JSONString = "{ \"rest-api\": {\"name\":\"" + restServerName + "\",\"database\":\"" + dbName
                + "\",\"port\":\"" + restPort + "\"}}";
        //         System.out.println(JSONString);      
        post.addHeader("Content-type", "application/json");
        post.setEntity(new StringEntity(JSONString));

        HttpResponse response = client.execute(post);
        //         System.out.println(JSONString);
        if (response.getStatusLine().getStatusCode() == 400) {
            // EntityUtils to get the response content
            System.out.println("AppServer already exist");
            if (dbName.equals("Documents")) {
                System.out.println("and Context database is Documents DB");
            } else {
                System.out.println("and changing context database to " + dbName);
                associateRESTServerWithDB(restServerName, dbName);
            }

        }
    } catch (Exception e) {
        // writing error to Log
        e.printStackTrace();
    }
}

From source file:com.marklogic.client.functionaltest.ConnectedRESTQA.java

public static void waitForServerRestart() {
    try {//  w w  w.j  av  a  2  s.c  o m
        int count = 0;
        while (count < 20) {
            DefaultHttpClient client = new DefaultHttpClient();
            client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8001),
                    new UsernamePasswordCredentials("admin", "admin"));

            count++;
            try {
                HttpGet getrequest = new HttpGet("http://localhost:8001/admin/v1/timestamp");
                HttpResponse response = client.execute(getrequest);
                if (response.getStatusLine().getStatusCode() == 503) {
                    Thread.sleep(5000);
                } else if (response.getStatusLine().getStatusCode() == 200) {
                    break;
                } else {
                    System.out.println("Waiting for response from server, Trial :"
                            + response.getStatusLine().getStatusCode() + count);
                    Thread.sleep(6000);
                }
            } catch (Exception e) {
                Thread.sleep(6000);
            }
        }
    } catch (Exception e) {
        System.out.println("Inside wait for server restart is throwing an error");
        e.printStackTrace();
    }
}

From source file:com.marklogic.client.functionaltest.ConnectedRESTQA.java

public static void addElementRangeIndexTemporalCollection(String dbName, String collectionName,
        String systemAxisName, String validAxisName) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode rootNode = mapper.createObjectNode();

    rootNode.put("collection-name", collectionName);
    rootNode.put("system-axis", systemAxisName);
    rootNode.put("valid-axis", validAxisName);

    System.out.println(rootNode.toString());

    DefaultHttpClient client = new DefaultHttpClient();
    client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8002),
            new UsernamePasswordCredentials("admin", "admin"));

    HttpPost post = new HttpPost(
            "http://localhost:8002/manage/v2/databases/" + dbName + "/temporal/collections?format=json");

    post.addHeader("Content-type", "application/json");
    post.addHeader("accept", "application/json");
    post.setEntity(new StringEntity(rootNode.toString()));

    HttpResponse response = client.execute(post);
    HttpEntity respEntity = response.getEntity();
    if (response.getStatusLine().getStatusCode() == 400) {
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        System.out.println(responseString);
    } else if (respEntity != null) {
        // EntityUtils to get the response content
        String content = EntityUtils.toString(respEntity);
        System.out.println(content);

        System.out.println("Temporal collection: " + collectionName + " created");
        System.out.println("==============================================================");
    } else {/*  ww  w . j  a  va  2  s .  c om*/
        System.out.println("No Proper Response");
    }

}

From source file:com.marklogic.client.functionaltest.ConnectedRESTQA.java

public static String getBootStrapHostFromML() {
    InputStream jstream = null;/*from   w  ww  . ja v  a  2s . c om*/
    try {
        DefaultHttpClient client = new DefaultHttpClient();
        client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8002),
                new UsernamePasswordCredentials("admin", "admin"));
        HttpGet getrequest = new HttpGet("http://localhost:8002/manage/v2/properties?format=json");
        HttpResponse resp = client.execute(getrequest);
        jstream = resp.getEntity().getContent();
        JsonNode jnode = new ObjectMapper().readTree(jstream);
        String propName = "bootstrap-host";
        if (!jnode.isNull()) {

            if (jnode.has(propName)) {
                System.out.println("Bootstrap Host: "
                        + jnode.withArray(propName).get(0).get("bootstrap-host-name").asText());
                return jnode.withArray(propName).get(0).get("bootstrap-host-name").asText();
            } else {
                System.out.println("Missing " + propName
                        + " field from properties end point so sending java conanical host name\n"
                        + jnode.toString());
                return InetAddress.getLocalHost().getCanonicalHostName().toLowerCase();
            }
        } else {
            System.out.println("Rest endpoint returns empty stream");
            return InetAddress.getLocalHost().getCanonicalHostName().toLowerCase();
        }

    } catch (Exception e) {
        // writing error to Log
        e.printStackTrace();

        return "localhost";
    } finally {
        jstream = null;
    }
}

From source file:com.marklogic.client.functionaltest.ConnectedRESTQA.java

public static void setDatabaseProperties(String dbName, String prop, String propValue) throws IOException {
    InputStream jsonstream = null;
    try {// ww  w. ja  va  2s  .co  m
        DefaultHttpClient client = new DefaultHttpClient();
        client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8002),
                new UsernamePasswordCredentials("admin", "admin"));
        HttpGet getrequest = new HttpGet(
                "http://localhost:8002" + "/manage/v2/databases/" + dbName + "/properties?format=json");
        HttpResponse response1 = client.execute(getrequest);
        jsonstream = response1.getEntity().getContent();
        JsonNode jnode = new ObjectMapper().readTree(jsonstream);
        if (!jnode.isNull()) {
            ((ObjectNode) jnode).put(prop, propValue);
            //            System.out.println(jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode());
            HttpPut put = new HttpPut(
                    "http://localhost:8002" + "/manage/v2/databases/" + dbName + "/properties?format=json");
            put.addHeader("Content-type", "application/json");
            put.setEntity(new StringEntity(jnode.toString()));

            HttpResponse response2 = client.execute(put);
            HttpEntity respEntity = response2.getEntity();
            if (respEntity != null) {
                String content = EntityUtils.toString(respEntity);
                System.out.println(content);
            }
        } else {
            System.out.println("REST call for database properties returned NULL ");
        }
    } catch (Exception e) {
        // writing error to Log
        e.printStackTrace();
    } finally {
        if (jsonstream == null) {
        } else {
            jsonstream.close();
        }
    }
}

From source file:com.marklogic.client.functionaltest.ConnectedRESTQA.java

public static void setDatabaseProperties(String dbName, String prop, boolean propValue) throws IOException {
    InputStream jsonstream = null;
    try {//w  w w  .j a va  2s. c o m
        DefaultHttpClient client = new DefaultHttpClient();
        client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8002),
                new UsernamePasswordCredentials("admin", "admin"));
        HttpGet getrequest = new HttpGet(
                "http://localhost:8002" + "/manage/v2/databases/" + dbName + "/properties?format=json");
        HttpResponse response1 = client.execute(getrequest);
        jsonstream = response1.getEntity().getContent();
        JsonNode jnode = new ObjectMapper().readTree(jsonstream);
        if (!jnode.isNull()) {
            ((ObjectNode) jnode).put(prop, propValue);
            //            System.out.println(jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode());
            HttpPut put = new HttpPut(
                    "http://localhost:8002" + "/manage/v2/databases/" + dbName + "/properties?format=json");
            put.addHeader("Content-type", "application/json");
            put.setEntity(new StringEntity(jnode.toString()));

            HttpResponse response2 = client.execute(put);
            HttpEntity respEntity = response2.getEntity();
            if (respEntity != null) {
                String content = EntityUtils.toString(respEntity);
                System.out.println(content);
            }
        } else {
            System.out.println("REST call for database properties returned NULL ");
        }
    } catch (Exception e) {
        // writing error to Log
        e.printStackTrace();
    } finally {
        if (jsonstream == null) {
        } else {
            jsonstream.close();
        }
    }
}

From source file:com.marklogic.client.functionaltest.ConnectedRESTQA.java

public static void updateTemporalCollectionForLSQT(String dbName, String collectionName, boolean enable)
        throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode rootNode = mapper.createObjectNode();
    rootNode.put("lsqt-enabled", enable);

    // Set system time values
    ObjectNode automation = mapper.createObjectNode();
    automation.put("enabled", true);

    rootNode.set("automation", automation);

    System.out.println(rootNode.toString());

    DefaultHttpClient client = new DefaultHttpClient();
    client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8002),
            new UsernamePasswordCredentials("admin", "admin"));

    HttpPut put = new HttpPut("http://localhost:8002/manage/v2/databases/" + dbName
            + "/temporal/collections/lsqt/properties?collection=" + collectionName);

    put.addHeader("Content-type", "application/json");
    put.addHeader("accept", "application/json");
    put.setEntity(new StringEntity(rootNode.toString()));

    HttpResponse response = client.execute(put);
    HttpEntity respEntity = response.getEntity();
    if (response.getStatusLine().getStatusCode() == 400) {
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        System.out.println(responseString);
    } else if (respEntity != null) {
        // EntityUtils to get the response content
        String content = EntityUtils.toString(respEntity);
        System.out.println(content);

        System.out.println("Temporal collection: " + collectionName + " created");
        System.out.println("==============================================================");
    } else {//from  ww  w  . ja v a 2  s .c  om
        System.out.println("No Proper Response");
    }

}

From source file:com.amazon.s3.http.HttpClientFactory.java

/**
  * Creates a new HttpClient object using the specified AWS
  * ClientConfiguration to configure the client.
  *//from w ww .  j a va 2 s .co m
  * @param config
  *            Client configuration options (ex: proxy settings, connection
  *            limits, etc).
  *
  * @return The new, configured HttpClient.
  */
public HttpClient createHttpClient(ClientConfiguration config) {
    /* Form User-Agent information */
    String userAgent = config.getUserAgent();
    if (!(userAgent.equals(ClientConfiguration.DEFAULT_USER_AGENT))) {
        userAgent += ", " + ClientConfiguration.DEFAULT_USER_AGENT;
    }

    /* Set HTTP client parameters */
    HttpParams httpClientParams = new BasicHttpParams();
    HttpClientParams.setRedirecting(httpClientParams, false);
    HttpProtocolParams.setUserAgent(httpClientParams, userAgent);
    HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout());
    HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout());
    HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, false);
    HttpConnectionParams.setTcpNoDelay(httpClientParams, true);

    int socketSendBufferSizeHint = config.getSocketBufferSizeHints()[0];
    int socketReceiveBufferSizeHint = config.getSocketBufferSizeHints()[1];
    if (socketSendBufferSizeHint > 0 || socketReceiveBufferSizeHint > 0) {
        HttpConnectionParams.setSocketBufferSize(httpClientParams,
                Math.max(socketSendBufferSizeHint, socketReceiveBufferSizeHint));
    }

    /* Set connection manager */
    ThreadSafeClientConnManager connectionManager = ConnectionManagerFactory
            .createThreadSafeClientConnManager(config, httpClientParams);
    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, httpClientParams);

    /* Set proxy if configured */
    String proxyHost = config.getProxyHost();
    int proxyPort = config.getProxyPort();
    if (proxyHost != null && proxyPort > 0) {
        Log.i(TAG, "Configuring Proxy. Proxy Host: " + proxyHost + " " + "Proxy Port: " + proxyPort);
        HttpHost proxyHttpHost = new HttpHost(proxyHost, proxyPort);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHttpHost);

        String proxyUsername = config.getProxyUsername();
        String proxyPassword = config.getProxyPassword();
        String proxyDomain = config.getProxyDomain();
        String proxyWorkstation = config.getProxyWorkstation();

        if (proxyUsername != null && proxyPassword != null) {
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort),
                    new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain));
        }
    }

    return httpClient;
}

From source file:com.marklogic.client.functionaltest.ConnectedRESTQA.java

public static void createRESTUser(String usrName, String pass, String... roleNames) {
    try {/*w  w w  .  ja v  a  2s  .c o  m*/
        DefaultHttpClient client = new DefaultHttpClient();
        client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8002),
                new UsernamePasswordCredentials("admin", "admin"));
        HttpGet getrequest = new HttpGet("http://localhost:8002" + "/manage/v2/users/" + usrName);
        HttpResponse resp = client.execute(getrequest);

        if (resp.getStatusLine().getStatusCode() == 200) {
            System.out.println("User already exist");
        } else {
            System.out.println("User dont exist");
            client = new DefaultHttpClient();
            client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8002),
                    new UsernamePasswordCredentials("admin", "admin"));

            ObjectMapper mapper = new ObjectMapper();
            ObjectNode mainNode = mapper.createObjectNode();
            //         ObjectNode childNode = mapper.createObjectNode();
            ArrayNode childArray = mapper.createArrayNode();
            mainNode.put("user-name", usrName);
            mainNode.put("description", "user discription");
            mainNode.put("password", pass);
            for (String rolename : roleNames)
                childArray.add(rolename);
            mainNode.withArray("role").addAll(childArray);
            //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString());
            System.out.println(mainNode.toString());
            HttpPost post = new HttpPost("http://localhost:8002" + "/manage/v2/users?format=json");
            post.addHeader("Content-type", "application/json");
            post.setEntity(new StringEntity(mainNode.toString()));

            HttpResponse response = client.execute(post);
            HttpEntity respEntity = response.getEntity();
            if (response.getStatusLine().getStatusCode() == 400) {
                System.out.println("User already exist");
            } else if (respEntity != null) {
                // EntityUtils to get the response content
                String content = EntityUtils.toString(respEntity);
                System.out.println(content);
            } else {
                System.out.println("No Proper Response");
            }
        }
    } catch (Exception e) {
        // writing error to Log
        e.printStackTrace();
    }
}