Skip to content

Commit

Permalink
HttpMessage is now extended by instead of nested in HttpRequest and H…
Browse files Browse the repository at this point in the history
…ttpResponse
  • Loading branch information
puneetkhanduri committed Oct 24, 2022
1 parent f6262d4 commit 3c1652a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/main/scala/ai/diffy/lifter/HttpLifter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class HttpLifter(settings: Settings) {
}

def liftRequest(req: HttpRequest): Message = {
val headers = req.getMessage.getHeaders.asScala.toMap
val headers = req.getHeaders.asScala.toMap

val canonicalResource: Option[String] = headers
.get("Canonical-Resource")
.orElse(resourceMatcher.flatMap(_.resourceName(req.getPath)))
.orElse(Some(s"${req.getMethod}:${req.getPath}"))

val params = req.getParams
val body = StringLifter.lift(req.getMessage.getBody)
val body = StringLifter.lift(req.getBody)
Message(
canonicalResource,
new FieldMap(
Expand All @@ -60,7 +60,7 @@ class HttpLifter(settings: Settings) {
}

def liftResponse(r: HttpResponse): Message = {
val responseMap = Map(r.getStatus -> StringLifter.lift(r.getMessage.getBody())) ++ headersMap(r.getMessage)
val responseMap = Map(r.getStatus -> StringLifter.lift(r.getBody())) ++ headersMap(r)
Message(None, new FieldMap(responseMap))
}
}
2 changes: 1 addition & 1 deletion src/main/scala/ai/diffy/proxy/HttpMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.TreeMap;
import java.util.stream.Collectors;

public class HttpMessage {
public abstract class HttpMessage {
HttpHeaders headers;
String body;

Expand Down
15 changes: 6 additions & 9 deletions src/main/scala/ai/diffy/proxy/HttpRequest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package ai.diffy.proxy;

import io.netty.handler.codec.http.HttpHeaders;

import java.util.Map;

public class HttpRequest {
public class HttpRequest extends HttpMessage {
private final String method;
private final String uri;
private final String path;
private final Map<String, String> params;
private final HttpMessage message;

public HttpRequest(String method, String uri, String path, Map<String, String> params, HttpMessage message) {
public HttpRequest(String method, String uri, String path, Map<String, String> params, HttpHeaders headers, String body) {
super(headers, body);
this.method = method;
this.uri = uri;
this.path = path;
this.params = params;
this.message = message;
}

public String getMethod() {
Expand All @@ -33,12 +34,8 @@ public Map<String, String> getParams() {
return params;
}

public HttpMessage getMessage() {
return message;
}

@Override
public String toString() {
return "path = "+ path+"\n"+"params =\n"+ params+"\n"+"message =\n"+ message+"\n";
return "path = "+ path+"\n"+"params =\n"+ params+"\n"+"message =\n"+ super.toString()+"\n";
}
}
15 changes: 6 additions & 9 deletions src/main/scala/ai/diffy/proxy/HttpResponse.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package ai.diffy.proxy;

import io.netty.handler.codec.http.HttpHeaders;

import java.util.Map;

public class HttpResponse {
public class HttpResponse extends HttpMessage {
private final String status;
private final HttpMessage message;

public HttpResponse(String status, HttpMessage message) {
public HttpResponse(String status, HttpHeaders headers, String body) {
super(headers, body);
this.status = status;
this.message = message;
}

public String getStatus() {
return status;
}

public HttpMessage getMessage() {
return message;
}

@Override
public String toString() {
return "\nstatus = "+ status+"\n"+"message =\n"+ message+"\n";
return "\nstatus = "+ status+"\n"+"message =\n"+ super.toString()+"\n";
}
}
8 changes: 4 additions & 4 deletions src/main/scala/ai/diffy/proxy/ReactorHttpDifferenceProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ private Publisher<Void> selectHandler(HttpServerRequest req, HttpServerResponse
req.uri(),
req.path(),
req.params(),
new HttpMessage(req.requestHeaders(), body)
req.requestHeaders(),
body
));
});

Expand Down Expand Up @@ -117,7 +118,7 @@ private Publisher<Void> selectHandler(HttpServerRequest req, HttpServerResponse
});

return Mono.fromFuture(messages[responseIndex])
.flatMap(r -> res.headers(r.getMessage().headers).sendString(Mono.just(r.getMessage().body)).then());
.flatMap(r -> res.headers(r.headers).sendString(Mono.just(r.body)).then());
}

@WithSpan
Expand All @@ -142,8 +143,7 @@ private Mono<HttpResponse> receiveMono(HttpClient client, HttpServerRequest req)
.responseSingle(
(headers, body) ->
body.asString()
.map(b -> new HttpMessage(headers.responseHeaders(), b))
.map(m -> new HttpResponse(headers.status().toString(), m))
.map(b -> new HttpResponse(headers.status().toString(), headers.responseHeaders(), b))
);
}
private ForkJoinPool pool = ForkJoinPool.commonPool();
Expand Down

0 comments on commit 3c1652a

Please sign in to comment.