Example usage for org.apache.http.client HttpClient getConnectionManager

List of usage examples for org.apache.http.client HttpClient getConnectionManager

Introduction

In this page you can find the example usage for org.apache.http.client HttpClient getConnectionManager.

Prototype

@Deprecated
ClientConnectionManager getConnectionManager();

Source Link

Document

Obtains the connection manager used by this client.

Usage

From source file:com.datatorrent.demos.mrmonitor.MRUtil.java

/**
 * This method returns the response content for a given url
 * @param url/*from   w w  w  .java  2 s  .  co  m*/
 * @return
 */
public static String getJsonForURL(String url) {
    HttpClient httpclient = new DefaultHttpClient();
    logger.debug(url);
    try {

        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody;
        try {
            responseBody = httpclient.execute(httpget, responseHandler);

        } catch (ClientProtocolException e) {
            logger.debug(e.getMessage());
            return null;

        } catch (IOException e) {
            logger.debug(e.getMessage());
            return null;
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return null;
        }
        return responseBody.trim();
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.cloudifysource.azure.AbstractCliAzureDeploymentTest.java

/**
 * This methods extracts the number of machines running gs-agents using the rest admin api
 *
 * @param machinesRestAdminUrl/*from  www .j av  a  2s . c o  m*/
 * @return number of machines running gs-agents
 * @throws IOException
 * @throws java.net.URISyntaxException
 */
protected static int getNumberOfMachines(URL machinesRestAdminUrl) throws IOException, URISyntaxException {
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(machinesRestAdminUrl.toURI());
    try {
        String json = client.execute(httpGet, new BasicResponseHandler());
        Matcher matcher = Pattern.compile("\"Size\":\"([0-9]+)\"").matcher(json);
        if (matcher.find()) {
            String rawSize = matcher.group(1);
            int size = Integer.parseInt(rawSize);
            return size;
        } else {
            return 0;
        }
    } catch (Exception e) {
        return 0;
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:ca.sqlpower.enterprise.ClientSideSessionUtils.java

/**
 * This method reverts the server workspace specified by the given project location
 * to the specified revision number.//from w w w. j ava 2  s. c  o m
 * 
 * All sessions should automatically update to the reverted revision due to their Updater.
 * 
 * @returns The new global revision number, right after the reversion, or -1 if the server did not revert.
 * @throws IOException
 * @throws URISyntaxException
 * @throws JSONException 
 */
public static int revertServerWorkspace(ProjectLocation projectLocation, int revisionNo,
        CookieStore cookieStore) throws IOException, URISyntaxException, JSONException {
    SPServerInfo serviceInfo = projectLocation.getServiceInfo();
    HttpClient httpClient = ClientSideSessionUtils.createHttpClient(serviceInfo, cookieStore);

    try {
        JSONMessage message = ClientSideSessionUtils.executeServerRequest(httpClient,
                projectLocation.getServiceInfo(),
                "/" + ClientSideSessionUtils.REST_TAG + "/project/" + projectLocation.getUUID() + "/revert",
                "revisionNo=" + revisionNo, new JSONResponseHandler());
        if (message.isSuccessful()) {
            return new JSONObject(message.getBody()).getInt("currentRevision");
        } else {
            return -1;
        }
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:org.gw2InfoViewer.controllers.MainController.java

public static EventList getEventList(EventNames eventNames, MapNames mapNames, WorldNames worldNames,
        Options options) throws IOException {
    String eventsUrl = API_BASE_URL + API_VERSION + API_EVENTS;
    eventsUrl += "?world_id=" + options.getWorld();
    eventsUrl += "&map_id=" + options.getMap();
    eventsUrl += "&event_id=" + options.getEventId();

    EventList eventList;//from   w w w .  j  av a  2  s  .  c  o m
    HttpClient httpClient;
    HttpGet getEvents;

    getEvents = new HttpGet(eventsUrl);

    if (options.isProxyEnabled()) {
        httpClient = HttpsConnectionFactory.getHttpsClientWithProxy(StartComRootCertificate,
                options.getProxyAddress(), options.getProxyPort());
    } else {
        httpClient = HttpsConnectionFactory.getHttpsClient(StartComRootCertificate);
    }

    InputStream json = httpClient.execute(getEvents).getEntity().getContent();
    eventList = JsonConversionService.parseEventList(json, eventNames, mapNames, worldNames);
    httpClient.getConnectionManager().shutdown();

    Collections.sort(eventList.getEventList());

    return eventList;
}

From source file:au.edu.anu.portal.portlets.basiclti.support.HttpSupport.java

/**
 * Make a POST request with the given Map of parameters to be encoded
 * @param address   address to POST to// w w  w.j a v a2 s .  co  m
 * @param params   Map of params to use as the form parameters
 * @return
 */
public static String doPost(String address, Map<String, String> params) {

    HttpClient httpclient = new DefaultHttpClient();

    try {

        HttpPost httppost = new HttpPost(address);

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();

        for (Map.Entry<String, String> entry : params.entrySet()) {
            formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, HTTP.UTF_8);

        httppost.setEntity(entity);

        HttpResponse response = httpclient.execute(httppost);
        String responseContent = EntityUtils.toString(response.getEntity());

        return responseContent;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        httpclient.getConnectionManager().shutdown();
    }

    return null;
}

From source file:org.gw2InfoViewer.controllers.MainController.java

public static MapNames getMapNames(Options options) throws IOException {
    String eventNamesUrl = API_BASE_URL + API_VERSION + API_MAP_NAMES;
    MapNames mapNames;/*  w  w  w . j  a  va2  s. co  m*/
    HttpClient httpClient;
    HttpGet getMapNames;
    String mapNamesJson;

    getMapNames = new HttpGet(eventNamesUrl);

    if (options.isProxyEnabled()) {
        httpClient = HttpsConnectionFactory.getHttpsClientWithProxy(StartComRootCertificate,
                options.getProxyAddress(), options.getProxyPort());
    } else {
        httpClient = HttpsConnectionFactory.getHttpsClient(StartComRootCertificate);
    }
    mapNamesJson = HttpsConnectionFactory.getStringFromHttpResponse(httpClient.execute(getMapNames));
    mapNames = JsonConversionService.parseMapNames(mapNamesJson);
    httpClient.getConnectionManager().shutdown();
    return mapNames;
}

From source file:ca.sqlpower.enterprise.ClientSideSessionUtils.java

public static void deleteServerWorkspace(ProjectLocation projectLocation, CookieStore cookieStore,
        UserPrompterFactory userPrompterFactory)
        throws URISyntaxException, ClientProtocolException, IOException {
    SPServerInfo serviceInfo = projectLocation.getServiceInfo();
    HttpClient httpClient = ClientSideSessionUtils.createHttpClient(serviceInfo, cookieStore);

    try {//w  w  w  .  j  av a2s  . c  om
        executeServerRequest(httpClient, projectLocation.getServiceInfo(),
                "/" + ClientSideSessionUtils.REST_TAG + "/jcr/" + projectLocation.getUUID() + "/delete",
                new JSONResponseHandler());
    } catch (AccessDeniedException e) {
        userPrompterFactory
                .createUserPrompter("You do not have sufficient privileges to delete the selected workspace.",
                        UserPromptType.MESSAGE, UserPromptOptions.OK, UserPromptResponse.OK, "OK", "OK")
                .promptUser("");
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:org.gw2InfoViewer.controllers.MainController.java

public static EventNames getEventNames(Options options) throws IOException {
    String eventNamesUrl = API_BASE_URL + API_VERSION + API_EVENT_NAMES;
    EventNames eventNames;/*  www. j  av a 2 s .co m*/
    HttpClient httpClient;
    HttpGet getEventNames;
    String eventNamesJson;

    getEventNames = new HttpGet(eventNamesUrl);

    if (options.isProxyEnabled()) {
        httpClient = HttpsConnectionFactory.getHttpsClientWithProxy(StartComRootCertificate,
                options.getProxyAddress(), options.getProxyPort());
    } else {
        httpClient = HttpsConnectionFactory.getHttpsClient(StartComRootCertificate);
    }
    eventNamesJson = HttpsConnectionFactory.getStringFromHttpResponse(httpClient.execute(getEventNames));
    eventNames = JsonConversionService.parseEventNames(eventNamesJson);
    httpClient.getConnectionManager().shutdown();
    return eventNames;
}

From source file:org.gw2InfoViewer.controllers.MainController.java

public static WorldNames getWorldNames(Options options) throws IOException {
    String eventNamesUrl = API_BASE_URL + API_VERSION + API_WORLD_NAMES;
    WorldNames worldNames;/*from  w ww .  ja va2  s .c  o  m*/
    HttpClient httpClient;
    HttpGet getWorldNames;
    String worldNamesJson;

    getWorldNames = new HttpGet(eventNamesUrl);

    if (options.isProxyEnabled()) {
        httpClient = HttpsConnectionFactory.getHttpsClientWithProxy(StartComRootCertificate,
                options.getProxyAddress(), options.getProxyPort());
    } else {
        httpClient = HttpsConnectionFactory.getHttpsClient(StartComRootCertificate);
    }
    worldNamesJson = HttpsConnectionFactory.getStringFromHttpResponse(httpClient.execute(getWorldNames));
    worldNames = JsonConversionService.parseWorldNames(worldNamesJson);
    httpClient.getConnectionManager().shutdown();
    return worldNames;
}

From source file:org.keycloak.example.CustomerDatabaseClient.java

public static List<String> getCustomers(HttpServletRequest req) throws Failure {
    KeycloakSecurityContext session = (KeycloakSecurityContext) req
            .getAttribute(KeycloakSecurityContext.class.getName());

    HttpClient client = new DefaultHttpClient();
    try {//from  w  ww.  j av  a2 s  .c  o m
        HttpGet get = new HttpGet(UriUtils.getOrigin(req.getRequestURL().toString()) + "/database/customers");
        get.addHeader("Authorization", "Bearer " + session.getTokenString());
        try {
            HttpResponse response = client.execute(get);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new Failure(response.getStatusLine().getStatusCode());
            }
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            try {
                return JsonSerialization.readValue(is, TypedList.class);
            } finally {
                is.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}