How to check for a duplicate value in Apigee BaaS - apigee

I have to post a email in Apigee baas. But, I have to validate if the email is already present in Baas. I have been trying using the below xml but did not work. I have included conditions to check if the email I provided is similar to the one already existing in Baas. how do i validate it?
<Flow name="RequestInvite">
<Description/>
<Request>
<Step>
<FaultRules/>
<Name>ExtractRequestInvite</Name>
</Step>
<Step>
<FaultRules/>
<Name>AssignIsUserExist</Name>
</Step>
<Step>
<FaultRules/>
<Name>JSEnvironmentConfig</Name>
</Step>
<Step>
<FaultRules/>
<Name>CallOutIsUserExists</Name>
</Step>
<Step>
<FaultRules/>
<Name>AssignRequestInvite</Name>
</Step>
<Step>
<FaultRules/>
<Name>CallOutRequestInvite</Name>
</Step>
<Step>
<FaultRules/>
<Name>ExtractRequestInviteResponse</Name>
</Step>
</Request>
<Response>
<Step>
<FaultRules/>
<Name>AssignInviteResponse</Name>
</Step>
</Response>
<Condition>(proxy.pathsuffix MatchesPath "/v1/requestinvite") and (request.verb = "POST")</Condition>
</Flow>

Name attribute in BaaS entities is unique. So, if you know that email will be your unique field, you could use name attribute to store the email BaaS along with any other required attributes. The next time that you submit a new entity to BaaS with the same email as the name, BaaS will reject the submission and leave the previous entity as is. You can catch that response and return a response to the client.

Related

Assign Message Policy does not work on Error responses

In Apigee, the Assign Message Policy only seems to work on successful responses. If the server responds with an error code, say, 403 Forbidden, the policy does not change the response. Is there any other way to modify error responses in Apigee.
This is my policy.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
<DisplayName>Assign Message-1</DisplayName>
<Properties/>
<Remove>
<Headers>
<Header name="Server"/>
</Headers>
</Remove>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>
You can modify error responses in Apigee using FaultRules. You can take a look at Apigee's documentation about Fault handling .
The reason your AssignMessage policy is not executed is, that the error happens before your policy is being executed, thus being skipped.
You can either move your AssignMessage policy to be executed earlier in the flow or catch the error using fault handling logic and throw the error when you have executed all the desired logic.
In case of different response message of Success and Error,
I usually create 2 policy of Assign Message, one for Success and other for Error response.
Example of Success:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-ReturnSuccessResponse">
<DisplayName>AM-ReturnSuccessResponse</DisplayName>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="response"/>
<Set>
<Payload contentType="application/json" variablePrefix="#" variableSuffix="#">
#resp.responseMessage#
</Payload>
<StatusCode>{resp.httpStatusCode}</StatusCode>
<ReasonPhrase>{resp.reasonPhrase}</ReasonPhrase>
</Set>
</AssignMessage>
Example of Error:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <AssignMessage async="false" continueOnError="false" enabled="true" name="AM-ReturnErrorResponse">
<DisplayName>AM-ReturnErrorResponse"</DisplayName>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="response"/>
<Properties/>
<Set>
<Payload contentType="application/json" variablePrefix="#" variableSuffix="#">
{
"error" : "#resp.error#",
"timeStamp" : "#resp.responseDateTime#"
}
</Payload>
<StatusCode>{resp.httpStatusCode}</StatusCode>
<ReasonPhrase>{resp.reasonPhrase}</ReasonPhrase>
</Set> </AssignMessage>
And then in your flow you have to set condition for routing the policy like this
<Flow name="test">
<Description/>
<Request/>
<Response>
<Step>
<Name>AM-ReturnSuccessResponse</Name>
<Condition>resp.httpStatusCode == 200</Condition>
</Step>
<Step>
<Name>AM-ReturnErrorResponse</Name>
<Condition>resp.httpStatusCode != "200"</Condition>
</Step>
</Response>
</Flow>
Remark: Because normally http status code for success is 200, So I have set the condition like code above. But this is for example, you can design the condition up to your scenario.

Mule CE 3.6.1 HTTP Request url encoding issue

