Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeshLK committed Feb 18, 2025
2 parents 6b97a42 + d269bd6 commit 429dce8
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 37 deletions.
6 changes: 3 additions & 3 deletions ballerina/tests/basic_hub_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ listener Listener functionWithArgumentsListener = new(9090);

service /websubhub on functionWithArgumentsListener {

isolated remote function onRegisterTopic(TopicRegistration message)
isolated remote function onRegisterTopic(TopicRegistration message, http:Headers headers)
returns TopicRegistrationSuccess|TopicRegistrationError {
if (message.topic == "test") {
TopicRegistrationSuccess successResult = {
Expand All @@ -38,7 +38,7 @@ service /websubhub on functionWithArgumentsListener {
}
}

isolated remote function onDeregisterTopic(TopicDeregistration message)
isolated remote function onDeregisterTopic(TopicDeregistration message, http:Headers headers)
returns TopicDeregistrationSuccess|TopicDeregistrationError {

map<string> body = { isDeregisterSuccess: "true" };
Expand All @@ -52,7 +52,7 @@ service /websubhub on functionWithArgumentsListener {
}
}

isolated remote function onUpdateMessage(UpdateMessage msg)
isolated remote function onUpdateMessage(UpdateMessage msg, http:Headers headers)
returns Acknowledgement|UpdateMessageError {
Acknowledgement ack = {};
if (msg.hubTopic == "test") {
Expand Down
10 changes: 5 additions & 5 deletions ballerina/tests/hub_authentication_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final http:ListenerJwtAuthHandler handler = new({

service /websubhub on securedListener {

remote function onRegisterTopic(TopicRegistration message, http:Headers headers)
remote function onRegisterTopic(http:Headers headers, TopicRegistration message)
returns TopicRegistrationSuccess|TopicRegistrationError {
string? auth = doAuth(headers);
if (auth is string) {
Expand All @@ -49,7 +49,7 @@ service /websubhub on securedListener {
return TOPIC_REGISTRATION_SUCCESS;
}

remote function onDeregisterTopic(TopicDeregistration message, http:Headers headers)
remote function onDeregisterTopic(http:Headers headers, TopicDeregistration message)
returns TopicDeregistrationSuccess|TopicDeregistrationError {
string? auth = doAuth(headers);
if (auth is string) {
Expand All @@ -59,7 +59,7 @@ service /websubhub on securedListener {
return TOPIC_DEREGISTRATION_SUCCESS;
}

remote function onUpdateMessage(UpdateMessage message, http:Headers headers)
remote function onUpdateMessage(http:Headers headers, UpdateMessage message)
returns Acknowledgement|UpdateMessageError {
string? auth = doAuth(headers);
if (auth is string) {
Expand All @@ -69,7 +69,7 @@ service /websubhub on securedListener {
return ACKNOWLEDGEMENT;
}

remote function onSubscription(Subscription message, http:Headers headers)
remote function onSubscription(http:Headers headers, Subscription message)
returns SubscriptionAccepted|InternalSubscriptionError {
string? auth = doAuth(headers);
if (auth is string) {
Expand All @@ -82,7 +82,7 @@ service /websubhub on securedListener {
isolated remote function onSubscriptionIntentVerified(VerifiedSubscription message) {
}

remote function onUnsubscription(Unsubscription message, http:Headers headers)
remote function onUnsubscription(http:Headers headers, Unsubscription message)
returns UnsubscriptionAccepted|InternalUnsubscriptionError {
string? auth = doAuth(headers);
if (auth is string) {
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- [Introduce dynamic parameter order for the `websubhub:Service` remote methods](https://github.com/ballerina-platform/ballerina-library/issues/7600)

## [1.11.1] - 2024-11-22

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface Constants {
String PACKAGE_ORG = "ballerina";
String PACKAGE_NAME = "websubhub";

String SERVICE_OBJECT = "WEBSUBHUB_SERVICE_OBJECT";
String NATIVE_HUB_SERVICE = "NATIVE_HUB_SERVICE";

String ON_REGISTER_TOPIC = "onRegisterTopic";
String ON_DEREGISTER_TOPIC = "onDeregisterTopic";
Expand All @@ -37,6 +37,9 @@ public interface Constants {
String ON_UNSUBSCRIPTION_VALIDATION = "onUnsubscriptionValidation";
String ON_UNSUBSCRIPTION_INTENT_VERIFIED = "onUnsubscriptionIntentVerified";

String HTTP_HEADERS_TYPE = "http:Headers";
String WEBSUBHUB_CONTROLLER_TYPE = "websubhub:Controller";

String COMMON_RESPONSE = "CommonResponse";
String STATUS_CODE = "statusCode";
String SERVICE_EXECUTION_ERROR = "ServiceExecutionError";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. 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.types.Type;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BString;

import static io.ballerina.stdlib.websubhub.Constants.HTTP_HEADERS_TYPE;
import static io.ballerina.stdlib.websubhub.Constants.WEBSUBHUB_CONTROLLER_TYPE;

/**
* {@code InteropArgs} is a wrapper object which contains the parameters for inter-op calls.
*/
public class InteropArgs {
private final BMap<BString, Object> message;
private final BObject httpHeaders;
private BObject hubController;

InteropArgs(BMap<BString, Object> message, BObject httpHeaders) {
this.message = message;
this.httpHeaders = httpHeaders;
}

public InteropArgs(BMap<BString, Object> message, BObject httpHeaders, BObject hubController) {
this.message = message;
this.httpHeaders = httpHeaders;
this.hubController = hubController;
}

public Object getMappingArg(Type argType) {
String argTypeName = argType.toString();
if (HTTP_HEADERS_TYPE.equals(argTypeName)) {
return httpHeaders;
} else if (WEBSUBHUB_CONTROLLER_TYPE.equals(argTypeName)) {
return hubController;
}
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BString;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

Expand All @@ -48,7 +47,7 @@
import static io.ballerina.stdlib.websubhub.Constants.ON_UNSUBSCRIPTION_INTENT_VERIFIED;
import static io.ballerina.stdlib.websubhub.Constants.ON_UNSUBSCRIPTION_VALIDATION;
import static io.ballerina.stdlib.websubhub.Constants.ON_UPDATE_MESSAGE;
import static io.ballerina.stdlib.websubhub.Constants.SERVICE_OBJECT;
import static io.ballerina.stdlib.websubhub.Constants.NATIVE_HUB_SERVICE;

/**
* {@code NativeHttpToWebsubhubAdaptor} is a wrapper object used for service method execution.
Expand All @@ -57,124 +56,136 @@ public final class NativeHttpToWebsubhubAdaptor {
private NativeHttpToWebsubhubAdaptor() {}

public static void externInit(BObject adaptor, BObject serviceObj) {
adaptor.addNativeData(SERVICE_OBJECT, serviceObj);
adaptor.addNativeData(NATIVE_HUB_SERVICE, new NativeHubService(serviceObj));
}

public static BArray getServiceMethodNames(BObject adaptor) {
BObject bHubService = (BObject) adaptor.getNativeData(SERVICE_OBJECT);
List<BString> methodNamesList = new ArrayList<>();
ObjectType serviceType = (ObjectType) TypeUtils.getReferredType(TypeUtils.getType(bHubService));
for (MethodType method : serviceType.getMethods()) {
methodNamesList.add(StringUtils.fromString(method.getName()));
}
return ValueCreator.createArrayValue(methodNamesList.toArray(BString[]::new));
NativeHubService nativeHubService = (NativeHubService) adaptor.getNativeData(NATIVE_HUB_SERVICE);
List<BString> remoteMethodNames = nativeHubService.getRemoteMethodNames().stream()
.map(StringUtils::fromString).toList();
return ValueCreator.createArrayValue(remoteMethodNames.toArray(BString[]::new));
}

public static Object callRegisterMethod(Environment env, BObject adaptor,
BMap<BString, Object> message, BObject bHttpHeaders) {
BObject bHubService = (BObject) adaptor.getNativeData(SERVICE_OBJECT);
NativeHubService nativeHubService = (NativeHubService) adaptor.getNativeData(NATIVE_HUB_SERVICE);
BObject bHubService = nativeHubService.getBHubService();
boolean isReadOnly = isReadOnlyParam(bHubService, ON_REGISTER_TOPIC);
if (isReadOnly) {
message.freezeDirect();
}
Object[] args = new Object[]{message, bHttpHeaders};
Object[] args = nativeHubService.getMethodArgs(ON_REGISTER_TOPIC, new InteropArgs(message, bHttpHeaders));
return invokeRemoteFunction(env, bHubService, args,
"callRegisterMethod", ON_REGISTER_TOPIC);
}

public static Object callDeregisterMethod(Environment env, BObject adaptor,
BMap<BString, Object> message, BObject bHttpHeaders) {
BObject bHubService = (BObject) adaptor.getNativeData(SERVICE_OBJECT);
NativeHubService nativeHubService = (NativeHubService) adaptor.getNativeData(NATIVE_HUB_SERVICE);
BObject bHubService = nativeHubService.getBHubService();
boolean isReadOnly = isReadOnlyParam(bHubService, ON_DEREGISTER_TOPIC);
if (isReadOnly) {
message.freezeDirect();
}
Object[] args = new Object[]{message, bHttpHeaders};
Object[] args = nativeHubService.getMethodArgs(ON_DEREGISTER_TOPIC, new InteropArgs(message, bHttpHeaders));
return invokeRemoteFunction(env, bHubService, args,
"callDeregisterMethod", ON_DEREGISTER_TOPIC);
}

public static Object callOnUpdateMethod(Environment env, BObject adaptor,
BMap<BString, Object> message, BObject bHttpHeaders) {
BObject bHubService = (BObject) adaptor.getNativeData(SERVICE_OBJECT);
NativeHubService nativeHubService = (NativeHubService) adaptor.getNativeData(NATIVE_HUB_SERVICE);
BObject bHubService = nativeHubService.getBHubService();
boolean isReadOnly = isReadOnlyParam(bHubService, ON_UPDATE_MESSAGE);
if (isReadOnly) {
message.freezeDirect();
}
Object[] args = new Object[]{message, bHttpHeaders};
Object[] args = nativeHubService.getMethodArgs(ON_UPDATE_MESSAGE, new InteropArgs(message, bHttpHeaders));
return invokeRemoteFunction(env, bHubService, args,
"callOnUpdateMethod", ON_UPDATE_MESSAGE);
}

public static Object callOnSubscriptionMethod(Environment env, BObject adaptor, BMap<BString, Object> message,
BObject bHttpHeaders, BObject bHubController) {
BObject bHubService = (BObject) adaptor.getNativeData(SERVICE_OBJECT);
NativeHubService nativeHubService = (NativeHubService) adaptor.getNativeData(NATIVE_HUB_SERVICE);
BObject bHubService = nativeHubService.getBHubService();
boolean isReadOnly = isReadOnlyParam(bHubService, ON_SUBSCRIPTION);
if (isReadOnly) {
message.freezeDirect();
}
Object[] args = new Object[]{message, bHttpHeaders, bHubController};
Object[] args = nativeHubService.getMethodArgs(ON_SUBSCRIPTION, new InteropArgs(
message, bHttpHeaders, bHubController));
return invokeRemoteFunction(env, bHubService, args,
"callOnSubscriptionMethod", ON_SUBSCRIPTION);
}

public static Object callOnSubscriptionValidationMethod(Environment env, BObject adaptor,
BMap<BString, Object> message, BObject bHttpHeaders) {
BObject bHubService = (BObject) adaptor.getNativeData(SERVICE_OBJECT);
NativeHubService nativeHubService = (NativeHubService) adaptor.getNativeData(NATIVE_HUB_SERVICE);
BObject bHubService = nativeHubService.getBHubService();
boolean isReadOnly = isReadOnlyParam(bHubService, ON_SUBSCRIPTION_VALIDATION);
if (isReadOnly) {
message.freezeDirect();
}
Object[] args = new Object[]{message, bHttpHeaders};
Object[] args = nativeHubService.getMethodArgs(ON_SUBSCRIPTION_VALIDATION,
new InteropArgs(message, bHttpHeaders));
return invokeRemoteFunction(env, bHubService, args,
"callOnSubscriptionValidationMethod", ON_SUBSCRIPTION_VALIDATION);
}

public static Object callOnSubscriptionIntentVerifiedMethod(Environment env, BObject adaptor,
BMap<BString, Object> message, BObject bHttpHeaders) {
BObject bHubService = (BObject) adaptor.getNativeData(SERVICE_OBJECT);
NativeHubService nativeHubService = (NativeHubService) adaptor.getNativeData(NATIVE_HUB_SERVICE);
BObject bHubService = nativeHubService.getBHubService();
boolean isReadOnly = isReadOnlyParam(bHubService, ON_SUBSCRIPTION_INTENT_VERIFIED);
if (isReadOnly) {
message.freezeDirect();
}
Object[] args = new Object[]{message, bHttpHeaders};
Object[] args = nativeHubService.getMethodArgs(ON_SUBSCRIPTION_INTENT_VERIFIED,
new InteropArgs(message, bHttpHeaders));
return invokeRemoteFunction(env, bHubService, args,
"callOnSubscriptionIntentVerifiedMethod",
ON_SUBSCRIPTION_INTENT_VERIFIED);
}

public static Object callOnUnsubscriptionMethod(Environment env, BObject adaptor, BMap<BString, Object> message,
BObject bHttpHeaders, BObject bHubController) {
BObject bHubService = (BObject) adaptor.getNativeData(SERVICE_OBJECT);
NativeHubService nativeHubService = (NativeHubService) adaptor.getNativeData(NATIVE_HUB_SERVICE);
BObject bHubService = nativeHubService.getBHubService();
boolean isReadOnly = isReadOnlyParam(bHubService, ON_UNSUBSCRIPTION);
if (isReadOnly) {
message.freezeDirect();
}
Object[] args = new Object[]{message, bHttpHeaders, bHubController};
Object[] args = nativeHubService.getMethodArgs(ON_UNSUBSCRIPTION, new InteropArgs(
message, bHttpHeaders, bHubController));
return invokeRemoteFunction(env, bHubService, args,
"callOnUnsubscriptionMethod", ON_UNSUBSCRIPTION);
}

public static Object callOnUnsubscriptionValidationMethod(Environment env, BObject adaptor,
BMap<BString, Object> message, BObject bHttpHeaders) {
BObject bHubService = (BObject) adaptor.getNativeData(SERVICE_OBJECT);
NativeHubService nativeHubService = (NativeHubService) adaptor.getNativeData(NATIVE_HUB_SERVICE);
BObject bHubService = nativeHubService.getBHubService();
boolean isReadOnly = isReadOnlyParam(bHubService, ON_UNSUBSCRIPTION_VALIDATION);
if (isReadOnly) {
message.freezeDirect();
}
Object[] args = new Object[]{message, bHttpHeaders};
Object[] args = nativeHubService.getMethodArgs(ON_UNSUBSCRIPTION_VALIDATION,
new InteropArgs(message, bHttpHeaders));
return invokeRemoteFunction(env, bHubService, args, "callOnUnsubscriptionValidationMethod",
ON_UNSUBSCRIPTION_VALIDATION);
}

public static Object callOnUnsubscriptionIntentVerifiedMethod(Environment env, BObject adaptor,
BMap<BString, Object> message, BObject bHttpHeaders) {
BObject bHubService = (BObject) adaptor.getNativeData(SERVICE_OBJECT);
NativeHubService nativeHubService = (NativeHubService) adaptor.getNativeData(NATIVE_HUB_SERVICE);
BObject bHubService = nativeHubService.getBHubService();
boolean isReadOnly = isReadOnlyParam(bHubService, ON_UNSUBSCRIPTION_INTENT_VERIFIED);
if (isReadOnly) {
message.freezeDirect();
}
Object[] args = new Object[]{message, bHttpHeaders};
Object[] args = nativeHubService.getMethodArgs(ON_UNSUBSCRIPTION_INTENT_VERIFIED,
new InteropArgs(message, bHttpHeaders));
return invokeRemoteFunction(env, bHubService, args, "callOnUnsubscriptionIntentVerifiedMethod",
ON_UNSUBSCRIPTION_INTENT_VERIFIED);
}
Expand Down
Loading

0 comments on commit 429dce8

Please sign in to comment.