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.infoscoop.request.ProxyRequest.java

public int executeOptions() throws Exception {
    OptionsMethod method = null;//from  w  w  w.j av a  2  s. c  om
    try {
        HttpClient client = this.newHttpClient();
        method = new OptionsMethod(this.getTargetURL());
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(1, false));
        ProxyFilterContainer filterContainer = (ProxyFilterContainer) SpringUtil.getBean(filterType);
        return filterContainer.invoke(client, method, this);
    } catch (ProxyAuthenticationException e) {
        if (e.isTraceOn()) {
            log.error(this.getTargetURL());
            log.error("", e);
        } else {
            log.warn(this.getTargetURL() + " : " + e.getMessage());
        }
        return 401;
    }
}

From source file:org.infoscoop.request.ProxyRequest.java

public static void main(String args[]) throws MalformedURLException {
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setConnectionTimeout(3000);/*w  w w .j ava2s. c om*/
    HttpConnectionManager manager = new SimpleHttpConnectionManager();
    manager.setParams(params);
    HttpClient c = new HttpClient(manager);
    c.getParams().setParameter("http.socket.timeout", new Integer(5000));

    Credentials credentials = new NTCredentials("test", "test", "", "");//192.168.233.2INFOSCOOP
    //Credentials credentials = new UsernamePasswordCredentials("INFOSCOOP\test", "test");
    // the scope of the certification.
    URL urlObj = new URL("http://192.168.233.2/index.html");
    AuthScope scope1 = new AuthScope(urlObj.getHost(), urlObj.getPort(), null);
    // set a pair of a scope and an information of the certification.
    c.getState().setCredentials(scope1, credentials);

    //GetMethod g = new GetMethod("http://inicio/syanai/rss.do?category=3bb34-f8f19ded28-038fb3114a5cd639e1963371842f83c0&u=bQRF9JnX%2Bm7H0n4iwJJ3ZA%3D%3D");//"http://172.22.113.111");
    //GetMethod g = new GetMethod("http://172.22.113.111");
    GetMethod g = new GetMethod("http://192.168.233.2/index.html");
    g.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
    try {
        c.executeMethod(g);
        System.out.println(g.getStatusCode());
        System.out.println(g.getResponseBodyAsString());
    } catch (HttpException e) {
        log.error("", e);
    } catch (IOException e) {
        log.error("", e);
    }
}

From source file:org.intalio.tempo.workflow.fds.core.MessageSender.java

/**
 * Sends an XML request to a specific HTTP endpoint with a specific
 * <code>SOAPAction</code> header and returns the reply as XML.
 * //  w w w.j a v  a  2 s .  c om
 * @param requestMessage
 *            The request XML payload.
 * @param endpoint
 *            The endpoint URL, such as
 *            <code>http://localhost/webservice</code>
 * @param soapAction
 *            The <code>SOAPAction</code> to send the message with.
 * @return The reply from the endpoint as an XML <code>Document</code>.
 * @throws HttpException
 *             If an HTTP-level error happens.
 * @throws IOException
 *             If a low-level input/output error happens (e.g. a
 *             disconnection during the request/response).
 * @throws DocumentException 
 * @see <a href="http://www.w3.org/TR/soap/">The SOAP specification.</a>
 */
public Document requestAndGetReply(Document requestMessage, String endpoint, String soapAction)
        throws HttpException, IOException, DocumentException {
    Document result = null;

    PostMethod postMethod = new PostMethod(endpoint);
    postMethod.addRequestHeader("SOAPAction", soapAction);
    postMethod.setRequestEntity(
            new ByteArrayRequestEntity(requestMessage.asXML().getBytes(), "text/xml; charset=UTF-8"));

    HttpClient httpClient = new HttpClient();

    // turn off retrying, since retrying SOAP requests can cause side-effects
    DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(0, false);
    httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler);

    // set the timeout
    httpClient.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,
            FormDispatcherConfiguration.getInstance().getHttpTimeout());

    // Prepare an XML parser 
    SAXReader reader = new SAXReader();
    InputStream responseInputStream = null;
    try {
        httpClient.executeMethod(postMethod);
        responseInputStream = postMethod.getResponseBodyAsStream();
        result = reader.read(responseInputStream);
    } finally {
        postMethod.releaseConnection();
        if (responseInputStream != null)
            responseInputStream.close();
    }

    return result;
}

