Two distinct responses from RESTful web service for a single call - web-services

How can I get 2 or multiple responses back from a CXF based RESTFul webservice for a single call.
For example : For this http://localhost:8080/report/annual, I would like to get 2 JSON reponses back. The first one will give me the information about the report details & some other information. The second reponse will give me the actual report JSON. If these 2 be delivered async that will be really good.

I'm with #flesk, this really isn't a REST approach, this is more of an async messaging approach.
The first call should return "someinfo" after it starts the "actualReport" processing (in a separate thread/process since "actualReport" is time consuming). Then make a second call for "actualReport" and make sure the timeout value on that call is set high enough to let the report processing complete.
You could get fancy and loop on the second call, returning a 404 until the report is complete.
There are a number of ways to get what you want, just not with one RESTful call.

You can't. Why would you want to do that anyway, when you can just return something like
{"someInfo": {...}, "actualReport": {...}}

Related

Writing Cucumber scenarios for reactive code

I am very new to BDD, and having a bit of trouble outlining the scenarios for some code that I wrote. The code basically queries a Couchbase bucket for abandoned orders, and then calls a cancel order API to cancel those orders. For each call to the cancel order API, it calls another service to generate access token.
The entire code has been written in RxJava. In case of errors, I have fallback observables in place (for example, should anything go awry while querying Couchbase, it fallbacks to an empty observable). I have similar fallbacks in other places in the code as well.
I want to write Cucumber scenarios for my code. But I can't figure out how to go about it. For example, should I assume that the service has a valid access token and an orderId to cancel (querying CB returns a bunch of orderIds that need to be passed on to the cancel order API along with an access token)?
Ideally, I should be testing the following:
Querying Couchbase fails. In this case, I should get an empty observable.
Call to the access token API fails. In this case as well, I should get an empty observable.
Call to the cancel order API fails. In this case as well, I should get an empty observable.
Call to cancel order API returns some response code other than 200. Then assert on the response codes.
The happy case.
Now suppose I want to test the first case, that is, querying CB fails. What will be the background in this case? How should I simulate a query failure?

Abort or terminate the Request which takes long time to respond back

I have an application developed using SmartGWT,Jaxrs,ejb &jpa.
I have one scenario where user wants to extract the data(called Search Screen) by entering either firstname,lastname or middlebane,ssn,email,etc
Database contains the huge number of records in millions, which takes lot of time to respond back.
for example, user search with firstname which takes lot of time to respond, in that case user wants to cancel/terminate/abort the request.
Is it possible either in smartgwt or jaxrs(web api) to terminate the request.
So that user can terminate the request and move further
PS:: i tried lot of option,but i didn't get the proper solution.
One solution is to put the business logic in stateful bean and put the bean in the http session ... now you have access to the currently used persistence context and the open transaction so you can call Session.cancelQuery() .... but this method has some limitation .. It works only if Result set is not yet returned , if this limitation harms you, check this answer please
There are other workarounds to synchronize the web client with the business method but this is the one I like most
One more thing you need to consider as this is your use case is to introduce a new lexical search engine like solr or elasticsearch which can be updated frequently with data from the database ... It fits perfectly in lexical search, gives the ability to stand typo mistakes and returns result very quickly

WS2ESB: Store state between sequence invocations

