Extract information of request in response mediator in wso2 apim 4.1.0 - request

I need to use some information from request such as "query params" or "headers" in response mediator. for example use url query parameter in response flow. I tried to use <"inSequence"> and <"outSequence"> but I could'nt get any result.
For example I want to use this "log mediator" in response flow. "firstname" is query param in request:
<log level="custom">
<property name="firstname" expression="$url:firstname"/>
</log>

If you want to access the query parameters being sent in the original request from the out-sequence, simply set it to a property in the in-sequence. Then that property will be accessible in the out-sequence. See the example below.
In Sequence
<inSequence>
<property expression="$url:firstname" name="firstname" scope="default" type="STRING"/>
<log level="simple">
<property expression="$ctx:firstname" name="Firstname====" scope="default" type="STRING"/>
</log>
</inSequence>
Out Sequence
<outSequence>
<log level="simple">
<property expression="$ctx:firstname" name="Firstname====" scope="default" type="STRING"/>
</log>
<send/>
</outSequence>

I solved this problem by using two mediator. one for request flow and second for response flow. We can access in response flow, initial request properties.
This xml is for request flow:
<sequence name="main">
<property expression="$url:firstname" name="firstname" scope="default" type="STRING"/>
<log level="simple">
<property expression="$ctx:firstname" name="Firstname====" scope="default" type="STRING"/>
</log>
</sequence>
This request is for response flow:
<sequence name="main">
<log level="simple">
<property value="response$$$$$$$$$$$$$$$$$$$$$$$$$$" name="resp"
scope="default" type="STRING"/>
</log>
<log level="simple">
<property expression="$ctx:firstname" name="Firstname"
scope="default" type="STRING"/>
</log>
</sequence>

Related

Is there any Solution to download attachment from gmail in WSO2 ESB / WSO2 EI?