From source file:org.intermine.web.util.HttpClient.java

/**
 * @param url url of resource to be downloaded
 * @return downloaded data/*from w  w  w  .  j  a va 2 s  .  c  o  m*/
 */
public byte[] download(String url) {

    byte[] responseBody;

    // Create an instance of HttpClient.
    org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod(url);

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

    try {
        // Execute the method.
        client.executeMethod(method);

        // Read the response body.
        responseBody = method.getResponseBody();

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary
        // data
        return responseBody;
    } catch (HttpException e) {
        throw new RuntimeException("Fatal protocol violation.", e);
    } catch (IOException e) {
        throw new RuntimeException("Fatal transport error.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:org.intermine.webservice.client.util.HttpConnection.java

private void executeMethod() {
    HttpClient client = new HttpClient();
    client.getParams().setConnectionManagerTimeout(timeout);
    String url = request.getEncodedUrl();
    if (request.getType() == RequestType.GET) {
        executedMethod = new GetMethod(url);
    } else if (request.getType() == RequestType.DELETE) {
        executedMethod = new DeleteMethod(url);
    } else {/*from   w w w.  j  av  a2 s .  co m*/
        PostMethod postMethod;
        if (request.getContentType() == ContentType.MULTI_PART_FORM) {
            postMethod = new PostMethod(url);
            setMultiPartPostEntity(postMethod, ((MultiPartRequest) request));
        } else {
            url = request.getServiceUrl();
            postMethod = new PostMethod(url);
            setPostMethodParameters(postMethod, request.getParameterMap());
        }
        executedMethod = postMethod;
    }
    // Provide custom retry handler is necessary
    executedMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(retryCount, false));
    for (String name : request.getHeaders().keySet()) {
        executedMethod.setRequestHeader(name, request.getHeader(name));
    }
    try {
        // Execute the method.
        client.executeMethod(executedMethod);
        checkResponse();
    } catch (HttpException e) {
        throw new RuntimeException("Fatal protocol violation.", e);
    } catch (IOException e) {
        throw new RuntimeException("Fatal transport error connecting to " + url, e);
    }
}

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

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

    PostMethod method = new PostMethod(getFindPrincipalServletURL());
    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);

    if (statusCode != HttpStatus.SC_OK) {
        logger.error("Method failed: " + method.getStatusLine());
    }/*from   www . j  a  va2 s .c  om*/

    // Read the response body.
    String responseBody = method.getResponseBodyAsString();

    JSONArray jsonResults = new JSONArray(responseBody);

    assertNotNull("A proper JSONObject instance was expected, got null instead", jsonResults);

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

}

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

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

    PostMethod method = new PostMethod(getFindPrincipalServletURL());
    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);

    if (statusCode != HttpStatus.SC_OK) {
        logger.error("Method failed: " + method.getStatusLine());
    }// ww w .  j a  v a 2 s  . co  m

    // Read the response body.
    String responseBody = method.getResponseBodyAsString();

    JSONArray jsonResults = new JSONArray(responseBody);

    assertNotNull("A proper JSONObject instance was expected, got null instead", jsonResults);

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

}

