Example usage for org.apache.commons.httpclient.params HttpMethodParams RETRY_HANDLER

List of usage examples for org.apache.commons.httpclient.params HttpMethodParams RETRY_HANDLER

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpMethodParams RETRY_HANDLER.

Prototype

String RETRY_HANDLER

To view the source code for org.apache.commons.httpclient.params HttpMethodParams RETRY_HANDLER.

Click Source Link

Usage

From source file:org.jahia.modules.filter.WebClippingFilter.java

private String getURLContentWithGetMethod(String urlToClip, RenderContext renderContext, Resource resource,
        RenderChain chain, Map map) throws IOException {
    String path = urlToClip;//from   w w  w .j  a v a  2  s . c o  m
    Map parameters = (Map) map.get("URL_PARAMS");
    // Get the httpClient
    HttpClient httpClient = new HttpClient();
    Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
    httpClient.getParams().setContentCharset("UTF-8");
    //
    // Add parameters
    if (parameters != null) {
        StringBuffer params = new StringBuffer(4096);
        Iterator iterator = parameters.entrySet().iterator();
        int index = 0;
        String characterEncoding = httpClient.getParams().getContentCharset();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            if (!entry.getKey().toString().equals("original_method")
                    && !entry.getKey().toString().equals("jahia_url_web_clipping")) {
                // Is not a jahia params so pass it to the url
                if (!parameters.isEmpty()) {
                    final Object value = entry.getValue();
                    if (value instanceof String[]) {
                        String[] strings = (String[]) value;
                        StringBuffer buffer = new StringBuffer(4096);
                        for (int i = 0; i < strings.length; i++) {
                            String string = strings[i];
                            buffer.append((i != 0) ? "," : "").append(string);
                        }
                        params.append(index == 0 ? "?" : "&").append(entry.getKey().toString()).append("=")
                                .append(URLEncoder.encode(buffer.toString(), characterEncoding));
                        index++;
                    } else {
                        params.append(index == 0 ? "?" : "&").append(entry.getKey().toString()).append("=")
                                .append(URLEncoder.encode(value.toString(), characterEncoding));
                        index++;
                    }
                }
            }
        }
        path = path + params.toString();
    }
    // Rebuild Path by encoding the path
    URL targetURL = new URL(path);
    String[] pathInfo = targetURL.getPath().split("/");
    StringBuffer pathBuffer;
    if (pathInfo.length > 0) {
        pathBuffer = new StringBuffer(URLEncoder.encode(pathInfo[0], "UTF-8"));
        for (int i = 1; i < pathInfo.length; i++) {
            String s = pathInfo[i];
            String[] s2 = s.split(";");
            pathBuffer.append("/").append(URLEncoder.encode(s2[0], "UTF-8"));
            if (s2.length > 1) { // there is a jsessionid so let's add it again without encoding
                pathBuffer.append(";").append(s2[1]);
            }
        }
    } else {
        pathBuffer = new StringBuffer("");
    }
    path = targetURL.getProtocol() + "://" + targetURL.getHost()
            + (targetURL.getPort() == -1 ? "" : ":" + targetURL.getPort()) + pathBuffer.toString()
            + (targetURL.getQuery() != null ? "?" + targetURL.getQuery() : "");
    // Create a get method for accessing the url.
    HttpMethodBase httpMethod = new GetMethod(path);
    // Set a default retry handler (see httpclient doc).
    httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    String contentCharset = httpClient.getParams().getContentCharset();
    // Get the response of the url in a string.
    httpClient.getParams().setContentCharset(contentCharset);
    return getResponse(path, renderContext, resource, chain, httpMethod, httpClient);
}

From source file:org.jahia.modules.filter.WebClippingFilter.java

