Search in sources :

Example 71 with HttpRequest

use of org.apache.http.HttpRequest in project metrics by dropwizard.

the class HttpClientMetricNameStrategiesTest method hostAndMethodWithNameInWrappedRequest.

@Test
public void hostAndMethodWithNameInWrappedRequest() throws URISyntaxException {
    HttpRequest request = rewriteRequestURI(new HttpPost("http://my.host.com/whatever"));
    assertThat(HOST_AND_METHOD.getNameFor("some-service", request), is("org.apache.http.client.HttpClient.some-service.my.host.com.post-requests"));
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpPost(org.apache.http.client.methods.HttpPost) Test(org.junit.Test)

Example 72 with HttpRequest

use of org.apache.http.HttpRequest in project metrics by dropwizard.

the class HttpClientMetricNameStrategiesTest method querylessUrlAndMethodWithNameInWrappedRequest.

@Test
public void querylessUrlAndMethodWithNameInWrappedRequest() throws URISyntaxException {
    HttpRequest request = rewriteRequestURI(new HttpPut("https://thing.com:8090/my/path?ignore=this&and=this"));
    assertThat(QUERYLESS_URL_AND_METHOD.getNameFor("some-service", request), is("org.apache.http.client.HttpClient.some-service.https://thing.com:8090/my/path.put-requests"));
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 73 with HttpRequest

use of org.apache.http.HttpRequest in project stocator by SparkTC.

the class SwiftConnectionManager method getRetryHandler.

/**
 * Creates custom retry handler to be used if HTTP exception happens
 *
 * @return retry handler
 */
private HttpRequestRetryHandler getRetryHandler() {
    final HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= connectionConfiguration.getExecutionCount()) {
                // Do not retry if over max retry count
                LOG.debug("Execution count {} is bigger than threshold. Stop", executionCount);
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                LOG.debug("NoHttpResponseException exception. Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof UnknownHostException) {
                LOG.debug("UnknownHostException. Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof ConnectTimeoutException) {
                LOG.debug("ConnectTimeoutException. Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof SocketTimeoutException || exception.getClass() == SocketTimeoutException.class || exception.getClass().isInstance(SocketTimeoutException.class)) {
                // Connection refused
                LOG.debug("socketTimeoutException Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof InterruptedIOException) {
                // Timeout
                LOG.debug("InterruptedIOException Retry count {}", executionCount);
                return true;
            }
            if (exception instanceof SSLException) {
                LOG.debug("SSLException Retry count {}", executionCount);
                return true;
            }
            final HttpClientContext clientContext = HttpClientContext.adapt(context);
            final HttpRequest request = clientContext.getRequest();
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                LOG.debug("HttpEntityEnclosingRequest. Retry count {}", executionCount);
                return true;
            }
            LOG.debug("Retry stopped. Retry count {}", executionCount);
            return false;
        }
    };
    return myRetryHandler;
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) HttpRequest(org.apache.http.HttpRequest) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) HttpContext(org.apache.http.protocol.HttpContext) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 74 with HttpRequest

use of org.apache.http.HttpRequest in project cxf by apache.

the class AsyncHTTPConduitFactory method setupNIOClient.

public synchronized void setupNIOClient(HTTPClientPolicy clientPolicy) throws IOReactorException {
    if (client != null) {
        return;
    }
    IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(ioThreadCount).setSelectInterval(selectInterval).setInterestOpQueued(interestOpQueued).setSoLinger(soLinger).setSoTimeout(soTimeout).setSoKeepAlive(soKeepalive).setTcpNoDelay(tcpNoDelay).build();
    Registry<SchemeIOSessionStrategy> ioSessionFactoryRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE).register("https", SSLIOSessionStrategy.getSystemDefaultStrategy()).build();
    ManagedNHttpClientConnectionFactory connectionFactory = new ManagedNHttpClientConnectionFactory();
    DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(config);
    connectionManager = new PoolingNHttpClientConnectionManager(ioreactor, connectionFactory, ioSessionFactoryRegistry, DefaultSchemePortResolver.INSTANCE, SystemDefaultDnsResolver.INSTANCE, connectionTTL, TimeUnit.MILLISECONDS);
    connectionManager.setDefaultMaxPerRoute(maxPerRoute);
    connectionManager.setMaxTotal(maxConnections);
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(clientPolicy.getChunkLength() > 0 ? clientPolicy.getChunkLength() : 16332).build();
    connectionManager.setDefaultConnectionConfig(connectionConfig);
    RedirectStrategy redirectStrategy = new RedirectStrategy() {

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return false;
        }

        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return null;
        }
    };
    HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom().setConnectionManager(connectionManager).setRedirectStrategy(redirectStrategy).setDefaultCookieStore(new BasicCookieStore() {

        private static final long serialVersionUID = 1L;

        public void addCookie(Cookie cookie) {
        }
    });
    adaptClientBuilder(httpAsyncClientBuilder);
    client = httpAsyncClientBuilder.build();
    // Start the client thread
    client.start();
    // Always start the idle checker thread to validate pending requests and
    // use the ConnectionMaxIdle to close the idle connection
    new CloseIdleConnectionThread(connectionManager, client).start();
}
Also used : HttpRequest(org.apache.http.HttpRequest) Cookie(org.apache.http.cookie.Cookie) SchemeIOSessionStrategy(org.apache.http.nio.conn.SchemeIOSessionStrategy) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) ManagedNHttpClientConnectionFactory(org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionFactory) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) RedirectStrategy(org.apache.http.client.RedirectStrategy) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) ConnectionConfig(org.apache.http.config.ConnectionConfig)

