From 974cbaac07412db9f3567bc84496f903a33291ab Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 10 May 2022 00:43:25 +0530 Subject: [PATCH 01/16] [Automated] Update the native jar versions --- ballerina/Dependencies.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index fbc939e1..f0d8b43e 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -208,7 +208,7 @@ modules = [ [[package]] org = "ballerina" name = "mime" -version = "2.2.1" +version = "2.2.2" dependencies = [ {org = "ballerina", name = "io"}, {org = "ballerina", name = "jballerina.java"}, From 559b58a859a7ad56186ca5918c532cb968aaf199 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 10 May 2022 00:59:33 +0530 Subject: [PATCH 02/16] [Automated] Update the native jar versions --- ballerina/Dependencies.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index f0d8b43e..e2b98f71 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -208,7 +208,7 @@ modules = [ [[package]] org = "ballerina" name = "mime" -version = "2.2.2" +version = "2.2.1" dependencies = [ {org = "ballerina", name = "io"}, {org = "ballerina", name = "jballerina.java"}, @@ -233,7 +233,7 @@ dependencies = [ [[package]] org = "ballerina" name = "observe" -version = "1.0.3" +version = "1.0.4" dependencies = [ {org = "ballerina", name = "jballerina.java"} ] From 0980882ed5c7a5764fb6c95b535bf3f53a4b871d Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 10 May 2022 01:14:57 +0530 Subject: [PATCH 03/16] [Automated] Update the native jar versions --- ballerina/Dependencies.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index e2b98f71..c6c94a8a 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -208,7 +208,7 @@ modules = [ [[package]] org = "ballerina" name = "mime" -version = "2.2.1" +version = "2.2.2" dependencies = [ {org = "ballerina", name = "io"}, {org = "ballerina", name = "jballerina.java"}, From 790583ebabc8ee8bf44935d60ce4a3ad76a49967 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Wed, 1 Jun 2022 10:51:47 +0530 Subject: [PATCH 04/16] [Automated] Update the native jar versions --- ballerina/Ballerina.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index acd2e6ef..af8dbc30 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -7,7 +7,7 @@ keywords = ["websub", "hub", "publisher", "service", "listener", "client"] repository = "https://github.com/ballerina-platform/module-ballerina-websubhub" icon = "icon.png" license = ["Apache-2.0"] -distribution = "2201.0.1" +distribution = "2201.1.0" [[platform.java11.dependency]] path = "../native/build/libs/websubhub-native-1.3.0-SNAPSHOT.jar" From 9d55322400c819a9920bb54f8179bbfca0ad6fc2 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Thu, 2 Jun 2022 19:15:42 +0530 Subject: [PATCH 05/16] Add statusCode to the CommonResponse --- ballerina/commons.bal | 46 ++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/ballerina/commons.bal b/ballerina/commons.bal index 190cafcf..fb0cab47 100644 --- a/ballerina/commons.bal +++ b/ballerina/commons.bal @@ -104,10 +104,12 @@ final StatusPermanentRedirect STATUS_PERMANENT_REDIRECT = new; # Record to represent the parent type for all the response records. # +# + statusCode - HTTP status code for the response # + mediaType - Content-Type of the request received # + headers - Additional request headers received to be included in the request # + body - Received request body type CommonResponse record {| + int statusCode; string? mediaType = (); map? headers = (); string|byte[]|json|xml|map? body = (); @@ -271,46 +273,62 @@ public type Acknowledgement record { }; # Common response, which could be used for `websubhub:TopicRegistrationSuccess`. -public final readonly & TopicRegistrationSuccess TOPIC_REGISTRATION_SUCCESS = {}; +public final readonly & TopicRegistrationSuccess TOPIC_REGISTRATION_SUCCESS = { + statusCode: http:STATUS_OK +}; # Common response, which could be used for `websubhub:TopicRegistrationError`. -public final TopicRegistrationError TOPIC_REGISTRATION_ERROR = error TopicRegistrationError("Topic registration failed"); +public final TopicRegistrationError TOPIC_REGISTRATION_ERROR = error TopicRegistrationError("Topic registration failed", statusCode = http:STATUS_OK); # Common response, which could be used for `websubhub:TopicDeregistrationSuccess`. -public final readonly & TopicDeregistrationSuccess TOPIC_DEREGISTRATION_SUCCESS = {}; +public final readonly & TopicDeregistrationSuccess TOPIC_DEREGISTRATION_SUCCESS = { + statusCode: http:STATUS_OK +}; # Common response, which could be used for `websubhub:TopicDeregistrationError`. -public final TopicDeregistrationError TOPIC_DEREGISTRATION_ERROR = error TopicDeregistrationError("Topic deregistration failed!"); +public final TopicDeregistrationError TOPIC_DEREGISTRATION_ERROR = error TopicDeregistrationError("Topic deregistration failed!", statusCode = http:STATUS_OK); # Common response, which could be used for `websubhub:Acknowledgement`. -public final readonly & Acknowledgement ACKNOWLEDGEMENT = {}; +public final readonly & Acknowledgement ACKNOWLEDGEMENT = { + statusCode: http:STATUS_OK +}; # Common response, which could be used for `websubhub:UpdateMessageError`. -public final UpdateMessageError UPDATE_MESSAGE_ERROR = error UpdateMessageError("Error in accessing content"); +public final UpdateMessageError UPDATE_MESSAGE_ERROR = error UpdateMessageError("Error in accessing content", statusCode = http:STATUS_BAD_REQUEST); # Common response, which could be used for `websubhub:SubscriptionAccepted`. -public final readonly & SubscriptionAccepted SUBSCRIPTION_ACCEPTED = {}; +public final readonly & SubscriptionAccepted SUBSCRIPTION_ACCEPTED = { + statusCode: http:STATUS_ACCEPTED +}; # Common response, which could be used for `websubhub:BadSubscriptionError`. -public final BadSubscriptionError BAD_SUBSCRIPTION_ERROR = error BadSubscriptionError("Bad subscription request"); +public final BadSubscriptionError BAD_SUBSCRIPTION_ERROR = error BadSubscriptionError("Bad subscription request", statusCode = http:STATUS_BAD_REQUEST); # Common response, which could be used for `websubhub:InternalSubscriptionError`. -public final InternalSubscriptionError INTERNAL_SUBSCRIPTION_ERROR = error InternalSubscriptionError("Internal error occurred while processing subscription request"); +public final InternalSubscriptionError INTERNAL_SUBSCRIPTION_ERROR = error InternalSubscriptionError( + "Internal error occurred while processing subscription request", + statusCode = http:STATUS_INTERNAL_SERVER_ERROR +); # Common response, which could be used for `websubhub:SubscriptionDeniedError`. -public final SubscriptionDeniedError SUBSCRIPTION_DENIED_ERROR = error SubscriptionDeniedError("Subscription denied"); +public final SubscriptionDeniedError SUBSCRIPTION_DENIED_ERROR = error SubscriptionDeniedError("Subscription denied", statusCode = http:STATUS_BAD_REQUEST); # Common response, which could be used for `websubhub:UnsubscriptionAccepted`. -public final readonly & UnsubscriptionAccepted UNSUBSCRIPTION_ACCEPTED = {}; +public final readonly & UnsubscriptionAccepted UNSUBSCRIPTION_ACCEPTED = { + statusCode: http:STATUS_ACCEPTED +}; # Common response, which could be used for `websubhub:BadUnsubscriptionError`. -public final BadUnsubscriptionError BAD_UNSUBSCRIPTION_ERROR = error BadUnsubscriptionError("Bad unsubscription request"); +public final BadUnsubscriptionError BAD_UNSUBSCRIPTION_ERROR = error BadUnsubscriptionError("Bad unsubscription request", statusCode = http:STATUS_BAD_REQUEST); # Common response, which could be used for `websubhub:InternalUnsubscriptionError`. -public final InternalUnsubscriptionError INTERNAL_UNSUBSCRIPTION_ERROR = error InternalUnsubscriptionError("Internal error occurred while processing unsubscription request"); +public final InternalUnsubscriptionError INTERNAL_UNSUBSCRIPTION_ERROR = error InternalUnsubscriptionError( + "Internal error occurred while processing unsubscription request", + statusCode = http:STATUS_INTERNAL_SERVER_ERROR +); # Common response, which could be used for `websubhub:UnsubscriptionDeniedError`. -public final UnsubscriptionDeniedError UNSUBSCRIPTION_DENIED_ERROR = error UnsubscriptionDeniedError("Unsubscription denied"); +public final UnsubscriptionDeniedError UNSUBSCRIPTION_DENIED_ERROR = error UnsubscriptionDeniedError("Unsubscription denied", statusCode = http:STATUS_BAD_REQUEST); # Record to represent the client configuration for the HubClient/PublisherClient. # From 5cd4828f235ad067d4297bdea189ed17898c1285 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Thu, 2 Jun 2022 19:17:33 +0530 Subject: [PATCH 06/16] Refactor code base --- ballerina/commons.bal | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/ballerina/commons.bal b/ballerina/commons.bal index fb0cab47..ef20f108 100644 --- a/ballerina/commons.bal +++ b/ballerina/commons.bal @@ -278,7 +278,10 @@ public final readonly & TopicRegistrationSuccess TOPIC_REGISTRATION_SUCCESS = { }; # Common response, which could be used for `websubhub:TopicRegistrationError`. -public final TopicRegistrationError TOPIC_REGISTRATION_ERROR = error TopicRegistrationError("Topic registration failed", statusCode = http:STATUS_OK); +public final TopicRegistrationError TOPIC_REGISTRATION_ERROR = error TopicRegistrationError( + "Topic registration failed", + statusCode = http:STATUS_OK +); # Common response, which could be used for `websubhub:TopicDeregistrationSuccess`. public final readonly & TopicDeregistrationSuccess TOPIC_DEREGISTRATION_SUCCESS = { @@ -286,7 +289,10 @@ public final readonly & TopicDeregistrationSuccess TOPIC_DEREGISTRATION_SUCCESS }; # Common response, which could be used for `websubhub:TopicDeregistrationError`. -public final TopicDeregistrationError TOPIC_DEREGISTRATION_ERROR = error TopicDeregistrationError("Topic deregistration failed!", statusCode = http:STATUS_OK); +public final TopicDeregistrationError TOPIC_DEREGISTRATION_ERROR = error TopicDeregistrationError( + "Topic deregistration failed!", + statusCode = http:STATUS_OK +); # Common response, which could be used for `websubhub:Acknowledgement`. public final readonly & Acknowledgement ACKNOWLEDGEMENT = { @@ -294,7 +300,10 @@ public final readonly & Acknowledgement ACKNOWLEDGEMENT = { }; # Common response, which could be used for `websubhub:UpdateMessageError`. -public final UpdateMessageError UPDATE_MESSAGE_ERROR = error UpdateMessageError("Error in accessing content", statusCode = http:STATUS_BAD_REQUEST); +public final UpdateMessageError UPDATE_MESSAGE_ERROR = error UpdateMessageError( + "Error in accessing content", + statusCode = http:STATUS_BAD_REQUEST +); # Common response, which could be used for `websubhub:SubscriptionAccepted`. public final readonly & SubscriptionAccepted SUBSCRIPTION_ACCEPTED = { @@ -302,7 +311,10 @@ public final readonly & SubscriptionAccepted SUBSCRIPTION_ACCEPTED = { }; # Common response, which could be used for `websubhub:BadSubscriptionError`. -public final BadSubscriptionError BAD_SUBSCRIPTION_ERROR = error BadSubscriptionError("Bad subscription request", statusCode = http:STATUS_BAD_REQUEST); +public final BadSubscriptionError BAD_SUBSCRIPTION_ERROR = error BadSubscriptionError( + "Bad subscription request", + statusCode = http:STATUS_BAD_REQUEST +); # Common response, which could be used for `websubhub:InternalSubscriptionError`. public final InternalSubscriptionError INTERNAL_SUBSCRIPTION_ERROR = error InternalSubscriptionError( @@ -311,7 +323,10 @@ public final InternalSubscriptionError INTERNAL_SUBSCRIPTION_ERROR = error Inter ); # Common response, which could be used for `websubhub:SubscriptionDeniedError`. -public final SubscriptionDeniedError SUBSCRIPTION_DENIED_ERROR = error SubscriptionDeniedError("Subscription denied", statusCode = http:STATUS_BAD_REQUEST); +public final SubscriptionDeniedError SUBSCRIPTION_DENIED_ERROR = error SubscriptionDeniedError( + "Subscription denied", + statusCode = http:STATUS_BAD_REQUEST +); # Common response, which could be used for `websubhub:UnsubscriptionAccepted`. public final readonly & UnsubscriptionAccepted UNSUBSCRIPTION_ACCEPTED = { @@ -319,7 +334,10 @@ public final readonly & UnsubscriptionAccepted UNSUBSCRIPTION_ACCEPTED = { }; # Common response, which could be used for `websubhub:BadUnsubscriptionError`. -public final BadUnsubscriptionError BAD_UNSUBSCRIPTION_ERROR = error BadUnsubscriptionError("Bad unsubscription request", statusCode = http:STATUS_BAD_REQUEST); +public final BadUnsubscriptionError BAD_UNSUBSCRIPTION_ERROR = error BadUnsubscriptionError( + "Bad unsubscription request", + statusCode = http:STATUS_BAD_REQUEST +); # Common response, which could be used for `websubhub:InternalUnsubscriptionError`. public final InternalUnsubscriptionError INTERNAL_UNSUBSCRIPTION_ERROR = error InternalUnsubscriptionError( @@ -328,7 +346,10 @@ public final InternalUnsubscriptionError INTERNAL_UNSUBSCRIPTION_ERROR = error I ); # Common response, which could be used for `websubhub:UnsubscriptionDeniedError`. -public final UnsubscriptionDeniedError UNSUBSCRIPTION_DENIED_ERROR = error UnsubscriptionDeniedError("Unsubscription denied", statusCode = http:STATUS_BAD_REQUEST); +public final UnsubscriptionDeniedError UNSUBSCRIPTION_DENIED_ERROR = error UnsubscriptionDeniedError( + "Unsubscription denied", + statusCode = http:STATUS_BAD_REQUEST +); # Record to represent the client configuration for the HubClient/PublisherClient. # From caa6c05596bdde9732fd101a9c7ae221dca19854 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Thu, 2 Jun 2022 19:33:04 +0530 Subject: [PATCH 07/16] Migrate publisher client API to support status-code in response --- ballerina/publisher_client.bal | 36 ++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/ballerina/publisher_client.bal b/ballerina/publisher_client.bal index f8ab5f09..0dd8b2a7 100644 --- a/ballerina/publisher_client.bal +++ b/ballerina/publisher_client.bal @@ -49,12 +49,15 @@ public client class PublisherClient { if registrationResponse is http:Response { TopicRegistrationSuccess|error clientResponse = handleResponse(registrationResponse, topic, REGISTER_TOPIC_ACTION); if clientResponse is error { - return error TopicRegistrationError(clientResponse.message(), clientResponse); + return error TopicRegistrationError(clientResponse.message(), clientResponse, + statusCode = registrationResponse.statusCode); } else { return clientResponse; } } else { - return error TopicRegistrationError(string `"Error sending topic registration request for topic [${topic}]`, registrationResponse); + return error TopicRegistrationError( + string `Error sending topic registration request for topic [${topic}]`, registrationResponse, + statusCode = http:STATUS_INTERNAL_SERVER_ERROR); } } @@ -71,12 +74,15 @@ public client class PublisherClient { if deregistrationResponse is http:Response { TopicDeregistrationSuccess|error clientResponse = handleResponse(deregistrationResponse, topic, DEREGISTER_TOPIC_ACTION); if clientResponse is error { - return error TopicDeregistrationError(clientResponse.message(), clientResponse); + return error TopicDeregistrationError(clientResponse.message(), clientResponse, + statusCode = deregistrationResponse.statusCode); } else { return clientResponse; } } else { - return error TopicDeregistrationError(string `Error sending topic deregistration request for topic [${topic}]`, deregistrationResponse); + return error TopicDeregistrationError( + string `Error sending topic deregistration request for topic [${topic}]`, deregistrationResponse, + statusCode = http:STATUS_INTERNAL_SERVER_ERROR); } } @@ -104,7 +110,7 @@ public client class PublisherClient { error? setContent = contentUpdateRequest.setContentType(contentType); if setContent is error { string errorMsg = string `Invalid content type is set, found ${contentType}`; - return error UpdateMessageError(errorMsg, setContent); + return error UpdateMessageError(errorMsg, setContent, statusCode = http:STATUS_BAD_REQUEST); } } string queryParams = string `${HUB_MODE}=${MODE_PUBLISH}&${HUB_TOPIC}=${topic}`; @@ -112,12 +118,15 @@ public client class PublisherClient { if contentPublishResponse is http:Response { Acknowledgement|error clientResponse = handleResponse(contentPublishResponse, topic, CONTENT_PUBLISH_ACTION); if clientResponse is error { - return error UpdateMessageError(clientResponse.message(), clientResponse); + return error UpdateMessageError(clientResponse.message(), clientResponse, + statusCode = contentPublishResponse.statusCode); } else { return clientResponse; } } else { - return error UpdateMessageError(string `Publish failed for topic [${topic}]`, contentPublishResponse); + return error UpdateMessageError( + string `Publish failed for topic [${topic}]`, contentPublishResponse, + statusCode = http:STATUS_INTERNAL_SERVER_ERROR); } } @@ -137,12 +146,14 @@ public client class PublisherClient { if notifyResponse is http:Response { Acknowledgement|error clientResponse = handleResponse(notifyResponse, topic, NOTIFY_UPDATE_ACTION); if clientResponse is error { - return error UpdateMessageError(clientResponse.message(), clientResponse); + return error UpdateMessageError(clientResponse.message(), clientResponse, statusCode = notifyResponse.statusCode); } else { return clientResponse; } } else { - return error UpdateMessageError(string `Update availability notification failed for topic [${topic}]`, notifyResponse); + return error UpdateMessageError( + string `Update availability notification failed for topic [${topic}]`, notifyResponse, + statusCode = http:STATUS_INTERNAL_SERVER_ERROR); } } } @@ -154,17 +165,18 @@ isolated function handleResponse(http:Response response, string topic, string ac string errorMsg = string `Error occurred while executing ${action} action for topic [${topic}], Status code : ${response.statusCode}, payload : ${responsePayload}`; return error(errorMsg); } else { - map? params = getFormData(responsePayload); + map params = getFormData(responsePayload); if params[HUB_MODE] == MODE_ACCEPTED { CommonResponse successResult = { + statusCode: response.statusCode, headers: getHeaders(response), body: params }; return successResult; } else { string? failureReason = params[HUB_REASON]; - string nmm = string `Unknown error occurred while executing ${action} action for topic [${topic}]`; - string errorMsg = failureReason is string ? failureReason : nmm; + string constructedErrorMsg = string `Unknown error occurred while executing ${action} action for topic [${topic}]`; + string errorMsg = failureReason is string ? failureReason : constructedErrorMsg; return error(errorMsg); } } From e049c8f6bffe3db6c1b96f0a7586d25dd91243a2 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Thu, 2 Jun 2022 19:38:32 +0530 Subject: [PATCH 08/16] Migrate hub client API to support statusCode in CommonResponse --- ballerina/hub_client.bal | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ballerina/hub_client.bal b/ballerina/hub_client.bal index a1652e6a..13bc9c6f 100644 --- a/ballerina/hub_client.bal +++ b/ballerina/hub_client.bal @@ -78,7 +78,7 @@ public client class HubClient { request.setPayload(payload); error? result = request.setContentType(contentType); if (result is error) { - return error Error("Error occurred while setting content type", result); + return error Error("Error occurred while setting content type", result, statusCode = http:STATUS_BAD_REQUEST); } request.setHeader(LINK, self.hubLinks); string? secret = self.secret; @@ -87,7 +87,7 @@ public client class HubClient { if hash is byte[] { request.setHeader(X_HUB_SIGNATURE, string `${SHA256_HMAC}=${hash.toBase16()}`); } else { - return error Error("Error retrieving content signature", hash); + return error Error("Error retrieving content signature", hash, statusCode = http:STATUS_BAD_REQUEST); } } http:Response|error response = self.httpClient->post("/", request); @@ -95,7 +95,7 @@ public client class HubClient { return processSubscriberResponse(response, self.topic); } else { string errorMsg = string `Content distribution failed for topic [${self.topic}]`; - return error ContentDeliveryError(errorMsg); + return error ContentDeliveryError(errorMsg, statusCode = http:STATUS_INTERNAL_SERVER_ERROR); } } } @@ -156,24 +156,26 @@ isolated function processSubscriberResponse(http:Response response, string topic map responseHeaders = retrieveResponseHeaders(response); if responseContentType.trim().length() > 1 { return { + statusCode: status, headers: responseHeaders, mediaType: responseContentType, body: retrieveResponseBody(response, responseContentType) }; } else { return { + statusCode: status, headers: responseHeaders }; } } else if status == http:STATUS_GONE { // HTTP 410 is used to communicate that subscriber no longer need to continue the subscription string errorMsg = string `Subscription to topic [${topic}] is terminated by the subscriber`; - return error SubscriptionDeletedError(errorMsg); + return error SubscriptionDeletedError(errorMsg, statusCode = status); } else { var result = response.getTextPayload(); string textPayload = result is string ? result : ""; string errorMsg = string `Error occurred distributing updated content: ${textPayload}`; - return error ContentDeliveryError(errorMsg); + return error ContentDeliveryError(errorMsg, statusCode = status); } } From 9b58c91bb8043e5f333960e6a2b2ed815974740b Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Thu, 2 Jun 2022 19:50:46 +0530 Subject: [PATCH 09/16] Add status code for listener life-cycle events --- ballerina/commons.bal | 6 ++++++ ballerina/hub_listener.bal | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ballerina/commons.bal b/ballerina/commons.bal index ef20f108..c4f63a4c 100644 --- a/ballerina/commons.bal +++ b/ballerina/commons.bal @@ -48,6 +48,12 @@ const string DEREGISTER_TOPIC_ACTION = "deregister-topic"; const string CONTENT_PUBLISH_ACTION = "publish-content"; const string NOTIFY_UPDATE_ACTION = "notify-update"; +const int LISTENER_INIT_ERROR = -1; +const int LISTENER_ATTACH_ERROR = -2; +const int LISTENER_START_ERROR = -3; +const int LISTENER_DETACH_ERROR = -4; +const int LISTENER_STOP_ERROR = -5; + # Options to compress using Gzip or deflate. # # `AUTO`: When service behaves as a HTTP gateway inbound request/response accept-encoding option is set as the diff --git a/ballerina/hub_listener.bal b/ballerina/hub_listener.bal index 5ea9af09..4ed69740 100644 --- a/ballerina/hub_listener.bal +++ b/ballerina/hub_listener.bal @@ -39,7 +39,7 @@ public class Listener { if httpListener is http:Listener { self.httpListener = httpListener; } else { - return error Error("Listener initialization failed", httpListener); + return error Error("Listener initialization failed", httpListener, statusCode = LISTENER_INIT_ERROR); } } else { self.httpListener = listenTo; @@ -78,7 +78,7 @@ public class Listener { } error? result = self.httpListener.attach( self.httpService, name); if (result is error) { - return error Error("Error occurred while attaching the service", result); + return error Error("Error occurred while attaching the service", result, statusCode = LISTENER_ATTACH_ERROR); } } @@ -116,7 +116,7 @@ public class Listener { public isolated function detach(Service s) returns Error? { error? result = self.httpListener.detach( self.httpService); if (result is error) { - return error Error("Error occurred while detaching the service", result); + return error Error("Error occurred while detaching the service", result, statusCode = LISTENER_DETACH_ERROR); } } @@ -129,7 +129,7 @@ public class Listener { public isolated function 'start() returns Error? { error? listenerError = self.httpListener.'start(); if (listenerError is error) { - return error Error("Error occurred while starting the service", listenerError); + return error Error("Error occurred while starting the service", listenerError, statusCode = LISTENER_START_ERROR); } } @@ -142,7 +142,7 @@ public class Listener { public isolated function gracefulStop() returns Error? { error? result = self.httpListener.gracefulStop(); if (result is error) { - return error Error("Error occurred while stopping the service", result); + return error Error("Error occurred while stopping the service", result, statusCode = LISTENER_STOP_ERROR); } } @@ -155,7 +155,7 @@ public class Listener { public isolated function immediateStop() returns Error? { error? result = self.httpListener.immediateStop(); if (result is error) { - return error Error("Error occurred while stopping the service", result); + return error Error("Error occurred while stopping the service", result, statusCode = LISTENER_STOP_ERROR); } } } From 4f7a0cfb74377103b3e9dfca2522386b0369e4fd Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Thu, 2 Jun 2022 19:52:52 +0530 Subject: [PATCH 10/16] Add client init error code --- ballerina/commons.bal | 1 + ballerina/utils.bal | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ballerina/commons.bal b/ballerina/commons.bal index c4f63a4c..f564839b 100644 --- a/ballerina/commons.bal +++ b/ballerina/commons.bal @@ -53,6 +53,7 @@ const int LISTENER_ATTACH_ERROR = -2; const int LISTENER_START_ERROR = -3; const int LISTENER_DETACH_ERROR = -4; const int LISTENER_STOP_ERROR = -5; +const int CLIENT_INIT_ERROR = -10; # Options to compress using Gzip or deflate. # diff --git a/ballerina/utils.bal b/ballerina/utils.bal index c47931ba..24315c9a 100644 --- a/ballerina/utils.bal +++ b/ballerina/utils.bal @@ -129,6 +129,6 @@ isolated function retrieveHttpClient(string url, http:ClientConfiguration config if (clientEp is http:Client) { return clientEp; } else { - return error Error("Client initialization failed", clientEp); + return error Error("Client initialization failed", clientEp, statusCode = CLIENT_INIT_ERROR); } } From 99bf7ec145d1055e38605563e4ce1ccbb5862cb1 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Thu, 2 Jun 2022 19:58:24 +0530 Subject: [PATCH 11/16] Migrate hub API to use statusCode in CommonResponse --- ballerina/content_distribution.bal | 2 +- ballerina/http_service.bal | 2 +- ballerina/subscription.bal | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ballerina/content_distribution.bal b/ballerina/content_distribution.bal index b8e65542..3d13266d 100644 --- a/ballerina/content_distribution.bal +++ b/ballerina/content_distribution.bal @@ -65,7 +65,7 @@ isolated function retrieveRequestBody(string contentType, http:Request request) return check request.getBinaryPayload(); } _ => { - return error Error("Requested content type is not supported"); + return error Error("Requested content type is not supported", statusCode = http:STATUS_BAD_REQUEST); } } } diff --git a/ballerina/http_service.bal b/ballerina/http_service.bal index 156e94ed..191b2ab7 100644 --- a/ballerina/http_service.bal +++ b/ballerina/http_service.bal @@ -194,6 +194,6 @@ isolated function handleResult(http:Caller caller, http:Response|error result) r isolated function respondToRequest(http:Caller caller, http:Response response) returns Error? { http:ListenerError? responseError = caller->respond(response); if responseError is http:ListenerError { - return error Error("Error occurred while responding to the request ", responseError); + return error Error("Error occurred while responding to the request ", responseError, statusCode = http:STATUS_INTERNAL_SERVER_ERROR); } } diff --git a/ballerina/subscription.bal b/ballerina/subscription.bal index 848d453b..a440da44 100644 --- a/ballerina/subscription.bal +++ b/ballerina/subscription.bal @@ -119,7 +119,7 @@ isolated function validateSubscription(boolean isRemoteMethodAvailable, Subscrip return adaptor.callOnSubscriptionValidationMethod(message, headers); } else { if !message.hubCallback.startsWith("http://") && !message.hubCallback.startsWith("https://") { - return error SubscriptionDeniedError("Invalid hub.callback param in the request."); + return error SubscriptionDeniedError("Invalid hub.callback param in the request.", statusCode = http:STATUS_BAD_REQUEST); } } } @@ -206,7 +206,7 @@ isolated function validateUnsubscription(boolean isRemoteMethodAvailable, Unsubs return adaptor.callOnUnsubscriptionValidationMethod(message, headers); } else { if !message.hubCallback.startsWith("http://") && !message.hubCallback.startsWith("https://") { - return error SubscriptionDeniedError("Invalid hub.callback param in the request."); + return error SubscriptionDeniedError("Invalid hub.callback param in the request.", statusCode = http:STATUS_BAD_REQUEST); } } } From 416d69de865c80053a920f4861395cc46e67dffb Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Fri, 3 Jun 2022 03:25:39 +0530 Subject: [PATCH 12/16] Update test cases --- .../tests/additional_error_details_test.bal | 18 +++++--- ballerina/tests/basic_hub_test.bal | 44 ++++++++++--------- ballerina/tests/hub_authentication_test.bal | 10 ++--- .../tests/hub_with_header_details_test.bal | 5 ++- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/ballerina/tests/additional_error_details_test.bal b/ballerina/tests/additional_error_details_test.bal index f151d169..c7338513 100644 --- a/ballerina/tests/additional_error_details_test.bal +++ b/ballerina/tests/additional_error_details_test.bal @@ -26,23 +26,26 @@ Service hubServiceToTestAdditionalErrorDetails = service object { isolated remote function onRegisterTopic(TopicRegistration message) returns TopicRegistrationError { return error TopicRegistrationError("Topic registration failed!", + statusCode = http:STATUS_OK, body = { "hub.additional.details": "Feature is not supported in the hub"}); } isolated remote function onDeregisterTopic(TopicDeregistration message) returns TopicDeregistrationError { - return error TopicDeregistrationError("Topic deregistration failed!"); + return error TopicDeregistrationError("Topic deregistration failed!", statusCode = http:STATUS_OK); } isolated remote function onUpdateMessage(UpdateMessage msg) returns UpdateMessageError { - return error UpdateMessageError("Error in accessing content", - body = { "hub.additiona.details": "Content update failed!"}); + return error UpdateMessageError("Error in accessing content", + statusCode = http:STATUS_BAD_REQUEST, + body = { "hub.additiona.details": "Content update failed!"}); } isolated remote function onSubscription(Subscription msg) returns SubscriptionAccepted|BadSubscriptionError|InternalSubscriptionError { SubscriptionAccepted successResult = { + statusCode: http:STATUS_ACCEPTED, body: >{ isSuccess: "true" } @@ -50,14 +53,14 @@ Service hubServiceToTestAdditionalErrorDetails = service object { if (msg.hubTopic == "test") { return successResult; } else { - return error BadSubscriptionError("Bad subscription"); + return error BadSubscriptionError("Bad subscription", statusCode = http:STATUS_BAD_REQUEST); } } isolated remote function onSubscriptionValidation(Subscription msg) returns SubscriptionDeniedError? { if (msg.hubTopic != "test") { - return error SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return error SubscriptionDeniedError("Denied subscription for topic 'test1'", statusCode = http:STATUS_BAD_REQUEST); } } @@ -69,20 +72,21 @@ Service hubServiceToTestAdditionalErrorDetails = service object { returns UnsubscriptionAccepted|BadUnsubscriptionError|InternalUnsubscriptionError { if (msg.hubTopic == "test") { UnsubscriptionAccepted successResult = { + statusCode: http:STATUS_ACCEPTED, body: >{ isSuccess: "true" } }; return successResult; } else { - return error BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return error BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'", statusCode = http:STATUS_BAD_REQUEST); } } isolated remote function onUnsubscriptionValidation(Unsubscription msg) returns UnsubscriptionDeniedError? { if (msg.hubTopic != "test") { - return error UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return error UnsubscriptionDeniedError("Denied subscription for topic 'test1'", statusCode = http:STATUS_BAD_REQUEST); } return (); } diff --git a/ballerina/tests/basic_hub_test.bal b/ballerina/tests/basic_hub_test.bal index c3a633e5..a8d40342 100644 --- a/ballerina/tests/basic_hub_test.bal +++ b/ballerina/tests/basic_hub_test.bal @@ -28,46 +28,49 @@ service /websubhub on functionWithArgumentsListener { returns TopicRegistrationSuccess|TopicRegistrationError { if (message.topic == "test") { TopicRegistrationSuccess successResult = { + statusCode: http:STATUS_OK, body: >{ isSuccess: "true" } }; return successResult; } else { - return error TopicRegistrationError("Registration Failed!"); + return error TopicRegistrationError("Registration Failed!", statusCode = http:STATUS_OK); } } isolated remote function onDeregisterTopic(TopicDeregistration message) returns TopicDeregistrationSuccess|TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return { + statusCode: http:STATUS_OK, + body: > { + isDeregisterSuccess: "true" + } + }; } else { - return error TopicDeregistrationError("Topic Deregistration Failed!"); + return error TopicDeregistrationError("Topic Deregistration Failed!", statusCode = http:STATUS_OK); } } isolated remote function onUpdateMessage(UpdateMessage msg) returns Acknowledgement|UpdateMessageError { - Acknowledgement ack = {}; if (msg.hubTopic == "test") { - return ack; - } else if (!(msg.content is ())) { - return ack; + return { + statusCode: http:STATUS_OK + }; + } else if msg.content !is () { + return { + statusCode: http:STATUS_OK + }; } else { - return error UpdateMessageError("Error in accessing content"); + return error UpdateMessageError("Error in accessing content", statusCode = http:STATUS_BAD_REQUEST); } } - isolated remote function onSubscription(Subscription msg) - returns SubscriptionAccepted|SubscriptionPermanentRedirect|SubscriptionTemporaryRedirect - |BadSubscriptionError|InternalSubscriptionError { + isolated remote function onSubscription(Subscription msg) returns SubscriptionAccepted|BadSubscriptionError { SubscriptionAccepted successResult = { + statusCode: http:STATUS_ACCEPTED, body: >{ isSuccess: "true" } @@ -77,14 +80,14 @@ service /websubhub on functionWithArgumentsListener { } else if (msg.hubTopic == "test1") { return successResult; } else { - return error BadSubscriptionError("Bad subscription"); + return error BadSubscriptionError("Bad subscription", statusCode = http:STATUS_BAD_REQUEST); } } isolated remote function onSubscriptionValidation(Subscription msg) returns SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return error SubscriptionDeniedError("Denied subscription for topic 'test1'", statusCode = http:STATUS_BAD_REQUEST); } return (); } @@ -97,20 +100,21 @@ service /websubhub on functionWithArgumentsListener { returns UnsubscriptionAccepted|BadUnsubscriptionError|InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { UnsubscriptionAccepted successResult = { + statusCode: http:STATUS_ACCEPTED, body: >{ isSuccess: "true" } }; return successResult; } else { - return error BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return error BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'", statusCode = http:STATUS_BAD_REQUEST); } } isolated remote function onUnsubscriptionValidation(Unsubscription msg) returns UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return error UnsubscriptionDeniedError("Denied subscription for topic 'test1'", statusCode = http:STATUS_BAD_REQUEST); } return (); } diff --git a/ballerina/tests/hub_authentication_test.bal b/ballerina/tests/hub_authentication_test.bal index 5b47465e..318247cc 100644 --- a/ballerina/tests/hub_authentication_test.bal +++ b/ballerina/tests/hub_authentication_test.bal @@ -43,7 +43,7 @@ service /websubhub on securedListener { returns TopicRegistrationSuccess|TopicRegistrationError { string? auth = doAuth(req); if (auth is string) { - return error TopicRegistrationError(auth); + return error TopicRegistrationError(auth, statusCode = http:STATUS_UNAUTHORIZED); } log:printDebug("Received topic-registration request ", message = message); return TOPIC_REGISTRATION_SUCCESS; @@ -53,7 +53,7 @@ service /websubhub on securedListener { returns TopicDeregistrationSuccess|TopicDeregistrationError { string? auth = doAuth(req); if (auth is string) { - return error TopicDeregistrationError(auth); + return error TopicDeregistrationError(auth, statusCode = http:STATUS_UNAUTHORIZED); } log:printDebug("Received topic-deregistration request ", message = message); return TOPIC_DEREGISTRATION_SUCCESS; @@ -63,7 +63,7 @@ service /websubhub on securedListener { returns Acknowledgement|UpdateMessageError { string? auth = doAuth(req); if (auth is string) { - return error UpdateMessageError(auth); + return error UpdateMessageError(auth, statusCode = http:STATUS_UNAUTHORIZED); } log:printDebug("Received content-update request ", message = message); return ACKNOWLEDGEMENT; @@ -73,7 +73,7 @@ service /websubhub on securedListener { returns SubscriptionAccepted|InternalSubscriptionError { string? auth = doAuth(req); if (auth is string) { - return error InternalSubscriptionError(auth); + return error InternalSubscriptionError(auth, statusCode = http:STATUS_UNAUTHORIZED); } log:printDebug("Received subscription request ", message = message); return SUBSCRIPTION_ACCEPTED; @@ -86,7 +86,7 @@ service /websubhub on securedListener { returns UnsubscriptionAccepted|InternalUnsubscriptionError { string? auth = doAuth(req); if (auth is string) { - return error InternalUnsubscriptionError(auth); + return error InternalUnsubscriptionError(auth, statusCode = http:STATUS_UNAUTHORIZED); } log:printDebug("Received unsubscription request ", message = message); return UNSUBSCRIPTION_ACCEPTED; diff --git a/ballerina/tests/hub_with_header_details_test.bal b/ballerina/tests/hub_with_header_details_test.bal index f53d03e8..ca8fadd3 100644 --- a/ballerina/tests/hub_with_header_details_test.bal +++ b/ballerina/tests/hub_with_header_details_test.bal @@ -26,6 +26,7 @@ Service hubWithHeaderDetails = service object { returns TopicRegistrationSuccess { log:printDebug("Executing topic registration", message = message, headers = headers.getHeaderNames()); TopicRegistrationSuccess successResult = { + statusCode: http:STATUS_OK, body: >{ isSuccess: "true" } @@ -36,9 +37,9 @@ Service hubWithHeaderDetails = service object { isolated remote function onDeregisterTopic(TopicDeregistration message, http:Headers headers) returns TopicDeregistrationSuccess { log:printDebug("Executing topic de-registration", message = message, headers = headers.getHeaderNames()); - map body = { isDeregisterSuccess: "true" }; TopicDeregistrationSuccess deregisterResult = { - body + statusCode: http:STATUS_OK, + body: > { isDeregisterSuccess: "true" } }; return deregisterResult; } From 39b3a7dad64f8d0b081a930d009c919588d2c996 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Fri, 3 Jun 2022 10:12:47 +0530 Subject: [PATCH 13/16] Update logic to use statusCode from API response to be used as HTTP response status code --- ballerina/content_distribution.bal | 8 ++++---- ballerina/subscription.bal | 20 ++++++++------------ ballerina/topic_registration.bal | 12 ++++++------ ballerina/utils.bal | 12 ++++++------ 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/ballerina/content_distribution.bal b/ballerina/content_distribution.bal index 3d13266d..ab70d2bc 100644 --- a/ballerina/content_distribution.bal +++ b/ballerina/content_distribution.bal @@ -76,11 +76,11 @@ isolated function processResult(Acknowledgement|error result) returns http:Respo if (result is Acknowledgement) { response.setTextPayload("hub.mode=accepted", mime:APPLICATION_FORM_URLENCODED); } else if (result is UpdateMessageError) { - var errorDetails = result.detail(); - updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); + CommonResponse errorDetails = result.detail(); + updateErrorResponse(response, errorDetails, result.message()); } else { - var errorDetails = UPDATE_MESSAGE_ERROR.detail(); - updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); + CommonResponse errorDetails = UPDATE_MESSAGE_ERROR.detail(); + updateErrorResponse(response, errorDetails, result.message()); } return response; } diff --git a/ballerina/subscription.bal b/ballerina/subscription.bal index a440da44..0ca8a8d8 100644 --- a/ballerina/subscription.bal +++ b/ballerina/subscription.bal @@ -67,14 +67,12 @@ isolated function processOnSubscriptionResult(SubscriptionAccepted|error result) response.statusCode = http:STATUS_ACCEPTED; return response; } else if result is BadSubscriptionError { - response.statusCode = http:STATUS_BAD_REQUEST; - var errorDetails = result.detail(); - updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); + CommonResponse errorDetails = result.detail(); + updateErrorResponse(response, errorDetails, result.message()); return response; } else { - response.statusCode = http:STATUS_INTERNAL_SERVER_ERROR; - var errorDetails = result is InternalSubscriptionError ? result.detail() : INTERNAL_SUBSCRIPTION_ERROR.detail(); - updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); + CommonResponse errorDetails = result is InternalSubscriptionError ? result.detail() : INTERNAL_SUBSCRIPTION_ERROR.detail(); + updateErrorResponse(response, errorDetails, result.message()); return response; } } @@ -157,14 +155,12 @@ isolated function processOnUnsubscriptionResult(UnsubscriptionAccepted|error res response.statusCode = http:STATUS_ACCEPTED; return response; } else if result is BadUnsubscriptionError { - response.statusCode = http:STATUS_BAD_REQUEST; - var errorDetails = result.detail(); - updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); + CommonResponse errorDetails = result.detail(); + updateErrorResponse(response, errorDetails, result.message()); return response; } else { - response.statusCode = http:STATUS_INTERNAL_SERVER_ERROR; - var errorDetails = result is InternalSubscriptionError ? result.detail() : INTERNAL_UNSUBSCRIPTION_ERROR.detail(); - updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); + CommonResponse errorDetails = result is InternalSubscriptionError ? result.detail() : INTERNAL_UNSUBSCRIPTION_ERROR.detail(); + updateErrorResponse(response, errorDetails, result.message()); return response; } } diff --git a/ballerina/topic_registration.bal b/ballerina/topic_registration.bal index 05f49159..608723f0 100644 --- a/ballerina/topic_registration.bal +++ b/ballerina/topic_registration.bal @@ -25,10 +25,10 @@ isolated function processTopicRegistration(http:Headers headers, map par TopicRegistrationSuccess|error result = adaptor.callRegisterMethod(msg, headers); http:Response response = new; if result is TopicRegistrationSuccess { - updateSuccessResponse(response, result["body"], result["headers"]); + updateSuccessResponse(response, result); } else { - var errorDetails = result is TopicRegistrationError ? result.detail() : TOPIC_REGISTRATION_ERROR.detail(); - updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); + CommonResponse errorDetails = result is TopicRegistrationError ? result.detail() : TOPIC_REGISTRATION_ERROR.detail(); + updateErrorResponse(response, errorDetails, result.message()); } return response; } @@ -42,10 +42,10 @@ isolated function processTopicDeregistration(http:Headers headers, map p TopicDeregistrationSuccess|error result = adaptor.callDeregisterMethod(msg, headers); http:Response response = new; if result is TopicDeregistrationSuccess { - updateSuccessResponse(response, result["body"], result["headers"]); + updateSuccessResponse(response, result); } else { - var errorDetails = result is TopicDeregistrationError ? result.detail() : TOPIC_DEREGISTRATION_ERROR.detail(); - updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); + CommonResponse errorDetails = result is TopicDeregistrationError ? result.detail() : TOPIC_DEREGISTRATION_ERROR.detail(); + updateErrorResponse(response, errorDetails, result.message()); } return response; } diff --git a/ballerina/utils.bal b/ballerina/utils.bal index 24315c9a..5435bf49 100644 --- a/ballerina/utils.bal +++ b/ballerina/utils.bal @@ -61,14 +61,14 @@ isolated function generateQueryString(string callbackUrl, [string, string?][] pa return (strings:includes(callbackUrl, ("?")) ? "&" : "?") + strings:'join("&", ...keyValPairs); } -isolated function updateErrorResponse(http:Response response, anydata? messageBody, - map? headers, string reason) { - updateHubResponse(response, "denied", messageBody, headers, reason); +isolated function updateErrorResponse(http:Response httpResponse, CommonResponse response, string reason) { + httpResponse.statusCode = response.statusCode; + updateHubResponse(httpResponse, "denied", response.body, response.headers, reason); } -isolated function updateSuccessResponse(http:Response response, anydata? messageBody, - map? headers) { - updateHubResponse(response, "accepted", messageBody, headers); +isolated function updateSuccessResponse(http:Response httpResponse, CommonResponse response) { + httpResponse.statusCode = response.statusCode; + updateHubResponse(httpResponse, "accepted", response.body, response.headers); } isolated function updateHubResponse(http:Response response, string hubMode, From 2fce1310037bf39b737cda86dd5cd1cc808da25e Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Fri, 3 Jun 2022 10:52:06 +0530 Subject: [PATCH 14/16] Migrate compiler plugin test cases to support CommonResponse API change --- .../ballerina_sources/sample_1/service.bal | 52 ++++-------- .../ballerina_sources/sample_10/service.bal | 46 +++++------ .../ballerina_sources/sample_11/service.bal | 46 +++++------ .../ballerina_sources/sample_12/service.bal | 34 +++----- .../ballerina_sources/sample_13/service.bal | 34 +++----- .../ballerina_sources/sample_14/service.bal | 37 +++------ .../ballerina_sources/sample_15/service.bal | 36 +++------ .../ballerina_sources/sample_16/service.bal | 35 +++----- .../ballerina_sources/sample_17/service.bal | 80 ++++++++----------- .../ballerina_sources/sample_18/service.bal | 80 ++++++++----------- .../ballerina_sources/sample_19/service.bal | 52 ++++-------- .../ballerina_sources/sample_2/service.bal | 50 ++++-------- .../ballerina_sources/sample_20/service.bal | 52 ++++-------- .../ballerina_sources/sample_3/service.bal | 43 +++------- .../ballerina_sources/sample_4/service.bal | 50 ++++-------- .../ballerina_sources/sample_5/service.bal | 41 +++------- .../ballerina_sources/sample_6/service.bal | 40 +++------- .../ballerina_sources/sample_7/service.bal | 40 +++------- .../ballerina_sources/sample_8/service.bal | 43 ++++------ .../ballerina_sources/sample_9/service.bal | 36 ++++----- 20 files changed, 307 insertions(+), 620 deletions(-) diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_1/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_1/service.bal index 00309afb..a32283d0 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_1/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_1/service.bal @@ -20,68 +20,51 @@ import ballerina/io; listener websubhub:Listener functionWithArgumentsListener = new(9090); service /websubhub on functionWithArgumentsListener { - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { - websubhub:TopicRegistrationSuccess successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:TOPIC_REGISTRATION_SUCCESS; } else { - return error websubhub:TopicRegistrationError("Registration Failed!"); + return websubhub:TOPIC_REGISTRATION_ERROR; } } isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; - } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; + } else { + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - websubhub:Acknowledgement ack = {}; if (msg.hubTopic == "test") { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else if (!(msg.content is ())) { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else { - return error websubhub:UpdateMessageError("Error in accessing content"); + return websubhub:UPDATE_MESSAGE_ERROR; } } isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -93,21 +76,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_10/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_10/service.bal index 804dad66..1ea58f62 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_10/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_10/service.bal @@ -29,7 +29,6 @@ listener websubhub:Listener functionWithArgumentsListener = new(httpListener, { }); service /websubhub on functionWithArgumentsListener { - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { @@ -39,46 +38,42 @@ service /websubhub on functionWithArgumentsListener { } } - isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message, http:Headers headers) + isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } - isolated remote function onUpdateMessage(websubhub:UpdateMessage message) + isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - return websubhub:ACKNOWLEDGEMENT; + if (msg.hubTopic == "test") { + return websubhub:ACKNOWLEDGEMENT; + } else if (!(msg.content is ())) { + return websubhub:ACKNOWLEDGEMENT; + } else { + return websubhub:UPDATE_MESSAGE_ERROR; + } } isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -90,21 +85,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_11/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_11/service.bal index baa58711..eb0646c2 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_11/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_11/service.bal @@ -29,7 +29,6 @@ websubhub:ListenerConfiguration listenerConfig = { }; service /websubhub on new websubhub:Listener(httpListener, listenerConfig) { - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { @@ -39,46 +38,42 @@ service /websubhub on new websubhub:Listener(httpListener, listenerConfig) { } } - isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message, http:Headers headers) + isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } - isolated remote function onUpdateMessage(websubhub:UpdateMessage message) + isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - return websubhub:ACKNOWLEDGEMENT; + if (msg.hubTopic == "test") { + return websubhub:ACKNOWLEDGEMENT; + } else if (!(msg.content is ())) { + return websubhub:ACKNOWLEDGEMENT; + } else { + return websubhub:UPDATE_MESSAGE_ERROR; + } } isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -90,21 +85,16 @@ service /websubhub on new websubhub:Listener(httpListener, listenerConfig) { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_12/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_12/service.bal index ddff881f..a823f849 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_12/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_12/service.bal @@ -21,7 +21,6 @@ import ballerina/io; listener http:Listener httpListener = new http:Listener(10012); service /websubhub on new foo:Listener(httpListener) { - isolated remote function onRegisterTopic(foo:TopicRegistration message) returns foo:TopicRegistrationSuccess|foo:TopicRegistrationError { if (message.topic == "test") { @@ -33,15 +32,10 @@ service /websubhub on new foo:Listener(httpListener) { isolated remote function onDeregisterTopic(foo:TopicDeregistration message, http:Headers headers) returns foo:TopicDeregistrationSuccess|foo:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - foo:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return foo:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error foo:TopicDeregistrationError("Topic Deregistration Failed!"); + return foo:TOPIC_DEREGISTRATION_ERROR; } } @@ -53,24 +47,19 @@ service /websubhub on new foo:Listener(httpListener) { isolated remote function onSubscription(foo:Subscription msg) returns foo:SubscriptionAccepted|foo:SubscriptionPermanentRedirect|foo:SubscriptionTemporaryRedirect |foo:BadSubscriptionError|foo:InternalSubscriptionError { - foo:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return foo:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return foo:SUBSCRIPTION_ACCEPTED; } else { - return error foo:BadSubscriptionError("Bad subscription"); + return foo:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(foo:Subscription msg) returns foo:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error foo:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return foo:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -82,21 +71,16 @@ service /websubhub on new foo:Listener(httpListener) { isolated remote function onUnsubscription(foo:Unsubscription msg) returns foo:UnsubscriptionAccepted|foo:BadUnsubscriptionError|foo:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - foo:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return foo:UNSUBSCRIPTION_ACCEPTED; } else { - return error foo:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return foo:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(foo:Unsubscription msg) returns foo:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error foo:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return foo:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_13/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_13/service.bal index 5eb220a9..06d8bee7 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_13/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_13/service.bal @@ -21,7 +21,6 @@ import ballerina/io; listener http:Listener httpListener = new http:Listener(10012); service /websubhub on new foo:Listener(httpListener) { - isolated remote function onRegisterTopic(foo:TopicRegistration message) returns foo:TopicRegistrationSuccess|foo:TopicRegistrationError { if (message.topic == "test") { @@ -33,15 +32,10 @@ service /websubhub on new foo:Listener(httpListener) { isolated remote function onDeregisterTopic(foo:TopicDeregistration message, http:Headers headers) returns foo:TopicDeregistrationSuccess|foo:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - foo:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return foo:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error foo:TopicDeregistrationError("Topic Deregistration Failed!"); + return foo:TOPIC_DEREGISTRATION_ERROR; } } @@ -53,24 +47,19 @@ service /websubhub on new foo:Listener(httpListener) { isolated remote function onSubscription(foo:Subscription msg) returns foo:SubscriptionAccepted|foo:SubscriptionPermanentRedirect|foo:SubscriptionTemporaryRedirect |foo:BadSubscriptionError|foo:InternalSubscriptionError { - foo:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return foo:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return foo:SUBSCRIPTION_ACCEPTED; } else { - return error foo:BadSubscriptionError("Bad subscription"); + return foo:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(foo:Subscription msg) returns foo:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error foo:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return foo:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -82,21 +71,16 @@ service /websubhub on new foo:Listener(httpListener) { isolated remote function onUnsubscription(http:Headers headers, foo:Unsubscription msg) returns foo:UnsubscriptionAccepted|foo:BadUnsubscriptionError|foo:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - foo:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return foo:UNSUBSCRIPTION_ACCEPTED; } else { - return error foo:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return foo:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(foo:Unsubscription msg) returns foo:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error foo:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return foo:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_14/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_14/service.bal index bf8b9c1b..c84186c8 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_14/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_14/service.bal @@ -28,7 +28,6 @@ listener foo:Listener securedHub = new(9090, ); service /websubhub on securedHub { - isolated remote function onRegisterTopic(foo:TopicRegistration message) returns foo:TopicRegistrationSuccess|foo:TopicRegistrationError { if (message.topic == "test") { @@ -40,15 +39,10 @@ service /websubhub on securedHub { isolated remote function onDeregisterTopic(foo:TopicDeregistration message, http:Headers headers) returns foo:TopicDeregistrationSuccess|foo:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - foo:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return foo:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error foo:TopicDeregistrationError("Topic Deregistration Failed!"); + return foo:TOPIC_DEREGISTRATION_ERROR; } } @@ -60,24 +54,19 @@ service /websubhub on securedHub { isolated remote function onSubscription(foo:Subscription msg) returns foo:SubscriptionAccepted|foo:SubscriptionPermanentRedirect|foo:SubscriptionTemporaryRedirect |foo:BadSubscriptionError|foo:InternalSubscriptionError { - foo:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return foo:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return foo:SUBSCRIPTION_ACCEPTED; } else { - return error foo:BadSubscriptionError("Bad subscription"); + return foo:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(foo:Subscription msg) returns foo:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error foo:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return foo:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -89,26 +78,20 @@ service /websubhub on securedHub { isolated remote function onUnsubscription(foo:Unsubscription msg) returns foo:UnsubscriptionAccepted|foo:BadUnsubscriptionError|foo:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - foo:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return foo:UNSUBSCRIPTION_ACCEPTED; } else { - return error foo:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return foo:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(foo:Unsubscription msg) returns foo:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error foo:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return foo:UNSUBSCRIPTION_DENIED_ERROR; } return (); } isolated remote function onUnsubscriptionIntentVerified(foo:VerifiedUnsubscription msg){ - io:println("Unsubscription Intent verified invoked!"); - } + } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_15/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_15/service.bal index dd55471f..50d56ed6 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_15/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_15/service.bal @@ -28,7 +28,6 @@ listener foo:Listener securedHub = new(9090, ); service /websubhub on securedHub { - isolated remote function onRegisterTopic(foo:TopicRegistration message) returns foo:TopicRegistrationSuccess|foo:TopicRegistrationError|error { if (message.topic == "test") { @@ -40,15 +39,10 @@ service /websubhub on securedHub { isolated remote function onDeregisterTopic(foo:TopicDeregistration message, http:Headers headers) returns foo:TopicDeregistrationSuccess|foo:TopicDeregistrationError|error { - - map body = { isDeregisterSuccess: "true" }; - foo:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; - } else { - return error foo:TopicDeregistrationError("Topic Deregistration Failed!"); + return foo:TOPIC_DEREGISTRATION_SUCCESS; + } else { + return foo:TOPIC_DEREGISTRATION_ERROR; } } @@ -60,24 +54,19 @@ service /websubhub on securedHub { isolated remote function onSubscription(foo:Subscription msg) returns foo:SubscriptionAccepted|foo:SubscriptionPermanentRedirect|foo:SubscriptionTemporaryRedirect |foo:BadSubscriptionError|foo:InternalSubscriptionError|error { - foo:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return foo:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return foo:SUBSCRIPTION_ACCEPTED; } else { - return error foo:BadSubscriptionError("Bad subscription"); + return foo:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(foo:Subscription msg) returns foo:SubscriptionDeniedError|error? { if (msg.hubTopic == "test1") { - return error foo:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return foo:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -89,21 +78,16 @@ service /websubhub on securedHub { isolated remote function onUnsubscription(foo:Unsubscription msg) returns foo:UnsubscriptionAccepted|foo:BadUnsubscriptionError|foo:InternalUnsubscriptionError|error { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - foo:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return foo:UNSUBSCRIPTION_ACCEPTED; } else { - return error foo:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return foo:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(foo:Unsubscription msg) returns foo:UnsubscriptionDeniedError|error? { if (msg.hubTopic == "test1") { - return error foo:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return foo:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_16/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_16/service.bal index 802114fb..0ac9e343 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_16/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_16/service.bal @@ -21,7 +21,6 @@ import ballerina/io; listener http:Listener httpListener = new http:Listener(10012); service /websubhub on new websubhub:Listener(httpListener) { - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError|error { if (message.topic == "test") { @@ -34,14 +33,10 @@ service /websubhub on new websubhub:Listener(httpListener) { isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message, http:Headers headers) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError|error { - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; - } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; + } else { + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } @@ -53,24 +48,19 @@ service /websubhub on new websubhub:Listener(httpListener) { isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError|error { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError|error? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -82,21 +72,16 @@ service /websubhub on new websubhub:Listener(httpListener) { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError|error { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError|error? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_17/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_17/service.bal index 196dd8de..4e25de2a 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_17/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_17/service.bal @@ -21,9 +21,8 @@ import ballerina/io; listener http:Listener httpListener = new http:Listener(10012); service /websubhub on new websubhub:Listener(httpListener) { - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) - returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError|error { + returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { return websubhub:TOPIC_REGISTRATION_SUCCESS; } else { @@ -31,82 +30,73 @@ service /websubhub on new websubhub:Listener(httpListener) { } } - isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message, http:Headers headers) - returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError|error { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; + isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message) + returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { if (message.topic == "test") { - return deregisterResult; - } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; + } else { + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } - isolated remote function onUpdateMessage(websubhub:UpdateMessage message) - returns websubhub:Acknowledgement|websubhub:UpdateMessageError|error { - return websubhub:ACKNOWLEDGEMENT; + isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) + returns websubhub:Acknowledgement|websubhub:UpdateMessageError { + if (msg.hubTopic == "test") { + return websubhub:ACKNOWLEDGEMENT; + } else if (!(msg.content is ())) { + return websubhub:ACKNOWLEDGEMENT; + } else { + return websubhub:UPDATE_MESSAGE_ERROR; + } } - + isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect - |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError|error { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - if (msg.hubTopic == "test" || msg.hubTopic == "test1") { - return successResult; + |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { + if (msg.hubTopic == "test") { + return websubhub:SUBSCRIPTION_ACCEPTED; + } else if (msg.hubTopic == "test1") { + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) - returns websubhub:SubscriptionDeniedError|error? { + returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } - isolated remote function onSubscriptionIntentVerified(websubhub:VerifiedSubscription msg) returns error? { + isolated remote function onSubscriptionIntentVerified(websubhub:VerifiedSubscription msg) { io:println("Subscription Intent verified invoked!"); - check self.subscriber(msg); - } - - isolated function subscriber(websubhub:VerifiedSubscription msg) returns error? { - } isolated remote function onUnsubscription(websubhub:Unsubscription msg) - returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError|error { + returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) - returns websubhub:UnsubscriptionDeniedError|error? { + returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } - isolated remote function onUnsubscriptionIntentVerified(websubhub:VerifiedUnsubscription msg) returns error? { + isolated remote function onUnsubscriptionIntentVerified(websubhub:VerifiedUnsubscription msg){ io:println("Unsubscription Intent verified invoked!"); - check self.unsubscriber(msg); + } + + isolated function subscriber(websubhub:VerifiedSubscription msg) returns error? { + } isolated function unsubscriber(websubhub:VerifiedUnsubscription msg) returns error? { diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_18/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_18/service.bal index eecdc14e..d2bfda37 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_18/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_18/service.bal @@ -23,9 +23,8 @@ import ballerina/jballerina.java; listener http:Listener httpListener = new http:Listener(10012); service /websubhub on new websubhub:Listener(httpListener) { - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) - returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError|error { + returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { return websubhub:TOPIC_REGISTRATION_SUCCESS; } else { @@ -33,82 +32,73 @@ service /websubhub on new websubhub:Listener(httpListener) { } } - isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message, http:Headers headers) - returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError|error { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; + isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message) + returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { if (message.topic == "test") { - return deregisterResult; - } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; + } else { + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } - isolated remote function onUpdateMessage(websubhub:UpdateMessage message) - returns websubhub:Acknowledgement|websubhub:UpdateMessageError|error { - return websubhub:ACKNOWLEDGEMENT; + isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) + returns websubhub:Acknowledgement|websubhub:UpdateMessageError { + if (msg.hubTopic == "test") { + return websubhub:ACKNOWLEDGEMENT; + } else if (!(msg.content is ())) { + return websubhub:ACKNOWLEDGEMENT; + } else { + return websubhub:UPDATE_MESSAGE_ERROR; + } } - + isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect - |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError|error { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - if (msg.hubTopic == "test" || msg.hubTopic == "test1") { - return successResult; + |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { + if (msg.hubTopic == "test") { + return websubhub:SUBSCRIPTION_ACCEPTED; + } else if (msg.hubTopic == "test1") { + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) - returns websubhub:SubscriptionDeniedError|error? { + returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } - isolated remote function onSubscriptionIntentVerified(websubhub:VerifiedSubscription msg) returns error? { + isolated remote function onSubscriptionIntentVerified(websubhub:VerifiedSubscription msg) { io:println("Subscription Intent verified invoked!"); - check self.subscriber(msg); - } - - isolated function subscriber(websubhub:VerifiedSubscription msg) returns error? { - } isolated remote function onUnsubscription(websubhub:Unsubscription msg) - returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError|error { + returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) - returns websubhub:UnsubscriptionDeniedError|error? { + returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } - isolated remote function onUnsubscriptionIntentVerified(websubhub:VerifiedUnsubscription msg) returns error? { + isolated remote function onUnsubscriptionIntentVerified(websubhub:VerifiedUnsubscription msg){ io:println("Unsubscription Intent verified invoked!"); - check self.unsubscriber(msg); + } + + isolated function subscriber(websubhub:VerifiedSubscription msg) returns error? { + } isolated function unsubscriber(websubhub:VerifiedUnsubscription msg) returns error? { diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_19/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_19/service.bal index 39f55997..1d3bd211 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_19/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_19/service.bal @@ -20,68 +20,51 @@ import ballerina/io; listener websubhub:Listener functionWithArgumentsListener = new(9090); service /websubhub on functionWithArgumentsListener { - isolated remote function onRegisterTopic(readonly & websubhub:TopicRegistration message) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { - websubhub:TopicRegistrationSuccess successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:TOPIC_REGISTRATION_SUCCESS; } else { - return error websubhub:TopicRegistrationError("Registration Failed!"); + return websubhub:TOPIC_REGISTRATION_ERROR; } } isolated remote function onDeregisterTopic(readonly & websubhub:TopicDeregistration message) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; - } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; + } else { + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } isolated remote function onUpdateMessage(readonly & websubhub:UpdateMessage msg) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - websubhub:Acknowledgement ack = {}; if (msg.hubTopic == "test") { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else if (!(msg.content is ())) { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else { - return error websubhub:UpdateMessageError("Error in accessing content"); + return websubhub:UPDATE_MESSAGE_ERROR; } } isolated remote function onSubscription(readonly & websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(readonly & websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -93,21 +76,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(readonly & websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(readonly & websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_2/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_2/service.bal index 134a3093..8f92aa14 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_2/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_2/service.bal @@ -20,68 +20,51 @@ import ballerina/io; listener websubhub:Listener functionWithArgumentsListener = new(9090); service /websubhub on functionWithArgumentsListener { - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { - websubhub:TopicRegistrationSuccess successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:TOPIC_REGISTRATION_SUCCESS; } else { - return error websubhub:TopicRegistrationError("Registration Failed!"); + return websubhub:TOPIC_REGISTRATION_ERROR; } } isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - websubhub:Acknowledgement ack = {}; if (msg.hubTopic == "test") { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else if (!(msg.content is ())) { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else { - return error websubhub:UpdateMessageError("Error in accessing content"); + return websubhub:UPDATE_MESSAGE_ERROR; } } isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -93,21 +76,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_20/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_20/service.bal index 988ea3ed..768de4ac 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_20/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_20/service.bal @@ -21,68 +21,51 @@ import ballerina/io; listener websubhub:Listener functionWithArgumentsListener = new(9090); service /websubhub on functionWithArgumentsListener { - isolated remote function onRegisterTopic(readonly & websubhub:TopicRegistration message, readonly & http:Headers headers) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { - websubhub:TopicRegistrationSuccess successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:TOPIC_REGISTRATION_SUCCESS; } else { - return error websubhub:TopicRegistrationError("Registration Failed!"); + return websubhub:TOPIC_REGISTRATION_ERROR; } } isolated remote function onDeregisterTopic(readonly & websubhub:TopicDeregistration message, readonly & http:Headers headers) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; - } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; + } else { + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } isolated remote function onUpdateMessage(readonly & websubhub:UpdateMessage msg, readonly & http:Headers headers) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - websubhub:Acknowledgement ack = {}; if (msg.hubTopic == "test") { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else if (!(msg.content is ())) { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else { - return error websubhub:UpdateMessageError("Error in accessing content"); + return websubhub:UPDATE_MESSAGE_ERROR; } } isolated remote function onSubscription(readonly & websubhub:Subscription msg, readonly & http:Headers headers) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(readonly & websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -94,21 +77,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(readonly & websubhub:Unsubscription msg, readonly & http:Headers headers) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(readonly & websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_3/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_3/service.bal index 222c3d94..0f3851c6 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_3/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_3/service.bal @@ -20,56 +20,40 @@ import ballerina/io; listener websubhub:Listener functionWithArgumentsListener = new(9090); service /websubhub on functionWithArgumentsListener { - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { - websubhub:TopicRegistrationSuccess successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:TOPIC_REGISTRATION_SUCCESS; } else { - return error websubhub:TopicRegistrationError("Registration Failed!"); + return websubhub:TOPIC_REGISTRATION_ERROR; } } isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -81,21 +65,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_4/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_4/service.bal index 1c683651..6a7b09ab 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_4/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_4/service.bal @@ -20,68 +20,51 @@ import ballerina/io; listener websubhub:Listener functionWithArgumentsListener = new(9090); service /websubhub on functionWithArgumentsListener { - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { - websubhub:TopicRegistrationSuccess successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:TOPIC_REGISTRATION_SUCCESS; } else { - return error websubhub:TopicRegistrationError("Registration Failed!"); + return websubhub:TOPIC_REGISTRATION_ERROR; } } isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - websubhub:Acknowledgement ack = {}; if (msg.hubTopic == "test") { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else if (!(msg.content is ())) { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else { - return error websubhub:UpdateMessageError("Error in accessing content"); + return websubhub:UPDATE_MESSAGE_ERROR; } } isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -93,21 +76,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_5/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_5/service.bal index 94a70b13..5d51a65f 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_5/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_5/service.bal @@ -25,7 +25,6 @@ public type MetaDetails record {| |}; service /websubhub on functionWithArgumentsListener { - isolated remote function onRegisterTopic(websubhub:TopicRegistration|MetaDetails message) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message is websubhub:TopicRegistration) { @@ -41,51 +40,40 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - websubhub:Acknowledgement ack = {}; if (msg.hubTopic == "test") { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else if (!(msg.content is ())) { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else { - return error websubhub:UpdateMessageError("Error in accessing content"); + return websubhub:UPDATE_MESSAGE_ERROR; } } isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -97,21 +85,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_6/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_6/service.bal index 633e2c69..f236b74b 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_6/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_6/service.bal @@ -39,51 +39,40 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message, http:Headers headers, SimpleObj metaData) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - websubhub:Acknowledgement ack = {}; if (msg.hubTopic == "test") { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else if (!(msg.content is ())) { - return ack; + return websubhub:ACKNOWLEDGEMENT; } else { - return error websubhub:UpdateMessageError("Error in accessing content"); + return websubhub:UPDATE_MESSAGE_ERROR; } } isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -95,21 +84,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_7/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_7/service.bal index 78ed2892..f98332d9 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_7/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_7/service.bal @@ -20,9 +20,8 @@ import ballerina/io; listener websubhub:Listener functionWithArgumentsListener = new(9090); -service /websubhub on functionWithArgumentsListener { - - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) +service /websubhub on functionWithArgumentsListener { + isolated remote function onRegisterTopic(websubhub:TopicRegistration message, http:Headers headers) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { return websubhub:TOPIC_REGISTRATION_SUCCESS; @@ -31,17 +30,12 @@ service /websubhub on functionWithArgumentsListener { } } - isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message, http:Headers headers) + isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } @@ -53,24 +47,19 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -82,21 +71,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_8/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_8/service.bal index 221e36f3..cd870f07 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_8/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_8/service.bal @@ -37,44 +37,40 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message, http:Headers headers) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } - isolated remote function onUpdateMessage(websubhub:UpdateMessage message) + isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - return websubhub:ACKNOWLEDGEMENT; + if (msg.hubTopic == "test") { + return websubhub:ACKNOWLEDGEMENT; + } else if (!(msg.content is ())) { + return websubhub:ACKNOWLEDGEMENT; + } else { + return websubhub:UPDATE_MESSAGE_ERROR; + } } isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError|NewResponse { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -86,21 +82,16 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onUnsubscription(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionAccepted|websubhub:BadUnsubscriptionError|websubhub:InternalUnsubscriptionError { if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { - websubhub:UnsubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; - return successResult; + return websubhub:UNSUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + return websubhub:BAD_UNSUBSCRIPTION_ERROR; } } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_9/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_9/service.bal index 998421c9..44015e10 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_9/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_9/service.bal @@ -21,7 +21,6 @@ import ballerina/io; listener websubhub:Listener functionWithArgumentsListener = new(9090); service /websubhub on functionWithArgumentsListener { - isolated remote function onRegisterTopic(websubhub:TopicRegistration message) returns websubhub:TopicRegistrationSuccess|websubhub:TopicRegistrationError { if (message.topic == "test") { @@ -33,44 +32,40 @@ service /websubhub on functionWithArgumentsListener { isolated remote function onDeregisterTopic(websubhub:TopicDeregistration message, http:Headers headers) returns websubhub:TopicDeregistrationSuccess|websubhub:TopicDeregistrationError { - - map body = { isDeregisterSuccess: "true" }; - websubhub:TopicDeregistrationSuccess deregisterResult = { - body - }; if (message.topic == "test") { - return deregisterResult; + return websubhub:TOPIC_DEREGISTRATION_SUCCESS; } else { - return error websubhub:TopicDeregistrationError("Topic Deregistration Failed!"); + return websubhub:TOPIC_DEREGISTRATION_ERROR; } } - isolated remote function onUpdateMessage(websubhub:UpdateMessage message) + isolated remote function onUpdateMessage(websubhub:UpdateMessage msg) returns websubhub:Acknowledgement|websubhub:UpdateMessageError { - return websubhub:ACKNOWLEDGEMENT; + if (msg.hubTopic == "test") { + return websubhub:ACKNOWLEDGEMENT; + } else if (!(msg.content is ())) { + return websubhub:ACKNOWLEDGEMENT; + } else { + return websubhub:UPDATE_MESSAGE_ERROR; + } } isolated remote function onSubscription(websubhub:Subscription msg) returns websubhub:SubscriptionAccepted|websubhub:SubscriptionPermanentRedirect|websubhub:SubscriptionTemporaryRedirect |websubhub:BadSubscriptionError|websubhub:InternalSubscriptionError { - websubhub:SubscriptionAccepted successResult = { - body: >{ - isSuccess: "true" - } - }; if (msg.hubTopic == "test") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else if (msg.hubTopic == "test1") { - return successResult; + return websubhub:SUBSCRIPTION_ACCEPTED; } else { - return error websubhub:BadSubscriptionError("Bad subscription"); + return websubhub:BAD_SUBSCRIPTION_ERROR; } } isolated remote function onSubscriptionValidation(websubhub:Subscription msg) returns websubhub:SubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:SUBSCRIPTION_DENIED_ERROR; } return (); } @@ -80,12 +75,13 @@ service /websubhub on functionWithArgumentsListener { } isolated remote function onUnsubscription(websubhub:Unsubscription msg) { + return; } isolated remote function onUnsubscriptionValidation(websubhub:Unsubscription msg) returns websubhub:UnsubscriptionDeniedError? { if (msg.hubTopic == "test1") { - return error websubhub:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + return websubhub:UNSUBSCRIPTION_DENIED_ERROR; } return (); } From c1984bbceb099cfc9d296214b495867b9c57ec28 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Fri, 3 Jun 2022 12:46:24 +0530 Subject: [PATCH 15/16] Revert internal logic migration to use CommonResponse status code as outbound response status code --- ballerina/content_distribution.bal | 8 ++++---- ballerina/subscription.bal | 20 ++++++++++++-------- ballerina/topic_registration.bal | 12 ++++++------ ballerina/utils.bal | 12 ++++++------ 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/ballerina/content_distribution.bal b/ballerina/content_distribution.bal index ab70d2bc..3d13266d 100644 --- a/ballerina/content_distribution.bal +++ b/ballerina/content_distribution.bal @@ -76,11 +76,11 @@ isolated function processResult(Acknowledgement|error result) returns http:Respo if (result is Acknowledgement) { response.setTextPayload("hub.mode=accepted", mime:APPLICATION_FORM_URLENCODED); } else if (result is UpdateMessageError) { - CommonResponse errorDetails = result.detail(); - updateErrorResponse(response, errorDetails, result.message()); + var errorDetails = result.detail(); + updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); } else { - CommonResponse errorDetails = UPDATE_MESSAGE_ERROR.detail(); - updateErrorResponse(response, errorDetails, result.message()); + var errorDetails = UPDATE_MESSAGE_ERROR.detail(); + updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); } return response; } diff --git a/ballerina/subscription.bal b/ballerina/subscription.bal index 0ca8a8d8..a440da44 100644 --- a/ballerina/subscription.bal +++ b/ballerina/subscription.bal @@ -67,12 +67,14 @@ isolated function processOnSubscriptionResult(SubscriptionAccepted|error result) response.statusCode = http:STATUS_ACCEPTED; return response; } else if result is BadSubscriptionError { - CommonResponse errorDetails = result.detail(); - updateErrorResponse(response, errorDetails, result.message()); + response.statusCode = http:STATUS_BAD_REQUEST; + var errorDetails = result.detail(); + updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); return response; } else { - CommonResponse errorDetails = result is InternalSubscriptionError ? result.detail() : INTERNAL_SUBSCRIPTION_ERROR.detail(); - updateErrorResponse(response, errorDetails, result.message()); + response.statusCode = http:STATUS_INTERNAL_SERVER_ERROR; + var errorDetails = result is InternalSubscriptionError ? result.detail() : INTERNAL_SUBSCRIPTION_ERROR.detail(); + updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); return response; } } @@ -155,12 +157,14 @@ isolated function processOnUnsubscriptionResult(UnsubscriptionAccepted|error res response.statusCode = http:STATUS_ACCEPTED; return response; } else if result is BadUnsubscriptionError { - CommonResponse errorDetails = result.detail(); - updateErrorResponse(response, errorDetails, result.message()); + response.statusCode = http:STATUS_BAD_REQUEST; + var errorDetails = result.detail(); + updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); return response; } else { - CommonResponse errorDetails = result is InternalSubscriptionError ? result.detail() : INTERNAL_UNSUBSCRIPTION_ERROR.detail(); - updateErrorResponse(response, errorDetails, result.message()); + response.statusCode = http:STATUS_INTERNAL_SERVER_ERROR; + var errorDetails = result is InternalSubscriptionError ? result.detail() : INTERNAL_UNSUBSCRIPTION_ERROR.detail(); + updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); return response; } } diff --git a/ballerina/topic_registration.bal b/ballerina/topic_registration.bal index 608723f0..fd7e64b6 100644 --- a/ballerina/topic_registration.bal +++ b/ballerina/topic_registration.bal @@ -25,10 +25,10 @@ isolated function processTopicRegistration(http:Headers headers, map par TopicRegistrationSuccess|error result = adaptor.callRegisterMethod(msg, headers); http:Response response = new; if result is TopicRegistrationSuccess { - updateSuccessResponse(response, result); + updateSuccessResponse(response, result["body"], result["headers"]); } else { - CommonResponse errorDetails = result is TopicRegistrationError ? result.detail() : TOPIC_REGISTRATION_ERROR.detail(); - updateErrorResponse(response, errorDetails, result.message()); + var errorDetails = result is TopicRegistrationError ? result.detail() : TOPIC_REGISTRATION_ERROR.detail(); + updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); } return response; } @@ -42,10 +42,10 @@ isolated function processTopicDeregistration(http:Headers headers, map p TopicDeregistrationSuccess|error result = adaptor.callDeregisterMethod(msg, headers); http:Response response = new; if result is TopicDeregistrationSuccess { - updateSuccessResponse(response, result); + updateSuccessResponse(response, result["body"], result["headers"]); } else { - CommonResponse errorDetails = result is TopicDeregistrationError ? result.detail() : TOPIC_DEREGISTRATION_ERROR.detail(); - updateErrorResponse(response, errorDetails, result.message()); + var errorDetails = result is TopicDeregistrationError ? result.detail() : TOPIC_DEREGISTRATION_ERROR.detail(); + updateErrorResponse(response, errorDetails["body"], errorDetails["headers"], result.message()); } return response; } diff --git a/ballerina/utils.bal b/ballerina/utils.bal index 5435bf49..24315c9a 100644 --- a/ballerina/utils.bal +++ b/ballerina/utils.bal @@ -61,14 +61,14 @@ isolated function generateQueryString(string callbackUrl, [string, string?][] pa return (strings:includes(callbackUrl, ("?")) ? "&" : "?") + strings:'join("&", ...keyValPairs); } -isolated function updateErrorResponse(http:Response httpResponse, CommonResponse response, string reason) { - httpResponse.statusCode = response.statusCode; - updateHubResponse(httpResponse, "denied", response.body, response.headers, reason); +isolated function updateErrorResponse(http:Response response, anydata? messageBody, + map? headers, string reason) { + updateHubResponse(response, "denied", messageBody, headers, reason); } -isolated function updateSuccessResponse(http:Response httpResponse, CommonResponse response) { - httpResponse.statusCode = response.statusCode; - updateHubResponse(httpResponse, "accepted", response.body, response.headers); +isolated function updateSuccessResponse(http:Response response, anydata? messageBody, + map? headers) { + updateHubResponse(response, "accepted", messageBody, headers); } isolated function updateHubResponse(http:Response response, string hubMode, From a82d05a3d590f9b6a0a459129c604db67fe38ddd Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Fri, 3 Jun 2022 13:02:17 +0530 Subject: [PATCH 16/16] [Automated] Update the native jar versions --- ballerina/Ballerina.toml | 4 ++-- ballerina/CompilerPlugin.toml | 2 +- ballerina/Dependencies.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index 298f6918..e824e7b9 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -1,7 +1,7 @@ [package] org = "ballerina" name = "websubhub" -version = "1.3.0" +version = "1.3.1" authors = ["Ballerina"] keywords = ["websub", "hub", "publisher", "service", "listener", "client"] repository = "https://github.com/ballerina-platform/module-ballerina-websubhub" @@ -10,4 +10,4 @@ license = ["Apache-2.0"] distribution = "2201.1.0" [[platform.java11.dependency]] -path = "../native/build/libs/websubhub-native-1.3.0.jar" +path = "../native/build/libs/websubhub-native-1.3.1-SNAPSHOT.jar" diff --git a/ballerina/CompilerPlugin.toml b/ballerina/CompilerPlugin.toml index 75bbf0f1..fd58d35b 100644 --- a/ballerina/CompilerPlugin.toml +++ b/ballerina/CompilerPlugin.toml @@ -3,4 +3,4 @@ id = "websubhub-compiler-plugin" class = "io.ballerina.stdlib.websubhub.WebSubHubCompilerPlugin" [[dependency]] -path = "../compiler-plugin/build/libs/websubhub-compiler-plugin-1.3.0.jar" +path = "../compiler-plugin/build/libs/websubhub-compiler-plugin-1.3.1-SNAPSHOT.jar" diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 4e0fcb4c..25d65a8d 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -316,7 +316,7 @@ modules = [ [[package]] org = "ballerina" name = "websubhub" -version = "1.3.0" +version = "1.3.1" dependencies = [ {org = "ballerina", name = "crypto"}, {org = "ballerina", name = "http"},