Example usage for org.apache.commons.httpclient HttpClient getHttpConnectionManager

List of usage examples for org.apache.commons.httpclient HttpClient getHttpConnectionManager

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpClient getHttpConnectionManager.

Prototype

public HttpConnectionManager getHttpConnectionManager()

Source Link

Usage

From source file:org.geworkbench.engine.ccm.ComponentConfigurationManagerWindow2.java

private void findAvailableUpdatesFromTomcat() {

    installedRow = initialRow;/*w  w  w. j  a  v  a2s.c o  m*/
    //      String url = System.getProperty("remote_components_config_url");
    //remote_components_config_url=http://califano11.cgc.cpmc.columbia.edu:8080/componentRepository/deploycomponents.txt

    GlobalPreferences prefs = GlobalPreferences.getInstance();
    String url = prefs.getRCM_URL().trim();
    if (url == null || url == "") {
        log.info("No Remote Component Manager URL configured.");
        return;
    }
    url += "/deploycomponents.txt";

    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
    GetMethod method = new GetMethod(url);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    method.setFollowRedirects(true);

    try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            JOptionPane.showMessageDialog(null,
                    "No updates are available at this time.\nPlease try again later.",
                    "Remote Component Update", JOptionPane.PLAIN_MESSAGE);
            return;
        }

        String deploycomponents = method.getResponseBodyAsString();
        //         String[] rows = deploycomponents.split("\\r\\n");
        String[] folderNameVersions = deploycomponents.split("\\r\\n");

        for (int i = 0; i < ccmTableModel.getModelRowCount(); i++) {
            String localFolderName = ccmTableModel.getResourceFolder(i);

            String localVersion = ((String) ccmTableModel.getModelValueAt(i, CCMTableModel2.VERSION_INDEX));
            Double dLocalVersion = new Double(localVersion);

            for (int j = 0; j < folderNameVersions.length; j++) {
                String[] cols = folderNameVersions[j].split(",");
                String remoteFolderName = cols[0];
                String remoteVersion = cols[1];
                Double dRemoteVersion = new Double(remoteVersion);

                if (localFolderName.equalsIgnoreCase(remoteFolderName)) {
                    if (dRemoteVersion.compareTo(dLocalVersion) > 0) {
                        ccmTableModel.setModelValueAt(remoteVersion, i, CCMTableModel2.AVAILABLE_UPDATE_INDEX,
                                CCMTableModel2.NO_VALIDATION);
                    }
                }
            }
        }
    } catch (HttpException e) {
        JOptionPane.showMessageDialog(null, "No updates are available at this time.\nPlease try again later.",
                "Remote Component Update", JOptionPane.PLAIN_MESSAGE);
        //e.printStackTrace();
        return;
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null,
                e.getMessage() + ".\n(" + e.getClass().getName() + ")\nPlease try again later.",
                "No Update Available", JOptionPane.PLAIN_MESSAGE);
        //e.printStackTrace();
        return;
    } catch (Exception e) { // IllegalArgumentException
        JOptionPane.showMessageDialog(null, e.getMessage() + ".", "No Update Available",
                JOptionPane.PLAIN_MESSAGE);
    }
}

From source file:org.hammer.santamaria.mapper.dataset.CKAN2DataSetInput.java

