Skip to content

Commit

Permalink
forward headers from monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
tkote committed Jan 4, 2023
1 parent 6407b0d commit 5182baa
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/main/java/oracle/demo/lra/LRAMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation.Builder;
import jakarta.ws.rs.container.AsyncResponse;
import jakarta.ws.rs.container.Suspended;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
Expand Down Expand Up @@ -108,28 +110,45 @@ public Response notifyLRAFinished(@HeaderParam(LRA_HTTP_CONTEXT_HEADER) URI lraI
@POST
@Path("monitor")
@Produces(MediaType.TEXT_PLAIN)
public void monitor(String[] urls, @QueryParam("raise-error") boolean raiseError, @Context UriInfo uriInfo, @Suspended AsyncResponse asyncResponse){
public void monitor(String[] urls,
@QueryParam("raise-error") boolean raiseError,
@Context UriInfo uriInfo,
@Context HttpHeaders httpHeaders,
@Suspended AsyncResponse asyncResponse){


new Thread(() -> {
URI target = uriInfo.getBaseUriBuilder().path("lra-main/start").queryParam("raise-error", raiseError).build();
logger.info("Calling LRA initiator: " + target);

// TODO: forward header values "x-b3-", "oracle-tmm-", "authorization", "refresh-"
Response response = client.target(target).request().post(Entity.json(urls));
// forward header values "x-b3-", "oracle-tmm-", "authorization", "refresh-"
//Response response = client.target(target).request().post(Entity.json(urls));
Builder builder = client.target(target).request();
httpHeaders.getRequestHeaders().entrySet().stream().forEach(h -> {
final String key = h.getKey().toLowerCase();
if(key.startsWith("x-b3-") || key.startsWith("oracle-tmm-")
|| key.startsWith("authorization") || key.startsWith("refresh-")){
builder.header(h.getKey(), h.getValue());
}
});
builder.header(LRA_HTTP_CONTEXT_HEADER, asyncResponse);
Response response = builder.post(Entity.json(urls));
logger.info("LRA initiator returned with status " + response.getStatus());

// TODO: handle error case

String lraId = response.getHeaderString(LRA_HTTP_CONTEXT_HEADER);
String status = statusMonitor.getStatus(URI.create(lraId), 10*1000);
logger.log(Level.INFO, "LRA id: {0} final status \"{1}\"", new Object[]{lraId, status});

Response acyncRes = status.equals(LRAStatus.Closed.name()) ?
Response.ok(status).build() :
Response.status(Status.INTERNAL_SERVER_ERROR).entity(status).build();
// handle error case
if(response.getStatus() / 100 != 2){
Response acyncRes = Response.status(Status.INTERNAL_SERVER_ERROR).entity("Failed to forward request: " + response.getStatus()).build();
asyncResponse.resume(acyncRes);
}else{
String lraId = response.getHeaderString(LRA_HTTP_CONTEXT_HEADER);
String status = statusMonitor.getStatus(URI.create(lraId), 10*1000);
logger.log(Level.INFO, "LRA id: {0} final status \"{1}\"", new Object[]{lraId, status});
Response acyncRes = status.equals(LRAStatus.Closed.name()) ?
Response.ok(status).build() :
Response.status(Status.INTERNAL_SERVER_ERROR).entity(status).build();
asyncResponse.resume(acyncRes);
}

asyncResponse.resume(acyncRes);
}).start();

}
Expand Down

0 comments on commit 5182baa

Please sign in to comment.