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:net.sf.taverna.t2.activities.biomoby.ExecuteAsyncCgiService.java

/**
 *
 * @param endpoint//w  w  w.j ava 2s .c o m
 *            the url to the service to call
 * @param xml
 *            the BioMOBY input message
 * @return EndpointReference the EPR returned by the service
 * @throws MobyException
 */
private static EndpointReference launchCgiAsyncService(String endpoint, String xml) throws MobyException {
    // construct the Httpclient
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "jMoby/Taverna2");
    // create the post method
    PostMethod method = new PostMethod(endpoint);

    // put our data in the request
    RequestEntity entity;
    try {
        entity = new StringRequestEntity(xml, "text/xml", null);
    } catch (UnsupportedEncodingException e) {
        throw new MobyException("Problem posting data to webservice", e);
    }
    method.setRequestEntity(entity);

    // retry up to 10 times
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(10, true));

    // call the method
    try {
        int result = client.executeMethod(method);
        if (result != HttpStatus.SC_OK)
            throw new MobyException(
                    "Async HTTP POST service returned code: " + result + "\n" + method.getStatusLine());
        return EndpointReference.createFromXML(method.getResponseHeader("moby-wsrf").getValue());
    } catch (IOException e) {
        throw new MobyException("Problem reading response from webservice", e);
    } finally {
        // Release current connection to the connection pool once you are
        // done
        method.releaseConnection();
    }
}

From source file:com.bigdata.rdf.sail.remoting.GraphRepositoryClient.java

private HttpClient getHttpClient() {
    if (httpClient == null) {
        httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());

        httpClient.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(300000));
        // httpClient.getParams().setParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT,
        // new Integer(300000));
        httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new NeverRetryHandler());

        httpClient.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(300000));

        httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, new Integer(300000));
        httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, new Integer(300000));
    }/*  ww w. j  a  v a2  s.c  o m*/
    if (proxyHost != null) {
        httpClient.getHostConfiguration().setProxyHost(proxyHost);
    }
    return httpClient;
}

From source file:edu.uci.ics.pregelix.example.util.TestExecutor.java

public InputStream executeQuery(String str, OutputFormat fmt, String url,
        List<CompilationUnit.Parameter> params) throws Exception {
    HttpMethodBase method = null;//from ww w .j av a 2s. c  o  m
    if (str.length() + url.length() < MAX_URL_LENGTH) {
        //Use GET for small-ish queries
        method = new GetMethod(url);
        NameValuePair[] parameters = new NameValuePair[params.size() + 1];
        parameters[0] = new NameValuePair("query", str);
        int i = 1;
        for (CompilationUnit.Parameter param : params) {
            parameters[i++] = new NameValuePair(param.getName(), param.getValue());
        }
        method.setQueryString(parameters);
    } else {
        //Use POST for bigger ones to avoid 413 FULL_HEAD
        // QQQ POST API doesn't allow encoding additional parameters
        method = new PostMethod(url);
        ((PostMethod) method).setRequestEntity(new StringRequestEntity(str));
    }

    //Set accepted output response type
    method.setRequestHeader("Accept", fmt.mimeType());
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    executeHttpMethod(method);
    return method.getResponseBodyAsStream();
}

From source file:net.bpelunit.framework.control.deploy.activebpel.ActiveBPELDeployer.java

/**
 * @param re SOAP request entity to be sent to ActiveBPEL.
 * @return Response from the ActiveBPEL administration service.
 * @throws IOException//w  ww .  j  a  v  a2s.c o  m
 */