private String getURLContentWithPostMethod(String urlToClip, RenderContext renderContext, Resource resource,
        RenderChain chain, Map map) {
    String path = urlToClip;/*from  w  w  w.ja v a 2 s.  c  o m*/
    Map parameters = (Map) map.get("URL_PARAMS");
    // Get the httpClient
    HttpClient httpClient = new HttpClient();
    Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
    httpClient.getParams().setContentCharset("UTF-8");
    // Create a post method for accessing the url.
    PostMethod postMethod = new PostMethod(path);
    // Set a default retry handler (see httpclient doc).
    postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    if (parameters != null) {
        Iterator iterator = parameters.entrySet().iterator();
        StringBuffer buffer = new StringBuffer(4096);
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            if (!entry.getKey().toString().equals("original_method")
                    && !entry.getKey().toString().equals("jahia_url_web_clipping")) {
                final Object value = entry.getValue();
                if (value instanceof String[]) {
                    buffer.setLength(0);
                    String[] strings = (String[]) entry.getValue();
                    for (int i = 0; i < strings.length; i++) {
                        String string = strings[i];
                        buffer.append((i != 0) ? "," : "").append(string);
                    }
                    postMethod.addParameter(entry.getKey().toString(), buffer.toString());
                } else {
                    postMethod.addParameter(entry.getKey().toString(), value.toString());
                }
            }
        }
    }
    String contentCharset = httpClient.getParams().getContentCharset();
    httpClient.getParams().setContentCharset(contentCharset);
    return getResponse(path, renderContext, resource, chain, postMethod, httpClient);
}

From source file:org.jahia.modules.filter.WebClippingFilter.java

private String getResponse(String urlToClip, RenderContext renderContext, Resource resource, RenderChain chain,
        HttpMethodBase httpMethod, HttpClient httpClient) {
    try {//from  w  w w.  j ava 2  s  .co  m
        httpMethod.getParams().setParameter("http.connection.timeout",
                resource.getNode().getPropertyAsString("connectionTimeout"));
        httpMethod.getParams().setParameter("http.protocol.expect-continue",
                Boolean.valueOf(resource.getNode().getPropertyAsString("expectContinue")));
        httpMethod.getParams().setCookiePolicy(CookiePolicy.RFC_2965);

        int statusCode = httpClient.executeMethod(httpMethod);

        if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                || statusCode == HttpStatus.SC_SEE_OTHER || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
            if (log.isDebugEnabled()) {
                log.debug("We follow a redirection ");
            }
            String redirectLocation;
            Header locationHeader = httpMethod.getResponseHeader("location");
            if (locationHeader != null) {
                redirectLocation = locationHeader.getValue();
                if (!redirectLocation.startsWith("http")) {
                    URL siteURL = new URL(urlToClip);
                    String tmpURL = siteURL.getProtocol() + "://" + siteURL.getHost()
                            + ((siteURL.getPort() > 0) ? ":" + siteURL.getPort() : "") + "/" + redirectLocation;
                    httpMethod = new GetMethod(tmpURL);
                    // Set a default retry handler (see httpclient doc).
                    httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                            new DefaultHttpMethodRetryHandler(3, false));
                    httpMethod.getParams().setParameter("http.connection.timeout",
                            resource.getNode().getPropertyAsString("connectionTimeout"));
                    httpMethod.getParams().setParameter("http.protocol.expect-continue",
                            resource.getNode().getPropertyAsString("expectContinue"));
                    httpMethod.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
                } else {
                    httpMethod = new GetMethod(redirectLocation);
                }
            }
        }
        if (statusCode != HttpStatus.SC_OK) {
            //this.cacheable = false;
            StringBuffer buffer = new StringBuffer("<html>\n<body>");
            buffer.append('\n' + "Error getting ").append(urlToClip).append(" failed with error code ")
                    .append(statusCode);
            buffer.append("\n</body>\n</html>");
            return buffer.toString();
        }

        String[] type = httpMethod.getResponseHeader("Content-Type").getValue().split(";");
        String contentCharset = "UTF-8";
        if (type.length == 2) {
            contentCharset = type[1].split("=")[1];
        }
        InputStream inputStream = new BufferedInputStream(httpMethod.getResponseBodyAsStream());
        if (inputStream != null) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(100 * 1024);
            byte[] buffer = new byte[100 * 1024];
            int len;
            while ((len = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, len);
            }
            outputStream.close();
            inputStream.close();
            final byte[] responseBodyAsBytes = outputStream.toByteArray();
            String responseBody = new String(responseBodyAsBytes, "US-ASCII");
            Source source = new Source(responseBody);
            List list = source.getAllStartTags(HTMLElementName.META);
            for (Object aList : list) {
                StartTag startTag = (StartTag) aList;
                Attributes attributes = startTag.getAttributes();
                final Attribute attribute = attributes.get("http-equiv");
                if (attribute != null && attribute.getValue().equalsIgnoreCase("content-type")) {
                    type = attributes.get("content").getValue().split(";");
                    if (type.length == 2) {
                        contentCharset = type[1].split("=")[1];
                    }
                }
            }
            final String s = contentCharset.toUpperCase();
            return rewriteBody(new String(responseBodyAsBytes, s), urlToClip, s, resource, renderContext);
        }
    } catch (Exception e) {
        e.printStackTrace();
        //this.cacheable = false;
        StringBuffer buffer = new StringBuffer("<html>\n<body>");
        buffer.append('\n' + "Error getting ").append(urlToClip).append(" failed with error : ")
                .append(e.toString());
        buffer.append("\n</body>\n</html>");
        return buffer.toString();
    }
    return null;
}

