Skip to content

Commit

Permalink
Fix parsing inline listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
anuruddhal committed Sep 26, 2023
1 parent e908fc1 commit 4edaa49
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public void visit(ServiceDeclarationNode serviceDeclarationNode) {
List<TypeSymbol> typeSymbols = symbol.listenerTypes();
if (typeSymbols.isEmpty()) {
return;
}
}
String servicePath = toAbsoluteServicePath(serviceDeclarationNode.absoluteResourcePath());
if (!isC2CNativelySupportedListener(typeSymbols)) {
processCustomExposedAnnotatedListeners(typeSymbols, servicePath, serviceDeclarationNode);
Expand All @@ -325,7 +325,7 @@ public void visit(ServiceDeclarationNode serviceDeclarationNode) {
}
services.add(serviceInfo);
}

private List<ListenerInfo> extractListeners(ServiceDeclarationNode serviceDeclarationNode, String servicePath) {
SeparatedNodeList<ExpressionNode> expressions = serviceDeclarationNode.expressions();
List<ListenerInfo> listeners = new ArrayList<>();
Expand All @@ -344,6 +344,9 @@ private List<ListenerInfo> extractListeners(ServiceDeclarationNode serviceDeclar
}
listenerInfo = httpsListener.get();
} else {
if (expressionNode.kind() != SyntaxKind.EXPLICIT_NEW_EXPRESSION) {
return Collections.emptyList();
}
//Inline Listener
ExplicitNewExpressionNode refNode = (ExplicitNewExpressionNode) expressionNode;
FunctionArgumentNode functionArgumentNode = refNode.parenthesizedArgList().arguments().get(0);
Expand All @@ -354,7 +357,7 @@ private List<ListenerInfo> extractListeners(ServiceDeclarationNode serviceDeclar
return Collections.emptyList();
}
listenerInfo = newListenerInfo.get();

//Inline Http config
if (refNode.parenthesizedArgList().arguments().size() > 1) {
FunctionArgumentNode secondParamExpression = refNode.parenthesizedArgList().arguments().get(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public void testSimpleHttpNoServiceDecl() {
Assert.assertEquals(listener.getPort(), 9090);
}

@Test
public void testVariableHttpServiceListenerDecl() {
Path projectPath = Paths.get("src", "test", "resources", "service", "listener-variable");

BuildProject project = BuildProject.load(projectPath);
ProjectServiceInfo projectServiceInfo = new ProjectServiceInfo(project);
List<ServiceInfo> serviceList = projectServiceInfo.getServiceList();

Assert.assertEquals(serviceList.size(), 1);
}

@Test
public void testExposeOnListenerDeclaration() {
Path projectPath = Paths.get("src", "test", "resources", "service", "expose-on-listener");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
org = "ballerina"
name = "clientconfig"
version = "0.1.0"

[build-options]
observabilityIncluded = true
cloud = "k8s"
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2023 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.

import ballerina/log;
import ballerina/http;

isolated service /registerAPI on internalLs {

isolated resource function post .(string apiName, string candidateGateway)
returns http:Created|http:InternalServerError {
do {
log:printInfo("Registering API: " + apiName + " with gateway: " + candidateGateway);
} on fail {
return http:INTERNAL_SERVER_ERROR;
}
log:printInfo("Registered API: " + apiName + " with gateway: " + candidateGateway);
return http:CREATED;
}
}

configurable int INTERNAL_SERVICE_PORT = 9090;
configurable string ADMIN_SERVICE_HOST = "localhost:9443";
configurable string CERT_PATH = "./resources/wso2carbon.cer";
configurable string ADMIN_SERVICE_SCOPES = "apim:tenant_theme_manage apim:tier_manage apim:tier_view openid";
configurable string TOKEN_URL = "https://localhost:9443/oauth2/token";
configurable string CLIENT_ID = "xxxxxxx";
configurable string CLIENT_SECRET = "xxxxxxx";

final http:Client apimAdmin = check new (ADMIN_SERVICE_HOST, auth = {
tokenUrl: TOKEN_URL,
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
scopes: ADMIN_SERVICE_SCOPES,
clientConfig: {
secureSocket: {cert: CERT_PATH}
}
}, secureSocket = {cert: CERT_PATH});

public listener http:Listener internalLs = new (INTERNAL_SERVICE_PORT);

0 comments on commit 4edaa49

Please sign in to comment.