How to publish the message in Google Pubsub using camel route - apache-camel

I have to publish the message, "hello" to pub sub using the camel route.
How the message should be formed in camle before sending it to google pubsub.
Is there any example available.
Regards,
Jagan

In your route, you can set the body using the setBody function:
from("direct:a")
.setBody(constant("hello"))
.to("google-pubsub://project-id:destinationName");

according to the documentation Google Pubsub - uri format, it comes in the format:
"google-pubsub://<project>:<topic>".
Also the body should be of type byte[] though String.class works well for me.
I use .toD instead of .to
That has worked for me.

Related

How to add an trailing slash to the camel http component of camel

Im am trying to call a rest post service from my Camel route.
The rest service is deployed at https://csp-verteileauftrag-camunda-v1-csp-ims-dev-az.apcn.osp4-preprod.hel.kko.ch/api/v1/verteileauftrag/.
Calling the Service from a Rest Client like VS Code works if there is a trailing slash (after verteileauftrag).
In my camel route I have configured the following:
restConfiguration().host("https://csp-verteileauftrag-camunda-v1-csp-ims-dev-az.apcn.osp4-preprod.hel.kko.ch/api/v1").component("http").bindingMode(RestBindingMode.json);
and in then later using the config:
.to("rest:post:verteileauftrag?outType=ch.helsana.csp.verteileauftrag.rest.model.apiadapter.ResponseType")
If I execute the code, I get a 404 HTTP Error from the Backend as the URL used is
https://csp-verteileauftrag-camunda-v1-csp-ims-dev-az.apcn.osp4-preprod.hel.kko.ch/api/v1/verteileauftrag. So without a trailing slash.
I have tried to add it like .to("rest:post:verteileauftrag/?outType=ch.helsana.csp.verteileauftrag.rest.model.apiadapter.ResponseType")
but no success.
Do you have any idea how to tell the http component in the rest configuration how to add the trailing slash?
Thank you very much.
Using redhat fuse 7_10_2.
Regards Michel
Based on a collegues example I reimplemented the Rest Service call with a normal .to("URL") configuration in the routebuilder with setting the appropriate Headers for HTTP Post and content type json. Would still be interessted in any answer, but not waiting on one.

No endpoint could be found for - Camel Http Integration

I'm trying to run a simple test with Apache Camel:
from("http://localhost:61554/api/v1/MyController/my-endpoint")
.to("direct:a")
.log("$({body}");
I'm getting the following error: "No endpoint could be found for: http://localhost:61554/api/v1/MyController/my-endpoint, please check your classpath contains the needed Camel component jar"
I'm very new to Camel and Java. Can someone please tell me why this error is coming up? Should I be using from("direct:x")... ? If, so where do I map my "direct" endpoints to concrete ones?
Thanks
You cannot use the http component as consumer (eg in from) - its a http client for calling HTTP servers (so its a producer, eg to).
Instead to have HTTP as consumer you can use camel-servlet, camel-jetty, camel-undertow, etc.

How to use camel consul component for agent API?