Example 75 with HttpRequest

use of org.apache.http.HttpRequest in project acceptance-test-harness by jenkinsci.

the class MockUpdateCenter method ensureRunning.

public void ensureRunning(Jenkins jenkins) {
    if (original != null) {
        return;
    }
    JsonNode ucNode = new UpdateCenter(jenkins).getJson("tree=sites[url,id]");
    List<String> sites = ucNode.findValuesAsText("url");
    List<String> ids = ucNode.findValuesAsText("id");
    if (sites.size() != 1) {
        // TODO ideally it would rather delegate to all of them, but that implies deprecating CachedUpdateCenterMetadataLoader.url and using whatever site(s) Jenkins itself specifies
        LOGGER.log(Level.WARNING, "found an unexpected number of update sites: {0}", sites);
        return;
    } else if (!"default".equals(ids.get(0))) {
        LOGGER.log(Level.WARNING, "the default update site has been replaced by a site with id: {0}. Will not setup the mock update center", ids.get(0));
        return;
    }
    UpdateCenterMetadata ucm;
    try {
        ucm = ucmd.get(jenkins);
    } catch (IOException x) {
        throw new Error("cannot load data for mock update center", x);
    }
    JSONObject all;
    try {
        all = new JSONObject(ucm.originalJSON);
        all.remove("signature");
        JSONObject plugins = all.getJSONObject("plugins");
        LOGGER.info(() -> "editing JSON with " + plugins.length() + " plugins to reflect " + ucm.plugins.size() + " possible overrides");
        for (PluginMetadata meta : ucm.plugins.values()) {
            String name = meta.getName();
            String version = meta.getVersion();
            JSONObject plugin = plugins.optJSONObject(name);
            if (plugin == null) {
                LOGGER.log(Level.INFO, "adding plugin {0}", name);
                plugin = new JSONObject().accumulate("name", name);
                plugins.put(name, plugin);
            }
            plugin.put("url", name + ".hpi");
            updating(plugin, "version", version);
            updating(plugin, "gav", meta.gav);
            updating(plugin, "requiredCore", meta.requiredCore().toString());
            updating(plugin, "dependencies", new JSONArray(meta.getDependencies().stream().map(d -> {
                try {
                    return new JSONObject().accumulate("name", d.name).accumulate("version", d.version).accumulate("optional", d.optional);
                } catch (JSONException x) {
                    throw new AssertionError(x);
                }
            }).collect(Collectors.toList())));
            // The fingerprints are not going to match after injecting different binary so we need to fix/recalculate
            // - For JUT before 2.168, only sha1 is checked if present, so let's simply remove it
            plugin.remove("sha1");
            // - For JUT after 2.168, it is enough to recalculate the strongest cypher
            if (meta instanceof PluginMetadata.ModifyingMetadata) {
                String sha512 = ((PluginMetadata.ModifyingMetadata) meta).getSha512Checksum(injector);
                plugin.put("sha512", sha512);
            }
        }
    } catch (JSONException | NoSuchAlgorithmException | IOException x) {
        LOGGER.log(Level.WARNING, "cannot prepare mock update center", x);
        return;
    }
    HttpProcessor proc = HttpProcessorBuilder.create().add(new ResponseServer("MockUpdateCenter")).add(new ResponseContent()).add(new RequestConnControl()).build();
    UriHttpRequestHandlerMapper handlerMapper = new UriHttpRequestHandlerMapper();
    String json = "updateCenter.post(\n" + all + "\n);";
    handlerMapper.register("/update-center.json", (HttpRequest request, HttpResponse response, HttpContext context) -> {
        response.setStatusCode(HttpStatus.SC_OK);
        response.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
    });
    handlerMapper.register("*.hpi", (HttpRequest request, HttpResponse response, HttpContext context) -> {
        String plugin = request.getRequestLine().getUri().replaceFirst("^/(.+)[.]hpi$", "$1");
        PluginMetadata meta = ucm.plugins.get(plugin);
        if (meta == null) {
            LOGGER.log(Level.WARNING, "no such plugin {0}", plugin);
            response.setStatusCode(HttpStatus.SC_NOT_FOUND);
            return;
        }
        File local = meta.resolve(injector, meta.getVersion());
        LOGGER.log(Level.INFO, "serving {0}", local);
        response.setStatusCode(HttpStatus.SC_OK);
        response.setEntity(new FileEntity(local));
    });
    handlerMapper.register("*", (HttpRequest request, HttpResponse response, HttpContext context) -> {
        String location = original.replace("/update-center.json", request.getRequestLine().getUri());
        LOGGER.log(Level.INFO, "redirect to {0}", location);
        /* TODO for some reason DownloadService.loadJSONHTML does not seem to process the redirect, despite calling setInstanceFollowRedirects(true):
            response.setStatusCode(HttpStatus.SC_MOVED_TEMPORARILY);
            response.setHeader("Location", location);
             */
        HttpURLConnection uc = (HttpURLConnection) new URL(location).openConnection();
        uc.setInstanceFollowRedirects(true);
        // TODO consider caching these downloads locally like CachedUpdateCenterMetadataLoader does for the main update-center.json
        byte[] data = IOUtils.toByteArray(uc);
        String contentType = uc.getContentType();
        response.setStatusCode(HttpStatus.SC_OK);
        response.setEntity(new ByteArrayEntity(data, ContentType.create(contentType)));
    });
    server = ServerBootstrap.bootstrap().setHttpProcessor(proc).setHandlerMapper(handlerMapper).setExceptionLogger(serverExceptionHandler()).create();
    try {
        server.start();
    } catch (IOException x) {
        LOGGER.log(Level.WARNING, "cannot start mock update center", x);
        return;
    }
    original = sites.get(0);
    // TODO figure out how to deal with Docker-based controllers which would need to have an IP address for the host
    String override = "http://" + server.getInetAddress().getHostAddress() + ":" + server.getLocalPort() + "/update-center.json";
    LOGGER.log(Level.INFO, "replacing update site {0} with {1}", new Object[] { original, override });
    jenkins.runScript("DownloadService.signatureCheck = false; Jenkins.instance.updateCenter.sites.replaceBy([new UpdateSite(UpdateCenter.ID_DEFAULT, '%s')])", override);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) UriHttpRequestHandlerMapper(org.apache.http.protocol.UriHttpRequestHandlerMapper) AutoCleaned(org.jenkinsci.test.acceptance.guice.AutoCleaned) URL(java.net.URL) Inject(com.google.inject.Inject) HttpStatus(org.apache.http.HttpStatus) ExceptionLogger(org.apache.http.ExceptionLogger) RequestConnControl(org.apache.http.protocol.RequestConnControl) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Level(java.util.logging.Level) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) HttpProcessorBuilder(org.apache.http.protocol.HttpProcessorBuilder) TestScope(org.jenkinsci.test.acceptance.guice.TestScope) JsonNode(com.fasterxml.jackson.databind.JsonNode) ResponseContent(org.apache.http.protocol.ResponseContent) Jenkins(org.jenkinsci.test.acceptance.po.Jenkins) ContentType(org.apache.http.entity.ContentType) FileEntity(org.apache.http.entity.FileEntity) StringEntity(org.apache.http.entity.StringEntity) ServerBootstrap(org.apache.http.impl.bootstrap.ServerBootstrap) IOException(java.io.IOException) HttpProcessor(org.apache.http.protocol.HttpProcessor) Logger(java.util.logging.Logger) HttpRequest(org.apache.http.HttpRequest) HttpServer(org.apache.http.impl.bootstrap.HttpServer) Collectors(java.util.stream.Collectors) File(java.io.File) ResponseServer(org.apache.http.protocol.ResponseServer) Injector(com.google.inject.Injector) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) HttpContext(org.apache.http.protocol.HttpContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpResponse(org.apache.http.HttpResponse) ConnectionClosedException(org.apache.http.ConnectionClosedException) UpdateCenter(org.jenkinsci.test.acceptance.po.UpdateCenter) JSONArray(org.json.JSONArray) ResponseContent(org.apache.http.protocol.ResponseContent) UpdateCenter(org.jenkinsci.test.acceptance.po.UpdateCenter) HttpProcessor(org.apache.http.protocol.HttpProcessor) JsonNode(com.fasterxml.jackson.databind.JsonNode) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URL(java.net.URL) StringEntity(org.apache.http.entity.StringEntity) HttpURLConnection(java.net.HttpURLConnection) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) UriHttpRequestHandlerMapper(org.apache.http.protocol.UriHttpRequestHandlerMapper) HttpRequest(org.apache.http.HttpRequest) FileEntity(org.apache.http.entity.FileEntity) JSONArray(org.json.JSONArray) HttpContext(org.apache.http.protocol.HttpContext) JSONException(org.json.JSONException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) RequestConnControl(org.apache.http.protocol.RequestConnControl) JSONObject(org.json.JSONObject) File(java.io.File) ResponseServer(org.apache.http.protocol.ResponseServer)

Aggregations

HttpRequest (org.apache.http.HttpRequest)155 HttpResponse (org.apache.http.HttpResponse)57 HttpContext (org.apache.http.protocol.HttpContext)56 HttpHost (org.apache.http.HttpHost)52 Test (org.junit.Test)40 IOException (java.io.IOException)36 Header (org.apache.http.Header)33 HttpException (org.apache.http.HttpException)33 HttpEntity (org.apache.http.HttpEntity)27 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)25 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)22 HttpGet (org.apache.http.client.methods.HttpGet)21 HttpPost (org.apache.http.client.methods.HttpPost)21 URI (java.net.URI)19 ProtocolException (org.apache.http.ProtocolException)18 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)16 StringEntity (org.apache.http.entity.StringEntity)16 ArrayList (java.util.ArrayList)14 NameValuePair (org.apache.http.NameValuePair)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14