|
25 | 25 | import io.vertx.core.impl.ContextInternal; |
26 | 26 | import io.vertx.core.net.HostAndPort; |
27 | 27 | import io.vertx.core.net.impl.HostAndPortImpl; |
| 28 | +import io.vertx.core.json.JsonObject; |
28 | 29 | import io.vertx.core.streams.Pipe; |
29 | 30 | import io.vertx.httpproxy.Body; |
30 | 31 | import io.vertx.httpproxy.ProxyRequest; |
31 | 32 | import io.vertx.httpproxy.ProxyResponse; |
32 | 33 |
|
| 34 | +import java.net.URI; |
| 35 | +import java.util.HashMap; |
33 | 36 | import java.util.Map; |
34 | 37 | import java.util.Objects; |
35 | 38 |
|
@@ -228,4 +231,89 @@ public Future<ProxyResponse> send(HttpClientRequest request) { |
228 | 231 | sendRequest(promise); |
229 | 232 | return promise.future(); |
230 | 233 | } |
| 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 | + } |
231 | 319 | } |
0 commit comments