From source file:org.jahia.test.bin.FindPrincipalTest.java

@Before
public void setUp() throws Exception {

    // Create an instance of HttpClient.
    client = new HttpClient();

    // todo we should really insert content to test the find.

    PostMethod loginMethod = new PostMethod(getLoginServletURL());
    try {/*from   www.java 2  s  . c  om*/
        loginMethod.addParameter("username", "root");
        loginMethod.addParameter("password", "root1234");
        loginMethod.addParameter("redirectActive", "false");
        // the next parameter is required to properly activate the valve check.
        loginMethod.addParameter(LoginEngineAuthValveImpl.LOGIN_TAG_PARAMETER, "1");
        // Provide custom retry handler is necessary
        loginMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        int statusCode = client.executeMethod(loginMethod);
        assertEquals("Method failed: " + loginMethod.getStatusLine(), HttpStatus.SC_OK, statusCode);
    } finally {
        loginMethod.releaseConnection();
    }
}

From source file:org.jahia.test.bin.FindPrincipalTest.java

@After
public void tearDown() throws Exception {

    PostMethod logoutMethod = new PostMethod(getLogoutServletURL());
    try {/*from  w ww  .  jav  a 2  s .c  o m*/
        logoutMethod.addParameter("redirectActive", "false");
        // Provide custom retry handler is necessary
        logoutMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        int statusCode = client.executeMethod(logoutMethod);
        assertEquals("Method failed: " + logoutMethod.getStatusLine(), HttpStatus.SC_OK, statusCode);
    } finally {
        logoutMethod.releaseConnection();
    }
}

From source file:org.jahia.test.bin.FindPrincipalTest.java

@Test
public void testFindUsers() throws IOException, JSONException, JahiaException {

    PostMethod method = new PostMethod(getFindPrincipalServletURL());
    try {/*from  w  w  w .  j a  v  a  2 s. c  om*/
        method.addParameter("principalType", "users");
        method.addParameter("wildcardTerm", "*root*");

        // Provide custom retry handler is necessary
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        // Execute the method.
        int statusCode = client.executeMethod(method);

        assertEquals("Method failed: " + method.getStatusLine(), HttpStatus.SC_OK, statusCode);

        // Read the response body.
        StringBuilder responseBodyBuilder = new StringBuilder();
        responseBodyBuilder.append("[").append(method.getResponseBodyAsString()).append("]");
        String responseBody = responseBodyBuilder.toString();

        JSONArray jsonResults = new JSONArray(responseBody);

        assertNotNull("A proper JSONObject instance was expected, got null instead", jsonResults);
    } finally {
        method.releaseConnection();
    }

    // @todo we need to add more tests to validate results.

}

