e6fee58fb15e646417d9b0d2bc27b7f29cbbc80e
[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.authentication.CadiAuthFilter;
34 import org.onap.music.main.MusicUtil;
35 import org.onap.music.main.PropertiesLoader;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
38 import org.springframework.boot.autoconfigure.SpringBootApplication;
39 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
40 import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration;
41 import org.springframework.boot.builder.SpringApplicationBuilder;
42 import org.springframework.boot.web.servlet.FilterRegistrationBean;
43 //import org.springframework.boot.web.support.SpringBootServletInitializer;
44 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
45 import org.springframework.context.ApplicationContext;
46 import org.springframework.context.annotation.Bean;
47 import org.springframework.context.annotation.ComponentScan;
48 import org.springframework.context.annotation.DependsOn;
49 import org.springframework.scheduling.annotation.EnableScheduling;
50 import org.springframework.web.context.request.RequestContextListener;
51
52
53 @SpringBootApplication(scanBasePackages = { "org.onap.music.rest"})
54 @EnableAutoConfiguration(exclude={CassandraDataAutoConfiguration.class})
55 @ComponentScan(value = {"org.onap.music"})
56 @EnableScheduling
57 public class MusicApplication extends SpringBootServletInitializer {
58
59     @Autowired
60     PropertiesLoader propertyLoader;
61     
62     
63     public static void main(String[] args) {
64         System.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG","false");
65         System.setProperty("AFT_DME2_CLIENT_KEYSTORE","/opt/app/music/etc/truststore2018.jks");
66         System.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD","changeit");
67         System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS","TLSv1.1,TLSv1.2");
68         new MusicApplication().configure(new SpringApplicationBuilder(MusicApplication.class)).run(args);
69     }
70
71     @Override
72     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
73         
74         return application.sources(MusicApplication.class);
75     }
76     
77
78     @Bean("loadProperties")
79     public void loadProperties() {
80         propertyLoader.loadProperties();
81     }
82
83     @Autowired
84     private ApplicationContext appContext;
85     
86     @Bean
87     @DependsOn("loadProperties")
88     public PropAccess propAccess() {
89         if(MusicUtil.getIsCadi())
90             return new PropAccess(new String[] { "cadi_prop_files=/opt/app/music/etc/cadi.properties" });
91         else
92             return null;
93     }
94     
95     @Bean(name = "cadiFilter")
96     @DependsOn("loadProperties")
97     public Filter cadiFilter() throws ServletException {
98         propertyLoader.loadProperties();
99         if(MusicUtil.getIsCadi()) {
100             PropAccess propAccess = propAccess();
101             CadiAuthFilter cadiFilter = new CadiAuthFilter(propAccess);
102             return cadiFilter;
103         } else 
104             return (ServletRequest request, ServletResponse response, FilterChain chain) -> {
105                 //do nothing for now.
106             };
107         
108     }
109
110     @Bean
111     @DependsOn("loadProperties")
112     public FilterRegistrationBean<Filter> cadiFilterRegistration() throws ServletException {
113         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
114         frb.setFilter(cadiFilter());
115         // The Following Patterns are used to control what APIs will be secure
116         // TODO: Make this a configurable item. Build this from an array?
117         if(MusicUtil.getIsCadi()) {
118             frb.addUrlPatterns(
119                 "/v2/keyspaces/*",
120                 "/v2/locks/*",
121                 "/v3/locks/*",
122                 "/v2/priorityq/*",
123                 "/v2/admin/*"
124         );
125         } else {
126             frb.addUrlPatterns("/v0/test");
127         }
128         frb.setName("cadiFilter");
129         frb.setOrder(0);
130         return frb;
131     }
132
133     @Bean
134     @ConditionalOnMissingBean(RequestContextListener.class)
135     public RequestContextListener requestContextListener() {
136         return new RequestContextListener();
137     }
138 }