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