Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/io/vertx/httpproxy/ProxyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ static ProxyRequest reverseProxy(HttpServerRequest proxiedRequest) {
return new ProxiedRequest(proxiedRequest);
}

/**
* Create a new {@code ProxyRequest} instance with contextual data, the proxied request will be paused.
*
* @param proxiedRequest the {@code HttpServerRequest} that is proxied
* @param contextData a map containing contextual data to be associated with the proxy request
* @return a reference to this, so the API can be used fluently
*/
static ProxyRequest reverseProxy(HttpServerRequest proxiedRequest, Map<String, Object> contextData) {
proxiedRequest.pause();
ProxiedRequest proxiedRequestInstance = new ProxiedRequest(proxiedRequest);

// Set contextual data in the proxy request
if (contextData != null) {
for (Map.Entry<String, Object> entry : contextData.entrySet()) {
proxiedRequestInstance.setContextData(entry.getKey(), entry.getValue());
}
}

return proxiedRequestInstance;
}


/**
* @return the HTTP version of the proxied request
*/
Expand Down
98 changes: 98 additions & 0 deletions src/main/java/io/vertx/httpproxy/impl/ProxiedRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.net.HostAndPort;
import io.vertx.core.net.impl.HostAndPortImpl;
import io.vertx.core.json.JsonObject;
import io.vertx.core.streams.Pipe;
import io.vertx.httpproxy.Body;
import io.vertx.httpproxy.ProxyRequest;
import io.vertx.httpproxy.ProxyResponse;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -57,6 +60,7 @@ public class ProxiedRequest implements ProxyRequest {
private final MultiMap headers;
HttpClientRequest request;
private final HttpServerRequest proxiedRequest;
private final Map<String, Object> contextData = new HashMap<>();

public ProxiedRequest(HttpServerRequest proxiedRequest) {

Expand Down Expand Up @@ -228,4 +232,98 @@ public Future<ProxyResponse> send(HttpClientRequest request) {
sendRequest(promise);
return promise.future();
}

public ProxiedRequest addPathPrefix(String prefix) {
this.uri = prefix + this.uri;
return this;
}

public ProxiedRequest removePathPrefix(String prefix) {
if (this.uri.startsWith(prefix)) {
this.uri = this.uri.substring(prefix.length());
}
return this;
}

public ProxiedRequest transformQueryToPathParams() {
URI originalUri = URI.create(this.uri);
String originalPath = originalUri.getPath();
String query = originalUri.getQuery();
if (query != null && !query.isEmpty()) {
Map<String, String> queryParams = new HashMap<>();
for (String param : query.split("&")) {
String[] pair = param.split("=");
queryParams.put(pair[0], pair[1]);
}
StringBuilder newPath = new StringBuilder(originalPath);
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
newPath.append("/").append(entry.getValue());
}
this.uri = newPath.toString();
}
return this;
}

public ProxiedRequest addHeader(String name, String value) {
this.headers.add(name, value);
return this;
}

public ProxiedRequest removeHeader(String name) {
this.headers.remove(name);
return this;
}

public ProxiedRequest modifyHeader(String name, String newVal) {
this.headers.set(name, newVal);
return this;
}

public ProxiedRequest setBody(JsonObject jsonObject) {
body = Body.body(jsonObject.toBuffer());
return this;
}

public void getBodyAsJson(Handler<JsonObject> resultHandler) {
long len = body.length();
if (body != null && len >=0) {
BufferingWriteStream buffer = new BufferingWriteStream();
body.stream().pipeTo(buffer).onComplete( ar->{
if(ar.succeeded()){
resultHandler.handle(buffer.content().toJsonObject());
} else {
System.out.println("not implemented");
}
});
}
}

public Future<JsonObject> getBodyAsJson() {
Promise<JsonObject> promise = Promise.promise();
long len = body.length();
if (body != null && len >= 0) {
BufferingWriteStream buffer = new BufferingWriteStream();

body.stream().pipeTo(buffer).onComplete( ar -> {
if (ar.succeeded()) {
JsonObject jsonObject = buffer.content().toJsonObject();
promise.complete(jsonObject);
} else {
promise.fail(ar.cause());
}
});
} else {
promise.fail("Body is null");
}
return promise.future();
}

public void setContextData(String key, Object value) {
contextData.put(key, value);
}

public <T> T getContextData(String key, Class<T> type) {
Object o = contextData.get(key);
return type.isInstance(o) ? type.cast(o) : null;
}
}