diff --git a/changelog.md b/changelog.md index 04c4769e..52bf2bcf 100644 --- a/changelog.md +++ b/changelog.md @@ -5,8 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] -- Multipart content delivery using websubhub-client. -- Subscription Lease seconds expired event. + +### Fixed + +- [Log error when return from the remote method leads to an error](https://github.com/ballerina-platform/ballerina-standard-library/issues/1449) +- [WebSubHub Compiler Plugin does not allow additional methods inside service declaration](https://github.com/ballerina-platform/ballerina-standard-library/issues/1417) ## [0.2.0-beta.1] - 2021-05-06 diff --git a/websubhub-ballerina/commons.bal b/websubhub-ballerina/commons.bal index d7c2548e..7ed7444a 100644 --- a/websubhub-ballerina/commons.bal +++ b/websubhub-ballerina/commons.bal @@ -350,7 +350,7 @@ isolated function isSuccessStatusCode(int statusCode) returns boolean { # + topic - Name of the `topic` # + return - a `string` containing the value for `HTTP Link Header` isolated function generateLinkUrl(string hubUrl, string topic) returns string { - return string`${hubUrl}; rel=\"hub\", ${topic}; rel=\"self\"`; + return string `${hubUrl}; rel=\"hub\", ${topic}; rel=\"self\"`; } # Converts {@code websubhub:ClientConfiguration} to {@code http:ClientConfiguration} diff --git a/websubhub-ballerina/hub_client.bal b/websubhub-ballerina/hub_client.bal index fdb5aaa7..0678783a 100644 --- a/websubhub-ballerina/hub_client.bal +++ b/websubhub-ballerina/hub_client.bal @@ -100,7 +100,7 @@ public client class HubClient { } else { hash = check retrievePayloadSignature(self.secret, msg.content); } - request.setHeader(X_HUB_SIGNATURE, string`${SHA256_HMAC}=${hash.toBase16()}`); + request.setHeader(X_HUB_SIGNATURE, string `${SHA256_HMAC}=${hash.toBase16()}`); } http:Response|error response = self.httpClient->post("/", request); diff --git a/websubhub-ballerina/tests/hub_with_error_return_types.bal b/websubhub-ballerina/tests/hub_with_error_return_types.bal index 3a805dc6..473c38c1 100644 --- a/websubhub-ballerina/tests/hub_with_error_return_types.bal +++ b/websubhub-ballerina/tests/hub_with_error_return_types.bal @@ -23,48 +23,40 @@ listener Listener hubWithErrorReturnTypesListener = new(9101); service /websubhub on hubWithErrorReturnTypesListener { - isolated remote function onRegisterTopic(TopicRegistration message) - returns TopicRegistrationSuccess|TopicRegistrationError|error { - return error Error("Registration Failed!"); + isolated remote function onRegisterTopic(TopicRegistration message) returns error { + return error ("Registration Failed!"); } - isolated remote function onDeregisterTopic(TopicDeregistration message) - returns TopicDeregistrationSuccess|TopicDeregistrationError|error { - return error Error("Topic Deregistration Failed!"); + isolated remote function onDeregisterTopic(TopicDeregistration message) returns error { + return error ("Topic Deregistration Failed!"); } - isolated remote function onUpdateMessage(UpdateMessage msg) - returns Acknowledgement|UpdateMessageError|error { - return error UpdateMessageError("Error in accessing content"); + isolated remote function onUpdateMessage(UpdateMessage msg) returns error { + return error ("Error in accessing content"); } - isolated remote function onSubscription(Subscription msg) - returns SubscriptionAccepted|SubscriptionPermanentRedirect|SubscriptionTemporaryRedirect - |BadSubscriptionError|InternalSubscriptionError|error { - return error Error("Error occurred while processing subscription"); + isolated remote function onSubscription(Subscription msg) returns error { + return error ("Error occurred while processing subscription"); } - isolated remote function onSubscriptionValidation(Subscription msg) - returns SubscriptionDeniedError|error? { - return error Error("Denied subscription with Hub"); + isolated remote function onSubscriptionValidation(Subscription msg) returns error? { + return error ("Denied subscription with Hub"); } isolated remote function onSubscriptionIntentVerified(VerifiedSubscription msg) returns error? { - return error Error("Error occcurred while verifying subscription intent"); + return error ("Error occcurred while verifying subscription intent"); } - isolated remote function onUnsubscription(Unsubscription msg) - returns UnsubscriptionAccepted|BadUnsubscriptionError|InternalUnsubscriptionError|error { - return error Error("Denied unsubscription for topic '" + msg.hubTopic + "'"); + isolated remote function onUnsubscription(Unsubscription msg) returns error { + return error ("Denied unsubscription for topic '" + msg.hubTopic + "'"); } - isolated remote function onUnsubscriptionValidation(Unsubscription msg) - returns UnsubscriptionDeniedError|error? { - return error UnsubscriptionDeniedError("Denied subscription with Hub"); + isolated remote function onUnsubscriptionValidation(Unsubscription msg) returns error? { + return error ("Denied subscription with Hub"); } isolated remote function onUnsubscriptionIntentVerified(VerifiedUnsubscription msg) returns error? { - return error Error("Error occcurred while verifying unsubscription intent"); + return error ("Error occcurred while verifying unsubscription intent"); } } diff --git a/websubhub-examples/kafka-hub/hub/modules/config/configurations.bal b/websubhub-examples/kafka-hub/hub/modules/config/configurations.bal index 5f40174b..f97512c6 100644 --- a/websubhub-examples/kafka-hub/hub/modules/config/configurations.bal +++ b/websubhub-examples/kafka-hub/hub/modules/config/configurations.bal @@ -51,4 +51,4 @@ public configurable int MESSAGE_DELIVERY_COUNT = 3; # The message delivery timeout public configurable decimal MESSAGE_DELIVERY_TIMEOUT = 10; -public final string CONSTRUCTED_SERVER_ID = string`${SERVER_ID}-${util:generateRandomString()}`; +public final string CONSTRUCTED_SERVER_ID = string `${SERVER_ID}-${util:generateRandomString()}`; diff --git a/websubhub-native/spotbugs-exclude.xml b/websubhub-native/spotbugs-exclude.xml index f0e03fab..5d815dd6 100644 --- a/websubhub-native/spotbugs-exclude.xml +++ b/websubhub-native/spotbugs-exclude.xml @@ -18,7 +18,7 @@ - + diff --git a/websubhub-native/src/main/java/io/ballerina/stdlib/websubhub/Constants.java b/websubhub-native/src/main/java/io/ballerina/stdlib/websubhub/Constants.java new file mode 100644 index 00000000..4a05761a --- /dev/null +++ b/websubhub-native/src/main/java/io/ballerina/stdlib/websubhub/Constants.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.ballerina.stdlib.websubhub; + +/** + * {@code Constants} contains the public constants to be used. + */ +public interface Constants { + String PACKAGE_ORG = "ballerina"; + String PACKAGE_NAME = "websubhub"; + + String SERVICE_OBJECT = "WEBSUBHUB_SERVICE_OBJECT"; +} diff --git a/websubhub-native/src/main/java/io/ballerina/stdlib/websubhub/HubCallback.java b/websubhub-native/src/main/java/io/ballerina/stdlib/websubhub/HubCallback.java new file mode 100644 index 00000000..db80aa75 --- /dev/null +++ b/websubhub-native/src/main/java/io/ballerina/stdlib/websubhub/HubCallback.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.ballerina.stdlib.websubhub; + +import io.ballerina.runtime.api.Future; +import io.ballerina.runtime.api.Module; +import io.ballerina.runtime.api.async.Callback; +import io.ballerina.runtime.api.creators.ErrorCreator; +import io.ballerina.runtime.api.types.Type; +import io.ballerina.runtime.api.values.BError; +import io.ballerina.runtime.api.values.BString; + +import static io.ballerina.runtime.api.utils.StringUtils.fromString; + +/** + * {@code HubCallback} used to handle the websubhub remote method invocation results. + */ +public class HubCallback implements Callback { + private final Future future; + private final Module module; + + public HubCallback(Future future, Module module) { + this.future = future; + this.module = module; + } + + @Override + public void notifySuccess(Object result) { + if (result instanceof BError) { + BError error = (BError) result; + if (!isModuleDefinedError(error)) { + error.printStackTrace(); + } + } + + future.complete(result); + } + + @Override + public void notifyFailure(BError bError) { + bError.printStackTrace(); + BString errorMessage = fromString("service method invocation failed: " + bError.getErrorMessage()); + BError invocationError = ErrorCreator.createError(module, "ServiceExecutionError", + errorMessage, bError, null); + future.complete(invocationError); + } + + private boolean isModuleDefinedError(BError error) { + Type errorType = error.getType(); + Module packageDetails = errorType.getPackage(); + String orgName = packageDetails.getOrg(); + String packageName = packageDetails.getName(); + return Constants.PACKAGE_ORG.equals(orgName) && Constants.PACKAGE_NAME.equals(packageName); + } +} diff --git a/websubhub-native/src/main/java/io/ballerina/stdlib/websubhub/NativeHttpToWebsubhubAdaptor.java b/websubhub-native/src/main/java/io/ballerina/stdlib/websubhub/NativeHttpToWebsubhubAdaptor.java index fb2e2c78..3407f715 100644 --- a/websubhub-native/src/main/java/io/ballerina/stdlib/websubhub/NativeHttpToWebsubhubAdaptor.java +++ b/websubhub-native/src/main/java/io/ballerina/stdlib/websubhub/NativeHttpToWebsubhubAdaptor.java @@ -21,28 +21,23 @@ import io.ballerina.runtime.api.Environment; import io.ballerina.runtime.api.Future; import io.ballerina.runtime.api.Module; -import io.ballerina.runtime.api.async.Callback; import io.ballerina.runtime.api.async.StrandMetadata; -import io.ballerina.runtime.api.creators.ErrorCreator; import io.ballerina.runtime.api.creators.ValueCreator; import io.ballerina.runtime.api.types.MethodType; import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.values.BArray; -import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BObject; import io.ballerina.runtime.api.values.BString; import java.util.ArrayList; -import static io.ballerina.runtime.api.utils.StringUtils.fromString; +import static io.ballerina.stdlib.websubhub.Constants.SERVICE_OBJECT; /** * {@code NativeHttpToWebsubhubAdaptor} is a wrapper object used for service method execution. */ public class NativeHttpToWebsubhubAdaptor { - private static final String SERVICE_OBJECT = "WEBSUBHUB_SERVICE_OBJECT"; - public static void externInit(BObject adaptor, BObject serviceObj) { adaptor.addNativeData(SERVICE_OBJECT, serviceObj); } @@ -135,20 +130,8 @@ private static Object invokeRemoteFunction(Environment env, BObject bHubService, Module module = ModuleUtils.getModule(); StrandMetadata metadata = new StrandMetadata(module.getOrg(), module.getName(), module.getVersion(), parentFunctionName); - env.getRuntime().invokeMethodAsync(bHubService, remoteFunctionName, null, metadata, new Callback() { - @Override - public void notifySuccess(Object result) { - balFuture.complete(result); - } - - @Override - public void notifyFailure(BError bError) { - BString errorMessage = fromString("service method invocation failed: " + bError.getErrorMessage()); - BError invocationError = ErrorCreator.createError(module, "ServiceExecutionError", - errorMessage, bError, null); - balFuture.complete(invocationError); - } - }, args); + env.getRuntime().invokeMethodAsync(bHubService, remoteFunctionName, null, metadata, + new HubCallback(balFuture, module), args); return null; } }