How to read request parameter values in wso2 esb script mediator - wso2

I need to catch the url parameters parsing in a get request by script mediator. I uses WSO2 esb 4.8.1. and I tried with the following js code. but it gives an error saying window is not defined.
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
var requestNew="<m:ViW xmlns:m=\"wom\">
<m:request>
<RequestHeader>
<RemoteIP>dummyIp</RemoteIP>
<AppName>dummyAppName</AppName>
<AppPassword>dummyPassword</AppPassword>
<UserName>dummyUserName</UserName>
</RequestHeader>
<OrderId>23</OrderId>
<AccountNo>23</AccountNo>
<Cir>23</Cir>
<DocketNo>23</DocketNo>
<Status>23</Status>
<RequestType>23</RequestType>
<RequestedFrom>2016-04-19T22:47:37.000Z</RequestedFrom>";
requestNew=requestNew+"
<RequestedTo>"+getUrlVars()['RequestedTo']+"</RequestedTo>
</m:request>
</m:ViW>";
mc.setPayloadXML(new XML(requestNew));
So how can I access url parameter values in the wso2 esb script mediator?

Following blog will tell you how to do it. Its written targetting WSO2 API Cloud. But since synapse is used in both ESB and API Manager, its valid for your question as well. In summary,
To retrieve path parameters
var id = mc.getProperty('uri.var.id');
To retrieve query parameters
var mask = mc.getProperty('query.param.mask');

Related

C# Webclient Post to AWS API Gateway Endpoint

I am having some problems attempting to post to an API gateway endpoint.
On my API gateway I have my gateway all set up, and tested via the tool and am getting results and can verify that the step function is in fact executing the request appropriately.
{
"executionArn": "arn:aws:states:us-east-2:xxxxxxxxxxxx:execution:DevStateMachine-XXXXXXXXXXX:c9047982-e7f8-4b72-98d3-281db0eb4c30",
"startDate": 1531170720.489
}
I have set up a Stage for this for my dev environment and all looks good there as well. where I am given a URL to post against.
https://xxxxxxxxxx.execute-api.us-east-2.amazonaws.com/dev/assignments
In my c# code I have the web client defined as follows:
public Guid QueueAssignment(AssignmentDTO assignment)
{
using (var client = new HttpClient())
{
var data = JsonConvert.SerializeObject(assignment);
var content = new StringContent(data);
var uri = "https://xxxxxxxxxx.execute-api.us-east-2.amazonaws.com/dev/assignments"
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = client.PostAsync(uri, content).Result;
if (response.IsSuccessStatusCode)
{
_logger.Info("Successfully posted to AWS Step Function");
_logger.Info(response);
}
else
_logger.Error("Error posting to AWS Step Function");
_logger.Error(response);
}
}
Everytime this post is attempted I get the following error:
System.Net.WebException: The remote name could not be resolved: 'https://xxxxxxxxxx.execute-api.us-east-2.amazonaws.com'
Is there something I am missing in posting to this URI or some type of conversion I need to do? Im kind of at a loss on where to go on this on.

Error when accessing ESB Proxy with Jaggery WSStub

