9d057b5834986a357e8ea3daea675a827e1a6762
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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 org.apache.camel.component.servlet.CamelHttpTransportServlet;
30 import org.apache.catalina.connector.Connector;
31 import org.onap.clamp.clds.model.properties.Holmes;
32 import org.onap.clamp.clds.model.properties.ModelProperties;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.boot.SpringApplication;
35 import org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration;
36 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
37 import org.springframework.boot.autoconfigure.SpringBootApplication;
38 import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
39 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
40 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
41 import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
42 import org.springframework.boot.builder.SpringApplicationBuilder;
43 import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
44 import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
45 import org.springframework.boot.web.servlet.ServletRegistrationBean;
46 import org.springframework.boot.web.support.SpringBootServletInitializer;
47 import org.springframework.context.annotation.Bean;
48 import org.springframework.context.annotation.ComponentScan;
49 import org.springframework.scheduling.annotation.EnableAsync;
50
51 @SpringBootApplication
52 @ComponentScan(basePackages = {
53         "org.onap.clamp.clds"
54 })
55 @EnableAutoConfiguration(exclude = {
56         DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class,
57         SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class
58 })
59 @EnableAsync
60 public class Application extends SpringBootServletInitializer {
61
62     protected static final EELFLogger EELF_LOGGER = EELFManager.getInstance().getLogger(Application.class);
63     // This settings is an additional one to Spring config,
64     // only if we want to have an additional port automatically redirected to
65     // HTTPS
66     @Value("${server.http-to-https-redirection.port:none}")
67     private String httpRedirectedPort;
68     /**
69      * This 8080 is the default port used by spring if this parameter is not
70      * specified in application.properties.
71      */
72     @Value("${server.port:8080}")
73     private String springServerPort;
74     @Value("${server.ssl.key-store:none}")
75     private String sslKeystoreFile;
76
77     @Override
78     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
79         return application.sources(Application.class);
80     }
81
82     public static void main(String[] args) {
83         // This is to initialize some Onap Clamp components
84         initializeComponents();
85         // Start the Spring application
86         SpringApplication.run(Application.class, args); // NOSONAR
87     }
88
89     private static void initializeComponents() {
90         ModelProperties.registerModelElement(Holmes.class, Holmes.getType());
91     }
92
93     /**
94      * This method is used to declare the camel servlet.
95      * 
96      * @return A servlet bean
97      */
98     @Bean
99     public ServletRegistrationBean camelServletRegistrationBean() {
100         ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(),
101                 "/restservices/clds/v2");
102         registration.setName("CamelServlet");
103         return registration;
104     }
105
106     /**
107      * This method is used by Spring to create the servlet container factory.
108      * 
109      * @return The TomcatEmbeddedServletContainerFactory just created
110      */
111     @Bean
112     public EmbeddedServletContainerFactory getEmbeddedServletContainerFactory() {
113         TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
114         if (!"none".equals(httpRedirectedPort) && !"none".equals(sslKeystoreFile)) {
115             // Automatically redirect to HTTPS
116             tomcat = new TomcatEmbeddedServletContainerFactoryRedirection();
117             Connector newConnector = createRedirectConnector(Integer.parseInt(springServerPort));
118             if (newConnector != null) {
119                 tomcat.addAdditionalTomcatConnectors(newConnector);
120             }
121         }
122         return tomcat;
123     }
124
125     private Connector createRedirectConnector(int redirectSecuredPort) {
126         if (redirectSecuredPort <= 0) {
127             EELF_LOGGER.warn(
128                     "HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1 (Connector disabled)");
129             return null;
130         }
131         Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
132         connector.setScheme("http");
133         connector.setSecure(false);
134         connector.setPort(Integer.parseInt(httpRedirectedPort));
135         connector.setRedirectPort(redirectSecuredPort);
136         return connector;
137     }
138 }