Skip to content

Can we uniquely identify request to send respond from a POST method?

I am working on a web-service which is asynchronous. In my client code, I am using a boto3 session client to call a GET API of my Jetty Server which is S3 alike service. GET API fetched original data from S3 and modifies the request so as to be able to forward the request to flask server. Python flask then get the request processed (where data transformation is done) and calls the POST API of the Jetty Server.

Now I am stuck at figuring out how can I respond to the original caller? Because I am not sure if a API request can have a session-id to identify the original caller?

How can my POST API respond back to client? Following is the overall conceptualization of what I am trying to achieve. How can I do it?

enter image description here

Answer

Since I am using embedded Jetty, I used the built-in org.eclipse.jetty.server.HttpChannel.Listener.

I now have access to the raw internal Jetty org.eclipse.jetty.server.Request object that has the HTTP Fields for that request.

To use it, I’ll create an instance of that HttpChannel.Listener, and add it as a bean to my connectors.

public class RequestChannelListener implements HttpChannel.Listener {
    @Override
    public void onRequestBegin(Request request) {
        HttpFields.Mutable replacement = HttpFields.build(request.getHttpFields())
                .put("X-Request-ID", UUID.randomUUID().toString().toUpperCase());
        request.setHttpFields(replacement);
    }
}

Add as a bean in the connector –

RequestChannelListener channelListener = new RequestChannelListener();
connector.addBean(channelListener);

Then all other access of that request, be it internal components of Jetty, a webapp, a specific servlet, filters, forwarding, includes, error handling in the servlet spec, error handling outside of a servlet context, etc can all see it.

To check if the custom header got added into the request or not –

Enumeration<String> headerNames = request.getHeaderNames();
        while(headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            System.out.println("Header Name - " + headerName + ", Value - " + request.getHeader(headerName));
        }