@SuppressWarnings("unchecked")
public BSONObject getDataSet(String url, String datasource, String id, BSONObject c) {
    BSONObject dataset = new BasicBSONObject();
    dataset.put("datasource", datasource);
    dataset.put("id", id);

    String sId = EncodeURIComponent(id);
    LOG.info("---> id " + id + " - " + sId);

    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false);

    LOG.info(//from  www. j a va2 s . c  o  m
            "******************************************************************************************************");
    LOG.info(" ");
    LOG.info(url + PACKAGE_GET + sId);
    LOG.info(" ");
    LOG.info(
            "******************************************************************************************************");

    GetMethod method = new GetMethod(url + PACKAGE_GET + sId);

    method.setRequestHeader("User-Agent", "Hammer Project - SantaMaria crawler");
    method.getParams().setParameter(HttpMethodParams.USER_AGENT, "Hammer Project - SantaMaria crawler");
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new Exception("Method failed: " + method.getStatusLine());
        }
        byte[] responseBody = method.getResponseBody();
        LOG.debug(new String(responseBody));
        Document result = Document.parse(new String(responseBody));

        if (result != null) {
            dataset.put("title", result.get("title"));
            dataset.put("author", result.get("author"));
            dataset.put("author_email", result.get("author_email"));
            dataset.put("license_id", result.get("license"));
        }

        ArrayList<String> tags = new ArrayList<String>();
        ArrayList<String> meta = new ArrayList<String>();
        ArrayList<String> other_tags = new ArrayList<String>();

        if (result.containsKey("author"))
            other_tags.add(result.get("author").toString());
        if (result.containsKey("title"))
            other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("title").toString()));
        if (result.containsKey("description"))
            other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("description").toString()));

        ArrayList<Document> resources = new ArrayList<Document>();
        if (result != null && result.containsKey("resources")) {
            resources = (ArrayList<Document>) result.get("resources");
            for (Document resource : resources) {
                if (resource.getString("format").toUpperCase().equals("JSON")) {
                    dataset.put("dataset-type", "JSON");
                    dataset.put("url", resource.get("url"));
                    dataset.put("created", resource.get("created"));
                    dataset.put("description", resource.get("description"));
                    dataset.put("revision_timestamp", resource.get("revision_timestamp"));
                    meta = DSSUtils.GetMetaByResource(resource.get("url").toString());

                }
            }
        }

        if (result != null && result.containsKey("tags")) {
            ArrayList<Document> tagsFromCKAN = (ArrayList<Document>) result.get("tags");
            for (Document tag : tagsFromCKAN) {
                if (tag.containsKey("state") && tag.getString("state").toUpperCase().equals("ACTIVE")) {
                    tags.add(tag.getString("display_name").trim().toLowerCase());
                } else if (tag.containsKey("display_name")) {
                    tags.add(tag.getString("display_name").trim().toLowerCase());
                }
            }

        }

        dataset.put("tags", tags);
        dataset.put("meta", meta);
        dataset.put("resources", resources);
        dataset.put("other_tags", other_tags);

    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
    } finally {
        method.releaseConnection();
    }
    return dataset;
}

From source file:org.hammer.santamaria.mapper.dataset.CKANDataSetInput.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public BSONObject getDataSet(String url, String datasource, String id, BSONObject c) {
    BSONObject dataset = new BasicBSONObject();
    dataset.put("datasource", datasource);
    dataset.put("id", id);

    String sId = EncodeURIComponent(id);
    LOG.info("---> id " + id + " - " + sId);

    HttpClient client = new HttpClient();

    // some ckan site doesn't allow connection with hight timeout!!!
    // client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
    // client.getHttpConnectionManager().getParams().setSoTimeout(2000);
    ///////from w  ww . ja  v a2  s .c  o m

    // add to prevent redirect (?)
    client.getHttpConnectionManager().getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false);

    LOG.info(
            "******************************************************************************************************");
    LOG.info(" ");
    LOG.info(url + PACKAGE_GET + sId);
    LOG.info(" ");
    LOG.info(
            "******************************************************************************************************");

    GetMethod method = new GetMethod(url + PACKAGE_GET + sId);

    method.setRequestHeader("User-Agent", "Hammer Project - SantaMaria crawler");
    method.getParams().setParameter(HttpMethodParams.USER_AGENT, "Hammer Project - SantaMaria crawler");
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new Exception("Method failed: " + method.getStatusLine());
        }
        byte[] responseBody = method.getResponseBody();
        LOG.debug(new String(responseBody));
        Document doc = Document.parse(new String(responseBody));

        if (doc != null && doc.containsKey("result") && doc.get("result") != null) {
            Document result = new Document();
            LOG.info(doc.get("result").getClass().toString());
            if (doc.get("result") instanceof Document) {
                LOG.info("!!! Document result !!!!");
                result = (Document) doc.get("result");
            } else if (doc.get("result") instanceof ArrayList) {
                LOG.info("!!! Document list !!!!");

                result = (Document) (((ArrayList) doc.get("result")).get(0));
            } else {
                LOG.info("!!! NOT FOUND !!!!");
                result = null;
            }
            LOG.info("result find!");
            if (result != null) {
                dataset.put("title", result.get("title"));
                dataset.put("author", result.get("author"));
                dataset.put("author_email", result.get("author_email"));
                dataset.put("license_id", result.get("license_id"));
            }

            ArrayList<String> tags = new ArrayList<String>();
            ArrayList<String> meta = new ArrayList<String>();
            ArrayList<String> other_tags = new ArrayList<String>();

            if (result.containsKey("author") && result.get("author") != null)
                other_tags.add(result.get("author").toString());
            if (result.containsKey("title") && result.get("title") != null)
                other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("title").toString()));
            if (result.containsKey("description") && result.get("description") != null)
                other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("description").toString()));
            if (result.containsKey("notes") && result.get("notes") != null)
                other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("notes").toString()));

            ArrayList<Document> resources = new ArrayList<Document>();
            if (result != null && result.containsKey("resources")) {
                resources = (ArrayList<Document>) result.get("resources");
                for (Document resource : resources) {
                    if (resource.getString("format").toUpperCase().equals("JSON")) {
                        dataset.put("dataset-type", "JSON");
                        dataset.put("url", resource.get("url"));
                        dataset.put("created", resource.get("created"));
                        dataset.put("description", resource.get("description"));
                        dataset.put("revision_timestamp", resource.get("revision_timestamp"));
                        meta = DSSUtils.GetMetaByResource(resource.get("url").toString());
                    }
                }
            }

            if (result != null && result.containsKey("tags")) {
                ArrayList<Document> tagsFromCKAN = (ArrayList<Document>) result.get("tags");
                for (Document tag : tagsFromCKAN) {
                    if (tag.containsKey("state") && tag.getString("state").toUpperCase().equals("ACTIVE")) {
                        tags.add(tag.getString("display_name").trim().toLowerCase());
                    } else if (tag.containsKey("display_name")) {
                        tags.add(tag.getString("display_name").trim().toLowerCase());
                    }
                }

            }

            dataset.put("tags", tags);
            dataset.put("meta", meta);
            dataset.put("resources", resources);
            dataset.put("other_tags", other_tags);

        }
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
    } finally {
        method.releaseConnection();
    }
    return dataset;
}