As per camel documentation for consul(camel.apache.org/consul-component.html), the supported HTTP API are kv, event and agent. There are example of kv (key/value store) which are working fine but there is no such example for agent API. I went thruogh the documentation of Consul [www.consul.io/docs/agent/http/agent.html] and the corresponding java client [github.com/OrbitzWorldwide/consul-client] as well and tried to figure out how consul:agent component should work but I have found nothing simple there.
main.getCamelTemplate().sendBodyAndHeader(
"consul:agent?url=http://localhost:8500/v1/agent/service/register",
payload,
ConsulConstants.CONSUL_ACTION, ConsulAgentActions.AGENT); //also tried with ConsulAgentActions.SERVICES, but no luck
I also checked the test cases mention at https://github.com/apache/camel/tree/master/components/camel-consul/src/test/java/org/apache/camel/component/consul but unable to find anything related to agent api.
So my question is that how to use consul:agent component.
UPDATE: I tried the below code and able to get the services.
Object res = main.getCamelTemplate().requestBodyAndHeader("consul:agent", "", ConsulConstants.CONSUL_ACTION, ConsulAgentActions.SERVICES);
It seems that the consul component only work for the GET operation of the HTTP agent API. But in that case how do I register a new service (like /v1/agent/service/register : Registers a new local service) with consul component?
This code works for me:
ImmutableService service =
ImmutableService.builder()
.id("service-1")
.service("service")
.addTags("camel", "service-call")
.address("127.0.0.1")
.port(9011)
.build();
ImmutableCatalogRegistration registration =
ImmutableCatalogRegistration.builder()
.datacenter("dc1")
.node("node1")
.address("127.0.0.1")
.service(service)
.build();
ProducerTemplate template = main.getCamelTemplate();
Object res = template.requestBodyAndHeader("consul:catalog", registration, ConsulConstants.CONSUL_ACTION, ConsulCatalogActions.REGISTER);
But it's looking some inelegantly (like workaround), and i think there are other solutions.
One can use
.to("consul:agent?action=SERVICES")
to retrieve the registered Services as Map<String, Service>, with service id as map key.
And
.to("consul:catalog?action=REGISTER")
to write registrations, expecting an ImmutableCatalogRegistration as body
Note that you can employ a CamelServiceRegistrationRoutePolicy to register Camel routes as services automatically.

Twilio MMS Send Local File

We can send MMS using Twilio api in Salesforce.
As i know that using that code i can send MMS using Twilio in Salesforce.
Sending a MMS
properties.add(new TwilioNameValuePair('MediaUrl','https://www.twilio.com/packages/company/img/logos_downloadable_round.png'));
But it's seams that we have need to pass media file url.
But how can we take a input of file form Local machine and send them using Twilio MMS?
You can use twilio assets for sending local files.
- First you need to upload the local files on twilio assets.
- use the assets url and media url for sending Mms
Rajendra, hello! Megan from Twilio here.
This sounds similar to what I ran into building an OpenCV MMS implementation in this blog post.
The example there is in Python specifically, after writing an image to my local filesystem and making the desired modifications on it, I send it through:
#app.route('/uploads/<filename>', methods=['GET', 'POST'])
def uploaded_file(filename):
return send_from_directory(UPLOAD_FOLDER,
filename)
which handles the delivery of the following TwiML:
message.media('http://YourNgrokURL/uploads/{}'.format(filename))
You'll be needing the equivalent of Flask's send_from_directory to handle this in your implementation.
Hope this helps!

using /upload urls with Google API client

Consider this:
I am fetching rawbody messages by this call:
service.users().messages().get(userId='me', format='raw', id=msgid)
Then I am pushing rawbody messages by this Call:
service.users().messages().insert(userId='me', body=message)
Now when the mails contain attachments bigger 5MB, I encounter 413 "Request Entity Too Large.", and I can't push mails.
GMail API messages.insert Documentation advices to use
POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages
instead of
POST https://www.googleapis.com/gmail/v1/users/userId/messages.
But Google API Client doesn't seem to have any documentation about how to call the above Url, and it keeps getting back to latter url.
How Can send post requests to first url(with /upload) with Google Api Client rather than its default?
How to use /upload url and set uploadType=multipart with Google APi Client?
Yeah, this was totally unclear from the documentation for the Google Python API client, but I found the solution in this other answer. It turns out that you use the same method (users().messages().insert()) but you pass media_body instead of body['raw']. Something like this should work:
from io import BytesIO
from base64 import urlsafe_b64decode
import googleapiclient.http
b = BytesIO()
message_bytes = urlsafe_b64decode(fetched_message['raw'])
b.write(message_bytes)
media_body = googleapiclient.http.MediaIoBaseUpload(b, mimetype='message/rfc822')
service.users().messages().insert(userId='me', media_body=media_body).execute()
I haven't tried using uploadType=multipart, but maybe you can figure it out from this documentation page and looking at the contents of the googleapiclient.http module.

Resources