Search in sources :

Example 11 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project maven-plugins by apache.

the class ClassicJiraDownloader method doExecute.

/**
     * Execute the query on the JIRA server.
     *
     * @throws Exception on error
     */
public void doExecute() throws Exception {
    try {
        HttpClient client = new HttpClient();
        // MCHANGES-89 Allow circular redirects
        HttpClientParams clientParams = client.getParams();
        clientParams.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        // MCHANGES-237
        clientParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        HttpState state = new HttpState();
        HostConfiguration hc = new HostConfiguration();
        client.setHostConfiguration(hc);
        client.setState(state);
        String baseUrl = JiraHelper.getBaseUrl(project.getIssueManagement().getUrl());
        getLog().debug("JIRA lives at: " + baseUrl);
        // Here we only need the host part of the URL
        determineProxy(baseUrl, client);
        prepareBasicAuthentication(client);
        boolean jiraAuthenticationSuccessful = false;
        if (isJiraAuthenticationConfigured()) {
            // Here we only need the parts up to and including the host part of the URL
            jiraAuthenticationSuccessful = doJiraAuthentication(client, baseUrl);
        }
        if ((isJiraAuthenticationConfigured() && jiraAuthenticationSuccessful) || !isJiraAuthenticationConfigured()) {
            String fullUrl;
            if (useJql) {
                fullUrl = getJqlQueryURL();
            } else {
                fullUrl = getParameterBasedQueryURL(client);
            }
            if (log.isDebugEnabled()) {
                log.debug("download jira issues from url " + fullUrl);
            }
            // execute the GET
            download(client, fullUrl);
        }
    } catch (Exception e) {
        if (project.getIssueManagement() != null) {
            getLog().error("Error accessing " + project.getIssueManagement().getUrl(), e);
        } else {
            getLog().error("Error accessing mock project issues", e);
        }
    }
}
Also used : HostConfiguration(org.apache.commons.httpclient.HostConfiguration) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) HttpState(org.apache.commons.httpclient.HttpState) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 12 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project bamboobsc by billchen198318.

the class ApplicationSiteUtils method checkTestConnection.

private static boolean checkTestConnection(String host, String contextPath, HttpServletRequest request) {
    boolean test = false;
    String basePath = request.getScheme() + "://" + host + "/" + contextPath;
    String urlStr = basePath + "/pages/system/testJsonResult.action";
    try {
        logger.info("checkTestConnection , url=" + urlStr);
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(urlStr);
        HttpClientParams params = new HttpClientParams();
        params.setConnectionManagerTimeout(TEST_JSON_HTTP_TIMEOUT);
        client.setParams(params);
        client.executeMethod(method);
        byte[] responseBody = method.getResponseBody();
        if (null == responseBody) {
            test = false;
            return test;
        }
        String content = new String(responseBody, Constants.BASE_ENCODING);
        ObjectMapper mapper = new ObjectMapper();
        @SuppressWarnings("unchecked") Map<String, Object> dataMap = (Map<String, Object>) mapper.readValue(content, HashMap.class);
        if (YesNo.YES.equals(dataMap.get("success"))) {
            test = true;
        }
    } catch (JsonParseException e) {
        logger.error(e.getMessage().toString());
    } catch (JsonMappingException e) {
        logger.error(e.getMessage().toString());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (!test) {
            logger.warn("checkTestConnection : " + String.valueOf(test));
        } else {
            logger.info("checkTestConnection : " + String.valueOf(test));
        }
    }
    return test;
}
Also used : HashMap(java.util.HashMap) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ServiceException(com.netsteadfast.greenstep.base.exception.ServiceException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) HttpClient(org.apache.commons.httpclient.HttpClient) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(org.apache.commons.httpclient.HttpMethod) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 13 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project dianping-open-sdk by dianping.

the class ApiTool method requestApi.

public static String requestApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap) {
    String queryString = getQueryString(appKey, secret, paramMap);
    StringBuffer response = new StringBuffer();
    HttpClientParams httpConnectionParams = new HttpClientParams();
    httpConnectionParams.setConnectionManagerTimeout(1000);
    HttpClient client = new HttpClient(httpConnectionParams);
    HttpMethod method = new GetMethod(apiUrl);
    try {
        if (StringUtils.isNotBlank(queryString)) {
            // Encode query string with UTF-8
            String encodeQuery = URIUtil.encodeQuery(queryString, "UTF-8");
            LOGGER.debug("Encoded Query:" + encodeQuery);
            method.setQueryString(encodeQuery);
        }
        client.executeMethod(method);
        BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            response.append(line).append(System.getProperty("line.separator"));
        }
        reader.close();
    } catch (URIException e) {
        LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e);
    } catch (IOException e) {
        LOGGER.error("Request URL: " + apiUrl + " failed. ", e);
    } finally {
        method.releaseConnection();
    }
    return response.toString();
}
Also used : URIException(org.apache.commons.httpclient.URIException) InputStreamReader(java.io.InputStreamReader) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) GetMethod(org.apache.commons.httpclient.methods.GetMethod) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 14 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project dianping-open-sdk by dianping.

the class DemoApiTool method requestApi.

public static String requestApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap) {
    String queryString = getQueryString(appKey, secret, paramMap);
    StringBuffer response = new StringBuffer();
    HttpClientParams httpConnectionParams = new HttpClientParams();
    httpConnectionParams.setConnectionManagerTimeout(1000);
    HttpClient client = new HttpClient(httpConnectionParams);
    HttpMethod method = new GetMethod(apiUrl);
    try {
        if (StringUtils.isNotBlank(queryString)) {
            // Encode query string with UTF-8
            String encodeQuery = URIUtil.encodeQuery(queryString, "UTF-8");
            LOGGER.debug("Encoded Query:" + encodeQuery);
            method.setQueryString(encodeQuery);
        }
        client.executeMethod(method);
        BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            response.append(line).append(System.getProperty("line.separator"));
        }
        reader.close();
    } catch (URIException e) {
        LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e);
    } catch (IOException e) {
        LOGGER.error("Request URL: " + apiUrl + " failed. ", e);
    } finally {
        method.releaseConnection();
    }
    return response.toString();
}
Also used : URIException(org.apache.commons.httpclient.URIException) InputStreamReader(java.io.InputStreamReader) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) GetMethod(org.apache.commons.httpclient.methods.GetMethod) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

HttpClientParams (org.apache.commons.httpclient.params.HttpClientParams)14 HttpClient (org.apache.commons.httpclient.HttpClient)12 IOException (java.io.IOException)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 HttpMethod (org.apache.commons.httpclient.HttpMethod)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)4 Map (java.util.Map)3 URIException (org.apache.commons.httpclient.URIException)3 HashMap (java.util.HashMap)2 MultiThreadedHttpConnectionManager (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 HttpConnectionManagerParams (org.apache.commons.httpclient.params.HttpConnectionManagerParams)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ServiceException (com.netsteadfast.greenstep.base.exception.ServiceException)1 URI (java.net.URI)1 LinkedHashSet (java.util.LinkedHashSet)1 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)1