From source file:org.hammer.santamaria.mapper.dataset.CKANDataSetInput.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] pArgs) throws Exception {
    String id = "proportion-of-children-under-5-years-who-have-ever-breastfed-by-county-xls-2005-6";
    String sId = EncodeURIComponent(id);
    String url = "https://africaopendata.org/api/action";

    BSONObject dataset = new BasicBSONObject();
    dataset.put("datasource", "Test");
    dataset.put("id", id);

    LOG.info("---> id " + id + " - " + sId);

    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false);
    LOG.info(/*from   w  w w.  j ava2 s  .  c o m*/
            "******************************************************************************************************");
    LOG.info(" ");
    LOG.info(url + PACKAGE_GET + sId);
    LOG.info(" ");
    LOG.info(
            "******************************************************************************************************");

    GetMethod method = new GetMethod(url + PACKAGE_GET + sId);

    method.setRequestHeader("User-Agent", "Hammer Project - SantaMaria crawler");
    method.getParams().setParameter(HttpMethodParams.USER_AGENT, "Hammer Project - SantaMaria crawler");
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new Exception("Method failed: " + method.getStatusLine());
        }
        byte[] responseBody = method.getResponseBody();
        LOG.debug(new String(responseBody));
        Document doc = Document.parse(new String(responseBody));

        if (doc != null && doc.containsKey("result")) {
            Document result = new Document();
            LOG.info(doc.get("result").getClass().toString());
            if (doc.get("result") instanceof Document) {
                LOG.info("!!! Document result !!!!");
                result = (Document) doc.get("result");
            } else if (doc.get("result") instanceof ArrayList) {
                LOG.info("!!! Document list !!!!");

                result = (Document) (((ArrayList) doc.get("result")).get(0));
            } else {
                LOG.info("!!! NOT FOUND !!!!");
                result = null;
            }
            LOG.info("result find!");
            if (result != null) {
                dataset.put("title", result.get("title"));
                dataset.put("author", result.get("author"));
                dataset.put("author_email", result.get("author_email"));
                dataset.put("license_id", result.get("license_id"));
            }

            ArrayList<String> tags = new ArrayList<String>();
            ArrayList<String> meta = new ArrayList<String>();
            ArrayList<String> other_tags = new ArrayList<String>();

            if (result.containsKey("author") && result.get("author") != null)
                other_tags.add(result.get("author").toString());
            if (result.containsKey("title") && result.get("title") != null)
                other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("title").toString()));
            if (result.containsKey("description") && result.get("description") != null)
                other_tags.addAll(DSSUtils.GetKeyWordsFromText(result.get("description").toString()));

            ArrayList<Document> resources = new ArrayList<Document>();
            if (result != null && result.containsKey("resources")) {
                resources = (ArrayList<Document>) result.get("resources");
                for (Document resource : resources) {
                    if (resource.getString("format").toUpperCase().equals("JSON")) {
                        dataset.put("dataset-type", "JSON");
                        dataset.put("url", resource.get("url"));
                        dataset.put("created", resource.get("created"));
                        dataset.put("description", resource.get("description"));
                        dataset.put("revision_timestamp", resource.get("revision_timestamp"));
                        meta = DSSUtils.GetMetaByResource(resource.get("url").toString());
                    }
                }
            }

            if (result != null && result.containsKey("tags")) {
                ArrayList<Document> tagsFromCKAN = (ArrayList<Document>) result.get("tags");
                for (Document tag : tagsFromCKAN) {
                    if (tag.containsKey("state") && tag.getString("state").toUpperCase().equals("ACTIVE")) {
                        tags.add(tag.getString("display_name").trim().toLowerCase());
                    } else if (tag.containsKey("display_name")) {
                        tags.add(tag.getString("display_name").trim().toLowerCase());
                    }
                }

            }

            dataset.put("tags", tags);
            dataset.put("meta", meta);
            dataset.put("resources", resources);
            dataset.put("other_tags", other_tags);

        }

    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
    } finally {
        method.releaseConnection();
    }

    //GetMetaByDocument("http://catalog.data.gov/api/action/package_show?id=1e68f387-5f1c-46c0-a0d1-46044ffef5bf");
}

