Passing Exchange Property between routes within same Camelcontext - apache-camel

I have below requirement:
Route 1: from(timer:foo?repeatcount=1).recepientList("rest service")
Route 2: from(sourcequeue).process(new myprocessor()).to(destinationqueue)
Need to use the json response from route 1 and pass it to Route 2 processor.
My problem is whenever i set the json response in exchange property and try to use in Route 2 processor it is null.
Any suggestion on how to pass the exchange property between these routes would be of great help.
Thanks in advance.

The reason you cannot use Exchange properties to pass information between routes is that they are not part of the message.
Take a look at this picture of the Camel Exchange model.
When a message is received by Camel, it embeds it into an Exchange and the Exchange is passed through the route. But when you send a message (.to(...)), only the message is sent.
Therefore you have to use (as answered by Thomas) the message body or a message header.

If you use http camel component, the http response should be in the body. You can load it from your processor.
String json = exchange.getIn().getBody(String.class);
from(timer:foo?repeatcount=1).recepientList("http://rest_service")
.to(direct:sourcequeue)
You can also use headers to pass data throw your route.
from(timer:foo?repeatcount=1).recepientList("http://rest_service")
.setHeader(“myJsonResponse”, simple("${body}"))
.to(direct:sourcequeue)
String json = exchange.getIn().getHeader(“myJsonResponse”, String.class);

Related

Pass data in body of GET request in React

I am trying to use an API which is of GET type and it is expecting the data in body, not as query parameter. I am using Axios as HTTP client but it seems Axios is not supporting body in GET request. Since the API is third party, I can not change it to read data from params or change the method to POST.
Is there any way to pass data in body using Axios or any other HTTP Client?
The same API is working with body when using with PHP Curl
Use data or params parameter of the GET according to your needs
Refer the docs for all the parameters
axios.get(< your request url >, {
params: {
custom_data : < Your Body here >
}
}).then(...)
Also Refer
Send object with axios get request
Axios get in url works but with second parameter as object it doesn't

React router -- How to send props without query params or Redux?

I want to send data to another route, but don't want to send it in query params.
I don't want a new store for every route, nor do I want a store that simply holds all routes / params separately from where they are sent / consumed.
Is there a standard way to specify props for an upcoming route?
I found the solution on the react-router location api docs.
this.props.router.push({
pathname: '/view-user',
state: { userId }
});
This seems great for interstitial, standalone modal pages.
May need to specify a fallback if the state is missing, but haven't quite gotten that far.
if (!this.props.location.state) this.props.router.goBack();
or
const locations = this.props.location.pathname.split('/');
// do something
this.props.route.push(locations.join('/'));
If you are not sending the information in the query param, then you can put it in some other kind of store that can also be associated with the route.
You can wrap the router.push() call with your own function that takes an extra object you want to pass along. Something like...
function navigateTo(url, extraData) {
if (extraData !== undefined) {
saveExtraDataForRoute(url, extraData);
}
router.push(url);
}
In react-router, there is an onEnter prop associated with the route that specifies a function to call. The code in this function can retrieve the extra data and do whatever you want to do with it.
function onMyScreenEnter() {
const extraData = getExtraDataForRoute(url);
goCrazyNutsWithYourExtraData(extraData);
}
You'd supply the two functions saveExtraDataForRoute() and getExtraDataForRoute(). They could use your store (e.g. Redux), set values of a singleton object, or use LocalStorage. But essentially, to save the data so it's retrievable by URL later, you'd be saying something like:
extraDataStore[url] = extraData;
The other thing you may wish to look into is using a POST method with react-router. I haven't done this, and am not sure how well it works. Here is a link: How to Handle Post Request in Isomorphic React + React Router Application

Pass (hidden) data with $location.url

I'm getting a server response on a request that includes the following:
redirect_url
job_name
I'm using angular's $location.url(redirectUrl) to navigate to the redirect url, which may have query string parameters. I'd also like to pass it the job_name for use by the next state, but even the ugly solution of tacking it onto the search params (with $location.url(redirect_url).search(job_name) doesn't work if redirect_url already HAS search parameters.
How can I pass data to a new state if I don't have the state's name, only the url? Do i have to parse it out?

in Backbone model, which method gets executed first, success or parse?

I am making a AJAX call using something like:
model.fetch(
dataType: "jsonp",
success: function(data){}
)
My question is if I want to modify the data return from the server, should I do it in success or model.parse(). Also, which method gets executed first?
WARNING: I am a backbone newbie :)
Thank you in advance!
Parse will be triggered first.
The backbone official documentation its not clear about it. It says:
parse is called whenever a model's data is returned by the server, in fetch, and save. The function is passed the raw response object, and should return the attributes hash to be set on the model. The default implementation is a no-op, simply passing through the JSON response. Override this if you need to work with a preexisting API, or better namespace your responses.
It doesn't talk about who is triggered first.
But i test it by my self, and parse was triggered first.
You can test it by yourself, if you don't have an API for test, Use dataType:"jsonp" and try to find a web site that is using REST.You'll see that JsonP is triggered first. :)

Converting existing web app to use hashtag URIs using Backbone.js

I'm attempting to use Backbone and it's Router to turn an app into an ajax app, however it currently uses several different methods (helpers) of generating links. Unfortunately, this means manually changing each and every link to use a hashtag is out of the question.
What would be the best method of ensuring every link, form post, redirect, etc. gets parsed as a hashtag URL that can be caught by Backbone's Router? Or, even better, is it possible for the Router to accept "true URL's" from a request? Example: a request to /app/mail/inbox.php is caught by a rule in the Router, and is turned into #/mail/inbox after firing the appropriate method to handle the request.
What would be the best method of ensuring every link, form post, redirect, etc. gets parsed as a hashtag URL that can be caught by Backbone's Router?
I don't think that Backbone.Router is supposed to handle, say, form posts. It's supposed to give your application view state—bookmark-friendly and refreshable URLs [1].
If you want to ‘ajaxify’ forms, then you probably should add a handler for form's submit event and do something like $.ajax() there, preventing the default action.
Regarding plain old links, History.pushState() support has been added to Backbone recently. It means that you can define your routes as /app/*, and don't need to replace old href attributes. However, you'll still need to catch link click events to prevent default action.
For example:
var handle_link_click = function(e) {
path = $(e.target).attr('href');
app.main_router.navigate(path, true); // This.
e.preventDefault();
};
$('a:internal').click(handle_link_click);
Router's navigate() method will do history.pushState() if it's available, falling back to old hashchange. And true as a second argument means that it will fire corresponding handler action.
[1] See also this presentation about Backbone

Resources