Fixed generic Sonar issue in the clamp project
[clamp.git] / src / main / java / org / onap / clamp / clds / Application.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END============================================
22  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.clds;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30
31 import java.io.IOException;
32
33 import org.apache.catalina.connector.Connector;
34 import org.onap.clamp.clds.model.properties.Holmes;
35 import org.onap.clamp.clds.model.properties.ModelProperties;
36 import org.onap.clamp.clds.util.ClampVersioning;
37 import org.onap.clamp.clds.util.ResourceFileUtil;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.boot.SpringApplication;
40 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
41 import org.springframework.boot.autoconfigure.SpringBootApplication;
42 import org.springframework.boot.autoconfigure.domain.EntityScan;
43 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
44 import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
45 import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
46 import org.springframework.boot.builder.SpringApplicationBuilder;
47 import org.springframework.boot.context.properties.EnableConfigurationProperties;
48 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
49 import org.springframework.boot.web.servlet.ServletRegistrationBean;
50 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
51 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
52 import org.springframework.context.annotation.Bean;
53 import org.springframework.context.annotation.ComponentScan;
54 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
55 import org.springframework.scheduling.annotation.EnableAsync;
56 import org.springframework.scheduling.annotation.EnableScheduling;
57 import org.springframework.transaction.annotation.EnableTransactionManagement;
58
59 @ComponentScan(basePackages = { "org.onap.clamp" })
60 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class,
61     UserDetailsServiceAutoConfiguration.class })
62 @EnableJpaRepositories(basePackages = { "org.onap.clamp" })
63 @EntityScan(basePackages = { "org.onap.clamp" })
64 @EnableTransactionManagement
65 @EnableConfigurationProperties
66 @EnableAsync
67 @EnableScheduling
68 public class Application extends SpringBootServletInitializer {
69
70     protected static final EELFLogger eelfLogger = EELFManager.getInstance().getLogger(Application.class);
71     // This settings is an additional one to Spring config,
72     // only if we want to have an additional port automatically redirected to
73     // HTTPS
74     @Value("${server.http-to-https-redirection.port:none}")
75     private String httpRedirectedPort;
76     /**
77      * This 8080 is the default port used by spring if this parameter is not
78      * specified in application.properties.
79      */
80     @Value("${server.port:8080}")
81     private String springServerPort;
82     @Value("${server.ssl.key-store:none}")
83     private String sslKeystoreFile;
84
85     @Override
86     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
87         return application.sources(Application.class);
88     }
89
90     public static void main(String[] args) {
91         // This is to initialize some Onap Clamp components
92         initializeComponents();
93         // Start the Spring application
94         SpringApplication.run(Application.class, args);
95     }
96
97     private static void initializeComponents() {
98         ModelProperties.registerModelElement(Holmes.class, Holmes.getType());
99     }
100
101     /**
102      * This method is used to declare the camel servlet.
103      *
104      * @return A servlet bean
105      * @throws IOException IO Exception
106      */
107     @Bean
108     public ServletRegistrationBean camelServletRegistrationBean() throws IOException {
109         eelfLogger.info(ResourceFileUtil.getResourceAsString("boot-message.txt") + "(v"
110             + ClampVersioning.getCldsVersionFromProps() + ")" + System.getProperty("line.separator"));
111         ServletRegistrationBean registration = new ServletRegistrationBean(new ClampServlet(),
112             "/restservices/clds/*");
113         registration.setName("CamelServlet");
114         return registration;
115     }
116
117     /**
118      * This method is used by Spring to create the servlet container factory.
119      *
120      * @return The TomcatEmbeddedServletContainerFactory just created
121      */
122     @Bean
123     public ServletWebServerFactory getEmbeddedServletContainerFactory() {
124         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
125         if (!"none".equals(httpRedirectedPort) && !"none".equals(sslKeystoreFile)) {
126             // Automatically redirect to HTTPS
127             tomcat = new TomcatEmbeddedServletContainerFactoryRedirection();
128             Connector newConnector = createRedirectConnector(Integer.parseInt(springServerPort));
129             if (newConnector != null) {
130                 tomcat.addAdditionalTomcatConnectors(newConnector);
131             }
132         }
133         return tomcat;
134     }
135
136     private Connector createRedirectConnector(int redirectSecuredPort) {
137         if (redirectSecuredPort <= 0) {
138             eelfLogger.warn(
139                 "HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1 (Connector disabled)");
140             return null;
141         }
142         Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
143         connector.setScheme("http");
144         connector.setSecure(false);
145         connector.setPort(Integer.parseInt(httpRedirectedPort));
146         connector.setRedirectPort(redirectSecuredPort);
147         return connector;
148     }
149 }