I created a web service and was able to send requests to it from a serverside Jaggery.js script with no problem. Then I created a WSDL Proxy Service inside WSO2 ESB and tested it using the "Try it!" feature.
After I redirected my serverside script from the original web service to its proxy inside ESB, I got the error in System Logs:
The endpoint reference (EPR) for the Operation not found is /services/BpmAdderProcessProxy.BpmAdderProcessProxyHttpSoap11Endpoint and the WSA Action = urn:anonOutInOpResponse. If this EPR was previously reachable, please contact the server administrator.
To see in detail what was happening I activated the "SOAP Message Tracer" of the ESB. Suddenly my serverside script could access the webservice via my ESB proxy. Then I deactivated the "SOAP Message Tracer" and the error message was back again. Is my serverside script correct? Or does the debugging tool modify behavior of debugged code?
I'm a JavaScript developer. Actually Jaggery and UES are targeted at people like me. I'm not supposed to look inside Java code, am I? Is there a forum where JavaScript developers discuss WSO2 UES and Jaggery?
My serverside code is as follows:
<%
var x = request.getParameter("x");
var y = request.getParameter("y");
//var sum = parseInt(x) + parseInt(y);
var sum = add(parseInt(x), parseInt(y));
response.content = {
success: true,
data: {
result: sum
}
};
function add(x, y) {
var ws = require('ws');
var stub = new ws.WSStub("http://02-128:8280/services/BpmAdderProcessProxy?wsdl");
var process = stub.services["BpmAdderProcessProxy"].operations["process"];
var payloadTemplate = process.payloadXML();
var payload = replaceQuestionMarks(payloadTemplate, arguments);
var resultXml = process.request(payload);
var resultValue = resultXml.children().text();
return parseInt(resultValue);
}
function replaceQuestionMarks(template, values) {
var i = 0;
return template.replace(
/\?/g,
function() {
return values[i++];
}
);
}
%>
In ESB v4.8.1, pass-through transport is enabled by default and it does not support SOAP body based dispatching (it does not build the message so it can't acces the body's first element to find the operation)
You can append the operation name to the endpoint url : http://host:8280/services/BpmAdderProcessProxy/OperationName
You can add this parameter in your proxy conf (BpmAdderProcessProxy) in WSO2 ESB : <parameter name="disableOperationValidation" locked="false">true</parameter>
You can edit wso2esb/repository/conf/axis2/axis2.xml and replace <handler class="org.apache.axis2.dispatchers.SOAPMessageBodyBasedDispatcher" name="SOAPMessageBodyBasedDispatcher"/>
with
<handler class="org.apache.synapse.core.axis2.SynapseSOAPMessageBodyBasedDispatcher" name="SOAPMessageBodyBasedDispatcher"/>

Trouble with SOAP request from Flex

SUM: I ended up having to form the XML manually. I also had to create an Operation and use its send(); method rather than just doing something like WebService.MyServiceFunction(); - not sure why that was the case.
I send off the request as follows:
var xm:XML =
<SetPropertiesForCurrentUser xmlns="http://asp.net/ApplicationServices/v200">
<values xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:KeyValueOfstringanyType>
<d4p1:Key>{obj.Key}</d4p1:Key>
<d4p1:Value xmlns:d6p1="http://www.w3.org/2001/XMLSchema" i:type="d6p1:string">{obj.Value}</d4p1:Value>
</d4p1:KeyValueOfstringanyType>
</values>
</SetPropertiesForCurrentUser>;
var profileService:WebService = new WebService();
profileService.useProxy = false;
profileService.loadWSDL(url);
var o:Operation = profileService.SetPropertiesForCurrentUser;
o.send(xm);
Here’s my scenario:
I have ASP.NET web services to handle authentication, user roles, and user profiles (default ASP.NET AuthenticationService, RoleService, and ProfileService, to be exact).
So from my Flex web app, I am able to successfully call the ASP.NET service. For example, something like this works fine:
var profileService:WebService = new WebService();
profileService.useProxy = false;
profileService.GetAllPropertiesForCurrentUser.addEventListener("result",getAllPropertiesForCurrentUser_EventHandler);
profileService.addEventListener("fault",getAllPropertiesForCurrentUserFault_EventHandler);
profileService.loadWSDL(url);
profileService.GetAllPropertiesForCurrentUser();
I run into trouble when I need to pass a Dictionary object to another function on the service (SetPropertiesForCurrentUser). The .NET service asks for this type of value:
System.Collections.Generic.IDictionary(Of String, Object)
Here are the two pertinent entries from the web.config entry from my ASP.NET service:
<properties>
<clear/>
<add name="coordinateFormat" />
</properties>
...
<profileService enabled="true"
readAccessProperties="coordinateFormat"
writeAccessProperties="coordinateFormat"/>
So after putting together a SOAP request from a Silverlight app (which works as expected) I’ve narrowed it down to a difference in the XML request sent to the SOAP handler:
From Flex:
<tns:Value>DMS</tns:Value>
From Silverlight:
<d4p1:Value xmlns:d6p1="http://www.w3.org/2001/XMLSchema" i:type="d6p1:string">DMS</d4p1:Value>
If I take the request generated by Flex, catch it with Fiddler, modify that one line to include the “type” namespace – it works.
Anyone have an idea how I can get that namespace onto my variable that is passed to the SOAP handler from Actionscript? Here is my code for sending off that SetPropertiesForCurrentUser function:
var obj:Object = {};
obj["Key"] = "coordinateFormat";
obj["Value"] = DMS;
var profileService:WebService = new WebService();
profileService.useProxy = false;
profileService.SetPropertiesForCurrentUser.addEventListener("result",setPropertiesForCurrentUser_EventHandler);
profileService.addEventListener("fault",setPropertiesForCurrentUserFault_EventHandler);
profileService.loadWSDL(url);
profileService.SetPropertiesForCurrentUser(new ArrayCollection([obj]),false);
Thanks,
Josh
The default SOAPEncoder that is used is some what limited in its capabilities (like not including the type attribute you mentioned above). Luckily, there is a way to control that by writing your own encoder.
see this link at adobe (read part about using custom web service serialization) Link on Adobe's Site

