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