I am working on wso2 esb since last 6 months. I want to download attachment from gmail in wso2 esb/ei and save them into local folder.Google it and get solution only for sending attachment using gmail connector, not downloading attachment.
Note: Already seen,but can't able to get solution by referring the following link-
WSO2 esb get attach files from email
Code:
ProxyService
<proxy name="GmailConfigAttachment-TaskProxy" startOnLoad="true"
transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<log level="custom">
<property name="Log Text"
value="Inside GmailConfigAttachment-TaskProxy Service"></property>
</log>
<property name="FORCE_SC_ACCEPTED" scope="axis2" type="STRING"
value="true" />
<property description="serviceName" name="serviceName"
scope="operation" type="STRING"
value="TaskScheduler_ASG_Read_Email_Body_Dummy_Service" />
<property description="messageId" expression="get-property('MessageID')"
name="messageId" scope="operation" type="STRING" />
<sequence key="Load_Gmail_Configuration" />
<gmail.init>
<userId>{$ctx:userId}</userId>
<accessToken>{$ctx:accessToken}</accessToken>
<apiUrl>{$ctx:apiUrl}</apiUrl>
<clientId>{$ctx:clientId}</clientId>
<clientSecret>{$ctx:clientSecret}</clientSecret>
<refreshToken>{$ctx:refreshToken}</refreshToken>
<accessTokenRegistryPath>{$ctx:registryPath}</accessTokenRegistryPath>
</gmail.init>
<gmail.listAllMails>
<labelIds>INBOX</labelIds>
<q>is:unread</q>
</gmail.listAllMails>
<log level="full" />
<iterate description="MailIterator" expression="//jsonObject/messages" sequential="true" id="listUnread">
<target>
<sequence>
<log description="Iterate Logger" level="custom" separator=",**, ">
<property expression="fn:concat('GmailConfigAttachment-TaskProxy_Service ESB-MessageId:',get-property('operation','messageId'))" name="LogText" />
<property expression="//id/text()" name="Gmail-MessageId" />
</log>
<property description="emailMsgId" expression="//id/text()" name="emailMsgId" scope="default" type="STRING" />
<property description="emailSubject" expression="//headers/name[text()='Subject']/../value/text()" name="emailSubject" scope="default" type="STRING"/>
<property description="emailDate" expression="//headers/name[text()='Date']/../value/text()" name="emailDate" scope="operation" type="STRING"/>
<property description="toAddress" expression="//headers/name[text()='To']/../value/text()" name="toAddress" scope="operation" type="STRING"/>
<header expression="fn:concat('Bearer ', $ctx:uri.var.gmail.accessToken)" name="Authorization" scope="transport" />
<gmail.readMail>
<id>{$ctx:emailMsgId}</id>
</gmail.readMail>
<payloadFactory media-type="xml">
<format>
<htmlProcessEmailBody xmlns="">
<emailMessageId>$1</emailMessageId>
<emailSubject>$2</emailSubject>
<emailBody>$3</emailBody>
</htmlProcessEmailBody>
</format>
<args>
<arg evaluator="xml" expression="get-property('emailMsgId')"/>
<arg evaluator="xml" expression="get-property('emailSubject')"/>
<arg evaluator="xml" expression="//payload"/>
</args>
</payloadFactory>
<log level="custom">
<property description="emailMsgId" expression="get-property('emailMsgId')" name="emailMsgId" scope="default" type="STRING" />
<property description="emailSubject" expression="//headers/name[text()='Subject']/../value/text()" name="emailSubject" scope="default" type="STRING"/>
<property description="emailDate" expression="//headers/name[text()='Date']/../value/text()" name="emailDate" scope="default" type="STRING"/>
<property description="toAddress" expression="//headers/name[text()='To']/../value/text()" name="toAddress" scope="default" type="STRING"/>
</log>
<!-- here i need further process to download attachment from gmail -->
</sequence>
</target>
</iterate>
</inSequence>
<outSequence />
<faultSequence />
</target>
Output of ESB
So Can anyone help me to provide solution?
Awaiting for your response.
Thank you.
Finally i found the solution for my question.
Idea :
Iterating each parts then getting email attachment id which will be passed to gmail api in order to get base64 encoded format.
Code snippet:
<iterate continueParent="true" description="MailIterator" expression="//parts" id="listUnread" sequential="true">
<target>
<sequence>
<property expression="//filename[text()!=' ']" name="AttachedFileName" scope="default" type="STRING"/>
<property expression="substring-after(get-property('AttachedFileName'),'.')" name="Attachmentextension" scope="default" type="STRING"/>
<filter description="check emailSubject" regex="jpg|jpeg|png|gif|webp|tiff|tif|psd|raw|bmp|dib|heif|heic|indd|ind|jp2" source="lower-case(get-property('Attachmentextension'))">
<then>
<property description="emailAttachmentId" expression="//attachmentId/text()" name="uri.var.attachmentId" scope="default" type="STRING"/>
<log level="custom">
<property expression="get-property('AttachedFileName')" name="=====ValidAttachmentFileName===="/>
<property expression="get-property('uri.var.attachmentId')" name="====emailAttachmentId====="/>
</log>
<header expression="fn:concat('Bearer ', $ctx:uri.var.gmail.accessToken)" name="Authorization" scope="transport"/>
<call>
<endpoint>
<http method="get" uri-template="{+uri.var.gmail.apiUrl}/{+uri.var.gmail.apiVersion}/users/{+uri.var.gmail.userId}/messages/{+uri.var.id}/attachments/{+uri.var.attachmentId}"/>
</endpoint>
</call>
<property description="emailAttachment" expression="//data/text()" name="emailAttachment" scope="default" type="STRING"/>
<!-- ==========Script for Base64 url to Base64 Encoding Format ========== -->
<script language="js"><![CDATA[var log=mc.getServiceLog();
var emailAttachment = mc.getProperty('emailAttachment');
emailAttachment=emailAttachment.replaceAll("_","/").replaceAll("-","+");
mc.setProperty("modifiedemailAttachment",emailAttachment)]]></script>
<log level="custom">
<property expression="get-property('modifiedemailAttachment')" name="========modifiedemailAttachment========"/>
</log>

How to use Camel to get message from remote WMQ and place message in ActiveMQ?

