Search in sources :

Example 21 with DefaultHttpMethodRetryHandler

use of org.apache.commons.httpclient.DefaultHttpMethodRetryHandler in project janrufmonitor by tbrandt77.

the class JakartaRequester method request.

public IHttpResponse request() {
    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpClientParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false));
    HttpClient client = new HttpClient(params);
    try {
        if (this.m_r instanceof Request) {
            HttpMethod m = null;
            byte[] result = null;
            if (this.m_r != null && this.m_r.getMethod().equalsIgnoreCase(Request.METHOD_POST)) {
                String url = "http://" + this.getServer() + ":" + this.getPort() + this.m_r.getURI();
                m = new PostMethod(url);
                if (this.m_r.getContent() != null) {
                    ((PostMethod) m).setRequestEntity(new ByteArrayRequestEntity(this.m_r.getContent()));
                // removed: 27.05.2005: changed to httpclient 3.0
                // ((PostMethod)m).setRequestBody(new ByteArrayInputStream(this.m_r.getContent()));
                // ((PostMethod)m).setRequestContentLength(this.m_r.getContent().length);
                }
                this.m_responseCode = client.executeMethod(m);
                if (this.m_responseCode == HttpStatus.SC_OK)
                    result = m.getResponseBody();
            }
            if (this.m_r != null && this.m_r.getMethod().equalsIgnoreCase(Request.METHOD_GET)) {
                String url = "http://" + this.getServer() + ":" + this.getPort() + this.m_r.getURI();
                m = new GetMethod(url);
                this.m_responseCode = client.executeMethod(m);
                if (this.m_responseCode == HttpStatus.SC_OK)
                    result = m.getResponseBody();
            }
            Response resp = new Response();
            Header[] headers = m.getResponseHeaders();
            for (int i = 0; i < headers.length; i++) {
                resp.setParameter(headers[i].getName(), headers[i].getValue());
            }
            resp.setCode(this.m_responseCode);
            resp.setContent(result);
            String debugEnabled = System.getProperty("jam.http.debug");
            if (debugEnabled != null && debugEnabled.equalsIgnoreCase("true")) {
                this.dump(this.m_r.toString());
                this.dump(resp.toString());
            }
            if (m != null)
                m.releaseConnection();
            return resp;
        }
    } catch (IOException ex) {
        this.m_responseCode = 0;
    } catch (Exception e) {
        this.m_responseCode = 0;
    }
    Response resp = new Response();
    resp.setCode(0);
    return resp;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) IMutableHttpRequest(de.janrufmonitor.service.commons.http.IMutableHttpRequest) IHttpRequest(de.janrufmonitor.service.commons.http.IHttpRequest) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IHttpResponse(de.janrufmonitor.service.commons.http.IHttpResponse) Header(org.apache.commons.httpclient.Header) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 22 with DefaultHttpMethodRetryHandler

use of org.apache.commons.httpclient.DefaultHttpMethodRetryHandler in project product-iots by wso2.

the class IOTHttpClient method delete.

public IOTResponse delete(String endpoint) {
    HttpClient client = new HttpClient();
    try {
        ProtocolSocketFactory socketFactory = new EasySSLProtocolSocketFactory();
        Protocol https = new Protocol(Constants.HTTPS, socketFactory, Constants.HTTPS_GATEWAY_PORT);
        Protocol.registerProtocol(Constants.HTTPS, https);
        String url = backEndUrl + endpoint;
        DeleteMethod method = new DeleteMethod(url);
        method.setRequestHeader(AUTHORIZATION, authorizationString);
        method.setRequestHeader(Constants.CONTENT_TYPE, requestHeaders.get(Constants.CONTENT_TYPE));
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        IOTResponse iotResponse = new IOTResponse();
        iotResponse.setStatus(client.executeMethod(method));
        iotResponse.setBody(method.getResponseBodyAsString());
        return iotResponse;
    } catch (GeneralSecurityException e) {
        log.error("Failure occurred at IOTResponse delete for GeneralSecurityException", e);
    } catch (IOException e) {
        log.error("Failure occurred at IOTResponse delete for IOException", e);
    }
    return null;
}
Also used : EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) HttpClient(org.apache.commons.httpclient.HttpClient) GeneralSecurityException(java.security.GeneralSecurityException) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) IOException(java.io.IOException) Protocol(org.apache.commons.httpclient.protocol.Protocol) EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory)

Example 23 with DefaultHttpMethodRetryHandler

use of org.apache.commons.httpclient.DefaultHttpMethodRetryHandler in project product-iots by wso2.

the class IOTHttpClient method get.