I was wondering about the proper way to store state between sequence invocations in WSO2ESB. In other words, if I have a scheduled task that invokes sequence S, at the end of iteration 0 I want to store some String variable (lets' call it ID), and then I want to read this ID at the start (or in the middle) of iteration 1, and so on.
To be more precise, I want to get a list of new SMS messages from an existing service, Twilio to be exact. However, Twilio only lets me get messages for selected days, i.e. there's no way for me to say give me only new messages (since I last checked / newer than certain message ID). Therefore, I'd like to create a scheduled task that will query Twilio and pass only new messages via REST call to my service. In order to do this, my sequence needs to query Twilio and then go through the returned list of messages, and discard messages that were already reported in the previous invocation. Now, to do this I need to store some state between different task/sequence invocations, i.e. at the end of the sequence I need to store the ID of the newest message in the current batch. This ID can then be used in subsequent invocation to determine which messages were already reported in the previous invocation.
I could use DBLookup and DB Report mediators, but it seems like an overkill (using a database to store a single string) and not very performance friendly. On the other hand, as far as I can see Class mediators are instantiated as singletons, therefore I could create a custom Class mediator that would manage this state and filter the list of messages to be sent to my service. I am quite sure that this will work, but I was wondering if this is the way to go, or there might be a more elegant solution that I missed.
We can think of 3 options here.
Using DBLookup/Report as you've suggested
Using the Carbon registry to store the values (this again uses DBs in the back end)
Using a Custom mediator to hold the state and read/write it from/to properties
Out of these three, obviously the third one will deliver the best performance since everything will be in-memory. It's also quite simple to implement and sometime back I did something similar and wrote a blog post here.
But on the other hand, the first two options can keep the state even when the server crashes, if it's a concern for your use case.
Since esb 490 you can persist and read properties from registry using property mediator.
https://docs.wso2.com/display/ESB490/Property+Mediator

Aggregate messages from an <all> flow in Mule

The story so far
I have a SOAP service that sends its response (say Response_A) to an <all> flow. Inside the flow, there are three SOAP services (say B, C and D) that take inputs from Response_A. I am taking fields from Response_A and using XSLT, I can formulate requests for B, C and D.
Quick question: I am using <async> blocks inside <all> to process messages in parallel. The processing was not parallel when using <all> and <processor-chain> tags inside it. Any ideas why?
The roadmap
I will read the responses from all three B, C and D and combine them into a single response (probably using XSLT again) and send it to E.
The roadblock
After coming out of the <all> flow, I get a MuleMessageCollection. How to read it, and combine the messages into a single message?
My attempts
I tried aggregating the messages based on a correlation ID but I noticed that a correlation id is present only when the message from A was split by the <all> tag and was being sent to B, C and D. The correlation ID vanishes in the SOAP envelope that comes as a response from these services, even if I turned enableMuleSoapHeaders to true. I cannot modify the services. So, how do I make the correlation id appear on the SOAP response (provided a correlation id is absolutely necessary if I want to merge messages)
I will also need the group size to aggregate messages, I guess.
I even tried adding a correlation id using message property transformer, but it did not work that way. I was stuck with a MessageCollection and did not know how to read it, even though there were probably messages with correlation id's inside it.
So, it boils down to one question. What are the ways to merge messages from a MessageCollection?
I want to do this in xml, without writing a custom transformer in Java. Is it possible? What should be my approach?
Note: The response messages from B, C and D have different DOMs structures. The merged message that I want to create has a different DO from all of A, B, C and D's responses and requests.
If it helps, I a trying to work on a similar situation as described here: http://ricston.com/blog/?p=640 only difference is, I am using flows and the all tag.
The processing was not parallel when using and tags inside it. Any ideas why?
This is because all and processor-chain are synchronous by nature: they do not parallelize anything.
Now for your problem of aggregating remote asynchronous responses, if the remote service doesn't reflect back the Mule headers (which is the case for the vast majority of non-Mule powered service), you need to find out if you can use maybe a value inside the response payload that would be reflected back from the remote service (that could be a SOAP header or a field, like an ID, inside the SOAP body). If that is the case, you can configure the collection-aggregator with a expression-message-info-mapping that specifies that correlation will not be done using the Mule header but another source.
Otherwise, you'd rather keep the all block and make the calls one after the other...

How best to design a RESTful API for initiating an action

I'm building a RESTful web service that has the usual flavor of CRUD operations for a set of data types. The HTTP verb mappings for these APIs are obvious.
The interesting part comes in where the client can request that a long-running (i.e., hours) operation against one of the data objects be initialized; the status of the operation is reported by querying the data type itself.
For example, assume an object with the following characteristics:
SomeDataType
{
Name: "Some name",
CurrentOperation: "LongOperationA",
CurrentOperationPercent: 0.75,
CurrentOperationEtaSeconds: 3600
}
My question, then, is what the best RESTful approach should be for starting LongOperationA?
The most obvious approach would seem to be making the operation itself the identifier, perhaps something along the lines of POST https://my-web-service.com/api/StartLongOperationA?DataID=xxxx, but that seems a bit clunky, even if I don't specify the data identifier as a query parameter.
It's also pretty trivial to implement this as an idempotent action, so using POST seems like a waste; on the other hand, PUT is awkward, since no data is actually being written to the service.
Has anybody else faced this type of scenario in their services? What have you done to expose an API for initializing actions that honors RESTful principals?
TIA,
-Mark
You could do,
POST /LongRunningOperations?DataId=xxxx
to create a new LongRunningOperation. The URI of the long running operation would be returned in the Location header along with a 201 status code.
Or if you want to keep the long running operations associated to the DataId you could do
POST /Data/xxx/LongRunningOperations
Both these options will give you the opportunity to inquire if there are long running operations still executing. If you need information after the operation has completed you can create things like
GET /CompletedLongRunningOperations
GET /Data/xxx/CompletedLongRunningOperations
GET /Data/xxx/LastCompletedLongRunningOperation