private RequestResult sendRequestToActiveBPEL(final String url, RequestEntity re) throws IOException {
    PostMethod method = null;
    try {
        HttpClient client = new HttpClient(new NoPersistenceConnectionManager());
        if (fUsername != null && !"".equals(fUsername) && fPassword != null && !"".equals(fPassword)) {
            client.getState().setCredentials(new AuthScope(null, -1),
                    new UsernamePasswordCredentials(fUsername, fPassword));
        }
        method = new PostMethod(url);
        method.setRequestEntity(re);

        // Provide custom retry handler is necessary
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(1, false));
        method.addRequestHeader("SOAPAction", "");
        client.executeMethod(method);

        // We need to read the response body right now: if it is called
        // after the connection is released, it will only return null
        RequestResult result = new RequestResult();
        result.setStatusCode(method.getStatusCode());
        result.setResponseBody(method.getResponseBodyAsString());

        return result;
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:edu.uci.ics.asterix.test.aql.TestsUtils.java

private static InputStream getHandleResult(String handle, OutputFormat fmt) throws Exception {
    final String url = "http://localhost:19002/query/result";

    // Create a method instance.
    GetMethod method = new GetMethod(url);
    method.setQueryString(new NameValuePair[] { new NameValuePair("handle", handle) });
    method.setRequestHeader("Accept", fmt.mimeType());

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

    executeHttpMethod(method);/*from  w  ww .j  a v  a2 s .  co  m*/
    return method.getResponseBodyAsStream();
}

From source file:edu.utah.further.core.ws.HttpUtil.java

/**
 * Open an HTTP GET method connection to a URL and read the returned response into an
 * object.//from  w  w w.  ja  v  a  2  s . co m
 * 
 * @param <T>
 *            return type
 * @param url
 *            remote URL (usually a web service's URL)
 * @param timeoutSecs
 *            HTTP socket connection timeout [seconds]
 * @return <code>HttpClient</code> {@link HttpMethod} transfer object, containing the
 *         response headers and body
 * @throws WsException
 *             if an error as occurred either in transport or protocol
 */
public static HttpResponseTo getHttpGetResponseBody(final String url, final int timeoutSecs)
        throws WsException {
    // Create an instance of HttpClient with appropriate parameters
    final SimpleHttpConnectionManager cm = new SimpleHttpConnectionManager();
    final HttpConnectionManagerParams params = cm.getParams();
    params.setConnectionTimeout(timeoutSecs * 1000);
    params.setSoTimeout(timeoutSecs * 1000);
    cm.setParams(params);
    final HttpClient client = new HttpClient(cm);

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

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(NUMBER_HTTP_REQUEST_TRIALS, false));
    try {
        // Execute the method
        /* final int statusCode = */client.executeMethod(method);
        // if (statusCode != HttpStatus.SC_OK)
        // {
        // log.error("Method failed: " + method.getStatusLine());
        // }
        return new HttpResponseTo(method);
    } catch (final HttpException e) {
        // HTTP protocol violation (e.g. bad method?!)
        throw new WsException(PROTOCOL_VIOLATION, e.getMessage());
    } catch (final IOException e) {
        // Transport error. Could be an invalid URL
        throw new WsException(TRANSPORT_ERROR, e.getMessage());
    } catch (final Throwable e) {
        // Exceptions
        throw new WsException(SERVER_ERROR, e.getMessage());
    } finally {
        // Important: release the connection
        method.releaseConnection();
    }
}

From source file:edu.uci.ics.pregelix.example.util.TestExecutor.java

public void executeUpdate(String str, String url) throws Exception {
    // Create a method instance.
    PostMethod method = new PostMethod(url);
    method.setRequestEntity(new StringRequestEntity(str));

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

    // Execute the method.
    executeHttpMethod(method);/*from   w  w  w  .  j a v  a2  s  . co m*/
}

From source file:edu.uci.ics.asterix.test.aql.TestsUtils.java

public static void executeDDL(String str) throws Exception {
    final String url = "http://localhost:19002/ddl";

    // Create a method instance.
    PostMethod method = new PostMethod(url);
    method.setRequestEntity(new StringRequestEntity(str));
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    // Execute the method.
    executeHttpMethod(method);/*www  .  jav  a2s. c om*/
}

From source file:com.celamanzi.liferay.portlets.rails286.OnlineClient.java

/** Prepares method headers.
 *
 * Modifies the given method by inserting, when defined:
 *  - Referer/*from w ww .j a v a  2 s  . c om*/
 *  - Accept-Language
 *
 * @since 0.8.0
 */
protected HttpMethod prepareMethodHeaders(HttpMethod method) {
    // Insert the HTTP Referer
    if (httpReferer != null) {
        log.debug("HTTP referer: " + httpReferer.toString());
        method.setRequestHeader("Referer", httpReferer.toString());
    } else {
        //log.debug("HTTP referer is null");
    }

    // Insert the Locale
    if (locale != null) {
        log.debug("Request's locale language: " + locale.toString());
        method.setRequestHeader("Accept-Language", locale.toString());
    } else {
        //log.debug("Locale is null");
    }

    //Correct the charset problem
    method.getParams().setContentCharset("UTF-8");

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

    return method;
}

From source file:com.legstar.http.client.CicsHttp.java

/**
 * Create a reusable HTTP Client with a set of parameters that match
 * the remote CICS Server characteristics. At this stage, the HTTPClient is not
 * associated with a state and a method yet.
 * @param cicsHttpEndpoint the connection configuration
 * @return the new HTTP Client//from w  ww. ja va  2 s  .  co  m
 */
protected HttpClient createHttpClient(final CicsHttpEndpoint cicsHttpEndpoint) {

    if (_log.isDebugEnabled()) {
        _log.debug("enter createHttpClient()");
    }
    HttpClientParams params = new HttpClientParams();

    /* Consider that if server is failing to respond, we should never retry.
     * A system such as CICS should always be responsive unless something
     * bad is happening in which case, retry makes things worse. */
    DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(0, false);
    params.setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler);

    /* Preemptive authentication forces the first HTTP payload to hold
     * credentials. This bypasses the inefficient default mechanism that
     * expects a 401 reply on the first request and then re-issues the same
     * request again with credentials.*/
    params.setAuthenticationPreemptive(true);

    /* Set the receive time out. */
    params.setSoTimeout(cicsHttpEndpoint.getReceiveTimeout());

    /* Tell the connection manager how long we are prepared to wait
     * for a connection. */
    params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, cicsHttpEndpoint.getConnectTimeout());

    /* Disable Nagle algorithm */
    params.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true);

    HttpClient httpClient = new HttpClient(params);

    httpClient.setHostConfiguration(createHostConfiguration(cicsHttpEndpoint));

    return httpClient;
}