public IOTResponse get(String endpoint) {
    HttpClient client = new HttpClient();
    try {
        ProtocolSocketFactory socketFactory = new EasySSLProtocolSocketFactory();
        Protocol https = new Protocol(Constants.HTTPS, socketFactory, Constants.HTTPS_GATEWAY_PORT);
        Protocol.registerProtocol(Constants.HTTPS, https);
        String url = backEndUrl + endpoint;
        GetMethod method = new GetMethod(url);
        method.setRequestHeader(AUTHORIZATION, authorizationString);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        IOTResponse iotResponse = new IOTResponse();
        iotResponse.setStatus(client.executeMethod(method));
        iotResponse.setBody(new String(method.getResponseBody()));
        return iotResponse;
    } catch (GeneralSecurityException e) {
        log.error("Failure occurred at IOTResponse get for GeneralSecurityException", e);
    } catch (IOException e) {
        log.error("Failure occurred at IOTResponse get for IOException", e);
    }
    return null;
}
Also used : EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) HttpClient(org.apache.commons.httpclient.HttpClient) GeneralSecurityException(java.security.GeneralSecurityException) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) IOException(java.io.IOException) Protocol(org.apache.commons.httpclient.protocol.Protocol) EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory)

Example 24 with DefaultHttpMethodRetryHandler

use of org.apache.commons.httpclient.DefaultHttpMethodRetryHandler in project ovirt-engine by oVirt.

the class HttpUtils method getConnection.

/**
 * There are places in the code where use http to communicate with vdsm or external providers
 *
 * @param connectionTimeOut
 *            - the instance type of the interface for this connection
 * @param clientRetries
 *            - Number of retries if timeout occurd
 * @param maxConnectionsPerHost
 *            - maximum number of connections allowed for a given host
 * @param maxTotalConnections
 *            - The maximum number of connections allowed
 * @return {@link HttpClient}.
 */
public static HttpClient getConnection(int connectionTimeOut, int clientRetries, int maxConnectionsPerHost, int maxTotalConnections) {
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setConnectionTimeout(connectionTimeOut);
    params.setDefaultMaxConnectionsPerHost(maxConnectionsPerHost);
    params.setMaxTotalConnections(maxTotalConnections);
    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    httpConnectionManager.setParams(params);
    // Create the client:
    HttpClient client = new HttpClient(httpConnectionManager);
    // Configure the HTTP client so it will retry the execution of
    // methods when there are IO errors:
    int retries = Config.getValue(ConfigValues.vdsRetries);
    HttpMethodRetryHandler handler = new DefaultHttpMethodRetryHandler(retries, false);
    HttpClientParams parameters = client.getParams();
    parameters.setParameter(HttpMethodParams.RETRY_HANDLER, handler);
    // Done:
    return client;
}
Also used : HttpMethodRetryHandler(org.apache.commons.httpclient.HttpMethodRetryHandler) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams)

Example 25 with DefaultHttpMethodRetryHandler

use of org.apache.commons.httpclient.DefaultHttpMethodRetryHandler in project pinpoint by naver.

the class HttpClientIT method hostConfig.

@Test
public void hostConfig() {
    HttpClient client = new HttpClient();
    client.getParams().setConnectionManagerTimeout(CONNECTION_TIMEOUT);
    client.getParams().setSoTimeout(SO_TIMEOUT);
    HostConfiguration config = new HostConfiguration();
    config.setHost("weather.naver.com", 80, "http");
    GetMethod method = new GetMethod("/rgn/cityWetrMain.nhn");
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
    method.setQueryString(new NameValuePair[] { new NameValuePair("key2", "value2") });
    try {
        // Execute the method.
        client.executeMethod(config, method);
    } catch (Exception ignored) {
    } finally {
        method.releaseConnection();
    }
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Aggregations

DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)26 HttpClient (org.apache.commons.httpclient.HttpClient)20 IOException (java.io.IOException)17 HttpException (org.apache.commons.httpclient.HttpException)12 GetMethod (org.apache.commons.httpclient.methods.GetMethod)10 HttpMethod (org.apache.commons.httpclient.HttpMethod)7 PostMethod (org.apache.commons.httpclient.methods.PostMethod)7 Header (org.apache.commons.httpclient.Header)6 NameValuePair (org.apache.commons.httpclient.NameValuePair)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 GeneralSecurityException (java.security.GeneralSecurityException)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)3 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)2 EngineUnavailableException (it.eng.spagobi.engines.talend.client.exception.EngineUnavailableException)2 ServiceInvocationFailedException (it.eng.spagobi.engines.talend.client.exception.ServiceInvocationFailedException)2 FileNotFoundException (java.io.FileNotFoundException)2 InflaterInputStream (java.util.zip.InflaterInputStream)2 ImageInputStream (javax.imageio.stream.ImageInputStream)2