Skip to content

Commit

Permalink
Merge pull request #99 from Nuvindu/2201.7.x
Browse files Browse the repository at this point in the history
Update API compatibility for the Ballerina 2201.7.2 lang version
  • Loading branch information
shafreenAnfar authored Sep 15, 2023
2 parents 1cbf4b0 + 99693da commit 9e492ff
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 135 deletions.
4 changes: 2 additions & 2 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
org = "ballerina"
name = "soap"
version = "0.1.0"
version = "0.2.0"
authors = ["Ballerina"]
keywords = ["soap"]
repository = "https://github.com/ballerina-platform/module-ballerina-soap"
icon = "icon.png"
license = ["Apache-2.0"]
distribution = "2201.5.0"
distribution = "2201.7.2"
22 changes: 5 additions & 17 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.7.0"
distribution-version = "2201.7.2"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -41,7 +41,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "crypto"
version = "2.4.0"
version = "2.4.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
Expand All @@ -50,7 +50,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "file"
version = "1.8.0"
version = "1.8.1"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -61,7 +61,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.9.1"
version = "2.9.3"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand Down Expand Up @@ -201,17 +201,6 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.xml"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "lang.xml", moduleName = "lang.xml"}
]

[[package]]
org = "ballerina"
name = "log"
Expand Down Expand Up @@ -269,10 +258,9 @@ dependencies = [
[[package]]
org = "ballerina"
name = "soap"
version = "0.1.0"
version = "0.2.0"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "lang.xml"},
{org = "ballerina", name = "mime"},
{org = "ballerina", name = "test"}
]
Expand Down
20 changes: 13 additions & 7 deletions ballerina/soap.bal
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public enum SoapVersion {
#
# + soapVersion - SOAP version
public type ClientConfiguration record {|
*http:ClientConfiguration;
SoapVersion soapVersion = SOAP11;
*http:ClientConfiguration;
SoapVersion soapVersion = SOAP11;
|};

# Object for the basic SOAP client endpoint.
Expand All @@ -46,7 +46,7 @@ public isolated client class Client {
public function init(string url, *ClientConfiguration config) returns Error? {
self.soapVersion = config.soapVersion;
do {
self.soapClient = check new (url,retrieveHttpClientConfig(config));
self.soapClient = check new (url, retrieveHttpClientConfig(config));
} on fail var err {
return error Error("Failed to initialize soap client", err);
}
Expand All @@ -58,9 +58,12 @@ public isolated client class Client {
# ```
#
# + body - SOAP request body as an `XML` or `mime:Entity[]` to work with SOAP attachments
# + action - SOAP action as a `string`
# + headers - SOAP headers as a `map<string|string[]>`
# + return - If successful, returns the response. Else, returns an error
remote function sendReceive(xml|mime:Entity[] body) returns xml|mime:Entity[]|Error {
return sendReceive(self.soapVersion, body, self.soapClient);
remote function sendReceive(xml|mime:Entity[] body, string? action = (),
map<string|string[]> headers = {})returns xml|mime:Entity[]|Error {
return sendReceive(self.soapVersion, body, self.soapClient, action, headers);
}

# Fires and forgets requests. Sends the request without the possibility of any response from the
Expand All @@ -70,8 +73,11 @@ public isolated client class Client {
# ```
#
# + body - SOAP request body as an `XML` or `mime:Entity[]` to work with SOAP attachments
# + action - SOAP action as a `string`
# + headers - SOAP headers as a `map<string|string[]>`
# + return - If successful, returns `nil`. Else, returns an error
remote function sendOnly(xml|mime:Entity[] body) returns Error? {
return sendOnly(self.soapVersion, body, self.soapClient);
remote function sendOnly(xml|mime:Entity[] body, string? action = (),
map<string|string[]> headers = {}) returns Error? {
return sendOnly(self.soapVersion, body, self.soapClient, action, headers);
}
}
107 changes: 26 additions & 81 deletions ballerina/soap_utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -15,77 +15,22 @@
// under the License.

import ballerina/http;
import ballerina/lang.'xml as xmllib;
import ballerina/mime;

# Provides an empty SOAP envelope for the given SOAP version.
#
# + soapVersion - The SOAP version of the request
# + return - XML with the empty SOAP envelope
function createSoapEnvelop(SoapVersion soapVersion) returns xmllib:Element {
if soapVersion == SOAP11 {
return <xmllib:Element> xml `<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
</soap:Envelope>`;
} else {
return <xmllib:Element> xml `<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
</soap:Envelope>`;
}
}

# Provides the SOAP headers in the request as XML.
#
# + soapVersion - The SOAP version of the request
# + return - XML with the empty SOAP header
function createSoapHeader(SoapVersion soapVersion) returns xml {
xmllib:Element headersRoot;
if soapVersion == SOAP11 {
headersRoot = <xmllib:Element> xml `<soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"></soap:Header>`;
} else {
headersRoot = <xmllib:Element> xml `<soap:Header xmlns:soap="http://www.w3.org/2003/05/soap-envelope"></soap:Header>`;
}
return headersRoot;
}

# Provides the SOAP body in the request as XML.
#
# + payload - The payload to be sent
# + soapVersion - The SOAP version of the request
# + return - XML with the SOAP body
function createSoapBody(xml payload, SoapVersion soapVersion) returns xml {
xmllib:Element bodyRoot;
if soapVersion == SOAP11 {
bodyRoot = <xmllib:Element> xml `<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"></soap:Body>`;
} else {
bodyRoot = <xmllib:Element> xml `<soap:Body xmlns:soap="http://www.w3.org/2003/05/soap-envelope"></soap:Body>`;
}
bodyRoot.setChildren(payload);
return bodyRoot;
}

# Prepares a SOAP envelope with the XML to be sent.
# Creates a SOAP Request as an `http:Request`
#
# + soapAction - SOAP action
# + body - SOAP request body as an `XML` or `mime:Entity[]` to work with soap attachments
# + soapVersion - The SOAP version of the request
# + return - The SOAP Request sent as `http:Request` with the SOAP envelope
function fillSoapEnvelope(SoapVersion soapVersion, xml | mime:Entity[] body, string? soapAction = ())
returns http:Request {
xml soapPayload = createSoapHeader(soapVersion);
# + headers - SOAP headers as a `map<string|string[]>`
# + return - The SOAP Request sent as `http:Request`
function createHttpRequest(SoapVersion soapVersion, xml|mime:Entity[] body,
string? soapAction, map<string|string[]> headers = {}) returns http:Request {
http:Request req = new;
var requestPayload = body;
if requestPayload is xml {
xml bodyPayload = createSoapBody(requestPayload, soapVersion);
soapPayload = soapPayload + bodyPayload;

xmllib:Element soapEnv = createSoapEnvelop(soapVersion);
soapEnv.setChildren(soapPayload);
req.setXmlPayload(soapEnv);
if body is xml {
req.setXmlPayload(body);
} else {
req.setBodyParts(requestPayload);
req.setBodyParts(body);
}
if soapVersion == SOAP11 {
req.setHeader(mime:CONTENT_TYPE, mime:TEXT_XML);
Expand All @@ -105,6 +50,9 @@ returns http:Request {
req.setHeader(mime:CONTENT_TYPE, mime:APPLICATION_SOAP_XML);
}
}
foreach string key in headers.keys() {
req.addHeader(key, headers[key].toBalString());
}
return req;
}

Expand All @@ -113,42 +61,39 @@ returns http:Request {
# + response - The request to be sent
# + soapVersion - The SOAP version of the request
# + return - The SOAP response created from the `http:Response` or the `error` object when reading the payload
function createSoapResponse(http:Response response, SoapVersion soapVersion) returns xml | error {
function createSoapResponse(http:Response response, SoapVersion soapVersion) returns xml|error {
xml payload = check response.getXmlPayload();
xmlns "http://schemas.xmlsoap.org/soap/envelope/" as soap11;
xmlns "http://www.w3.org/2003/05/soap-envelope" as soap12;

xml soapResponsePayload;
if soapVersion == SOAP11 {
soapResponsePayload = payload/<soap11:Body>;
} else {
soapResponsePayload = payload/<soap12:Body>;
}
return soapResponsePayload;
return soapVersion == SOAP11 ? payload/<soap11:Body> : payload/<soap12:Body>;
}

string path = "";

function sendReceive(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient, string? soapAction = ()) returns xml|Error {
http:Request req = fillSoapEnvelope(soapVersion, body, soapAction = soapAction);
function sendReceive(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient,
string? soapAction = (), map<string|string[]> headers = {}) returns xml|Error {
http:Request req = createHttpRequest(soapVersion, body, soapAction, headers);
http:Response response;
do {
response = check httpClient->post(path, req);
response = check httpClient->post(path, req);
} on fail var err {
return error Error("Failed to receive soap response", err);
return error Error("Failed to receive soap response", err);
}
do {
return check createSoapResponse(response, soapVersion);
return check createSoapResponse(response, soapVersion);
} on fail var err {
return error Error("Failed to create soap response", err);
return error Error("Failed to create soap response", err);
}
}
function sendOnly(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient, string? soapAction = ()) returns Error? {
http:Request req = fillSoapEnvelope(SOAP11, body, soapAction = soapAction);

function sendOnly(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient,
string? soapAction = (), map<string|string[]> headers = {}) returns Error? {
http:Request req = createHttpRequest(SOAP11, body, soapAction, headers);
do {
http:Response _ = check httpClient->post(path, req);
http:Response _ = check httpClient->post(path, req);
} on fail var err {
return error Error("Failed to create soap response", err);
return error Error("Failed to create soap response", err);
}
}

Expand Down
Loading

0 comments on commit 9e492ff

Please sign in to comment.