Search in sources :

Example 81 with Method

use of org.apache.hc.core5.http.Method in project JAuswertung by dennisfabri.

the class DPRequestHandler method handle.

@Override
public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context) throws HttpException, IOException {
    String method = request.getMethod().toUpperCase(Locale.ENGLISH);
    if (!(method.equals("GET") || method.equals("POST"))) {
        throw new MethodNotSupportedException(method + " method not supported");
    }
    String target = request.getRequestUri();
    if (dp.knowsFile(target)) {
        DataProducer producer = new DataProducer(dp, target);
        response.setCode(HttpStatus.SC_OK);
        byte[] data = producer.writeTo();
        ByteArrayEntity body = new ByteArrayEntity(data, producer.getContentType());
        response.setEntity(body);
    } else {
        response.setCode(HttpStatus.SC_NOT_FOUND);
        StringEntity body = new StringEntity("<html><body><h1>Page not found</h1></body></html>", ContentType.TEXT_HTML);
        response.setEntity(body);
    }
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) MethodNotSupportedException(org.apache.hc.core5.http.MethodNotSupportedException)

Example 82 with Method

use of org.apache.hc.core5.http.Method in project commercetools-sdk-java-v2 by commercetools.

the class CtApacheHttpClient method toApacheRequest.

private AsyncRequestProducer toApacheRequest(final ApiHttpRequest httpRequest) {
    final String method = httpRequest.getMethod().toString();
    final AsyncRequestBuilder builder = AsyncRequestBuilder.create(method);
    builder.setUri(httpRequest.getUri());
    httpRequest.getHeaders().getHeaders().forEach((entry) -> builder.addHeader(entry.getKey(), entry.getValue()));
    if (httpRequest.getBody() != null) {
        // default media type is JSON, if other media type is set as a header, use it
        ContentType mediaType = ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8);
        if (httpRequest.getHeaders().getHeaders().stream().anyMatch(s -> s.getKey().equalsIgnoreCase(ApiHttpHeaders.CONTENT_TYPE))) {
            mediaType = ContentType.parse(Objects.requireNonNull(httpRequest.getHeaders().getFirst(ApiHttpHeaders.CONTENT_TYPE)));
        }
        builder.setEntity(httpRequest.getBody(), mediaType);
    }
    return builder.build();
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder)

Example 83 with Method

use of org.apache.hc.core5.http.Method in project commercetools-jvm-sdk by commercetools.

the class IntegrationTestHttpClient method toApacheRequest.

private AsyncRequestProducer toApacheRequest(final HttpRequest httpRequest) throws UnsupportedEncodingException {
    final String method = httpRequest.getHttpMethod().toString();
    final String uri = httpRequest.getUrl();
    final AsyncRequestBuilder builder = AsyncRequestBuilder.create(method);
    builder.setUri(uri);
    httpRequest.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
    if (httpRequest.getBody() != null) {
        final HttpRequestBody body = httpRequest.getBody();
        if (body instanceof StringHttpRequestBody) {
            builder.setEntity(((StringHttpRequestBody) body).getString(), ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8));
        } else if (body instanceof FileHttpRequestBody) {
            builder.setEntity(AsyncEntityProducers.create(((FileHttpRequestBody) body).getFile(), ContentType.DEFAULT_BINARY));
        } else if (body instanceof FormUrlEncodedHttpRequestBody) {
            builder.setEntity(urlEncodedOf((FormUrlEncodedHttpRequestBody) body), ContentType.APPLICATION_FORM_URLENCODED);
        } else {
            throw new HttpException("Cannot interpret request " + httpRequest);
        }
    }
    return builder.build();
}
Also used : AsyncEntityProducers(org.apache.hc.core5.http.nio.entity.AsyncEntityProducers) GZIPInputStream(java.util.zip.GZIPInputStream) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) WWWFormCodec(org.apache.hc.core5.net.WWWFormCodec) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) Function(java.util.function.Function) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) SimpleResponseConsumer(org.apache.hc.client5.http.async.methods.SimpleResponseConsumer) Nullable(javax.annotation.Nullable) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) SimpleBody(org.apache.hc.client5.http.async.methods.SimpleBody) Header(org.apache.hc.core5.http.Header) io.sphere.sdk.http(io.sphere.sdk.http) IOReactorStatus(org.apache.hc.core5.reactor.IOReactorStatus) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) ContentType(org.apache.hc.core5.http.ContentType) AutoCloseInputStream(org.apache.commons.io.input.AutoCloseInputStream) AsyncRequestProducer(org.apache.hc.core5.http.nio.AsyncRequestProducer) Optional(java.util.Optional) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InputStream(java.io.InputStream) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder)

Example 84 with Method

use of org.apache.hc.core5.http.Method in project skywalking-java by apache.

the class IOSessionImplPollInterceptor method afterMethod.

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
    Command command = (Command) ret;
    if (!(command instanceof RequestExecutionCommand)) {
        return ret;
    }
    HttpContext httpContext = ((RequestExecutionCommand) command).getContext();
    ContextSnapshot snapshot = (ContextSnapshot) httpContext.getAttribute(Constants.SKYWALKING_CONTEXT_SNAPSHOT);
    if (snapshot == null) {
        return ret;
    }
    httpContext.removeAttribute(Constants.SKYWALKING_CONTEXT_SNAPSHOT);
    AbstractSpan localSpan = ContextManager.createLocalSpan("httpasyncclient/local");
    localSpan.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT);
    localSpan.setLayer(SpanLayer.HTTP);
    ContextManager.continued(snapshot);
    final ContextCarrier contextCarrier = new ContextCarrier();
    BasicHttpRequest request = (BasicHttpRequest) httpContext.getAttribute(HttpClientContext.HTTP_REQUEST);
    URI uri = request.getUri();
    String operationName = uri.getPath();
    int port = uri.getPort();
    AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, uri.getHost() + ":" + (port == -1 ? 80 : port));
    span.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT);
    Tags.URL.set(span, uri.toURL().toString());
    Tags.HTTP.METHOD.set(span, request.getMethod());
    SpanLayer.asHttp(span);
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        request.setHeader(next.getHeadKey(), next.getHeadValue());
    }
    return ret;
}
Also used : ContextSnapshot(org.apache.skywalking.apm.agent.core.context.ContextSnapshot) ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) Command(org.apache.hc.core5.reactor.Command) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand) URI(java.net.URI) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Aggregations

Header (org.apache.hc.core5.http.Header)38 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)29 Test (org.junit.jupiter.api.Test)29 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)16 IOException (java.io.IOException)15 URI (java.net.URI)14 URISyntaxException (java.net.URISyntaxException)14 Map (java.util.Map)14 HttpEntity (org.apache.hc.core5.http.HttpEntity)13 HttpHost (org.apache.hc.core5.http.HttpHost)12 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)10 ContentType (org.apache.hc.core5.http.ContentType)9 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)9 HttpRequest (org.apache.hc.core5.http.HttpRequest)8 HashMap (java.util.HashMap)7 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)7 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)7 SocketTimeoutException (java.net.SocketTimeoutException)6 ArrayList (java.util.ArrayList)6 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)6