I have been looking at these for days and still haven't have mush success.
I have install and run activeMQ on a server. Created a queue called "testUpdate". I also have another queue on another server, let call this "forward" and this is on the IBM MQ (WMQ).
So we have testUpdate on ActiveMQ and forward on WMQ. I want the message on forward queue to be placed into testUpdate queue. Trying to use Camel for this process.
So the setup in ActiveMQ has an XML(activemq.xml) that I can place spring beans and configure to do the routing. In this xml, I added http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd to the xmlns and then added
import resource="camel.xml"
In this new xml, I have the following:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<description>Example Camel Route</description>
<from uri="mqseries:forward"/>
<to uri="activemq:testUpdate"/>
</route>
</camelContext>
<bean id="mqseries" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="transportType" value="1"/>
<property name="hostName" value="172.00.12.21/>
<property name="port" value="xyza"/>
<property name="queueManager" value="manager"/>
<property name="channel" value="srvcChannel"/>
</bean>
<!--
Lets configure some Camel endpoints
http://camel.apache.org/components.html
-->
<!-- configure the camel activemq component to use the current broker -->
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://amq-broker?create=false"/>
<property name="userName" value="${activemq.username}"/>
<property name="password" value="${activemq.password}"/>
</bean>
</property>
</bean>
</beans>
I added the jar file com.ibm.mq.allclient.jar into the lib folder of ActiveMQ. When running the program, I am getting this exception:
java.lang.ClassNotFoundException: javax.jms.JMSRuntimeException
It sounds like I am missing the websphere jar file? Is this correct?
I use both IBM MQ and ActiveMQ on a regular basis. This example below will show you some sample configuration options. Please make sure you configure these to your own use cases.
//ActiveMQ connection factory
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" destroy-method="doStop">
<property name="configuration">
<bean class="org.apache.camel.component.jms.JmsConfiguration">
<property name="concurrentConsumers" value="1" />
<property name="maxConcurrentConsumers" value="1" />
<property name="acceptMessagesWhileStopping" value="true" />
<property name="acknowledgementModeName" value="CLIENT_ACKNOWLEDGE" />
<property name="cacheLevelName" value="CACHE_CONSUMER" />
<property name="connectionFactory">
<bean class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop">
<property name="maxConnections" value="1" />
<property name="MaximumActiveSessionPerConnection" value="500" />
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${activemq1.brokerUrl}" />
<property name="userName" value="${activemq1.username}" />
<property name="password" value="${activemq1.password}" />
<property name="redeliveryPolicy">
<bean class="org.apache.activemq.RedeliveryPolicy">
<property name="maximumRedeliveries" value="-1" />
</bean>
</property>
</bean>
</property>
</bean>
</property>
</bean>
</property>
</bean>
//IBM MQ connection factory
<bean id="ibmmq" class="org.apache.camel.component.jms.JmsComponent" destroy-method="doStop">
<property name="concurrentConsumers" value="1" />
<property name="maxConcurrentConsumers" value="1" />
<property name="connectionFactory">
<bean class="org.springframework.jms.connection.SingleConnectionFactory" destroy-method="destroy">
<constructor-arg>
<bean class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="transportType" value="1" />
<property name="channel" value="${channel}" />
<property name="hostName" value="${hostname}" />
<property name="port" value="${port}" />
<property name="queueManager" value="${queueManager}" />
</bean>
</constructor-arg>
</bean>
</property>
</bean>
You can use jms component to consume the message from IBM MQ and use activemq component to post it to ActiveMQ
have a look at the following links
http://camel.apache.org/jms.html
http://camel.apache.org/activemq.html
Thanks,
Gibu
You can use users camel extra component https://github.com/camel-extra/camel-extra/blob/master/components/camel-wmq/README.md to get messages from wmq without using the JMS wrapping and then send messages to ActiveMQ with custom camel component https://camel.apache.org/components/latest/activemq-component.html. See also a similar topic Apache Camel with IBM MQ.

WSO2 ESB How we can split Property's value and log after split chars?

