This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch CAMEL-22013-2 in repository https://gitbox.apache.org/repos/asf/camel.git
commit ae56a6d6f798c93afb188045a7c351a57c1a4935 Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Fri May 9 10:01:28 2025 +0200 CAMEL-22013 - camel-api - Remove all usage of component.extension and the component.extension package content itself - MongoDB Signed-off-by: Andrea Cosentino <anco...@gmail.com> --- .../camel/component/mongodb/MongoDbComponent.java | 4 - .../mongodb/meta/MongoDBMetaExtension.java | 107 -------------- .../verifier/MongoComponentVerifierExtension.java | 111 --------------- .../meta/integration/MongoDbMetaExtensionIT.java | 157 --------------------- .../integration/MongoDbVerifierExtensionIT.java | 134 ------------------ 5 files changed, 513 deletions(-) diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java index d4866d3e82f..4ee1c075dc7 100644 --- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java +++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java @@ -24,8 +24,6 @@ import java.util.Set; import com.mongodb.client.MongoClient; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; -import org.apache.camel.component.mongodb.meta.MongoDBMetaExtension; -import org.apache.camel.component.mongodb.verifier.MongoComponentVerifierExtension; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.DefaultComponent; @@ -49,8 +47,6 @@ public class MongoDbComponent extends DefaultComponent { public MongoDbComponent(CamelContext context) { super(context); - registerExtension(MongoComponentVerifierExtension::new); - registerExtension(MongoDBMetaExtension::new); } @Override diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/meta/MongoDBMetaExtension.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/meta/MongoDBMetaExtension.java deleted file mode 100644 index a42a06cb7cd..00000000000 --- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/meta/MongoDBMetaExtension.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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 org.apache.camel.component.mongodb.meta; - -import java.io.IOException; -import java.util.Map; -import java.util.Optional; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.mongodb.ConnectionString; -import com.mongodb.client.MongoClient; -import com.mongodb.client.MongoClients; -import com.mongodb.client.model.Filters; -import org.apache.camel.CamelContext; -import org.apache.camel.component.extension.metadata.AbstractMetaDataExtension; -import org.apache.camel.component.extension.metadata.MetaDataBuilder; -import org.apache.camel.component.mongodb.conf.ConnectionParamsConfiguration; -import org.bson.Document; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static org.apache.camel.util.CastUtils.cast; - -public class MongoDBMetaExtension extends AbstractMetaDataExtension { - - private static final Logger LOGGER = LoggerFactory.getLogger(MongoDBMetaExtension.class); - - private ObjectMapper objectMapper = new ObjectMapper(); - - public MongoDBMetaExtension() { - this(null); - } - - public MongoDBMetaExtension(CamelContext context) { - super(context); - } - - @Override - public Optional<MetaData> meta(Map<String, Object> parameters) { - Map<String, String> textParameters = cast(parameters); - LOGGER.debug("Fetching mongodb meta information with params: {}", textParameters); - - ConnectionParamsConfiguration mongoConf = new ConnectionParamsConfiguration(textParameters); - ConnectionString connectionString = new ConnectionString(mongoConf.getMongoClientURI()); - - JsonNode collectionInfoRoot; - try (MongoClient mongoConnection = MongoClients.create(connectionString)) { - Document collectionInfo = mongoConnection.getDatabase(textParameters.get("database")) - .listCollections() - .filter(Filters.eq("name", textParameters.get("collection"))) - .first(); - LOGGER.debug("Collection info: {}", collectionInfo); - if (collectionInfo == null) { - LOGGER.error( - "Cannot read information for collection {}.{}", - textParameters.get("database"), - textParameters.get("collection")); - return Optional.empty(); - } - String collectionInfoJson = collectionInfo.toJson(); - collectionInfoRoot = objectMapper.readTree(collectionInfoJson.replace("bsonType", "type")); - } catch (IOException e) { - LOGGER.error("Error occurred while reading schema information", e); - return Optional.empty(); - } - - JsonNode schemaRoot = collectionInfoRoot.path("options").path("validator").path("$jsonSchema"); - - if (!schemaRoot.isMissingNode()) { - ObjectNode root = (ObjectNode) schemaRoot; - root.put("$schema", "http://json-schema.org/schema#"); - root.put("id", String.format("urn:jsonschema:%s:%s:%s)", - "org:apache:camel:component:mongodb", - textParameters.get("database"), - textParameters.get("collection"))); - - return Optional.of( - MetaDataBuilder.on(getCamelContext()) - .withAttribute(MetaData.CONTENT_TYPE, "application/schema+json") - .withAttribute(MetaData.JAVA_TYPE, JsonNode.class) - .withPayload(root) - .build()); - } else { - LOGGER.warn( - "Cannot retrieve info for : {}.{} collection. Likely the collection has not been provided with a validator", - textParameters.get("database"), - textParameters.get("collection")); - return Optional.empty(); - } - } -} diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/verifier/MongoComponentVerifierExtension.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/verifier/MongoComponentVerifierExtension.java deleted file mode 100644 index 0ecf3e0575b..00000000000 --- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/verifier/MongoComponentVerifierExtension.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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 org.apache.camel.component.mongodb.verifier; - -import java.util.Map; -import java.util.concurrent.TimeUnit; - -import com.mongodb.ConnectionString; -import com.mongodb.MongoClientSettings; -import com.mongodb.MongoSecurityException; -import com.mongodb.MongoTimeoutException; -import com.mongodb.client.MongoClient; -import com.mongodb.client.MongoClients; -import com.mongodb.client.MongoDatabase; -import org.apache.camel.component.extension.verifier.DefaultComponentVerifierExtension; -import org.apache.camel.component.extension.verifier.ResultBuilder; -import org.apache.camel.component.extension.verifier.ResultErrorBuilder; -import org.apache.camel.component.extension.verifier.ResultErrorHelper; -import org.apache.camel.component.mongodb.conf.ConnectionParamsConfiguration; -import org.bson.Document; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import static org.apache.camel.util.CastUtils.cast; - -public class MongoComponentVerifierExtension extends DefaultComponentVerifierExtension { - private static final Logger LOG = LoggerFactory.getLogger(MongoComponentVerifierExtension.class); - - private static final int CONNECTION_TIMEOUT = 2000; - - public MongoComponentVerifierExtension() { - super("mongodb"); - } - - @Override - public Result verifyParameters(Map<String, Object> parameters) { - ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.PARAMETERS) - .error(ResultErrorHelper.requiresOption(parameters, "host")) - .error(ResultErrorHelper.requiresOption(parameters, "user")) - .error(ResultErrorHelper.requiresOption(parameters, "password")); - return builder.build(); - } - - @Override - public Result verifyConnectivity(Map<String, Object> parameters) { - return ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.CONNECTIVITY) - .error(parameters, this::verifyCredentials) - .build(); - } - - private void verifyCredentials(ResultBuilder builder, Map<String, Object> parameters) { - ConnectionParamsConfiguration mongoConf = new ConnectionParamsConfiguration(cast(parameters)); - - MongoClientSettings.Builder optionsBuilder = MongoClientSettings.builder(); - optionsBuilder.applyToSocketSettings( - socketBuilder -> socketBuilder.connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); - optionsBuilder.applyToConnectionPoolSettings( - connectionPoolBuilder -> connectionPoolBuilder.maxWaitTime(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); - optionsBuilder.applyToClusterSettings( - clusterBuilder -> clusterBuilder.serverSelectionTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); - - ConnectionString connectionString = new ConnectionString(mongoConf.getMongoClientURI()); - optionsBuilder.applyConnectionString(connectionString); - - LOG.info("Testing connection against {}", connectionString); - try (MongoClient mongoClient = MongoClients.create(connectionString)) { - // Just ping the server - MongoDatabase database = mongoClient.getDatabase(mongoConf.getAdminDB()); - database.runCommand(Document.parse("{ ping: 1 }")); - LOG.info("Testing connection successful!"); - } catch (MongoSecurityException e) { - ResultErrorBuilder errorBuilder = ResultErrorBuilder.withCodeAndDescription( - VerificationError.StandardCode.AUTHENTICATION, - String.format("Unable to authenticate %s against %s authentication database!", mongoConf.getUser(), - mongoConf.getAdminDB())); - builder.error(errorBuilder.build()); - } catch (MongoTimeoutException e) { - // When there is any connection exception, the driver tries to reconnect until timeout is reached - // wrapping the original security/socket exception into a timeout exception - String description; - VerificationError.StandardCode code = VerificationError.StandardCode.GENERIC; - if (e.getMessage().contains("com.mongodb.MongoSecurityException")) { - code = VerificationError.StandardCode.AUTHENTICATION; - description = String.format("Unable to authenticate %s against %s authentication database!", - mongoConf.getUser(), mongoConf.getAdminDB()); - } else if (e.getMessage().contains("com.mongodb.MongoSocket") && e.getMessage().contains("Exception")) { - description = String.format("Unable to connect to %s!", mongoConf.getHost()); - } else { - description = String.format("Generic exception while connecting to %s!", mongoConf.getHost()); - } - ResultErrorBuilder errorBuilder = ResultErrorBuilder.withCodeAndDescription( - code, - description); - builder.error(errorBuilder.build()); - } - } -} diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/meta/integration/MongoDbMetaExtensionIT.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/meta/integration/MongoDbMetaExtensionIT.java deleted file mode 100644 index 1427deb722b..00000000000 --- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/meta/integration/MongoDbMetaExtensionIT.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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 org.apache.camel.component.mongodb.meta.integration; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -import com.fasterxml.jackson.databind.JsonNode; -import com.mongodb.client.model.CreateCollectionOptions; -import com.mongodb.client.model.Filters; -import com.mongodb.client.model.ValidationOptions; -import org.apache.camel.component.extension.MetaDataExtension; -import org.apache.camel.component.mongodb.MongoDbComponent; -import org.apache.camel.component.mongodb.integration.AbstractMongoDbITSupport; -import org.bson.Document; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; - -public class MongoDbMetaExtensionIT extends AbstractMongoDbITSupport { - // We simulate the presence of an authenticated user - @BeforeEach - public void createAuthorizationUser() { - super.createAuthorizationUser(); - } - - protected MongoDbComponent getComponent() { - return context.getComponent(SCHEME, MongoDbComponent.class); - } - - @Test - public void testValidMetaData() { - // When - final String database = "test"; - final String collection = "validatedCollection"; - MongoDbComponent component = this.getComponent(); - // Given - Document jsonSchema = Document.parse("{ \n" - + " bsonType: \"object\", \n" - + " required: [ \"name\", \"surname\", \"email\" ], \n" - + " properties: { \n" - + " name: { \n" - + " bsonType: \"string\", \n" - + " description: \"required and must be a string\" }, \n" - + " surname: { \n" - + " bsonType: \"string\", \n" - + " description: \"required and must be a string\" }, \n" - + " email: { \n" - + " bsonType: \"string\", \n" - + " pattern: \"^.+@.+$\", \n" - + " description: \"required and must be a valid email address\" }, \n" - + " year_of_birth: { \n" - + " bsonType: \"int\", \n" - + " minimum: 1900, \n" - + " maximum: 2018,\n" - + " description: \"the value must be in the range 1900-2018\" }, \n" - + " gender: { \n" - + " enum: [ \"M\", \"F\" ], \n" - + " description: \"can be only M or F\" } \n" - + " }}"); - ValidationOptions collOptions = new ValidationOptions().validator(Filters.jsonSchema(jsonSchema)); - AbstractMongoDbITSupport.mongo.getDatabase(database).createCollection(collection, - new CreateCollectionOptions().validationOptions(collOptions)); - Map<String, Object> parameters = new HashMap<>(); - parameters.put("database", database); - parameters.put("collection", collection); - parameters.put("host", service.getConnectionAddress()); - parameters.put("user", USER); - parameters.put("password", PASSWORD); - - MetaDataExtension.MetaData result = component.getExtension(MetaDataExtension.class).get().meta(parameters) - .orElseThrow(UnsupportedOperationException::new); - // Then - assertEquals("application/schema+json", result.getAttribute(MetaDataExtension.MetaData.CONTENT_TYPE)); - assertEquals(JsonNode.class, result.getAttribute(MetaDataExtension.MetaData.JAVA_TYPE)); - assertNotNull(result.getPayload(JsonNode.class)); - assertNotNull(result.getPayload(JsonNode.class).get("properties")); - assertNotNull(result.getPayload(JsonNode.class).get("$schema")); - assertEquals("http://json-schema.org/schema#", result.getPayload(JsonNode.class).get("$schema").asText()); - assertNotNull(result.getPayload(JsonNode.class).get("id")); - assertNotNull(result.getPayload(JsonNode.class).get("type")); - } - - @Test - public void testMissingCollection() { - // When - final String database = "test"; - final String collection = "missingCollection"; - MongoDbComponent component = this.getComponent(); - // Given - Map<String, Object> parameters = new HashMap<>(); - parameters.put("database", database); - parameters.put("collection", collection); - parameters.put("host", service.getConnectionAddress()); - parameters.put("user", USER); - parameters.put("password", PASSWORD); - - final Optional<MetaDataExtension.MetaData> meta - = component.getExtension(MetaDataExtension.class).get().meta(parameters); - - // Then - assertThrows(IllegalArgumentException.class, () -> meta.orElseThrow(IllegalArgumentException::new)); - } - - @Test - public void testMissingParameters() { - // When - MongoDbComponent component = this.getComponent(); - // Given - Map<String, Object> parameters = new HashMap<>(); - - final MetaDataExtension metaDataExtension = component.getExtension(MetaDataExtension.class).get(); - - // Then - assertThrows(IllegalArgumentException.class, () -> metaDataExtension.meta(parameters)); - } - - @Test - public void testNotValidatedCollection() { - // When - final String database = "test"; - final String collection = "notValidatedCollection"; - MongoDbComponent component = this.getComponent(); - AbstractMongoDbITSupport.mongo.getDatabase(database).createCollection(collection); - // Given - Map<String, Object> parameters = new HashMap<>(); - parameters.put("database", database); - parameters.put("collection", collection); - parameters.put("host", service.getConnectionAddress()); - parameters.put("user", USER); - parameters.put("password", PASSWORD); - - final Optional<MetaDataExtension.MetaData> meta - = component.getExtension(MetaDataExtension.class).get().meta(parameters); - - // Then - assertThrows(UnsupportedOperationException.class, () -> meta.orElseThrow(UnsupportedOperationException::new)); - } -} diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/verifier/integration/MongoDbVerifierExtensionIT.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/verifier/integration/MongoDbVerifierExtensionIT.java deleted file mode 100644 index 41f5b9cddef..00000000000 --- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/verifier/integration/MongoDbVerifierExtensionIT.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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 org.apache.camel.component.mongodb.verifier.integration; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.camel.Component; -import org.apache.camel.component.extension.ComponentVerifierExtension; -import org.apache.camel.component.mongodb.integration.AbstractMongoDbITSupport; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class MongoDbVerifierExtensionIT extends AbstractMongoDbITSupport { - // We simulate the presence of an authenticated user - @BeforeEach - public void createAuthorizationUser() { - super.createAuthorizationUser(); - } - - protected ComponentVerifierExtension getExtension() { - Component component = context.getComponent(SCHEME); - ComponentVerifierExtension verifier - = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new); - - return verifier; - } - - @Test - public void verifyConnectionOK() { - //When - Map<String, Object> parameters = new HashMap<>(); - parameters.put("host", service.getConnectionAddress()); - parameters.put("user", USER); - parameters.put("password", PASSWORD); - //Given - ComponentVerifierExtension.Result result - = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - //Then - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void verifyConnectionKO() { - //When - Map<String, Object> parameters = new HashMap<>(); - parameters.put("host", "notReachable.host"); - parameters.put("user", USER); - parameters.put("password", PASSWORD); - //Given - ComponentVerifierExtension.Result result - = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - //Then - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to connect")); - } - - @Test - public void verifyConnectionMissingParams() { - //When - Map<String, Object> parameters = new HashMap<>(); - parameters.put("host", service.getConnectionAddress()); - parameters.put("user", USER); - //Given - ComponentVerifierExtension.Result result - = getExtension().verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); - //Then - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertTrue(result.getErrors().get(0).getDescription().startsWith("password should be set")); - } - - @Test - public void verifyConnectionNotAuthenticated() { - //When - Map<String, Object> parameters = new HashMap<>(); - parameters.put("host", service.getConnectionAddress()); - parameters.put("user", USER); - parameters.put("password", "wrongPassword"); - //Given - ComponentVerifierExtension.Result result - = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - //Then - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to authenticate")); - } - - @Test - public void verifyConnectionAdminDBKO() { - //When - Map<String, Object> parameters = new HashMap<>(); - parameters.put("host", service.getConnectionAddress()); - parameters.put("user", USER); - parameters.put("password", PASSWORD); - parameters.put("adminDB", "someAdminDB"); - //Given - ComponentVerifierExtension.Result result - = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - //Then - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to authenticate")); - } - - @Test - public void verifyConnectionPortKO() { - //When - Map<String, Object> parameters = new HashMap<>(); - parameters.put("host", "localhost:12343"); - parameters.put("user", USER); - parameters.put("password", PASSWORD); - //Given - ComponentVerifierExtension.Result result - = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - //Then - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to connect")); - } -}