Refactor the java packages
[clamp.git] / src / main / java / org / onap / clamp / clds / Application.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 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.ajsc.common.utility.SystemPropertiesLoader;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 import java.util.ArrayList;
31 import java.util.Collection;
32
33 import javax.ws.rs.client.Client;
34 import javax.ws.rs.client.ClientBuilder;
35
36 import org.apache.camel.component.servlet.CamelHttpTransportServlet;
37 import org.apache.catalina.connector.Connector;
38 import org.camunda.bpm.spring.boot.starter.webapp.CamundaBpmWebappAutoConfiguration;
39 import org.onap.clamp.clds.model.prop.Holmes;
40 import org.onap.clamp.clds.model.prop.ModelProperties;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.beans.factory.annotation.Value;
43 import org.springframework.boot.SpringApplication;
44 import org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration;
45 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
46 import org.springframework.boot.autoconfigure.SpringBootApplication;
47 import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
48 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
49 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
50 import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
51 import org.springframework.boot.builder.SpringApplicationBuilder;
52 import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
53 import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
54 import org.springframework.boot.web.servlet.ServletRegistrationBean;
55 import org.springframework.boot.web.support.SpringBootServletInitializer;
56 import org.springframework.context.ApplicationContext;
57 import org.springframework.context.annotation.Bean;
58 import org.springframework.context.annotation.ComponentScan;
59 import org.springframework.scheduling.annotation.EnableAsync;
60
61 @SpringBootApplication
62 @ComponentScan(basePackages = { "org.onap.clamp.clds", "com.att.ajsc" })
63 @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, CamundaBpmWebappAutoConfiguration.class,
64         HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, SecurityAutoConfiguration.class,
65         ManagementWebSecurityAutoConfiguration.class })
66 @EnableAsync
67 public class Application extends SpringBootServletInitializer {
68     protected static final EELFLogger EELF_LOGGER        = EELFManager.getInstance().getLogger(Application.class);
69     @Autowired
70     private ApplicationContext        appContext;
71     private static final String       CAMEL_SERVLET_NAME = "CamelServlet";
72     private static final String       CAMEL_URL_MAPPING  = "/restservices/clds/v1/*";
73     // This settings is an additional one to Spring config,
74     // only if we want to have an additional port automatically redirected to
75     // HTTPS
76     @Value("${server.http-to-https-redirection.port:none}")
77     private String                    httpRedirectedPort;
78     /**
79      * This 8080 is the default port used by spring if this parameter is not
80      * specified in application.properties.
81      */
82     @Value("${server.port:8080}")
83     private String                    springServerPort;
84     @Value("${server.ssl.key-store:none}")
85     private String                    sslKeystoreFile;
86
87     @Override
88     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
89         return application.sources(Application.class);
90     }
91
92     public static void main(String[] args) {
93         // This is to load the system.properties file parameters
94         SystemPropertiesLoader.addSystemProperties();
95         // This is to initialize some Onap Clamp components
96         initializeComponents();
97         // Start the Spring application
98         SpringApplication.run(Application.class, args); // NOSONAR
99     }
100
101     private static void initializeComponents() {
102         ModelProperties.registerModelElement(Holmes.class, Holmes.getType());
103     }
104
105     @Bean
106     public ServletRegistrationBean servletRegistrationBean() {
107         ServletRegistrationBean registration = new ServletRegistrationBean();
108         registration.setName(CAMEL_SERVLET_NAME);
109         registration.setServlet(new CamelHttpTransportServlet());
110         Collection<String> urlMappings = new ArrayList<>();
111         urlMappings.add(CAMEL_URL_MAPPING);
112         registration.setUrlMappings(urlMappings);
113         return registration;
114     }
115
116     @Bean
117     public Client restClient() {
118         return ClientBuilder.newClient();
119     }
120
121     /**
122      * This method is used by Spring to create the servlet container factory.
123      * 
124      * @return The TomcatEmbeddedServletContainerFactory just created
125      */
126     @Bean
127     public EmbeddedServletContainerFactory getEmbeddedServletContainerFactory() {
128         TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
129         if (!"none".equals(httpRedirectedPort) && !"none".equals(sslKeystoreFile)) {
130             // Automatically redirect to HTTPS
131             tomcat = new TomcatEmbeddedServletContainerFactoryRedirection();
132             Connector newConnector = createRedirectConnector(Integer.parseInt(springServerPort));
133             if (newConnector != null) {
134                 tomcat.addAdditionalTomcatConnectors(newConnector);
135             }
136         }
137         return tomcat;
138     }
139
140     private Connector createRedirectConnector(int redirectSecuredPort) {
141         if (redirectSecuredPort <= 0) {
142             EELF_LOGGER.warn(
143                     "HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1 (Connector disabled)");
144             return null;
145         }
146         Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
147         connector.setScheme("http");
148         connector.setSecure(false);
149         connector.setPort(Integer.parseInt(httpRedirectedPort));
150         connector.setRedirectPort(redirectSecuredPort);
151         return connector;
152     }
153 }