From source file:org.jahia.bin.FindTest.java

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

    PostMethod method = new PostMethod(getFindServletURL() + "/" + Constants.EDIT_WORKSPACE + "/en");
    method.addParameter("query",
            "/jcr:root" + SITECONTENT_ROOT_NODE + "//element(*, nt:base)[jcr:contains(.,'{$q}')]");
    method.addParameter("q", COMPLEX_QUERY_VALUE); // to test if the reserved characters work correctly.
    method.addParameter("language", javax.jcr.query.Query.XPATH);
    method.addParameter("propertyMatchRegexp", "{$q}.*");
    method.addParameter("removeDuplicatePropValues", "true");
    method.addParameter("depthLimit", "1");

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

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

    if (statusCode != HttpStatus.SC_OK) {
        logger.error("Method failed: " + method.getStatusLine());
    }//from  ww  w  .  ja  va 2 s . co  m

    // Read the response body.
    String responseBody = method.getResponseBodyAsString();

    logger.debug("Status code=" + statusCode + " JSON response=[" + responseBody + "]");

    JSONArray jsonResults = new JSONArray(responseBody);

    assertNotNull("A proper JSONObject instance was expected, got null instead", jsonResults);

    assertTrue("Result should not be empty !", (jsonResults.length() > 0));

    validateFindJSONResults(jsonResults, COMPLEX_QUERY_VALUE);

}

From source file:org.jahia.bin.FindTest.java

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

    PostMethod method = new PostMethod(getFindServletURL() + "/" + Constants.EDIT_WORKSPACE + "/en");
    method.addParameter("query", "select * from [nt:base] as base where isdescendantnode(["
            + SITECONTENT_ROOT_NODE + "/]) and contains(base.*,'{$q}*')");
    method.addParameter("q", INITIAL_ENGLISH_TEXT_NODE_PROPERTY_VALUE);
    method.addParameter("language", javax.jcr.query.Query.JCR_SQL2);
    method.addParameter("propertyMatchRegexp", "{$q}.*");
    method.addParameter("removeDuplicatePropValues", "true");
    method.addParameter("depthLimit", "1");
    method.addParameter("getNodes", "true");

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

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

    if (statusCode != HttpStatus.SC_OK) {
        logger.error("Method failed: " + method.getStatusLine());
    }/*  w w  w . ja  v  a 2s.c om*/

    // Read the response body.
    String responseBody = method.getResponseBodyAsString();

    logger.debug("Status code=" + statusCode + " JSON response=[" + responseBody + "]");

    JSONArray jsonResults = new JSONArray(responseBody);

    assertNotNull("A proper JSONObject instance was expected, got null instead", jsonResults);

    assertTrue("Result should not be empty !", (jsonResults.length() > 0));

    validateFindJSONResults(jsonResults, INITIAL_ENGLISH_TEXT_NODE_PROPERTY_VALUE);

}

From source file:org.jahia.bin.FindTest.java

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

    PostMethod method = new PostMethod(getFindServletURL() + "/" + Constants.EDIT_WORKSPACE + "/en");
    method.addParameter("query", "select * from [nt:base] as base where isdescendantnode(["
            + SITECONTENT_ROOT_NODE + "/]) and contains(base.*,'{$q}')");
    method.addParameter("q", COMPLEX_QUERY_VALUE); // to test if the reserved characters work correctly.
    method.addParameter("language", javax.jcr.query.Query.JCR_SQL2);
    method.addParameter("propertyMatchRegexp", "{$q}.*");
    method.addParameter("removeDuplicatePropValues", "true");
    method.addParameter("depthLimit", "1");
    method.addParameter("getNodes", "true");

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

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

    if (statusCode != HttpStatus.SC_OK) {
        logger.error("Method failed: " + method.getStatusLine());
    }/* w ww.  j  a v  a  2s .c  o m*/

    // Read the response body.
    String responseBody = method.getResponseBodyAsString();

    logger.debug("Status code=" + statusCode + " JSON response=[" + responseBody + "]");

    JSONArray jsonResults = new JSONArray(responseBody);

    assertNotNull("A proper JSONObject instance was expected, got null instead", jsonResults);

    assertTrue("Result should not be empty !", (jsonResults.length() > 0));

    validateFindJSONResults(jsonResults, COMPLEX_QUERY_VALUE);

}