From source file:org.jahia.test.bin.FindPrincipalTest.java

@Test
public void testFindGroups() throws IOException, JSONException {

    PostMethod method = new PostMethod(getFindPrincipalServletURL());
    try {/* w  w  w.ja  v a 2 s .c  o  m*/
        method.addParameter("principalType", "groups");
        method.addParameter("siteKey", TESTSITE_NAME);
        method.addParameter("wildcardTerm", "*administrators*");

        // Provide custom retry handler is necessary
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        // Execute the method.
        int statusCode = client.executeMethod(method);

        assertEquals("Method failed: " + method.getStatusLine(), HttpStatus.SC_OK, statusCode);

        // Read the response body.
        StringBuilder responseBodyBuilder = new StringBuilder();
        responseBodyBuilder.append("[").append(method.getResponseBodyAsString()).append("]");
        String responseBody = responseBodyBuilder.toString();

        JSONArray jsonResults = new JSONArray(responseBody);

        assertNotNull("A proper JSONObject instance was expected, got null instead", jsonResults);
    } finally {
        method.releaseConnection();
    }

    // @todo we need to add more tests to validate results.

}

From source file:org.jahia.test.JahiaTestCase.java

protected PostResult post(String url, String[]... params) throws IOException {
    PostMethod method = new PostMethod(url);
    for (String[] param : params) {
        method.addParameter(param[0], param[1]);
    }//from  w  ww  .j av a  2 s  .c  o m

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    int statusCode = 0;
    String statusLine = null;
    String responseBody = null;
    try {
        // Execute the method.
        statusCode = getHttpClient().executeMethod(method);

        statusLine = method.getStatusLine().toString();
        if (statusCode != HttpStatus.SC_OK) {
            logger.warn("Method failed: {}", statusLine);
        }

        // Read the response body.
        responseBody = method.getResponseBodyAsString();
    } finally {
        method.releaseConnection();
    }

    return new PostResult(statusCode, statusLine, responseBody);
}

From source file:org.jasig.portal.services.MultiThreadedHttpConnectionManagerFactoryBean.java

@Override
protected MultiThreadedHttpConnectionManager createInstance() throws Exception {
    final MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();

    final HttpConnectionManagerParams pars = multiThreadedHttpConnectionManager.getParams();
    pars.setConnectionTimeout(this.connectionTimeout);
    pars.setSoTimeout(this.soTimeout);
    pars.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
    pars.setMaxTotalConnections(this.maxTotalConnections);
    pars.setDefaultMaxConnectionsPerHost(this.defaultMaxConnectionsPerHost);

    return multiThreadedHttpConnectionManager;
}

From source file:org.jasig.portlet.weather.dao.worldwide.WorldWeatherOnlineDaoImpl.java

public void afterPropertiesSet() throws Exception {
    final HttpConnectionManager httpConnectionManager = httpClient.getHttpConnectionManager();
    final HttpConnectionManagerParams params = httpConnectionManager.getParams();
    params.setConnectionTimeout(connectionTimeout);
    params.setSoTimeout(readTimeout);/*from  w ww .j ava  2  s .  c  om*/

    params.setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {
        public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) {
            if (executionCount >= timesToRetry) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (exception instanceof SocketException) {
                // Retry if the server reset connection on us
                return true;
            }
            if (exception instanceof SocketTimeoutException) {
                // Retry if the read timed out
                return true;
            }
            if (!method.isRequestSent()) {
                // Retry if the request has not been sent fully or
                // if it's OK to retry methods that have been sent
                return true;
            }
            // otherwise do not retry
            return false;
        }
    });
}