2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * ===================================================================
26 package org.onap.clamp.clds;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
31 import java.io.IOException;
32 import java.security.KeyStore;
33 import java.security.KeyStoreException;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.cert.CertificateException;
36 import java.security.cert.X509Certificate;
37 import java.util.Enumeration;
39 import org.apache.catalina.connector.Connector;
40 import org.onap.clamp.clds.model.properties.Holmes;
41 import org.onap.clamp.clds.model.properties.ModelProperties;
42 import org.onap.clamp.clds.util.ClampVersioning;
43 import org.onap.clamp.clds.util.ResourceFileUtil;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.beans.factory.annotation.Value;
46 import org.springframework.boot.SpringApplication;
47 import org.springframework.boot.autoconfigure.SpringBootApplication;
48 import org.springframework.boot.autoconfigure.domain.EntityScan;
49 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
50 import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
51 import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
52 import org.springframework.boot.builder.SpringApplicationBuilder;
53 import org.springframework.boot.context.properties.EnableConfigurationProperties;
54 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
55 import org.springframework.boot.web.servlet.ServletRegistrationBean;
56 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
57 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
58 import org.springframework.context.annotation.Bean;
59 import org.springframework.context.annotation.ComponentScan;
60 import org.springframework.core.env.Environment;
61 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
62 import org.springframework.scheduling.annotation.EnableAsync;
63 import org.springframework.scheduling.annotation.EnableScheduling;
64 import org.springframework.transaction.annotation.EnableTransactionManagement;
66 @ComponentScan(basePackages = { "org.onap.clamp" })
67 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class,
68 UserDetailsServiceAutoConfiguration.class })
69 @EnableJpaRepositories(basePackages = { "org.onap.clamp" })
70 @EntityScan(basePackages = { "org.onap.clamp" })
71 @EnableTransactionManagement
72 @EnableConfigurationProperties
75 public class Application extends SpringBootServletInitializer {
77 protected static final EELFLogger eelfLogger = EELFManager.getInstance().getLogger(Application.class);
78 // This settings is an additional one to Spring config,
79 // only if we want to have an additional port automatically redirected to
81 @Value("${server.http-to-https-redirection.port:none}")
82 private String httpRedirectedPort;
84 * This 8080 is the default port used by spring if this parameter is not
85 * specified in application.properties.
87 @Value("${server.port:8080}")
88 private String springServerPort;
89 @Value("${server.ssl.key-store:none}")
90 private String sslKeystoreFile;
93 private Environment env;
96 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
97 return application.sources(Application.class);
100 public static void main(String[] args) {
101 // This is to initialize some Onap Clamp components
102 initializeComponents();
103 // Start the Spring application
104 SpringApplication.run(Application.class, args);
107 private static void initializeComponents() {
108 ModelProperties.registerModelElement(Holmes.class, Holmes.getType());
112 * This method is used to declare the camel servlet.
114 * @return A servlet bean
115 * @throws IOException
119 public ServletRegistrationBean camelServletRegistrationBean() throws IOException {
121 ResourceFileUtil.getResourceAsString("boot-message.txt") + "(v" + ClampVersioning.getCldsVersionFromProps()
122 + ")" + System.getProperty("line.separator") + getSslExpirationDate());
123 ServletRegistrationBean registration = new ServletRegistrationBean(new ClampServlet(), "/restservices/clds/*");
124 registration.setName("CamelServlet");
129 * This method is used by Spring to create the servlet container factory.
131 * @return The TomcatEmbeddedServletContainerFactory just created
134 public ServletWebServerFactory getEmbeddedServletContainerFactory() {
135 TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
136 if (!"none".equals(httpRedirectedPort) && !"none".equals(sslKeystoreFile)) {
137 // Automatically redirect to HTTPS
138 tomcat = new TomcatEmbeddedServletContainerFactoryRedirection();
139 Connector newConnector = createRedirectConnector(Integer.parseInt(springServerPort));
140 if (newConnector != null) {
141 tomcat.addAdditionalTomcatConnectors(newConnector);
147 private Connector createRedirectConnector(int redirectSecuredPort) {
148 if (redirectSecuredPort <= 0) {
149 eelfLogger.warn("HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1"
150 + " (Connector disabled)");
153 Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
154 connector.setScheme("http");
155 connector.setSecure(false);
156 connector.setPort(Integer.parseInt(httpRedirectedPort));
157 connector.setRedirectPort(redirectSecuredPort);
161 private String getSslExpirationDate() throws IOException {
162 StringBuilder result = new StringBuilder(" :: SSL Certificates :: ");
164 if (env.getProperty("server.ssl.key-store") != null) {
166 KeyStore keystore = KeyStore.getInstance(env.getProperty("server.ssl.key-store-type"));
169 .getResourceAsStream(env.getProperty("server.ssl.key-store").replaceAll("classpath:", "")),
170 env.getProperty("server.ssl.key-store-password").toCharArray());
171 Enumeration<String> aliases = keystore.aliases();
172 while (aliases.hasMoreElements()) {
173 String alias = aliases.nextElement();
174 if ("X.509".equals(keystore.getCertificate(alias).getType())) {
175 result.append("* " + alias + " expires "
176 + ((X509Certificate) keystore.getCertificate(alias)).getNotAfter()
177 + System.getProperty("line.separator"));
181 result.append("* NONE HAS been configured");
183 } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException e) {
184 eelfLogger.warn("SSL certificate access error ", e);
187 return result.toString();