I want get char after spilt messagetext !
<api xmlns="http://ws.apache.org/ns/synapse" name="iterate" context="/tokenize">
<resource methods="GET" uri-template="/{temp}">
<inSequence>
<property name="MessageText" value="a,b,c," scope="default"></property>
<log>
<property name="MessageText" expression="get-property('MessageText')"></property>
</log>
[how ergodic this MessageText?]
<iterate continueParent="true" expression="MessageText" sequential="true">
<target>
<sequence>
<log level="full" separator=",">
<property name="arrayChar" value="?"></property>
</log>
</sequence>
</target>
</iterate>
</inSequence>
</resource>
</api>
I want result!
arrayChar = a
arrayChar = b
arrayChar = c
iterate mediator need a list of xml nodes so, create a new XML message with the list of values extracted from your property 'MessageText' (the purpose of script mediator) and then, iterate the nodes from this message with iterate mediator
<inSequence>
<property name="MessageText" value="a,b,c" scope="default"/>
<script language="js"><![CDATA[
var payloadXML = new XML(<root/>);
for each (var item in String(mc.getProperty("MessageText")).split(',')) {
payloadXML.appendChild(new XML(<item>{item}</item>));
}
mc.setPayloadXML(payloadXML);
]]></script>
<iterate xmlns:fn="http://www.w3.org/2005/xpath-functions" continueParent="true" expression="//item" sequential="true">
<target>
<sequence>
<log level="full" separator=",">
<property name="arrayChar" expression="$body/item"></property>
</log>
</sequence>
</target>
</iterate>
</inSequence>

BreezeJS unable to parse EDM metadata with more than one namespace?

I am writing a web application with Breeze, OData, WebAPI, and Angular (BOWA) using the integrated WebAPI ASP Identity schema. When breeze consumes the $metadata it is unable to finish and gives a 'null' error and fails. I disabled the ASP Identity to test and it works fine. Is Breeze unable to address more than a single namespace? The namespace is posted below. Are there any known work arounds to this? (OData v3)
<Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="Microsoft.AspNet.Identity.EntityFramework">
<ComplexType Name="IdentityUserRole">
<Property Name="UserId" Type="Edm.String"/>
<Property Name="RoleId" Type="Edm.String"/>
</ComplexType>
<EntityType Name="IdentityUserClaim">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false"/>
<Property Name="UserId" Type="Edm.String"/>
<Property Name="ClaimType" Type="Edm.String"/>
<Property Name="ClaimValue" Type="Edm.String"/>
</EntityType>
<ComplexType Name="IdentityUserLogin">
<Property Name="LoginProvider" Type="Edm.String"/>
<Property Name="ProviderKey" Type="Edm.String"/>
<Property Name="UserId" Type="Edm.String"/>
</ComplexType>
</Schema>
Take a look at the section "Correcting the namespace for EdmBuilder" on this page: http://www.getbreezenow.com/documentation/odata-server

Using JASYPT outside camel context

