diff --git a/ballerina-tests/http-interceptor-tests/tests/interceptors_basic_tests.bal b/ballerina-tests/http-interceptor-tests/tests/interceptors_basic_tests.bal index 30e79a1ab7..d29c452f88 100644 --- a/ballerina-tests/http-interceptor-tests/tests/interceptors_basic_tests.bal +++ b/ballerina-tests/http-interceptor-tests/tests/interceptors_basic_tests.bal @@ -25,10 +25,11 @@ final http:Client interceptorsBasicTestsClientEP1 = check new("http://localhost: listener http:Listener interceptorsBasicTestsServerEP1 = new(interceptorBasicTestsPort1, httpVersion = http:HTTP_1_1); -@http:ServiceConfig { - interceptors : [new DefaultRequestInterceptor(), new LastRequestInterceptor(), new DefaultRequestErrorInterceptor()] -} -service /defaultRequestInterceptor on interceptorsBasicTestsServerEP1 { +service http:InterceptableService /defaultRequestInterceptor on interceptorsBasicTestsServerEP1 { + + public function createInterceptors() returns [DefaultRequestInterceptor, LastRequestInterceptor, DefaultRequestErrorInterceptor] { + return [new DefaultRequestInterceptor(), new LastRequestInterceptor(), new DefaultRequestErrorInterceptor()]; + } resource function 'default .(http:Caller caller, http:Request req) returns error? { http:Response res = new(); @@ -82,10 +83,11 @@ function testinterceptableServiceReqInterceptor() returns error? { common:assertHeaderValue(check res.getHeader("last-request-interceptor"), "true"); } -@http:ServiceConfig { - interceptors : [new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new DefaultResponseInterceptor()] -} -service /defaultResponseInterceptor on interceptorsBasicTestsServerEP1 { +service http:InterceptableService /defaultResponseInterceptor on interceptorsBasicTestsServerEP1 { + + public function createInterceptors() returns [LastResponseInterceptor, DefaultResponseErrorInterceptor, DefaultResponseInterceptor] { + return [new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new DefaultResponseInterceptor()]; + } resource function 'default .(http:Request req) returns string { string|error payload = req.getTextPayload(); diff --git a/ballerina-tests/http-interceptor-tests/tests/interceptors_error_handling_tests.bal b/ballerina-tests/http-interceptor-tests/tests/interceptors_error_handling_tests.bal index 8e2aade6cf..c0c395a5a8 100644 --- a/ballerina-tests/http-interceptor-tests/tests/interceptors_error_handling_tests.bal +++ b/ballerina-tests/http-interceptor-tests/tests/interceptors_error_handling_tests.bal @@ -410,13 +410,12 @@ function testInvalidPathWithRootService() returns error? { common:assertHeaderValue(check res.getHeader("error-type"), "DispatchingError-Resource"); } -listener http:Listener singleServiceWithListenerInterceptorsEP = new(singleServiceWithListenerInterceptorsTestPort, - httpVersion = http:HTTP_1_1, interceptors = [new LastResponseInterceptor(), new DefaultResponseErrorInterceptor()]); +listener http:Listener singleServiceWithListenerInterceptorsEP = new(singleServiceWithListenerInterceptorsTestPort, httpVersion = http:HTTP_1_1); service http:InterceptableService /path1 on singleServiceWithListenerInterceptorsEP { - public function createInterceptors() returns [ResponseInterceptorReturnsResponse, ResponseErrorInterceptorWithReq] { - return [new ResponseInterceptorReturnsResponse(), new ResponseErrorInterceptorWithReq()]; + public function createInterceptors() returns [LastResponseInterceptor, DefaultResponseErrorInterceptor, ResponseInterceptorReturnsResponse, ResponseErrorInterceptorWithReq] { + return [new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new ResponseInterceptorReturnsResponse(), new ResponseErrorInterceptorWithReq()]; } resource function get .() returns string { diff --git a/ballerina-tests/http2-tests/tests/http2_service_dispatching_data_binding_test.bal b/ballerina-tests/http2-tests/tests/http2_service_dispatching_data_binding_test.bal index fbe53e220a..f10a15e90e 100644 --- a/ballerina-tests/http2-tests/tests/http2_service_dispatching_data_binding_test.bal +++ b/ballerina-tests/http2-tests/tests/http2_service_dispatching_data_binding_test.bal @@ -61,40 +61,44 @@ function testHTTPS2DataBinding() returns error? { test:assertEquals(payload, {'key:"wso2",age:12}); } -@http:ServiceConfig { - interceptors : [new DefaultRequestInterceptor(), new DataBindingRequestInterceptor(), new LastRequestInterceptor()] -} -service /interceptor1 on generalHTTP2Listener { +service http:InterceptableService /interceptor1 on generalHTTP2Listener { + + public function createInterceptors() returns [DefaultRequestInterceptor, DataBindingRequestInterceptor, LastRequestInterceptor] { + return [new DefaultRequestInterceptor(), new DataBindingRequestInterceptor(), new LastRequestInterceptor()]; + } resource function post databinding(@http:Payload string payload) returns string { return "Response : " + payload; } } -@http:ServiceConfig { - interceptors : [new DefaultRequestInterceptor(), new LastRequestInterceptor()] -} -service /interceptor2 on generalHTTP2Listener { +service http:InterceptableService /interceptor2 on generalHTTP2Listener { + + public function createInterceptors() returns [DefaultRequestInterceptor, LastRequestInterceptor] { + return [new DefaultRequestInterceptor(), new LastRequestInterceptor()]; + } resource function post databinding(@http:Payload string payload) returns string { return "Response : " + payload; } } -@http:ServiceConfig { - interceptors : [new DefaultRequestInterceptor(), new DataBindingRequestInterceptor(), new LastRequestInterceptor()] -} -service /interceptor1 on generalHTTPS2Listener { - +service http:InterceptableService /interceptor1 on generalHTTPS2Listener { + + public function createInterceptors() returns [DefaultRequestInterceptor, DataBindingRequestInterceptor, LastRequestInterceptor] { + return [new DefaultRequestInterceptor(), new DataBindingRequestInterceptor(), new LastRequestInterceptor()]; + } + resource function post databinding(@http:Payload string payload) returns string { return "Response : " + payload; } } -@http:ServiceConfig { - interceptors : [new DefaultRequestInterceptor(), new LastRequestInterceptor()] -} -service /interceptor2 on generalHTTPS2Listener { +service http:InterceptableService /interceptor2 on generalHTTPS2Listener { + + public function createInterceptors() returns [DefaultRequestInterceptor, LastRequestInterceptor] { + return [new DefaultRequestInterceptor(), new LastRequestInterceptor()]; + } resource function post databinding(@http:Payload string payload) returns string { return "Response : " + payload; diff --git a/ballerina/http_annotation.bal b/ballerina/http_annotation.bal index 2e0b82f6b0..9eb58f8b3c 100644 --- a/ballerina/http_annotation.bal +++ b/ballerina/http_annotation.bal @@ -33,11 +33,6 @@ public type HttpServiceConfig record {| ListenerAuthConfig[] auth?; string mediaTypeSubtypePrefix?; boolean treatNilableAsOptional = true; - # interceptors - An array of interceptor services - # # Deprecated - # Defining interceptor pipeline in `http:ServiceConfig` is deprecated. Define the interceptor pipeline via `http:InterceptableService` service type - @deprecated - Interceptor|Interceptor[] interceptors?; byte[] openApiDefinition = []; boolean validation = true; |}; diff --git a/ballerina/http_service_endpoint.bal b/ballerina/http_service_endpoint.bal index 241acc0828..2740d912f8 100644 --- a/ballerina/http_service_endpoint.bal +++ b/ballerina/http_service_endpoint.bal @@ -44,17 +44,8 @@ public isolated class Listener { return externInitEndpoint(self, config); } - isolated function createInterceptors(Interceptor|Interceptor[]? interceptorConfigValues) returns Interceptor[] { - Interceptor|Interceptor[]? configValues = interceptorConfigValues; - Interceptor[] interceptors = [new DefaultErrorInterceptor()]; - if configValues is Interceptor[] { - foreach Interceptor interceptor in configValues { - interceptors.push(interceptor); - } - } else if configValues is Interceptor { - interceptors.push(configValues); - } - return interceptors; + isolated function createInterceptors() returns Interceptor[] { + return [new DefaultErrorInterceptor()]; } # Starts the registered service programmatically. @@ -165,12 +156,6 @@ public type ListenerConfiguration record {| decimal timeout = DEFAULT_LISTENER_TIMEOUT; string? server = (); RequestLimitConfigs requestLimits = {}; - # interceptors - An array of interceptor services - # # Deprecated - # Defining interceptor pipeline in `http:ListenerConfiguration` is deprecated. - # Define the interceptor pipeline via `http:InterceptableService` service type - @deprecated - Interceptor|Interceptor[] interceptors?; decimal gracefulStopTimeout = DEFAULT_GRACEFULSTOP_TIMEOUT; ServerSocketConfig socketConfig = {}; int http2InitialWindowSize = 65535; diff --git a/changelog.md b/changelog.md index a2f4598541..12329e7588 100644 --- a/changelog.md +++ b/changelog.md @@ -15,6 +15,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - [Add open record support for query parameters](https://github.com/ballerina-platform/ballerina-standard-library/issues/4541) +### Changed + +- [Remove interceptor configuration from `http:ListenerConfiguration` and `http:ServiceConfig`](https://github.com/ballerina-platform/ballerina-standard-library/issues/4680) + ## [2.9.0] - 2023-06-30 ### Added @@ -28,7 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Changed -- [Removed listener level interceptors](https://github.com/ballerina-platform/ballerina-standard-library/issues/4420) +- [Deprecate listener level interceptors](https://github.com/ballerina-platform/ballerina-standard-library/issues/4420) - [Improved default error format and message](https://github.com/ballerina-platform/ballerina-standard-library/issues/3961) - [Move status code errors to `httpscerr` module](https://github.com/ballerina-platform/ballerina-standard-library/issues/4535) diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java index 81077493e7..3d6fc2edda 100644 --- a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java +++ b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java @@ -92,12 +92,6 @@ private void assertError(DiagnosticResult diagnosticResult, int index, String me } } - private void assertWarning(DiagnosticResult diagnosticResult, int index, String message, String code) { - Diagnostic diagnostic = (Diagnostic) diagnosticResult.warnings().toArray()[index]; - Assert.assertTrue(diagnostic.diagnosticInfo().messageFormat().contains(message)); - Assert.assertEquals(diagnostic.diagnosticInfo().code(), code); - } - private void assertTrue(DiagnosticResult diagnosticResult, int index, String message, String code) { Diagnostic diagnostic = (Diagnostic) diagnosticResult.errors().toArray()[index]; Assert.assertTrue(diagnostic.diagnosticInfo().messageFormat().contains(message)); @@ -884,66 +878,12 @@ public void testResourceSignatureParamValidations() { "'(http_test/sample_38:0.1.0:QueryRecord|map)[]?'", HTTP_106); } - @Test - public void testServiceConfigInterceptorDeprecationWarning() { - Package currentPackage = loadPackage("sample_package_39"); - PackageCompilation packageCompilation = currentPackage.getCompilation(); - DiagnosticResult diagnosticResult = packageCompilation.diagnosticResult(); - Assert.assertFalse(diagnosticResult.hasErrors()); - Assert.assertEquals(diagnosticResult.warnings().size(), 1); - assertWarning(diagnosticResult, 0, HttpDiagnosticCodes.HTTP_201.getMessage(), - HttpDiagnosticCodes.HTTP_201.getCode()); - } - - @Test - public void testServiceConfigInterceptorAndInterceptableServiceError() { - Package currentPackage = loadPackage("sample_package_40"); - PackageCompilation packageCompilation = currentPackage.getCompilation(); - DiagnosticResult diagnosticResult = packageCompilation.diagnosticResult(); - Assert.assertEquals(diagnosticResult.warningCount(), 1); - assertWarning(diagnosticResult, 0, HttpDiagnosticCodes.HTTP_201.getMessage(), - HttpDiagnosticCodes.HTTP_201.getCode()); - Assert.assertEquals(diagnosticResult.errorCount(), 1); - assertTrue(diagnosticResult, 0, HttpDiagnosticCodes.HTTP_153.getMessage(), - HttpDiagnosticCodes.HTTP_153.getCode()); - } - @Test public void testInterceptableServiceInterceptors() { - Package currentPackage = loadPackage("sample_package_41"); + Package currentPackage = loadPackage("sample_package_39"); PackageCompilation packageCompilation = currentPackage.getCompilation(); DiagnosticResult diagnosticResult = packageCompilation.diagnosticResult(); Assert.assertFalse(diagnosticResult.hasWarnings()); Assert.assertFalse(diagnosticResult.hasErrors()); } - - @Test - public void testInterceptorsDefinedInListener() { - Package currentPackage = loadPackage("sample_package_42"); - PackageCompilation packageCompilation = currentPackage.getCompilation(); - DiagnosticResult diagnosticResult = packageCompilation.diagnosticResult(); - Assert.assertFalse(diagnosticResult.hasErrors()); - Assert.assertEquals(diagnosticResult.warningCount(), 2); - assertWarning(diagnosticResult, 0, HttpDiagnosticCodes.HTTP_202.getMessage(), - HttpDiagnosticCodes.HTTP_202.getCode()); - assertWarning(diagnosticResult, 1, HttpDiagnosticCodes.HTTP_202.getMessage(), - HttpDiagnosticCodes.HTTP_202.getCode()); - } - - @Test - public void testInterceptorsDefinedInListenerAndServiceConfig() { - Package currentPackage = loadPackage("sample_package_43"); - PackageCompilation packageCompilation = currentPackage.getCompilation(); - DiagnosticResult diagnosticResult = packageCompilation.diagnosticResult(); - Assert.assertFalse(diagnosticResult.hasErrors()); - Assert.assertEquals(diagnosticResult.warningCount(), 4); - assertWarning(diagnosticResult, 0, HttpDiagnosticCodes.HTTP_201.getMessage(), - HttpDiagnosticCodes.HTTP_201.getCode()); - assertWarning(diagnosticResult, 1, HttpDiagnosticCodes.HTTP_202.getMessage(), - HttpDiagnosticCodes.HTTP_202.getCode()); - assertWarning(diagnosticResult, 2, HttpDiagnosticCodes.HTTP_202.getMessage(), - HttpDiagnosticCodes.HTTP_202.getCode()); - assertWarning(diagnosticResult, 3, HttpDiagnosticCodes.HTTP_201.getMessage(), - HttpDiagnosticCodes.HTTP_201.getCode()); - } } diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_39/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_39/service.bal index 48dd8a21e5..1adaae6d81 100644 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_39/service.bal +++ b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_39/service.bal @@ -18,10 +18,22 @@ service class MyResponseInterceptor { } } -@http:ServiceConfig { - interceptors: [new MyRequestInterceptor(), new MyResponseInterceptor()] +service http:InterceptableService / on new http:Listener(9099) { + + public function createInterceptors() returns [MyRequestInterceptor, MyResponseInterceptor] { + return [new MyRequestInterceptor(), new MyResponseInterceptor()]; + } + + resource function get hello(http:Caller caller) returns error? { + check caller->respond("Hello, World!"); + } } -service / on new http:Listener(9099) { + +service http:InterceptableService / on new http:Listener(9100) { + + public function createInterceptors() returns MyRequestInterceptor { + return new MyRequestInterceptor(); + } resource function get hello(http:Caller caller) returns error? { check caller->respond("Hello, World!"); diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_40/Ballerina.toml b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_40/Ballerina.toml deleted file mode 100644 index b7b8157477..0000000000 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_40/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "http_test" -name = "sample_40" -version = "0.1.0" diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_40/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_40/service.bal deleted file mode 100644 index 907d4923fc..0000000000 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_40/service.bal +++ /dev/null @@ -1,33 +0,0 @@ -import ballerina/http; - -service class MyRequestInterceptor { - *http:RequestInterceptor; - - resource function 'default [string... path](http:RequestContext ctx) - returns http:NextService|error? { - return ctx.next(); - } -} - -service class MyResponseInterceptor { - *http:ResponseInterceptor; - - remote function interceptResponse(http:RequestContext ctx) - returns http:NextService|error? { - return ctx.next(); - } -} - -@http:ServiceConfig { - interceptors: [new MyRequestInterceptor(), new MyResponseInterceptor()] -} -service http:InterceptableService / on new http:Listener(9099) { - - public function createInterceptors() returns [MyRequestInterceptor, MyResponseInterceptor] { - return [new MyRequestInterceptor(), new MyResponseInterceptor()]; - } - - resource function get hello(http:Caller caller) returns error? { - check caller->respond("Hello, World!"); - } -} diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_41/Ballerina.toml b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_41/Ballerina.toml deleted file mode 100644 index 78b28feb26..0000000000 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_41/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "http_test" -name = "sample_41" -version = "0.1.0" diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_41/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_41/service.bal deleted file mode 100644 index 1adaae6d81..0000000000 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_41/service.bal +++ /dev/null @@ -1,41 +0,0 @@ -import ballerina/http; - -service class MyRequestInterceptor { - *http:RequestInterceptor; - - resource function 'default [string... path](http:RequestContext ctx) - returns http:NextService|error? { - return ctx.next(); - } -} - -service class MyResponseInterceptor { - *http:ResponseInterceptor; - - remote function interceptResponse(http:RequestContext ctx) - returns http:NextService|error? { - return ctx.next(); - } -} - -service http:InterceptableService / on new http:Listener(9099) { - - public function createInterceptors() returns [MyRequestInterceptor, MyResponseInterceptor] { - return [new MyRequestInterceptor(), new MyResponseInterceptor()]; - } - - resource function get hello(http:Caller caller) returns error? { - check caller->respond("Hello, World!"); - } -} - -service http:InterceptableService / on new http:Listener(9100) { - - public function createInterceptors() returns MyRequestInterceptor { - return new MyRequestInterceptor(); - } - - resource function get hello(http:Caller caller) returns error? { - check caller->respond("Hello, World!"); - } -} diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_42/Ballerina.toml b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_42/Ballerina.toml deleted file mode 100644 index a7b9da0da4..0000000000 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_42/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "http_test" -name = "sample_42" -version = "0.1.0" diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_42/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_42/service.bal deleted file mode 100644 index 61852e3e67..0000000000 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_42/service.bal +++ /dev/null @@ -1,33 +0,0 @@ -import ballerina/http; - -service class MyRequestInterceptor { - *http:RequestInterceptor; - - resource function 'default [string... path](http:RequestContext ctx) returns http:NextService|error? { - return ctx.next(); - } -} - -service class MyResponseInterceptor { - *http:ResponseInterceptor; - - remote function interceptResponse(http:RequestContext ctx) returns http:NextService|error? { - return ctx.next(); - } -} - -service / on new http:Listener(9099, interceptors = [new MyRequestInterceptor(), new MyResponseInterceptor()]) { - - resource function get hello(http:Caller caller) returns error? { - check caller->respond("Hello, World!"); - } -} - -listener http:Listener 'listener = new (9090, interceptors = [new MyRequestInterceptor()]); - -service / on 'listener { - - resource function get hello(http:Caller caller) returns error? { - check caller->respond("Hello, World!"); - } -} diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_43/Ballerina.toml b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_43/Ballerina.toml deleted file mode 100644 index 851b81a01e..0000000000 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_43/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "http_test" -name = "sample_43" -version = "0.1.0" diff --git a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_43/service.bal b/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_43/service.bal deleted file mode 100644 index 1ad02de30f..0000000000 --- a/compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_43/service.bal +++ /dev/null @@ -1,35 +0,0 @@ -import ballerina/http; - -service class MyRequestInterceptor { - *http:RequestInterceptor; - - resource function 'default [string... path](http:RequestContext ctx) returns http:NextService|error? { - return ctx.next(); - } -} - -service class MyResponseInterceptor { - *http:ResponseInterceptor; - - remote function interceptResponse(http:RequestContext ctx) returns http:NextService|error? { - return ctx.next(); - } -} - -@http:ServiceConfig {interceptors: new MyResponseInterceptor()} -service / on new http:Listener(9099, interceptors = new MyRequestInterceptor()) { - - resource function get hello(http:Caller caller) returns error? { - check caller->respond("Hello, World!"); - } -} - -listener http:Listener 'listener = new (9090, interceptors = new MyRequestInterceptor()); - -@http:ServiceConfig {interceptors: new MyResponseInterceptor()} -service / on 'listener { - - resource function get hello(http:Caller caller) returns error? { - check caller->respond("Hello, World!"); - } -} diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/Constants.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/Constants.java index b27c369cb7..e9a982e2bb 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/Constants.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/Constants.java @@ -73,7 +73,6 @@ public class Constants { public static final String CACHE_ANNOTATION = "Cache"; public static final String SERVICE_CONFIG_ANNOTATION = "ServiceConfig"; public static final String MEDIA_TYPE_SUBTYPE_PREFIX = "mediaTypeSubtypePrefix"; - public static final String INTERCEPTORS_ANNOTATION_FIELD = "interceptors"; public static final String INTERCEPTABLE_SERVICE = "InterceptableService"; public static final String RESOURCE_CONFIG_ANNOTATION = "ResourceConfig"; public static final String PAYLOAD_ANNOTATION_TYPE = "HttpPayload"; diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpCompilerPluginUtil.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpCompilerPluginUtil.java index 3709c3e545..21b014b2ab 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpCompilerPluginUtil.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpCompilerPluginUtil.java @@ -29,11 +29,8 @@ import io.ballerina.compiler.api.symbols.TypeSymbol; import io.ballerina.compiler.syntax.tree.AnnotationNode; import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; -import io.ballerina.compiler.syntax.tree.NamedArgumentNode; -import io.ballerina.compiler.syntax.tree.NewExpressionNode; import io.ballerina.compiler.syntax.tree.Node; import io.ballerina.compiler.syntax.tree.NodeList; -import io.ballerina.compiler.syntax.tree.ParenthesizedArgList; import io.ballerina.compiler.syntax.tree.ReturnTypeDescriptorNode; import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; import io.ballerina.tools.diagnostics.DiagnosticFactory; @@ -92,7 +89,6 @@ import static io.ballerina.stdlib.http.compiler.Constants.TUPLE_OF_ANYDATA; import static io.ballerina.stdlib.http.compiler.Constants.UNNECESSARY_CHARS_REGEX; import static io.ballerina.stdlib.http.compiler.Constants.XML; -import static io.ballerina.stdlib.http.compiler.HttpDiagnosticCodes.HTTP_202; /** * Utility class providing http compiler plugin utility methods. @@ -154,23 +150,6 @@ public static void extractInterceptorReturnTypeAndValidate(SyntaxNodeAnalysisCon } } - public static void validateListenerExpressionNode(SyntaxNodeAnalysisContext context, - NewExpressionNode expressionNode) { - expressionNode.childEntries().stream().forEach(childNodeEntry -> { - if (childNodeEntry.name().equals("parenthesizedArgList")) { - if (childNodeEntry.node().isPresent() && childNodeEntry.node().get() instanceof ParenthesizedArgList) { - ParenthesizedArgList argList = (ParenthesizedArgList) childNodeEntry.node().get(); - argList.children().forEach(child -> { - if (child instanceof NamedArgumentNode && ((NamedArgumentNode) child).argumentName() - .name().text().contains("interceptor")) { - updateDiagnostic(context, child.location(), HTTP_202); - } - }); - } - } - }); - } - public static void validateResourceReturnType(SyntaxNodeAnalysisContext ctx, Node node, Map typeSymbols, String returnTypeStringValue, TypeSymbol returnTypeSymbol, HttpDiagnosticCodes diagnosticCode, diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpDiagnosticCodes.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpDiagnosticCodes.java index d76adf92a9..25f39a65c7 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpDiagnosticCodes.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpDiagnosticCodes.java @@ -25,7 +25,6 @@ import static io.ballerina.stdlib.http.compiler.Constants.RESOURCE_CONFIG_ANNOTATION; import static io.ballerina.tools.diagnostics.DiagnosticSeverity.ERROR; import static io.ballerina.tools.diagnostics.DiagnosticSeverity.INTERNAL; -import static io.ballerina.tools.diagnostics.DiagnosticSeverity.WARNING; /** * {@code DiagnosticCodes} is used to hold diagnostic codes. @@ -111,20 +110,11 @@ public enum HttpDiagnosticCodes { HTTP_151("HTTP_151", "ambiguous types for parameter '%s' and '%s'. Use annotations to avoid ambiguity", ERROR), HTTP_152("HTTP_152", "invalid union type for default payload param: '%s'. Use basic structured anydata types", ERROR), - HTTP_153("HTTP_153", "cannot initiate interceptors in services via both " + - "http:ServiceConfig and http:InterceptableService", ERROR), HTTP_HINT_101("HTTP_HINT_101", "Payload annotation can be added", INTERNAL), HTTP_HINT_102("HTTP_HINT_102", "Header annotation can be added", INTERNAL), HTTP_HINT_103("HTTP_HINT_103", "Response content-type can be added", INTERNAL), - HTTP_HINT_104("HTTP_HINT_104", "Response cache configuration can be added", INTERNAL), - - HTTP_201("HTTP_201", "defining interceptor pipeline using http:ServiceConfig annotation " + - "is deprecated. Use http:InterceptableService instead. See https://ballerina.io/learn/by-example/" + - "http-request-interceptor", WARNING), - HTTP_202("HTTP_202", "defining interceptor pipeline in the http:ListenerConfiguration " + - "is deprecated. Use http:InterceptableService instead. See https://ballerina.io/learn/by-example/" + - "http-request-interceptor", WARNING); + HTTP_HINT_104("HTTP_HINT_104", "Response cache configuration can be added", INTERNAL); private final String code; private final String message; diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpListenerValidator.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpListenerValidator.java deleted file mode 100644 index 9e5fe8385c..0000000000 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpListenerValidator.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com) All Rights Reserved. - * - * 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.http.compiler; - -import io.ballerina.compiler.api.symbols.Symbol; -import io.ballerina.compiler.syntax.tree.ListenerDeclarationNode; -import io.ballerina.compiler.syntax.tree.NewExpressionNode; -import io.ballerina.projects.plugins.AnalysisTask; -import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; - -import java.util.Optional; - -import static io.ballerina.stdlib.http.compiler.HttpCompilerPluginUtil.isHttpModule; -import static io.ballerina.stdlib.http.compiler.HttpCompilerPluginUtil.validateListenerExpressionNode; -import static io.ballerina.stdlib.http.compiler.HttpServiceValidator.diagnosticContainsErrors; - -/** - * Validates Ballerina listener initialization. - */ -public class HttpListenerValidator implements AnalysisTask { - - @Override - public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { - if (diagnosticContainsErrors(syntaxNodeAnalysisContext)) { - return; - } - ListenerDeclarationNode listenerDeclarationNode = getListenerDeclarationNode(syntaxNodeAnalysisContext); - if (listenerDeclarationNode == null) { - return; - } - validateListenerDeclarationNode(syntaxNodeAnalysisContext, listenerDeclarationNode); - } - - private void validateListenerDeclarationNode(SyntaxNodeAnalysisContext context, - ListenerDeclarationNode listenerDeclarationNode) { - listenerDeclarationNode.children().forEach(child -> { - if (child instanceof NewExpressionNode) { - validateListenerExpressionNode(context, (NewExpressionNode) child); - } - }); - } - - public static ListenerDeclarationNode getListenerDeclarationNode(SyntaxNodeAnalysisContext context) { - ListenerDeclarationNode listenerDeclarationNode = (ListenerDeclarationNode) context.node(); - if (listenerDeclarationNode.typeDescriptor().isPresent()) { - Optional symbol = context.semanticModel().symbol(listenerDeclarationNode.typeDescriptor().get()); - if (symbol.isPresent() && symbol.get().getModule().isPresent() && - isHttpModule(symbol.get().getModule().get())) { - return listenerDeclarationNode; - } - } - return null; - } -} diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpServiceAnalyzer.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpServiceAnalyzer.java index a99da27af0..e7d1041ac2 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpServiceAnalyzer.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpServiceAnalyzer.java @@ -29,7 +29,6 @@ public class HttpServiceAnalyzer extends CodeAnalyzer { @Override public void init(CodeAnalysisContext codeAnalysisContext) { codeAnalysisContext.addSyntaxNodeAnalysisTask(new HttpServiceValidator(), SyntaxKind.SERVICE_DECLARATION); - codeAnalysisContext.addSyntaxNodeAnalysisTask(new HttpListenerValidator(), SyntaxKind.LISTENER_DECLARATION); codeAnalysisContext.addSyntaxNodeAnalysisTask(new HttpInterceptorServiceValidator(), SyntaxKind.CLASS_DEFINITION); } diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpServiceValidator.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpServiceValidator.java index 189ab287c4..4e1caa9dad 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpServiceValidator.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpServiceValidator.java @@ -18,7 +18,6 @@ package io.ballerina.stdlib.http.compiler; -import io.ballerina.compiler.api.symbols.IntersectionTypeSymbol; import io.ballerina.compiler.api.symbols.ServiceDeclarationSymbol; import io.ballerina.compiler.api.symbols.Symbol; import io.ballerina.compiler.api.symbols.TypeDescKind; @@ -26,12 +25,10 @@ import io.ballerina.compiler.api.symbols.TypeSymbol; import io.ballerina.compiler.api.symbols.UnionTypeSymbol; import io.ballerina.compiler.syntax.tree.AnnotationNode; -import io.ballerina.compiler.syntax.tree.ChildNodeEntry; import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; import io.ballerina.compiler.syntax.tree.MappingConstructorExpressionNode; import io.ballerina.compiler.syntax.tree.MappingFieldNode; import io.ballerina.compiler.syntax.tree.MetadataNode; -import io.ballerina.compiler.syntax.tree.NewExpressionNode; import io.ballerina.compiler.syntax.tree.Node; import io.ballerina.compiler.syntax.tree.NodeList; import io.ballerina.compiler.syntax.tree.QualifiedNameReferenceNode; @@ -45,7 +42,6 @@ import io.ballerina.tools.diagnostics.DiagnosticInfo; import io.ballerina.tools.diagnostics.DiagnosticSeverity; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -55,7 +51,6 @@ import static io.ballerina.stdlib.http.compiler.Constants.DEFAULT; import static io.ballerina.stdlib.http.compiler.Constants.HTTP; import static io.ballerina.stdlib.http.compiler.Constants.INTERCEPTABLE_SERVICE; -import static io.ballerina.stdlib.http.compiler.Constants.INTERCEPTORS_ANNOTATION_FIELD; import static io.ballerina.stdlib.http.compiler.Constants.MEDIA_TYPE_SUBTYPE_PREFIX; import static io.ballerina.stdlib.http.compiler.Constants.MEDIA_TYPE_SUBTYPE_REGEX; import static io.ballerina.stdlib.http.compiler.Constants.PLUS; @@ -66,12 +61,9 @@ import static io.ballerina.stdlib.http.compiler.HttpCompilerPluginUtil.getCtxTypes; import static io.ballerina.stdlib.http.compiler.HttpCompilerPluginUtil.isHttpModule; import static io.ballerina.stdlib.http.compiler.HttpCompilerPluginUtil.updateDiagnostic; -import static io.ballerina.stdlib.http.compiler.HttpCompilerPluginUtil.validateListenerExpressionNode; import static io.ballerina.stdlib.http.compiler.HttpDiagnosticCodes.HTTP_101; import static io.ballerina.stdlib.http.compiler.HttpDiagnosticCodes.HTTP_119; import static io.ballerina.stdlib.http.compiler.HttpDiagnosticCodes.HTTP_120; -import static io.ballerina.stdlib.http.compiler.HttpDiagnosticCodes.HTTP_153; -import static io.ballerina.stdlib.http.compiler.HttpDiagnosticCodes.HTTP_201; /** * Validates a Ballerina Http Service. @@ -90,7 +82,6 @@ public void perform(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) { } extractServiceAnnotationAndValidate(syntaxNodeAnalysisContext, serviceDeclarationNode); - validateListenerDeclarationInService(syntaxNodeAnalysisContext, serviceDeclarationNode); LinksMetaData linksMetaData = new LinksMetaData(); NodeList members = serviceDeclarationNode.members(); @@ -146,31 +137,6 @@ private static boolean isListenerBelongsToHttpModule(TypeSymbol listenerType) { return false; } - private void validateListenerDeclarationInService(SyntaxNodeAnalysisContext context, - ServiceDeclarationNode serviceDeclarationNode) { - for (ChildNodeEntry nodeEntry: serviceDeclarationNode.childEntries()) { - if (nodeEntry.name().equals("expressions")) { - if (nodeEntry.node().isPresent() && nodeEntry.node().get() instanceof NewExpressionNode) { - validateListenerExpressionNode(context, (NewExpressionNode) nodeEntry.node().get()); - } - } - } - } - - public static TypeSymbol getEffectiveTypeFromReadonlyIntersection(IntersectionTypeSymbol intersectionTypeSymbol) { - List effectiveTypes = new ArrayList<>(); - for (TypeSymbol typeSymbol : intersectionTypeSymbol.memberTypeDescriptors()) { - if (typeSymbol.typeKind() == TypeDescKind.READONLY) { - continue; - } - effectiveTypes.add(typeSymbol); - } - if (effectiveTypes.size() == 1) { - return effectiveTypes.get(0); - } - return null; - } - public static TypeDescKind getReferencedTypeDescKind(TypeSymbol typeSymbol) { TypeDescKind kind = typeSymbol.typeKind(); if (kind == TypeDescKind.TYPE_REFERENCE) { @@ -180,15 +146,6 @@ public static TypeDescKind getReferencedTypeDescKind(TypeSymbol typeSymbol) { return kind; } - public static boolean isAllowedQueryParamBasicType(TypeDescKind kind, TypeSymbol typeSymbol) { - if (kind == TypeDescKind.TYPE_REFERENCE) { - TypeSymbol typeDescriptor = ((TypeReferenceTypeSymbol) typeSymbol).typeDescriptor(); - kind = getReferencedTypeDescKind(typeDescriptor); - } - return kind == TypeDescKind.STRING || kind == TypeDescKind.INT || kind == TypeDescKind.FLOAT || - kind == TypeDescKind.DECIMAL || kind == TypeDescKind.BOOLEAN; - } - private void validateResourceLinks(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext, LinksMetaData linksMetaData) { if (!linksMetaData.hasNameReferenceObjects()) { @@ -290,12 +247,6 @@ private static void validateServiceConfigAnnotation(SyntaxNodeAnalysisContext ct break; } } - if (INTERCEPTORS_ANNOTATION_FIELD.equals(strings[0].trim())) { - if (isInterceptableService) { - updateDiagnostic(ctx, field.location(), HTTP_153); - } - updateDiagnostic(ctx, field.location(), HTTP_201); - } } } } diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpService.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpService.java index 0f2baeb159..86b0318411 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpService.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpService.java @@ -441,7 +441,6 @@ public static void populateInterceptorServicesRegistries(List typeId.getName().equals(INTERCEPTABLE_SERVICE)); BArray interceptorsArrayFromService; - BMap serviceConfig = getHttpServiceConfigAnnotation(service.getBalService()); if (includesInterceptableService) { final Object[] createdInterceptors = new Object[1]; CountDownLatch latch = new CountDownLatch(1); @@ -480,16 +479,6 @@ public void notifyFailure(BError bError) { interceptorsArrayFromService = ValueCreator.createArrayValue(createdInterceptors, TypeCreator.createArrayType(((BObject) createdInterceptors[0]).getOriginalType())); } - // TODO need to remove following after removing `interceptors` from `http:ServiceConfig` - } else if (serviceConfig != null && serviceConfig.get(HttpConstants.ANN_INTERCEPTORS) != null) { - Object interceptorPipeline = serviceConfig.get(HttpConstants.ANN_INTERCEPTORS); - if (interceptorPipeline instanceof BArray) { - interceptorsArrayFromService = serviceConfig.getArrayValue(HttpConstants.ANN_INTERCEPTORS); - } else { - BObject interceptor = serviceConfig.getObjectValue(HttpConstants.ANN_INTERCEPTORS); - interceptorsArrayFromService = ValueCreator.createArrayValue(new Object[] { interceptor }, - TypeCreator.createArrayType(interceptor.getOriginalType())); - } } else { service.setInterceptorServicesRegistries(interceptorServicesRegistries); service.setBalInterceptorServicesArray(interceptorsArrayFromListener); diff --git a/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java b/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java index 9e2ba7cc11..e470d0ab71 100644 --- a/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java +++ b/native/src/main/java/io/ballerina/stdlib/http/api/HttpUtil.java @@ -135,7 +135,6 @@ import static io.ballerina.stdlib.http.api.HttpConstants.ANN_CONFIG_ATTR_SSL_ENABLED_PROTOCOLS; import static io.ballerina.stdlib.http.api.HttpConstants.CREATE_INTERCEPTORS_FUNCTION_NAME; import static io.ballerina.stdlib.http.api.HttpConstants.ENDPOINT_CONFIG_HTTP2_INITIAL_WINDOW_SIZE; -import static io.ballerina.stdlib.http.api.HttpConstants.ENDPOINT_CONFIG_INTERCEPTORS; import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_HEADERS; import static io.ballerina.stdlib.http.api.HttpConstants.RESOLVED_REQUESTED_URI; import static io.ballerina.stdlib.http.api.HttpConstants.RESPONSE_CACHE_CONTROL; @@ -148,7 +147,6 @@ import static io.ballerina.stdlib.http.api.HttpConstants.SECURESOCKET_CONFIG_SESSION_TIMEOUT; import static io.ballerina.stdlib.http.api.HttpConstants.SECURESOCKET_CONFIG_TRUSTSTORE_FILE_PATH; import static io.ballerina.stdlib.http.api.HttpConstants.SECURESOCKET_CONFIG_TRUSTSTORE_PASSWORD; -import static io.ballerina.stdlib.http.api.HttpConstants.SERVICE_ENDPOINT_CONFIG; import static io.ballerina.stdlib.http.api.HttpConstants.SINGLE_SLASH; import static io.ballerina.stdlib.http.api.HttpConstants.SOCKET_CONFIG_CONNECT_TIMEOUT; import static io.ballerina.stdlib.http.api.HttpConstants.SOCKET_CONFIG_KEEP_ALIVE; @@ -1606,9 +1604,6 @@ public static void populateInterceptorServicesFromService(BObject serviceEndpoin } public static void populateInterceptorServicesFromListener(BObject serviceEndpoint, Runtime runtime) { - List interceptorServices = new ArrayList<>(); - - BMap listenerConfig = ((BMap) serviceEndpoint.getNativeData(SERVICE_ENDPOINT_CONFIG)); final CountDownLatch latch = new CountDownLatch(1); final BArray[] interceptorResponse = new BArray[1]; Callback interceptorCallback = new Callback() { @@ -1627,14 +1622,8 @@ public void notifyFailure(BError bError) { System.exit(1); } }; - if (listenerConfig != null && listenerConfig.get(ENDPOINT_CONFIG_INTERCEPTORS) != null) { - Object interceptorConfig = listenerConfig.get(ENDPOINT_CONFIG_INTERCEPTORS); - runtime.invokeMethodAsyncSequentially(serviceEndpoint, CREATE_INTERCEPTORS_FUNCTION_NAME, null, null, - interceptorCallback, null, PredefinedTypes.TYPE_ANY, interceptorConfig, true); - } else { - runtime.invokeMethodAsyncSequentially(serviceEndpoint, CREATE_INTERCEPTORS_FUNCTION_NAME, null, null, - interceptorCallback, null, PredefinedTypes.TYPE_ANY, null, true); - } + runtime.invokeMethodAsyncSequentially(serviceEndpoint, CREATE_INTERCEPTORS_FUNCTION_NAME, null, null, + interceptorCallback, null, PredefinedTypes.TYPE_ANY, null, true); try { latch.await(); } catch (InterruptedException exception) { @@ -1643,27 +1632,17 @@ public void notifyFailure(BError bError) { if (interceptorResponse[0] == null) { return; } - Object[] interceptors = interceptorResponse[0].getValues(); - for (Object interceptor: interceptors) { - if (interceptor == null) { - break; - } - interceptorServices.add((BObject) interceptor); - } + BObject interceptorService = (BObject) interceptorResponse[0].getValues()[0]; serviceEndpoint.addNativeData(HttpConstants.INTERCEPTORS, interceptorResponse[0]); - Register.resetInterceptorRegistry(serviceEndpoint, interceptorServices.size()); + Register.resetInterceptorRegistry(serviceEndpoint, 1); List httpInterceptorServicesRegistries = Register.getHttpInterceptorServicesRegistries(serviceEndpoint); - // Registering all the interceptor services in separate service registries - for (int i = 0; i < interceptorServices.size(); i++) { - BObject interceptorService = interceptorServices.get(i); - HTTPInterceptorServicesRegistry servicesRegistry = httpInterceptorServicesRegistries.get(i); - servicesRegistry.setServicesType(HttpUtil.getInterceptorServiceType(interceptorService)); - servicesRegistry.registerInterceptorService(interceptorService, HttpConstants.DEFAULT_BASE_PATH, true); - servicesRegistry.setRuntime(runtime); - } + HTTPInterceptorServicesRegistry servicesRegistry = httpInterceptorServicesRegistries.get(0); + servicesRegistry.setServicesType(HttpUtil.getInterceptorServiceType(interceptorService)); + servicesRegistry.registerInterceptorService(interceptorService, HttpConstants.DEFAULT_BASE_PATH, true); + servicesRegistry.setRuntime(runtime); } public static void markPossibleLastInterceptors(HTTPServicesRegistry servicesRegistry) {