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