Skip to content

Commit 36ec15f

Browse files
authored
Merge pull request #1 from ramanadas/Out-of-the-box-transformations-for-proxied-requests
Out of the box transformations for proxied requests
2 parents c3609af + a84ab6c commit 36ec15f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

src/main/java/io/vertx/httpproxy/impl/ProxiedRequest.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
import io.vertx.core.impl.ContextInternal;
2626
import io.vertx.core.net.HostAndPort;
2727
import io.vertx.core.net.impl.HostAndPortImpl;
28+
import io.vertx.core.json.JsonObject;
2829
import io.vertx.core.streams.Pipe;
2930
import io.vertx.httpproxy.Body;
3031
import io.vertx.httpproxy.ProxyRequest;
3132
import io.vertx.httpproxy.ProxyResponse;
3233

34+
import java.net.URI;
35+
import java.util.HashMap;
3336
import java.util.Map;
3437
import java.util.Objects;
3538

@@ -228,4 +231,89 @@ public Future<ProxyResponse> send(HttpClientRequest request) {
228231
sendRequest(promise);
229232
return promise.future();
230233
}
234+
235+
public ProxiedRequest addPathPrefix(String prefix) {
236+
this.uri = prefix + this.uri;
237+
return this;
238+
}
239+
240+
public ProxiedRequest removePathPrefix(String prefix) {
241+
if (this.uri.startsWith(prefix)) {
242+
this.uri = this.uri.substring(prefix.length());
243+
}
244+
return this;
245+
}
246+
247+
public ProxiedRequest transformQueryToPathParams() {
248+
URI originalUri = URI.create(this.uri);
249+
String originalPath = originalUri.getPath();
250+
String query = originalUri.getQuery();
251+
if (query != null && !query.isEmpty()) {
252+
Map<String, String> queryParams = new HashMap<>();
253+
for (String param : query.split("&")) {
254+
String[] pair = param.split("=");
255+
queryParams.put(pair[0], pair[1]);
256+
}
257+
StringBuilder newPath = new StringBuilder(originalPath);
258+
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
259+
newPath.append("/").append(entry.getValue());
260+
}
261+
this.uri = newPath.toString();
262+
}
263+
return this;
264+
}
265+
266+
public ProxiedRequest addHeader(String name, String value) {
267+
this.headers.add(name, value);
268+
return this;
269+
}
270+
271+
public ProxiedRequest removeHeader(String name) {
272+
this.headers.remove(name);
273+
return this;
274+
}
275+
276+
public ProxiedRequest modifyHeader(String name, String newVal) {
277+
this.headers.set(name, newVal);
278+
return this;
279+
}
280+
281+
public ProxiedRequest setBody(JsonObject jsonObject) {
282+
body = Body.body(jsonObject.toBuffer());
283+
return this;
284+
}
285+
286+
public void getBodyAsJson(Handler<JsonObject> resultHandler) {
287+
long len = body.length();
288+
if (body != null && len >=0) {
289+
BufferingWriteStream buffer = new BufferingWriteStream();
290+
body.stream().pipeTo(buffer).onComplete( ar->{
291+
if(ar.succeeded()){
292+
resultHandler.handle(buffer.content().toJsonObject());
293+
} else {
294+
System.out.println("not implemented");
295+
}
296+
});
297+
}
298+
}
299+
300+
public Future<JsonObject> getBodyAsJson() {
301+
Promise<JsonObject> promise = Promise.promise();
302+
long len = body.length();
303+
if (body != null && len >= 0) {
304+
BufferingWriteStream buffer = new BufferingWriteStream();
305+
306+
body.stream().pipeTo(buffer).onComplete( ar -> {
307+
if (ar.succeeded()) {
308+
JsonObject jsonObject = buffer.content().toJsonObject();
309+
promise.complete(jsonObject);
310+
} else {
311+
promise.fail(ar.cause());
312+
}
313+
});
314+
} else {
315+
promise.fail("Body is null");
316+
}
317+
return promise.future();
318+
}
231319
}

0 commit comments

Comments
 (0)