I am trying to decrypt properties using JASYPT in camel blueprint outside camel context like follows.
Is it not possible to use JASYPT + Property Component outside camel context?
I am using servciemix 5.0.0
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<cm:property-placeholder id="prop" persistent-id="mypersistent">
<!-- list some properties for this test -->
<cm:default-properties>
<cm:property name="cool.result" value="ENC(jU1ypXF709gpmOsJ2nKGgTbtd3oAs0n3rUNxEmMp2G8=)"/>
<cm:property name="jms.password" value="ENC(QhKlLI3eMKUhsUSPEWIRFw==)"/>
</cm:default-properties>
</cm:property-placeholder>
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
<property name="location" value="blueprint:prop"/>
<property name="propertiesParser" ref="jasypt"></property>
</bean>
<!-- define the jasypt properties parser with the given password to be used -->
<bean id="jasypt" class="org.apache.camel.component.jasypt.JasyptPropertiesParser">
<property name="password" value="secret"/>
</bean>
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616" />
<property name="userName" value="smx"/>
<property name="password" value="${jms.password}"/>
</bean>
</property>
</bean>
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<!-- define the camel properties placeholder, and let it leverage jasypt -->
<!--<propertyPlaceholder id="properties"
location="blueprint:prop"
propertiesParserRef="jasypt"/>-->
<route>
<from uri="file://inputfolder"/>
<log message="jms password {{jms.password}}"/>
<setBody><constant>Foo message</constant></setBody>
<to uri="jms:queue:default.inputchannel?jmsMessageType=Text&transferExchange=true"/>
<to uri="{{cool.result}}"/>
</route>
</camelContext>
It is giving me following error
ava.lang.SecurityException: User name [smx] or password is invalid.
at org.apache.activemq.security.JaasAuthenticationBroker.addConnection(JaasAuthenticationBroker.java:80)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.broker.MutableBrokerFilter.addConnection(MutableBrokerFilter.java:97)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.broker.TransportConnection.processAddConnection(TransportConnection.java:733)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.broker.jmx.ManagedTransportConnection.processAddConnection(ManagedTransportConnection.java:79)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.command.ConnectionInfo.visit(ConnectionInfo.java:139)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.broker.TransportConnection.service(TransportConnection.java:292)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.broker.TransportConnection$1.onCommand(TransportConnection.java:149)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.transport.MutexTransport.onCommand(MutexTransport.java:50)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.transport.WireFormatNegotiator.onCommand(WireFormatNegotiator.java:113)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.transport.AbstractInactivityMonitor.onCommand(AbstractInactivityMonitor.java:270)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.transport.TransportSupport.doConsume(TransportSupport.java:83)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:214)[84:org.apache.activemq.activemq-osgi:5.9.0]
at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:196)[84:org.apache.activemq.activemq-osgi:5.9.0]
at java.lang.Thread.run(Thread.java:745)[:1.7.0_60]
You cannot use org.apache.camel.component.jasypt.JasyptPropertiesParser outside Apache Camel, or the <camelContext>.
For <bean> then its the blueprint propertyplaceholder that is responsible for setting up the properties. So you need some way to hook in jasypt into osgi blueprint property placeholder, eg this stuff
<cm:property-placeholder id="prop" persistent-id="mypersistent">
Like Ibsen said, blueprint property placeholder needs to be looked into. Here is a working blueprint solving the above mentioned issue
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
xmlns:enc="http://karaf.apache.org/xmlns/jasypt/v1.0.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<ext:property-placeholder id="prop">
<!-- list some properties for this test -->
<ext:default-properties>
<ext:property name="cool.result" value="ENC(jU1ypXF709gpmOsJ2nKGgTbtd3oAs0n3rUNxEmMp2G8=)"/>
<ext:property name="jms.password" value="ENC(QhKlLI3eMKUhsUSPEWIRFw==)"/>
</ext:default-properties>
</ext:property-placeholder>
<enc:property-placeholder>
<enc:encryptor class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config">
<bean class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
<property name="algorithm" value="PBEWithMD5AndDES" />
<property name="passwordEnvName" value="CAMEL_ENCRYPTION_PASSWORD" />
</bean>
</property>
</enc:encryptor>
</enc:property-placeholder>
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
<property name="location" value="blueprint:prop"/>
<property name="propertiesParser" ref="jasypt"></property>
</bean>
<!-- define the jasypt properties parser with the given password to be used -->
<bean id="jasypt" class="org.apache.camel.component.jasypt.JasyptPropertiesParser">
<property name="password" value="secret"/>
</bean>
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616" />
<property name="userName" value="smx"/>
<property name="password" value="${jms.password}"/>
</bean>
</property>
</bean>
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<!-- define the camel properties placeholder, and let it leverage jasypt -->
<!--<propertyPlaceholder id="properties"
location="blueprint:prop"
propertiesParserRef="jasypt"/>-->
<route>
<from uri="file://inputfolder"/>
<log message="jms password {{jms.password}}"/>
<setBody><constant>Foo message</constant></setBody>
<to uri="jms:queue:default.inputchannel?jmsMessageType=Text&transferExchange=true"/>
<to uri="{{cool.result}}"/>
</route>
</camelContext>
</blueprint>

Resources