Skip to content

Commit

Permalink
Merge branch 'master' into 5075_make_some_java_classes_proper_utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisaruGuruge authored Nov 3, 2023
2 parents 1c02642 + cb38b51 commit e56e65e
Show file tree
Hide file tree
Showing 18 changed files with 778 additions and 141 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function main() returns error? {
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
xml response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
}
```

Expand Down Expand Up @@ -172,7 +172,7 @@ public function main() returns error? {
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
xml response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
}
```

Expand Down Expand Up @@ -213,7 +213,7 @@ public function main() returns error? {
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
xml response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
}
```

Expand Down
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "soap"
version = "0.8.1"
version = "0.9.0"
authors = ["Ballerina"]
export=["soap", "soap.soap11", "soap.soap12"]
keywords = ["soap"]
Expand All @@ -19,8 +19,8 @@ graalvmCompatible = true
[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "soap-native"
version = "0.8.1"
path = "../native/build/libs/soap-native-0.8.1.jar"
version = "0.9.0"
path = "../native/build/libs/soap-native-0.9.0-SNAPSHOT.jar"

[[platform.java17.dependency]]
groupId = "org.apache.wss4j"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "soap"
version = "0.8.1"
version = "0.9.0"
dependencies = [
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "http"},
Expand Down
6 changes: 3 additions & 3 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function main() returns error? {
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
xml response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
}
```

Expand Down Expand Up @@ -162,7 +162,7 @@ public function main() returns error? {
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
xml response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
}
```

Expand Down Expand Up @@ -202,7 +202,7 @@ public function main() returns error? {
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
xml response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
}
```

Expand Down
2 changes: 1 addition & 1 deletion ballerina/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function main() returns error? {
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
xml response = check soapClient->sendReceive(envelope, "http://tempuri.org/Add");
}
```

Expand Down
36 changes: 26 additions & 10 deletions ballerina/modules/soap11/soap11.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import soap.wssec;

import ballerina/http;
import ballerina/mime;
import ballerina/jballerina.java;

# Object for the basic SOAP client endpoint.
# Object for the basic SOAP 1.1 client endpoint.
public isolated client class Client {
private final http:Client soapClient;
private final readonly & wssec:InboundSecurityConfig|wssec:InboundSecurityConfig[] inboundSecurity;
Expand All @@ -45,25 +46,35 @@ public isolated client class Client {

# Sends SOAP request and expects a response.
# ```ballerina
# xml|mime:Entity[] response = check soapClient->sendReceive(body, action);
# xml response = check soapClient->sendReceive(body, action);
# -- OR --
# mime:Entity[] response = check soapClient->sendReceive(body, action);
# ```
#
# + 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[]>`
# + path - The resource path
# + T - Default parameter use to infer the user specified type (`xml` or `mime:Entity[]`)
# + return - If successful, returns the response. Else, returns an error
remote isolated function sendReceive(xml|mime:Entity[] body, string action,
map<string|string[]> headers = {}, string path = "")
remote isolated function sendReceive(xml|mime:Entity[] body, string action, map<string|string[]> headers = {},
string path = "", typedesc<xml|mime:Entity[]> T = <>)
returns T|Error = @java:Method {
'class: "io.ballerina.lib.soap.Soap",
name: "sendReceive11"
} external;

