davsclaus commented on code in PR #14849:
URL: https://github.com/apache/camel/pull/14849#discussion_r1703673233


##########
components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/DefaultMainHttpServerFactory.java:
##########
@@ -59,6 +66,68 @@ public Service 
newHttpServer(HttpServerConfigurationProperties configuration) {
         server.setUploadEnabled(configuration.isUploadEnabled());
         server.setUploadSourceDir(configuration.getUploadSourceDir());
 
+        if (configuration.authentication().isEnabled()) {
+            configureAuthentication(server, configuration);
+        }
+
         return server;
     }
+
+    private void configureAuthentication(MainHttpServer server, 
HttpServerConfigurationProperties configuration) {
+        AuthenticationConfig authenticationConfig = 
server.getConfiguration().getAuthenticationConfig();
+
+        HttpServerAuthenticationConfigurationProperties 
authenticationProperties = configuration.getAuthentication();
+        Optional<MainAuthenticationConfigurer> authenticationConfigurer
+                = 
findAuthenticationConfigurerByConfigurationProperties(authenticationProperties);
+
+        authenticationConfigurer.ifPresentOrElse(
+                (configurer -> 
configurer.configureAuthentication(authenticationConfig, 
authenticationProperties)),
+                (() -> {
+                    throw new RuntimeException(
+                            "Authentication for camel-platform-http-main is 
enabled but no complete authentication configuration is found.");
+                }));
+    }
+
+    private Optional<MainAuthenticationConfigurer> 
findAuthenticationConfigurerByConfigurationProperties(
+            HttpServerAuthenticationConfigurationProperties 
authenticationConfigurationProperties) {
+
+        MainAuthenticationConfigurer result = null;
+        for (String authenticationTypeName : 
HttpServerAuthenticationConfigurationProperties.SUPPORTED_AUTHENTICATION_TYPES) 
{
+            if 
(authenticationTypeIsEnabled(authenticationConfigurationProperties, 
authenticationTypeName)) {
+                try {
+                    if (result != null) {
+                        throw new RuntimeException(
+                                "Cannot configure authentication for 
MainHttpServer as more than one authentication configuration is present");
+                    }
+                    String configurerQualifiedName = 
MainAuthenticationConfigurer.class.getPackageName() + "."
+                                                     + authenticationTypeName 
+ "AuthenticationConfigurer";
+                    result = (MainAuthenticationConfigurer) Class

Review Comment:
   We should use Camel `Injector` or `ClassResolver` to load and create objects 
from a class, and not directly use `Class.forName`



##########
components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/DefaultMainHttpServerFactory.java:
##########
@@ -59,6 +66,68 @@ public Service 
newHttpServer(HttpServerConfigurationProperties configuration) {
         server.setUploadEnabled(configuration.isUploadEnabled());
         server.setUploadSourceDir(configuration.getUploadSourceDir());
 
+        if (configuration.authentication().isEnabled()) {
+            configureAuthentication(server, configuration);
+        }
+
         return server;
     }
+
+    private void configureAuthentication(MainHttpServer server, 
HttpServerConfigurationProperties configuration) {
+        AuthenticationConfig authenticationConfig = 
server.getConfiguration().getAuthenticationConfig();
+
+        HttpServerAuthenticationConfigurationProperties 
authenticationProperties = configuration.getAuthentication();
+        Optional<MainAuthenticationConfigurer> authenticationConfigurer
+                = 
findAuthenticationConfigurerByConfigurationProperties(authenticationProperties);
+
+        authenticationConfigurer.ifPresentOrElse(
+                (configurer -> 
configurer.configureAuthentication(authenticationConfig, 
authenticationProperties)),
+                (() -> {
+                    throw new RuntimeException(
+                            "Authentication for camel-platform-http-main is 
enabled but no complete authentication configuration is found.");
+                }));
+    }
+
+    private Optional<MainAuthenticationConfigurer> 
findAuthenticationConfigurerByConfigurationProperties(
+            HttpServerAuthenticationConfigurationProperties 
authenticationConfigurationProperties) {
+
+        MainAuthenticationConfigurer result = null;
+        for (String authenticationTypeName : 
HttpServerAuthenticationConfigurationProperties.SUPPORTED_AUTHENTICATION_TYPES) 
{
+            if 
(authenticationTypeIsEnabled(authenticationConfigurationProperties, 
authenticationTypeName)) {
+                try {
+                    if (result != null) {
+                        throw new RuntimeException(
+                                "Cannot configure authentication for 
MainHttpServer as more than one authentication configuration is present");
+                    }
+                    String configurerQualifiedName = 
MainAuthenticationConfigurer.class.getPackageName() + "."
+                                                     + authenticationTypeName 
+ "AuthenticationConfigurer";
+                    result = (MainAuthenticationConfigurer) Class
+                            .forName(configurerQualifiedName)
+                            .getDeclaredConstructor()
+                            .newInstance();
+                } catch (Exception e) {
+                    throw new RuntimeException(
+                            "Could not create MainAuthenticationConfigurer for 
authentication type " + authenticationTypeName,
+                            e);
+                }
+            }
+        }
+        return Optional.ofNullable(result);
+    }
+
+    private boolean authenticationTypeIsEnabled(

Review Comment:
   Camel has some ReflectionHelper code or similar that can help with this kind 
of code.



##########
core/camel-main/src/main/java/org/apache/camel/main/ConfigurationPropertiesWithMandatoryFields.java:
##########
@@ -0,0 +1,23 @@
+/*
+ * 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.main;
+
+public interface ConfigurationPropertiesWithMandatoryFields {

Review Comment:
   Add javadoc to this class



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to