
io.vertx.ext.web.client.impl.HttpContext Maven / Gradle / Ivy
/*
* Copyright 2014 Red Hat, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.ext.web.client.impl;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.QueryStringEncoder;
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.VertxException;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import io.vertx.core.streams.Pump;
import io.vertx.core.streams.ReadStream;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.codec.BodyCodec;
import io.vertx.ext.web.codec.spi.BodyStream;
/**
* @author Julien Viet
*/
public class HttpContext {
private final Handler>> responseHandler;
private final HttpRequestImpl request;
private final Object body;
private String contentType;
private Map attrs;
private Handler>> currentResponseHandler;
private Iterator> it;
public HttpContext(HttpRequest request,
String contentType,
Object body,
Handler>> responseHandler) {
this.request = (HttpRequestImpl)request;
this.contentType = contentType;
this.body = body;
this.responseHandler = responseHandler;
}
/**
* Send the HTTP request, the context will traverse all interceptors. Any interceptor chain on the context
* will be reset.
*/
public void interceptAndSend() {
it = request.client.interceptors.iterator();
currentResponseHandler = responseHandler;
next();
}
public HttpRequest request() {
return request;
}
public String contentType() {
return contentType;
}
public Object body() {
return body;
}
public Handler>> getResponseHandler() {
return currentResponseHandler;
}
public void setResponseHandler(Handler>> responseHandler) {
this.currentResponseHandler = responseHandler;
}
/**
* Call the next interceptor in the chain or send the request when the end of the chain is reached.
*/
public void next() {
if (it.hasNext()) {
Handler next = it.next();
next.handle(this);
} else {
sendRequest();
}
}
private void sendRequest() {
Future responseFuture = Future.future().setHandler(ar -> {
Context context = Vertx.currentContext();
if (ar.succeeded()) {
HttpClientResponse resp = ar.result();
Future> fut = Future.future();
fut.setHandler(r -> {
// We are running on a context (the HTTP client mandates it)
context.runOnContext(v -> currentResponseHandler.handle(r));
});
resp.exceptionHandler(err -> {
if (!fut.isComplete()) {
fut.fail(err);
}
});
resp.pause();
((BodyCodec
© 2015 - 2025 Weber Informatics LLC | Privacy Policy