Perl SOAP::Lite server basics - perl

I'm trying to write a soap server with SOAP::Lite to work with existing soap clients (specifically tr-069 dsl modems), but it's unclear how incoming xml triggers calls. The soap lite mail list moderator is being slow about adding me to their list, so I thought I'd see if anyone here can help point me in the right direction...
For starters, I'm getting:
<faultstring>Unrecognized header has mustUnderstand attribute set to 'true'</faultstring>
which I suspect means that I haven't defined a function to handle something that it wants handled. The handler code (based on the example in the SOAP::Server man page):
SOAP::Transport::HTTP::CGI
->dispatch_to('PeakACS')
->handle;
BEGIN {
package PeakACS;
use vars qw(#ISA);
#ISA = qw(Exporter SOAP::Server::Parameters);
use SOAP::Lite;
my $debugging = 1;
my $console = 0;
my $prog_id = 'peakacs';
my $log = DebugLog->new($prog_id, $debugging, $console);
$log->debug_msg('info', 'handle', '%s', 'handler setup');
sub ID {
$log->debug_msg('info', 'id', '%s', 'got an id');
}
sub Header {
$log->debug_msg('info', 'header', '%s', 'heading');
}
sub Inform {
$log->debug_msg('info', 'inform', '%s', 'informing');
}
}
If I understand SOAP right (which is far from a given), the tag should translate into a call to Inform - the top of the xml request looks like:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cwmp="urn:dslforum-org:cwmp-1-0">
<SOAP-ENV:Header>
<cwmp:ID SOAP-ENV:mustUnderstand="1">1539095918</cwmp:ID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<cwmp:Inform>
<DeviceId>

If you want to set mustUnderstand attribute to some entity in Header, do this:
$sheader = SOAP::Header->name('someEntity');
$sheader->mustUnderstand(1);
And when calling a method:
$soap->someMethod($sheader,#request);
Which produces:
<soap:Header>
<someEntity soap:mustUnderstand="1" />
</soap:Header>

Related

Call soap web-service in mobilefirst hybrid app

I'm attempting to call SOAP Web-Service in hybrid app. How should I form SOAP message correctly if the back-end service displays the next error in log:
Caused by: com.ibm.websphere.security.WSSecurityException: Exception
org.apache.axis2.AxisFault: CWWSS7509W: The received SOAP request
message is rejected becasue it does not correctly specify SOAP action
and WS-Addressing action while there is at least one PolicySet
attachment at operation level of the
TestServiceService.TestServicePort service. ocurred while running
action:
com.ibm.ws.wssecurity.handler.WSSecurityConsumerHandler$1#9b5addf6 at
com.ibm.ws.security.context.ContextImpl.runWith(ContextImpl.java:394)
at
com.ibm.ws.wssecurity.platform.websphere.auth.WSSContextImpl.runWith(WSSContextImpl.java:65)
... 35 more
This is content of js file in adapter
function getToken(){
var token = WL.Server.getActiveUser().attributes.LtpaToken;
var fulltoken = "LtpaToken2=" + token;
return fulltoken;
}
function callService(){
WL.Logger.warn("INSIDE callService "+getToken());
var path="checkauth/TestServiceService";
var request=
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:q0="http://provider.ws/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<q0:callService />
</soapenv:Body>
</soapenv:Envelope>;
var input = {
method : 'post',
returnedContentType : 'xml',
path : path,
body: {
content: request.toString(),
contentType: 'text/xml; charset=utf-8',
},
headers: {"Cookie": getToken()}
};
var result= WL.Server.invokeHttp(input);
return result;
}
This is SOAP Envelope which was displayed via TCP/IP Monitor:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<s:Security xmlns:s="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:w2="http://www.ibm.com/websphere/appserver/tokentype" soapenv:mustUnderstand="1">
<u:Timestamp>
<u:Created>2015-08-10T13:18:56.644Z</u:Created>
</u:Timestamp>
<s:BinarySecurityToken ValueType="w2:LTPAv2" u:Id="ltpa_20">dt8G5gZ9PpZ/Ea5oXr6EQd8dpmfXKiqeXiShPlpSWntK59hUzyoDNX9TKq1nFLfxUEJyJdjMxoG7EVxw8Q1zhyZdYhTXnsMkNVqScvSsPpX7ln/ad+/WAHqaaFymD8XtVEsjOlezQDarPaUmnKAQRUSrLkRnL5B1MoCclTe129Oojg8o+hACgDKjuvPnvL8jaf45wNiou6Il5ZOayBcoHpNehI7i2hADa4fTKzX/T69OPnsZOyWYrNosdezNd24b61vs85k2YK26rLTp5dkEp8f3mwKZBwOOK4z1wQdiAXJf6kQvzR22SfFitbJA5MStlBcovHAvB5T+J5Ip80/kI5BPa2ogoufd9HZAdKTNII8cHpHBN2Ub/+atzg1L7EhIWuzO1BPI62KoU/hPqAHn3uGCGrbIILesKx0TPvlgmU4Bg54H9prC0I8hgXbO1HLuz4M5DNE5ASFbH0W3LJ/UU7BGXJs6iJmfAfJtQ+ip5ZFHlLItZA+ca2LkVWmyD/xKVxyxHE1uDz8zV/CfV9Km0T+8FTA0Cfi/PIb5KiAagdrmqtw6GuJDbSCsC3sdh21G/cA3Y0p/f+rhDw8m/e17y1cEuq9HOBharwn7ET3wO30V4D4rGoLhd4QsN6X1z89gZmZVaI6J9urpPAEiSndmyQ==</s:BinarySecurityToken>
</s:Security>
<wsa:To>http://X.X.X.X:9082/checkauth/TestServiceService</wsa:To>
<wsa:MessageID>urn:uuid:5d1f8656-5550-40d2-9f39-c58f57279489</wsa:MessageID>
<wsa:Action>http://provider.ws/TestServiceDelegate/callServiceRequest</wsa:Action>
</soapenv:Header>
<soapenv:Body>
<ns2:callService xmlns:ns2="http://provider.ws/"/>
</soapenv:Body></soapenv:Envelope>
The body consists of a single line, and this makes the scenario a peculiar one, as well as raises a question is this is meant to work at all.
I can suggest two things:
You can attempt to parse your WSDL file with the SOAPUI application; it should show you how the SOAP envelope is supposed to look like
Use the Service Discovery feature in MobileFirst Studio that can generate the adapter for you with a ready SOAP envelope. Read more how to use this feature, here: http://www-01.ibm.com/support/knowledgecenter/SSHS8R_7.0.0/com.ibm.worklight.dev.doc/dev/c_using_service_discovery_wizard_to_explore_backend-services.html

Mantis Bug Tracker SOAP API Response XML Error

I am using Mantis Bug Tracker SOAP API, but unfortunately every time it returns to me message like
"looks like we got no XML document",
after tracing the last response I got following message
"<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode xsi:type="xsd:string">Client</faultcode><faultactor xsi:type="xsd:string"></faultactor><faultstring xsi:type="xsd:string">Issue does not exist.</faultstring><detail xsi:type="xsd:string"></detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>"
I hope that, I am getting xml respose back, it appears that there is a addition of "" characters in the beginning.
Any clue or help would be great, in removing those characters.
The code for connecting to MANTIS SOAP API SERVER
<?php
$c = new \SoapClient("http://dev06/api/soap/mantisconnect.php?wsdl", array('trace'=> true, 'encoding'=>' UTF-8', 'soap_version'=>1.2));
$username = "xxxxx";
$password = "xxxxx";
try {
$c->mc_issue_get(trim($username), trim($password), 2331);
} catch (SoapFault $exception) {
var_dump($c->__getLastResponse());
}
?>
I don't see any issue with your code and it works perfectly in my environment with slight modifications:
$c = new \SoapClient("http://localhost/demo/mantisbt-1.2.15/api/soap/mantisconnect.php?wsdl", array('trace'=> true, 'encoding'=>' UTF-8', 'soap_version'=>SOAP_1_2));
$username = "XXXXXXXX";
$password = "XXXX";
try {
$issue = $c->mc_issue_get(trim($username), trim($password), 31);
var_dump($issue);
} catch (SoapFault $exception) {
var_dump($c->__getLastResponse());
}
It could be the soap_version, so may be you could try with soap_version=SOAP_1_1
Oh...!
Finally got the solution for it. Its very simple.
Firstly mantis SOAP API code base contains may be more than 20,000 lines of code. I think there is some where some one is printing some BOM characters.
So best solution would be, just use following function,
ob_clean();
This function must be used in
/library/nusoap/nusoap.php
Because this file has
send_response()
That printouts payload, So just use ob_clean() at the beginning of the send_response() function.
Thanks and hope it will help others.

Authentication Issue with eBatNS Trading API trying to get SessionID

I'm trying to get a session ID using the eBatNS SOAP API.
The function which makes the call is pretty simple, but always returns an Authentication error. You can see the function below
public function get_ebay_session()
{
error_reporting(0);
$ruName = "Forward_Thinker-ForwardT-57fe-4-rybapdi";
$sessionID = "";
//Connect to eBay and get a list of all products
require_once 'eBay/EbatNs/EbatNs_ServiceProxy.php';
require_once 'eBay/EbatNs/GetSessionIDRequestType.php';
require_once 'eBay/EbatNs/EbatNs_Logger.php';
//Get a SessionID
$session = new EbatNs_Session('eBay/config/ebay.config.php');
print_r($session);
echo "<hr>";
$cs = new EbatNs_ServiceProxy($session);
$cs->attachLogger(new EbatNs_Logger(false, 'stdout', true, false));
print_r($cs);
echo "<hr>";
$req = new GetSessionIDRequestType();
$req->setRuName($ruName);
print_r($req);
echo "<hr>";
$result = $cs->GetSessionID($req);
$sessionID = $result->SessionID;
print_r($result);
echo "<hr>";
session_start();
$_SESSION['eBaySessionID'] = $sessionID;
$return = $ruName."\n".$sessionID;
$this->set(compact('return'));
}
As you can see I have attached a logger. The logger shows that this is the request being made.
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns="urn:ebay:apis:eBLBaseComponents" ><soap:Header><RequesterCredentials><eBayAuthToken>AgAAAA**AQAAAA**aAAAAA**wS7oUQ**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6wHloSkD5aGog+dj6x9nY+seQ**It4BAA**AAMAAA**CB7yrHTyG3kQbA6KOwf0ZO2MqyPs/Dfn5u5r8ZDVGeWNvB</eBayAuthToken><Credentials><AppId>ForwardT-57fe-41ea-b90e-52fd0b541b88</AppId><DevId>5eefba38-e226-4876-9ada-d8743f571aeb</DevId><AuthCert>b57984cb-ba9c-430c-a8fc-c08f9ac46e75</AuthCert></Credentials></RequesterCredentials></soap:Header><soap:Body><GetSessionIDRequest><Version><![CDATA[815]]></Version><RuName><![CDATA[Forward_Thinker-ForwardT-57fe-4-rybapdi]]></RuName></GetSessionIDRequest></soap:Body></soap:Envelope>
And that this is the response being returned:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode xmlns:ns1="http://xml.apache.org/axis/">ns1:FailedCheck</faultcode>
<faultstring>Authorisation token is invalid.</faultstring>
<faultactor>http://www.ebay.com/ws/websvc/eBayAPI</faultactor>
<detail>
<FaultDetail>
<ErrorCode>931</ErrorCode>
<Severity>Error</Severity>
<DetailedMessage>Validation of the authentication token in API request failed.</DetailedMessage>
</FaultDetail>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
My ebay.config.php file has all the correct keys and, as far as I can tell, all of the correct information. Does anyone have any tips for resolution?
Danny
This issue was caused by the eBatNS API sending a token from a previous request in the request to get a new token. This isn't supported by ebay.
This is resolved by setting the token mode on the session to 0 or false.
$session->setTokenMode(0)

Accessing SOAP services with Google Apps Script

I'm trying to use Google Apps Scripts to call a SOAP service call, and I've been trying to tinker with a number of ways to get a response; however, I keep getting an error. I've defaulted to trying to send an exact copy of a message stored in my spreadsheet that I know works through another service... still no luck. Here's the Apps Script Code:
function getVesselSummaryXMLStringFromName() {
var wsdl = SoapService.wsdl("http://cgmix.uscg.mil/xml/PSIXData.asmx?WSDL");
Logger.log(wsdl.getServiceNames());
var uscgService = wsdl.getPSIXData();
var sheet = SpreadsheetApp.getActiveSheet();
//Get working SOAP message
var envelope = sheet.getRange("D1:D1").getValues();
Logger.log(envelope);
var result = uscgService.getenvelope;
Logger.log(result);
}
The SOAP message I'm sending that works through http://www.soapclient.com/soapclient is:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://cgmix.uscg.mil" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ><SOAP-ENV:Body><tns:getVesselSummaryXMLString xmlns:tns="http://cgmix.uscg.mil"><tns:VesselID></tns:VesselID><tns:VesselID></tns:VesselID><tns:VesselName>Atlantic Salvor</tns:VesselName><tns:VesselName></tns:VesselName><tns:CallSign></tns:CallSign><tns:CallSign></tns:CallSign><tns:VIN></tns:VIN><tns:VIN></tns:VIN><tns:HullNum></tns:HullNum><tns:HullNum></tns:HullNum><tns:Flag></tns:Flag><tns:Flag></tns:Flag><tns:Service></tns:Service><tns:Service></tns:Service><tns:BuildYear></tns:BuildYear><tns:BuildYear></tns:BuildYear></tns:getVesselSummaryXMLString></SOAP-ENV:Body></SOAP-ENV:Envelope>
The following code returns an answer, so maybe you can help.
...
var url = 'http://cgmix.uscg.mil/xml/PSIXData.asmx?WSDL';
var wsdl = SoapService.wsdl(url);
var servicePSIXData = wsdl.getPSIXData();
var params = Xml.element('getVesselSummaryXMLString', [
Xml.attribute('xmlns', 'http://cgmix.uscg.mil'),
Xml.element('VesselID', ['']),
Xml.element('VesselName', ['Atlantic Salvor']),
Xml.element('CallSign', ['']),
Xml.element('VIN', ['']),
Xml.element('HullNum', ['']),
Xml.element('Flag', ['']),
Xml.element('Service', ['']),
Xml.element('BuildYear', [''])
]);
var result = servicePSIXData.getVesselSummaryXMLString(params);
Logger.log(result.toXmlString());
...
UPDATE
Both Xml Services and Soap Services are considered deprecated.

xpath soap namespace grabbing items

I am feeling like i am doing something really not correct.
When doing a soap they return me with an xml which may or may not contain an error.
I would like to check if the error exists if not read the values.
somehow, I can't grab it directly :(
Below is a sample return of something with results and one which gives an error (name not found)
<?xml version="1.0" encoding="utf-8"?>
<soapEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<envHeader xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<wsaAction>http://www.rechtspraak.nl/namespaces/ccr01/searchPersonResponse</wsaAction>
<wsaMessageID>urn:uuid:b75d2932-5687-4871-9d07-3b74b084978a</wsaMessageID>
<wsaRelatesTo>urn:uuid:9112d870-248d-4d07-acd0-d88e4a48d547</wsaRelatesTo>
<wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
<wsseSecurity>
<wsuTimestamp wsu:Id="Timestamp-061df7b5-32a2-4021-852d-2df98953e076">
<wsuCreated>2011-05-27T12:11:45Z</wsuCreated>
<wsuExpires>2011-05-27T12:16:45Z</wsuExpires>
</wsuTimestamp>
</envHeader>
<soapBody>
<searchPersonResponse xmlns="http://www.rechtspraak.nl/namespaces/ccr01">
<searchPersonResult>
<CCR_WS xmlns="http://www.rechtspraak.nl/namespaces/ccr">
<curandus>
<ccn>1</ccn>
<cur_voornamen>Jan</cur_voornamen>
<cur_voorvoegsels>van</cur_voorvoegsels>
<cur_achternaam>Beek</cur_achternaam>
<geboorte_datum>1980-01-02</geboorte_datum>
<geboorte_plaats>Werkendam</geboorte_plaats>
</curandus>
</CCR_WS>
</searchPersonResult>
</searchPersonResponse>
</soapBody>
</soapEnvelope>
and the one without results
<?xml version="1.0" encoding="utf-8"?>
<soapEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<envHeader xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<wsaAction>http://www.rechtspraak.nl/namespaces/ccr01/searchPersonResponse</wsaAction>
<wsaMessageID>urn:uuid:b75d2932-5687-4871-9d07-3b74b084978a</wsaMessageID>
<wsaRelatesTo>urn:uuid:9112d870-248d-4d07-acd0-d88e4a48d547</wsaRelatesTo>
<wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
<wsseSecurity>
<wsuTimestamp wsu:Id="Timestamp-061df7b5-32a2-4021-852d-2df98953e076">
<wsuCreated>2011-05-27T12:11:45Z</wsuCreated>
<wsuExpires>2011-05-27T12:16:45Z</wsuExpires>
</wsuTimestamp>
</envHeader>
<soapBody>
<searchPersonResponse xmlns="http://www.rechtspraak.nl/namespaces/ccr01">
<searchPersonResult>
<CCR_WS xmlns="http://www.rechtspraak.nl/namespaces/ccr">
<exceptie errorcode="1">No Results found.</exceptie>
</CCR_WS>
</searchPersonResult>
</searchPersonResponse>
</soapBody>
</soapEnvelope>
Here is my code to select the namespace, then check
$results = simplexml_load_string($response);
$results->registerXPathNamespace('ccr','http://www.rechtspraak.nl/namespaces/ccr');
$lijst = $results->xpath('//ccr:CCR_WS');
$errorcode = $lijst[0]->exceptie->attributes()->errorcode;
$error = $lijst[0]->exceptie;
if (isset($errorcode) AND $errorcode != "") {
// do things with the error code
} else {
$lijst = $results->xpath('//ccr01:searchPersonResult');
$cur = $lijst[0]->CCR_WS->curandus;
echo $cur->ccn."<BR>";
echo $cur->cur_voornamen."<BR>";
echo $cur->cur_voorvoegsels."<BR>";
echo $cur->cur_achternaam."<BR>";
echo $cur->geboorte_datum."<BR>";
echo $cur->geboorte_plaats."<BR>";
}
surely there is a better way of grabbing
$lijst[0]->exceptie->attributes()->errorcode
for example...
...Don't know if this is a "better way" to everyone, but here is a direct XPath expression to select the errorcode. You can make it shorter and less efficient by dropping steps and using // (in the beginning or in the middle). Attributes are selected with # (or with attribute:: axis if you prefer the longer syntax). If attribute (or the exceptie element) doesn't exist, nothing is returned.
/*/*/ccr01:searchPersonResponse/ccr01:searchPersonResult/ccr:CCR_WS/ccr:exceptie/#errorcode
Remember to register all the namespace prefixes that yo use in your XPath expression.