isolated function generateResponse(xml|mime:Entity[] body, string action,
map<string|string[]> headers = {}, string path = "")
returns xml|mime:Entity[]|Error {
do {
xml securedBody;
xml mimeEntity = body is xml ? body : check body[0].getXml();
lock {
xml envelope = body is xml ? body.clone() : mimeEntity.clone();
securedBody = check soap:applySecurityPolicies(self.inboundSecurity.clone(), envelope.clone());
securedBody = check soap:applySecurityPolicies(self.inboundSecurity.clone(), envelope.clone(), false);
}
xml response;
xml|mime:Entity[] response;
if body is mime:Entity[] {
body[0].setXml(securedBody);
response = check soap:sendReceive(body, self.soapClient, action, headers, path, false);
Expand All @@ -73,13 +84,18 @@ public isolated client class Client {
lock {
wssec:OutboundSecurityConfig? outboundSecurity = self.outboundSecurity.clone();
do {
if outboundSecurity is wssec:OutboundSecurityConfig {
return check soap:applyOutboundConfig(outboundSecurity.clone(), response.clone());
if outboundSecurity is wssec:OutboundSecurityConfig && outboundSecurity != {} {
if response is xml {
return check soap:applyOutboundConfig(outboundSecurity.clone(), response.clone(), false);
} else {
return check soap:applyOutboundConfig(outboundSecurity.clone(),
check response[0].getXml().clone(), false);
}
}
} on fail var e {
return error Error(INVALID_OUTBOUND_SECURITY_ERROR, e.cause());
}
return response.clone();
return response;
}
} on fail var e {
return error Error(SOAP_ERROR, e.cause());
Expand All @@ -104,7 +120,7 @@ public isolated client class Client {
xml mimeEntity = body is xml ? body : check body[0].getXml();
lock {
xml envelope = body is xml ? body.clone() : mimeEntity.clone();
securedBody = check soap:applySecurityPolicies(self.inboundSecurity.clone(), envelope.clone());
securedBody = check soap:applySecurityPolicies(self.inboundSecurity.clone(), envelope.clone(), false);
}
return check soap:sendOnly(securedBody, self.soapClient, action, headers, path, false);
} on fail var e {
Expand Down
61 changes: 57 additions & 4 deletions ballerina/modules/soap11/tests/http_soap_service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/crypto;
import ballerina/http;
import ballerina/mime;
import ballerina/soap;

const crypto:KeyStore serverKeyStore = {
path: X509_KEY_STORE_PATH,
password: KEY_PASSWORD
};
crypto:PrivateKey serverPrivateKey = check crypto:decodeRsaPrivateKeyFromKeyStore(serverKeyStore, KEY_ALIAS,
KEY_PASSWORD);
KEY_PASSWORD);
crypto:PublicKey serverPublicKey = check crypto:decodeRsaPublicKeyFromTrustStore(serverKeyStore, KEY_ALIAS);

service / on new http:Listener(9090) {
Expand All @@ -34,13 +34,60 @@ service / on new http:Listener(9090) {
return response;
}

resource function post getMimePayload(http:Request request) returns http:Response|error {
http:Response response = new;
mime:Entity[] mtomMessage = [];
mime:Entity envelope = new;
check envelope.setContentType("application/xop+xml");
envelope.setContentId("<soap@envelope>");
envelope.setBody(check (check request.getBodyParts())[0].getXml());
mtomMessage.push(envelope);
response.setBodyParts(mtomMessage);
response.setPayload(mtomMessage);
return response;
}

resource function post getSamePayload(http:Request request) returns http:Response|error {
xml payload = check request.getXmlPayload();
http:Response response = new;
response.setPayload(payload);
return response;
}

resource function post getSecuredMimePayload(http:Request request) returns http:Response|error {
xml payload = check (check request.getBodyParts())[0].getXml();
xml applyOutboundConfig = check soap:applyOutboundConfig(
{
verificationKey: clientPublicKey,
signatureAlgorithm: soap:RSA_SHA256,
decryptionAlgorithm: soap:RSA_ECB,
decryptionKey: serverPrivateKey
},
payload,
false
);
xml securedEnv = check soap:applySecurityPolicies(
{
signatureAlgorithm: soap:RSA_SHA256,
encryptionAlgorithm: soap:RSA_ECB,
signatureKey: serverPrivateKey,
encryptionKey: clientPublicKey
},
applyOutboundConfig,
false
);
http:Response response = new;
mime:Entity[] mtomMessage = [];
mime:Entity envelope = new;
check envelope.setContentType("application/xop+xml");
envelope.setContentId("<soap@envelope>");
envelope.setBody(securedEnv);
mtomMessage.push(envelope);
response.setBodyParts(mtomMessage);
response.setPayload(mtomMessage);
return response;
}

resource function post getSecuredPayload(http:Request request) returns http:Response|error {
xml payload = check request.getXmlPayload();
xml applyOutboundConfig = check soap:applyOutboundConfig(
Expand All @@ -49,14 +96,20 @@ service / on new http:Listener(9090) {
signatureAlgorithm: soap:RSA_SHA256,
decryptionAlgorithm: soap:RSA_ECB,
decryptionKey: serverPrivateKey
}, payload);
},
payload,
false
);
xml securedEnv = check soap:applySecurityPolicies(
{
signatureAlgorithm: soap:RSA_SHA256,
encryptionAlgorithm: soap:RSA_ECB,
signatureKey: serverPrivateKey,
encryptionKey: clientPublicKey
}, applyOutboundConfig);
},
applyOutboundConfig,
false
);
http:Response response = new;
response.setPayload(securedEnv);
return response;
Expand Down
Loading

0 comments on commit e56e65e

Please sign in to comment.