Add new tests for DAO
[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.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.boot.SpringApplication;
42 import org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration;
43 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
44 import org.springframework.boot.autoconfigure.SpringBootApplication;
45 import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
46 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
47 import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
48 import org.springframework.boot.builder.SpringApplicationBuilder;
49 import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
50 import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
51 import org.springframework.boot.web.servlet.ServletRegistrationBean;
52 import org.springframework.boot.web.support.SpringBootServletInitializer;
53 import org.springframework.context.ApplicationContext;
54 import org.springframework.context.annotation.Bean;
55 import org.springframework.context.annotation.ComponentScan;
56 import org.springframework.scheduling.annotation.EnableAsync;
57
58 @SpringBootApplication
59 @ComponentScan(basePackages = { "org.onap.clamp.clds", "com.att.ajsc" })
60 @EnableAutoConfiguration(exclude = { CamundaBpmWebappAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
61         JpaRepositoriesAutoConfiguration.class, SecurityAutoConfiguration.class,
62         ManagementWebSecurityAutoConfiguration.class })
63 @EnableAsync
64 public class Application extends SpringBootServletInitializer {
65
66     protected static final EELFLogger eelfLogger         = EELFManager.getInstance().getLogger(Application.class);
67
68     @Autowired
69     protected ApplicationContext      appContext;
70
71     private static final String       CAMEL_SERVLET_NAME = "CamelServlet";
72     private static final String       CAMEL_URL_MAPPING  = "/restservices/clds/v1/*";
73
74     // This settings is an additional one to Spring config,
75     // only if we want to have an additional port automatically redirected to
76     // HTTPS
77     @Value("${server.http-to-https-redirection.port:none}")
78     private String                    httpRedirectedPort;
79
80     /**
81      * This 8080 is the default port used by spring if this parameter is not
82      * specified in application.properties.
83      */
84     @Value("${server.port:8080}")
85     private String                    springServerPort;
86
87     @Value("${server.ssl.key-store:none}")
88     private String                    sslKeystoreFile;
89
90     @Override
91     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
92         return application.sources(Application.class);
93     }
94
95     public static void main(String[] args) throws Exception {
96         SystemPropertiesLoader.addSystemProperties();
97         SpringApplication.run(Application.class, args); // NOSONAR
98     }
99
100     @Bean
101     public ServletRegistrationBean servletRegistrationBean() {
102         ServletRegistrationBean registration = new ServletRegistrationBean();
103         registration.setName(CAMEL_SERVLET_NAME);
104         registration.setServlet(new CamelHttpTransportServlet());
105         Collection<String> urlMappings = new ArrayList<>();
106         urlMappings.add(CAMEL_URL_MAPPING);
107         registration.setUrlMappings(urlMappings);
108         return registration;
109     }
110
111     @Bean
112     public Client restClient() {
113         return ClientBuilder.newClient();
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 EmbeddedServletContainerFactory getEmbeddedServletContainerFactory() {
123         TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
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         }
133         return tomcat;
134
135     }
136
137     private Connector createRedirectConnector(int redirectSecuredPort) {
138         if (redirectSecuredPort <= 0) {
139             eelfLogger.warn(
140                     "HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1 (Connector disabled)");
141             return null;
142         }
143         Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
144         connector.setScheme("http");
145         connector.setSecure(false);
146         connector.setPort(Integer.parseInt(httpRedirectedPort));
147         connector.setRedirectPort(redirectSecuredPort);
148         return connector;
149     }
150
151 }