I have a Mule flow which uses HTTP Request endpoint in Mule CE3.6.1. It needs to use encoded HTTP url to get the correct data.
Please note the encoded url: /items?q=%2Blabel%3ABOTV2309H*%20%2Bparent.uri%3A%5C%2Fbristol%5C%2Ftest%5C%2Fgateway%5C%2F*&rows=100&start=0
Here is the test flow.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.6.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8089" basePath="/" usePersistentConnections="false" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Config_Search" host="testserver" port="80" basePath="/index" usePersistentConnections="false" doc:name="HTTP Request Configuration"/>
<flow name="testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="*" allowedMethods="POST" doc:name="HTTP"/>
<object-to-string-transformer doc:name="Object to String"/>
<logger level="INFO" doc:name="Logger"/>
<http:request config-ref="HTTP_Request_Config_Search" path="/items?q=%2Blabel%3ABOTV2309H*%20%2Bparent.uri%3A%5C%2Fbristol%5C%2Ftest%5C%2Fgateway%5C%2F*&rows=100&start=0" method="GET" doc:name="HTTP"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
When the Mule flow runs, here is the url being requested (Found through wireshark):
/items?q=%20label%3ABOTV2309H%2A%20%20parent.uri%3A%5C%2Fbristol%5C%2Ftest%5C%2Fgateway%5C%2F%2A&rows=100&start=0
Please note %2B is replaced with %20 in two places, which obviously do not return the data I need.
Is this an issue with HTTP endpoint?
Thanks!
I divided the params into query and uri params. Then this issue got resolved.
<http:request config-ref="HTTP_Request_Config_Search" path="/items" method="GET" doc:name="HTTP">
<http:request-builder>
<http:query-param paramName="q" value="+label:BOTV2309H* +parent.uri:\/test\/gateway\/*"/>
<http:uri-param paramName="rows" value="100"/>
<http:uri-param paramName="start" value="0"/>
</http:request-builder>
</http:request>

How to pass hash map object to http outbound in mule?

