Expand my Community achievements bar.

SOLVED

Making a call to servlet from workflow process

Avatar

Level 7

Hi,

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);

  WCMMode.DISABLED.toRequest(req);

  /* 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.

Thanks,

Sri

1 Accepted Solution

Avatar

Correct answer by
Level 10

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.

View solution in original post

6 Replies

Avatar

Level 10

In your HTTP API Code that invokes the servlet to perform a GET- modify it so it authenticates - that will solve your issue.

httpurlconnection - Connecting to remote URL which requires authentication using Java - Stack Overfl...

Avatar

Correct answer by
Level 10

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.

Avatar

Level 7

Thanks,

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

or

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

Thanks again.

Avatar

Level 10

you should be able to get the service user to login in your service.

Avatar

Level 7

how about accessing a servlet with resource resolver with service user ?

Avatar

Level 10

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.