90bcbbae598d39cdd2a07c762740a0ae468ed44e
[music.git] / src / main / java / org / onap / music / MusicApplication.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music;
24
25
26 import javax.servlet.Filter;
27 import javax.servlet.FilterChain;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31
32 import org.onap.aaf.cadi.PropAccess;
33 import org.onap.music.main.MusicUtil;
34 import org.onap.music.main.PropertiesLoader;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
37 import org.springframework.boot.autoconfigure.SpringBootApplication;
38 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
39 import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration;
40 import org.springframework.boot.builder.SpringApplicationBuilder;
41 import org.springframework.boot.web.servlet.FilterRegistrationBean;
42 //import org.springframework.boot.web.support.SpringBootServletInitializer;
43 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.context.annotation.ComponentScan;
47 import org.springframework.context.annotation.DependsOn;
48 import org.springframework.scheduling.annotation.EnableScheduling;
49 import org.springframework.web.context.request.RequestContextListener;
50
51
52 @SpringBootApplication(scanBasePackages = { "org.onap.music.rest"})
53 @EnableAutoConfiguration(exclude={CassandraDataAutoConfiguration.class})
54 @ComponentScan(value = {"org.onap.music"})
55 @EnableScheduling
56 public class MusicApplication extends SpringBootServletInitializer {
57
58     @Autowired
59     PropertiesLoader propertyLoader;
60     
61     
62     public static void main(String[] args) {
63         System.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG","false");
64         System.setProperty("AFT_DME2_CLIENT_KEYSTORE","/opt/app/music/etc/truststore2018.jks");
65         System.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD","changeit");
66         System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS","TLSv1.1,TLSv1.2");
67         new MusicApplication().configure(new SpringApplicationBuilder(MusicApplication.class)).run(args);
68     }
69
70     @Override
71     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
72         
73         return application.sources(MusicApplication.class);
74     }
75     
76
77     @Bean("loadProperties")
78     public void loadProperties() {
79         propertyLoader.loadProperties();
80     }
81
82     @Autowired
83     private ApplicationContext appContext;
84     
85     @Bean
86     @DependsOn("loadProperties")
87     public PropAccess propAccess() {
88         if(MusicUtil.getIsCadi())
89             return new PropAccess(new String[] { "cadi_prop_files=/opt/app/music/etc/cadi.properties" });
90         else
91             return null;
92     }
93     
94     @Bean(name = "cadiFilter")
95     @DependsOn("loadProperties")
96     public Filter cadiFilter() throws ServletException {
97         propertyLoader.loadProperties();
98         if(MusicUtil.getIsCadi()) {
99             PropAccess propAccess = propAccess();
100             CadiAuthFilter cadiFilter = new CadiAuthFilter(propAccess);
101             return cadiFilter;
102         } else 
103             return (ServletRequest request, ServletResponse response, FilterChain chain) -> {
104                 //do nothing for now.
105             };
106         
107     }
108
109     @Bean
110     @DependsOn("loadProperties")
111     public FilterRegistrationBean<Filter> cadiFilterRegistration() throws ServletException {
112         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
113         frb.setFilter(cadiFilter());
114         // The Following Patterns are used to control what APIs will be secure
115         // TODO: Make this a configurable item. Build this from an array?
116         if(MusicUtil.getIsCadi()) {
117             frb.addUrlPatterns(
118                 "/v2/keyspaces/*",
119                 "/v2/locks/*",
120                 "/v3/locks/*",
121                 "/v2/priorityq/*",
122                 "/v2/admin/*"
123         );
124         } else {
125             frb.addUrlPatterns("/v0/test");
126         }
127         frb.setName("cadiFilter");
128         frb.setOrder(0);
129         return frb;
130     }
131
132     @Bean
133     @ConditionalOnMissingBean(RequestContextListener.class)
134     public RequestContextListener requestContextListener() {
135         return new RequestContextListener();
136     }
137 }