I need to pass hash map object to Http outbound using post method in mule.
How can it be done?
Hope this can help
<flow name="demoFlow1" doc:name="demoFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="hi" doc:name="HTTP"/>
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy">
<scripting:text><![CDATA[
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "Eddu");
map.put("lastname", "Melendez");
return map;
]]></scripting:text>
</scripting:script>
</scripting:component>
<http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="print" method="POST" doc:name="HTTP"/>
</flow>
<flow name="demoFlow2" doc:name="demoFlow2">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="print" doc:name="HTTP"/>
<logger level="INFO" message="#[message.payloadAs(java.lang.String)]" doc:name="Logger"/>
</flow>
This will be the output if you send a map through the service:
INFO 2015-01-22 08:30:42,298 [[demo].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: name=Eddu&lastname=Melendez
You can also add the line below, in order to get a json instead query params in http
As you can see the response will be different
INFO 2015-01-22 08:32:31,014 [[demo].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: {"name":"Eddu","lastname":"Melendez"}

Mule ESB HTTP POST Request with URL-Form-Encoded Body

I'm new in Mule ESB and trying to send a post request to a REST Service. The body of the request is in String form and the request should be x-www-form-urlencoded. I set the payload to my parameters and send the request to the REST Service but I get an exception:
Failed to invoke REST service "http://username:password#192.168.10.252/api/rest/session/login.json". Message payload is of type: String
My Rest URL is: (#[restURL]) ${dms.host}/api/rest/session/login.json
My parameter is (set as payload #[restBody]): username=user&password=pass
<sub-flow name="RESTrequestToDMS" doc:name="RESTrequestToDMS" processingStrategy="synchronous">
<set-variable variableName="originalMessage" value="#[payload]" doc:name="Backup original message"/>
<logger message="#[restBody]" level="INFO" doc:name="Logger"/>
<choice doc:name="Choice">
<when expression="restHTTPmethod == 'POST'">
<processor-chain>
<set-payload value="#[restBody]" doc:name="Set Payload"/>
<http:rest-service-component httpMethod="POST" serviceUrl="http://${dms.user}:${dms.pass}##[restURL]"></http:rest-service-component>
</processor-chain>
</when>
<when expression="restHTTPmethod == 'GET'">
<processor-chain>
<http:rest-service-component httpMethod="GET" serviceUrl="http://${dms.user}:${dms.pass}##[restURL]?#[restBody]"></http:rest-service-component>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<logger message="Unknown http method type is provided! " level="ERROR" doc:name="Logger"/>
</processor-chain>
</otherwise>
</choice>
<json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
<echo-component doc:name="Echo"/>
<set-variable variableName="RESTResponse" value="#[payload]" doc:name="set RESTResponse"/>
<set-payload value="#[flowVars['originalMessage']]" doc:name="Restore Original Message"/>
</sub-flow>
Can somebody help me figure this out please?
Thanks a lot in advance.
Note: Updated according to David's reply.
-------------------UPDATE--------------------
So now, for testing purposes, I have modified the flow like this:
<set-payload value="#[restBody]" doc:name="Set Payload"/>
<custom-transformer class="de.mfg.osii.http.StringToURIParametersMapTransformer" doc:name="String To URI Parameters"/>
<http:rest-service-component httpMethod="POST" serviceUrl="http://${dms.user}:${dms.pass}##[restURL]">
<http:requiredParameter key="username" value="#[payload.get('username')]" />
<http:requiredParameter key="password" value="#[payload.get('password')]" />
</http:rest-service-component>
So now the payload is: {username=user, password=pass}
But now when I try to run this, I get the following error:
1. Failed to invoke REST service "http://user:pass#192.168.10.252/api/rest/session/login.json". Message payload is of type: LinkedHashMap (org.mule.transport.http.components.RestServiceException)
Update: The above problem, the exception, has just been solved by adding a message-property "Content-Type" with the value "application/x-www-form-urlencoded".
Still the last part, below, remains a mystery for me :)
The other thing is, as I stated in the comment to David's answer, can I add the required parameters for the rest component in an adaptive way? To be more clearer, I do not want to create different flows for every different request. I have the parameter map in the payload, so I want to iterate over them and put them in the required parameters. Is something like this possible?
Thanks a lot in advance!
-------------------UPDATE--------------------
So, I've modified my flow and used David's suggestions. However, now, I'm getting an HTTP 405 error (Method not allowed). But I'm sure I'm posting to the correct address with correct content-type. What is the reason for this? Does anybody have an idea?
My payload coming into this flow is a string (url encoded parameters) such as ->
sessionId=9eub9gm7k7oc1ub81dhef6t46q&xml=%3CObjectList%3E%3CFolderObject%3E%3CAddToFolder%20RefType%3D%22Path%22%20ClassName%3D%22FolderObject%22%3E%3C!%5BCDATA%5B%2FfolderPath%5D%5D%3E%3C%2FAddToFolder%3E%3CName%3E%3C!%5BCDATA%5Baaa%5D%5D%3E%3C%2FName%3E%3CDescription%3E%3C!%5BCDATA%5BRequests%20from%20user%20aaa%20are%20stored%20in%20this%20folder%5D%5D%3E%3C%2FDescription%3E%3CNoErrorIfExist%2F%3E%3C%2FFolderObject%3E%3C%2FObjectList%3E
<flow name="RESTrequestToDMS" doc:name="RESTrequestToDMS" processingStrategy="synchronous">
<set-variable variableName="originalMessage" value="#[payload]" doc:name="Backup original message"/>
<choice doc:name="Choice">
<when expression="restHTTPmethod == 'POST'">
<processor-chain>
<set-payload value="#[restBody]" doc:name="Set Payload"/>
<logger message="The payload before http post is: #[message.payload]" level="INFO" doc:name="Logger"/>
<http:outbound-endpoint exchange-pattern="request-response" doc:name="HTTP" address="http://#[restURL]" user="${dms.user}" password="${dms.pass}" contentType="application/x-www-form-urlencoded">
</http:outbound-endpoint>
</processor-chain>
</when>
<when expression="restHTTPmethod == 'GET'">
<processor-chain>
<http:outbound-endpoint method="GET" exchange-pattern="request-response" doc:name="HTTP" address="http://#[restURL]?#[restBody]" user="${dms.user}" password="${dms.pass}" contentType="application/x-www-form-urlencoded">
</http:outbound-endpoint>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<logger message="Unknown http method type is provided! " level="ERROR" doc:name="Logger"/>
</processor-chain>
</otherwise>
</choice>
<echo-component doc:name="Echo"/>
<choice doc:name="Choice">
<when expression="payload != null && payload != empty && payload != ''">
<processor-chain>
<json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.Map"/>
<set-variable variableName="RESTResponse" value="#[payload]" doc:name="set RESTResponse"/>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<logger message="The payload is null or empty! HTTP response code is: #[message.inboundProperties['http.status']]" level="INFO" doc:name="Logger"/>
<set-variable variableName="RESTResponse" value="#[message.inboundProperties['http.status']]" doc:name="Set HTTP Status Code"/>
</processor-chain>
</otherwise>
</choice>
<set-payload value="#[flowVars['originalMessage']]" doc:name="Restore Original Message"/>
</flow>
Here is how you pass parameters to be URL encoded with the rest-service-component:
<http:rest-service-component httpMethod="POST"
serviceUrl="http://${dms.user}:${dms.pass}##[restURL]">
<http:requiredParameter key="username" value="user" />
<http:requiredParameter key="password" value="pass" />
</http:rest-service-component>

Mule - choice component issue

When I run my flow, I get the following error `
<org.apache.cxf.staxutils.DepthXMLStreamReader>
<reader class="org.mule.module.cxf.support.StreamClosingInterceptor$1">
<reader class="com.ctc.wstx.sr.ValidatingStreamReader">
<mXml11>false</mXml11>
<mInputBuffer>xmlns:ns2="http://wsdouane/"><return><douanePK><idConteneurId>ctr1</idConteneurId><idCritereId>C11</idCritereId></douanePK><valeurId>oui</valeurId></return></ns2:findResponse></S:Body></S:Envelope>.................
we can see the correct soap response starting from the <mInputBuffer>, is there a way to get only the soap response??
here is my flow
<flow name="SOAPWebService" doc:name="SOAPWebService">
<http:inbound-endpoint exchange-pattern="request-response" address="http://localhost:8088/esb" doc:name="HTTP"/>
<object-to-string-transformer doc:name="Object to String"/>
<choice doc:name="Choice">
<when expression="#[payload.contains('C22')]">
<set-variable variableName="paramCtr" value="#[message.inboundProperties['ctr']]" doc:name="conteneur"/>
<set-variable variableName="paramC" value="#[message.inboundProperties['c']]" doc:name="critere"/>
<component class="com.example.components.SampleComponent" doc:name="Java"/>
<mulexml:xslt-transformer maxIdleTransformers="2" maxActiveTransformers="5" xsl-file="C:\MuleStudio\SandBox\resources\PrepareRequestXMLPort.xsl" doc:name="XSLT">
<mulexml:context-property key="paramCtr" value="#[flowVars['paramCtr']]" />
<mulexml:context-property key="paramC" value="#[flowVars['paramC']]" />
</mulexml:xslt-transformer>
<cxf:proxy-client payload="body" enableMuleSoapHeaders="true" doc:name="SOAP"/>
<http:outbound-endpoint exchange-pattern="request-response" address="http://localhost:8080/ClientsDB/port" doc:name="PortWS"/>
<byte-array-to-string-transformer doc:name="Byte Array to String" />
</when>
<otherwise>
<set-variable variableName="paramCtr" value="#[message.inboundProperties['ctr']]" doc:name="conteneur"/>
<set-variable variableName="paramC" value="#[message.inboundProperties['c']]" doc:name="critere"/>
<component class="com.example.components.SampleComponent" doc:name="Java"/>
<mulexml:xslt-transformer maxIdleTransformers="2" maxActiveTransformers="5" xsl-file="C:\MuleStudio\SandBox\resources\PrepareRequestXMLDouane.xsl" doc:name="XSLT">
<mulexml:context-property key="paramCtr" value="#[flowVars['paramCtr']]" />
<mulexml:context-property key="paramC" value="#[flowVars['paramC']]" />
</mulexml:xslt-transformer>
<cxf:proxy-client payload="body" enableMuleSoapHeaders="true" doc:name="SOAP"/>
<http:outbound-endpoint exchange-pattern="request-response" address="http://localhost:8080/ClientsDB/douane" doc:name="DouaneWS"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
</otherwise>
</choice>
<xm:object-to-xml-transformer doc:name="Object to XML"/>
<file:outbound-endpoint path="C:\MuleStudio\SandBox\output" outputPattern="#[function:datestamp:dd-MM-yy]_#[function:systime].xml " responseTimeout="10000" doc:name="Outgoing File"/>
</flow>
thank you.
This expression #[payload.contains('c22')] can't work because the payload is an InputStream. Aren't you seeing stack traces in Mule's logs?
In any case, try adding <object-to-string-transformer /> before the choice and see if it fixes the issue.
EDIT:
The problem is that you're using XStream (in xm:object-to-xml-transformer) to serialize the CXF response object (org.apache.cxf.staxutils.DepthXMLStreamReader) into XML. The CXF response should not be messed with and should be handled by the cxf:proxy-client in the response phase of the flow. The xm:object-to-xml-transformer and file:outbound-endpoint after the choice router are probably disturbing this mechanism. Try to wrap them in a response element above the choice router to they execute after it in the response phase.
Note that I have already given you this advice in your other question https://stackoverflow.com/a/16615537/387927 but you did not react to it.
Also I don't think byte-array-to-string-transformer does anything: the message payload after the http:outbound-endpoints should be org.apache.cxf.staxutils.DepthXMLStreamReader, preventing this transformer to fire.

Resources