Making a call to servlet from workflow process




I have a use case where there is an already a servlet providing data of certain nodes in json format.

Now: There is a workflow which needs same json data to be pushed AWS S3 storage.

Trial 1: Is it possible to get a sling request in a workflow process step ?

I tried to common out all the reusable code out of servlet, but still there are references to request object.

Trial2: Used Apache HTTP Client api to make a call to servlet url and got anonymous user access not allowed, unauthorized.

Trial3: Used below code, even though servlet sending back all the response, bytearrayoutputstram does NOT have all the response.

String requestPath =  "/srvpub/topnav.consumer.json " // servlet path

  /* Setup request */

  HttpServletRequest req = requestResponseFactory.createRequest("GET", requestPath);


  /* Setup response */

  ByteArrayOutputStream out = new ByteArrayOutputStream();

  HttpServletResponse resp = requestResponseFactory.createResponse(out);

  /* Process request through Sling */

  requestProcessor.processRequest(req, resp, request.getResourceResolver());

  String html = out.toString();

Any help or ideas on what I am missing or working ways to solve this use case.



Accepted Solutions (0)

Answers (6)

Answers (6)



I have never tried to access a Sling Servlet in this manner. You can try and see if it works. I have successfully used HTTP API code to invoke a servlet - which works fine.

You can try to test your code at 1st by hard-coding authentication. Then once that works - you can try to figure out a better way to get authentication information. Maybe place it in a deep node in the JCR which would be secure.




Is it possible to get service user : user name , password from resource resolver?


Should I need to hard code a user name, password ?

Thanks again.




Are you making the jcr query within servlet to get the node data and create json ? if so, I would recommend to move the the logic of querying the node data and getting a json to a service and your servlet should just call that service method to respond to the http request.

If you have this solution in place, then you can refer the same service and call the respective method within your workflow process aswell.

Basically, I would keep any logical processing in a service and make servlet as a delegate class. This would keep your implementation clean and make it more re-usable.