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);
}
}
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();
}
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();
}
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;
}
Aggregations