WSO2 ESB Proxy stop replacing %26 with &

I need to send these parameters to domain
domain/page?param1=xxx&param2=yyy%26zzz
I am using proxy in wso2 for domain
localhost:8280/services/proxyfordomain/page?param1=xxx&param2=yyy%26zzz
Endpoint of proxyfordomain is domain
Proxy is replacing %26 with &
Actual URL to be logged in console is:
To domain/page?param1=xxx&param2=yyy%26zzz
But URL logged in console is :
To domain/page?param1=xxx&param2=yyy&zzz
Here param2 will take yyy%26zzz format values
but not yyy&zzz
How to stop WSO2 from replacing?
Thanks for spending your valuable time
You can use the following script mediator configuration to replace 'yyy&zzz' with 'yyy%26zzz'.
<script language="js">var url = mc.getTo().toString();
var newURL = url.replace("yyy&zzz","yyy%26zzz");
mc.setTo(newURL);</script>

WSO2 Gadget cannot access SOAP payload

I have created a data service, which tests out correctly via TryIt and SoapUI. However, when I try to include it into a Gadget, I always get this error:
"An error occurred while relaying a SOAP payload, to end point
https://data.stratoslive.wso2.com/services/t/inova8.com/ProductVendorDataService.SOAP11Endpoint/"
The gadget includes this fragment: function doSOAPCall(){ var endpoint
= "https://data.stratoslive.wso2.com/services/t/inova8.com/ProductVendorDataService.SOAP11Endpoint/"; var payload = ""; var operation = "urn:getproduct";
document.getElementById("response-disp").innerHTML =
wso2.io.makeSOAPRequest(endpoint, operation, payload); }
The data service is based on the example http://wso2.org/library/tutorials/2011/11/expose-your-cloud-data-as-rdf-data-model. Note that the operation needs no parameters, but I have tried every variant of a payload without success.
I tried your steps and found that there are two issues with your gadget code segment to do a SOAP call.
First one is,since the operation you are accessing from data service end point,do not need any payload to pass to it.Such that inside the gadget xml you have to set payload as 'null'[NOT payload=""].
Second issue is that,your defined operation name in the gadget xml is incorrect.Once I tried your data-service by try-it option,I found that your accessing operation name is "_getProduct" and it's not "getProduct".
Once corrected above two issues,SOAP gadget works well with your end point and able to get response in to gadget from your accessing operation.The corrected code segment for doSOAPCall() function is as below.
function doSOAPCall(){ var endpoint = "https://data.stratoslive.wso2.com/services/t/inova8.com/ProductVendorDataService.SOAP11Endpoint/";
var payload = null;
var operation = "urn:_getproduct";
document.getElementById("response-disp").innerHTML = wso2.io.makeSOAPRequest(endpoint, operation, payload); }
Thanks;
Lalaji