From source file:org.jahia.services.notification.HttpClientService.java

private static HttpClient cloneHttpClient(HttpClient source) {
    HttpClient cloned = new HttpClient(source.getParams(), source.getHttpConnectionManager());
    String sourceProxyHost = source.getHostConfiguration().getProxyHost();
    if (sourceProxyHost != null) {
        cloned.getHostConfiguration().setProxy(sourceProxyHost, source.getHostConfiguration().getProxyPort());
    }/*from ww w  . j a  v  a  2s . co  m*/
    Credentials proxyCredentials = source.getState().getProxyCredentials(AuthScope.ANY);
    if (proxyCredentials != null) {
        HttpState state = new HttpState();
        state.setProxyCredentials(AuthScope.ANY, proxyCredentials);
        cloned.setState(state);
    }

    return cloned;

}

From source file:org.jahia.services.notification.HttpClientService.java

private void shutdown(HttpClient client) {
    try {//w  ww .jav a  2s. co  m
        if (client.getHttpConnectionManager() instanceof MultiThreadedHttpConnectionManager) {
            ((MultiThreadedHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
        } else if (client.getHttpConnectionManager() instanceof SimpleHttpConnectionManager) {
            ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();
        }
    } catch (Exception e) {
        logger.warn("Error shutting down HttpClient. Cause: " + e.getMessage(), e);
    }
}

From source file:org.jasig.portlet.calendar.adapter.exchange.ExchangeHttpWebServiceMessageSenderTest.java

@Test
public void testGetClient() {
    HttpClient client = sender.getClient();
    assertNotNull(client);//from ww w.j  av  a  2s  .c o m
    assertEquals(1000, client.getHttpConnectionManager().getParams().getConnectionTimeout());
    assertEquals(1000, client.getHttpConnectionManager().getParams().getSoTimeout());
    assertEquals(5, client.getHttpConnectionManager().getParams().getMaxTotalConnections());
    assertEquals(5, client.getHttpConnectionManager().getParams().getDefaultMaxConnectionsPerHost());

    UsernamePasswordCredentials credentials = (UsernamePasswordCredentials) client.getState()
            .getCredentials(AuthScope.ANY);
    assertEquals("user", credentials.getUserName());
    assertEquals("pass", credentials.getPassword());
}

From source file:org.jasig.portlet.calendar.adapter.exchange.ExchangeHttpWebServiceMessageSenderTest.java

@Test
public void testUpdateConnectionParameters() {
    sender.setConnectionTimeout(2000);//w w w  . j  a va  2  s .  c  om
    sender.setReadTimeout(2000);
    sender.setMaxConnections(10);

    HttpClient client = sender.getClient();
    assertNotNull(client);
    assertEquals(2000, client.getHttpConnectionManager().getParams().getConnectionTimeout());
    assertEquals(2000, client.getHttpConnectionManager().getParams().getSoTimeout());
    assertEquals(10, client.getHttpConnectionManager().getParams().getMaxTotalConnections());
    assertEquals(10, client.getHttpConnectionManager().getParams().getDefaultMaxConnectionsPerHost());
}

From source file:org.jboss.tools.common.util.HttpUtil.java

private static HttpClient createHttpClient(String url, IProxyService proxyService) throws IOException {
    HttpClient httpClient = new HttpClient();

    if (proxyService.isProxiesEnabled()) {
        IProxyData[] proxyData = proxyService.getProxyData();
        URL netUrl = new URL(url);
        String hostName = netUrl.getHost();
        String[] nonProxiedHosts = proxyService.getNonProxiedHosts();
        boolean nonProxiedHost = false;
        for (int i = 0; i < nonProxiedHosts.length; i++) {
            String nonProxiedHostName = nonProxiedHosts[i];
            if (nonProxiedHostName.equalsIgnoreCase(hostName)) {
                nonProxiedHost = true;// w ww.j av a  2s. c o  m
                break;
            }
        }
        if (!nonProxiedHost) {
            for (int i = 0; i < proxyData.length; i++) {
                IProxyData proxy = proxyData[i];
                if (IProxyData.HTTP_PROXY_TYPE.equals(proxy.getType())) {
                    String proxyHostName = proxy.getHost();
                    if (proxyHostName == null) {
                        break;
                    }
                    int portNumber = proxy.getPort();
                    if (portNumber == -1) {
                        portNumber = 80;
                    }
                    httpClient.getHostConfiguration().setProxy(proxyHostName, portNumber);
                    if (proxy.isRequiresAuthentication()) {
                        String userName = proxy.getUserId();
                        if (userName != null) {
                            String password = proxy.getPassword();
                            httpClient.getState().setProxyCredentials(
                                    new AuthScope(null, AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME),
                                    new UsernamePasswordCredentials(userName, password));
                        }
                    }
                    break; // Use HTTP proxy only.
                }
            }
        }
    }

    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);

    return httpClient;
}

From source file:org.jbpm.process.workitem.rest.RESTWorkItemHandler.java

public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    // extract required parameters
    String urlStr = (String) workItem.getParameter("Url");
    String method = (String) workItem.getParameter("Method");
    if (urlStr == null) {
        throw new IllegalArgumentException("Url is a required parameter");
    }//from w  ww  .  ja  v  a2  s.  c  o m
    if (method == null || method.trim().length() == 0) {
        method = "GET";
    }
    Map<String, Object> params = workItem.getParameters();

    // optional timeout config parameters, defaulted to 60 seconds
    Integer connectTimeout = (Integer) params.get("ConnectTimeout");
    if (connectTimeout == null)
        connectTimeout = 60000;
    Integer readTimeout = (Integer) params.get("ReadTimeout");
    if (readTimeout == null)
        readTimeout = 60000;

    HttpClient httpclient = new HttpClient();
    httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectTimeout);
    httpclient.getHttpConnectionManager().getParams().setSoTimeout(readTimeout);

    HttpMethod theMethod = null;
    if ("GET".equals(method)) {
        theMethod = new GetMethod(urlStr);
    } else if ("POST".equals(method)) {
        theMethod = new PostMethod(urlStr);
        setBody(theMethod, params);
    } else if ("PUT".equals(method)) {
        theMethod = new PutMethod(urlStr);
        setBody(theMethod, params);
    } else if ("DELETE".equals(method)) {
        theMethod = new DeleteMethod(urlStr);
    } else {
        throw new IllegalArgumentException("Method " + method + " is not supported");
    }
    doAuthorization(httpclient, theMethod, params);
    try {
        int responseCode = httpclient.executeMethod(theMethod);
        Map<String, Object> results = new HashMap<String, Object>();
        if (responseCode >= 200 && responseCode < 300) {
            theMethod.getResponseBody();
            postProcessResult(theMethod.getResponseBodyAsString(), results);
            results.put("StatusMsg",
                    "request to endpoint " + urlStr + " successfully completed " + theMethod.getStatusText());
        } else {
            logger.warn("Unsuccessful response from REST server (status {}, endpoint {}, response {}",
                    responseCode, urlStr, theMethod.getResponseBodyAsString());
            results.put("StatusMsg",
                    "endpoint " + urlStr + " could not be reached: " + theMethod.getResponseBodyAsString());
        }
        results.put("Status", responseCode);

        // notify manager that work item has been completed
        manager.completeWorkItem(workItem.getId(), results);
    } catch (Exception e) {
        handleException(e);
    } finally {
        theMethod.releaseConnection();
    }
}