mardi 26 janvier 2021

How to send asynchronous Http Response?

I have created a Rest API that takes a request and gives response to the client. But My API is dependent on a third party service, that instead of giving a response, give a callback to my other endpoint. In order to send the service I need to wait for the callback to be received. How can I achieve it?

My Rest API that needs to send the response.

@POST
//  @Produces(MediaType.TEXT_XML)
//  @Consumes(MediaType.TEXT_XML)
    public ConnectResponse postQuestionnaire(String connectString, @Context HttpHeaders headers) {
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        for (Entry<String, List<String>> entry : headers.getRequestHeaders().entrySet()) {
            logger.info("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            for (String eachEntry : entry.getValue()) {
                logger.info("eachEntry " + eachEntry);
            }
        }
        logger.info("USSD received " + connectString);
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        logger.info("---------- in connect post request ----------------------");
        ConnectRequest requestObj = new ConnectRequest();
        try {
            if (connectString != null && connectString.startsWith("<")) {
                requestObj = marshallConnectRequest(connectString);
            } else {
                requestObj = convertKeyValueToObject(connectString);
            }

            logger.info("Request is " + requestObj);
        } catch (JAXBException e) {
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error("----------------- Error in UnMarshalling ----------");
            logger.error(e.getMessage(), e);
        }
        ConnectResponse connectResponse = new ConnectResponse();
        connectResponse.setSession(requestObj.getSessionid());
        connectResponse.setText("Hello");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        logger.info("---------- returning response ----------------------");
        return connectResponse;
    }

    public ConnectRequest marshallConnectRequest(String connectString) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(ConnectRequest.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return (ConnectRequest) jaxbUnmarshaller.unmarshal(new StringReader(connectString));
    }

    public ConnectRequest convertKeyValueToObject(String connectString) {
        return new ConnectRequest();
    }

Instead of sending a simple response object I want to wait for the callback to hit at the following API.

@Path("/rest")
public class RESTWebservice {
    /*
     * @Context private MessageContext messageContext;
     */
    final Logger logger = Logger.getLogger(RESTWebservice.class);

    @POST
    @Path("/sendResponse")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public ResponseJSON postQuestionnaire(RequestJSON requestJson) {
    // performing operations
}

Aucun commentaire:

Enregistrer un commentaire