aa4de01e10990ea7fb02b5185578108f86652710
[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 import javax.servlet.Filter;
26 import javax.servlet.FilterChain;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30
31 import org.onap.aaf.cadi.PropAccess;
32 import org.onap.music.authentication.CadiAuthFilter;
33 import org.onap.music.authentication.MusicAuthorizationFilter;
34 import org.onap.music.eelf.logging.EELFLoggerDelegate;
35 import org.onap.music.eelf.logging.MusicLoggingServletFilter;
36 import org.onap.music.main.MusicUtil;
37 import org.onap.music.main.PropertiesLoader;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
40 import org.springframework.boot.autoconfigure.SpringBootApplication;
41 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
42 import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration;
43 import org.springframework.boot.builder.SpringApplicationBuilder;
44 import org.springframework.boot.web.servlet.FilterRegistrationBean;
45 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
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 @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     private final String KEYSPACE_PATTERN = "/v2/keyspaces/*";
59     private final String LOCKS_PATTERN = "/v2/locks/*";
60     private final String Q_PATTERN = "/v2/priorityq/*";
61
62     @Autowired
63     private PropertiesLoader propertyLoader;
64     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicApplication.class);
65
66
67     public static void main(String[] args) {
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     @Bean("loadProperties")
78     public void loadProperties() {
79         propertyLoader.loadProperties();
80     }
81
82
83     @Bean
84     @DependsOn("loadProperties")
85     public PropAccess propAccess() {
86         if (MusicUtil.getIsCadi()) {
87             return new PropAccess(new String[] { 
88                 "cadi_prop_files=/opt/app/music/etc/cadi.properties" });
89         } else {
90             return null;
91         }
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             return new CadiAuthFilter(propAccess);
101         } else {
102             return (ServletRequest request, ServletResponse response, FilterChain chain) -> {
103                 // do nothing for now.
104             };
105         }
106     }
107     
108     /**
109      * Added for capturing custom header values from client.
110      * 
111      * order is set to 1 for this filter
112      * 
113      * sp931a
114      * 
115      * @return
116      * @throws ServletException
117      */
118     @Bean(name="logFilter")
119     @DependsOn("loadProperties")
120     public FilterRegistrationBean<Filter> loggingFilterRegistration() throws ServletException {
121         logger.info("loggingFilterRegistration called for log filter..");
122         propertyLoader.loadProperties();
123         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
124         frb.setFilter(new MusicLoggingServletFilter());
125         frb.addUrlPatterns(
126             KEYSPACE_PATTERN,
127             LOCKS_PATTERN,
128             Q_PATTERN
129         );
130         frb.setName("logFilter");
131         frb.setOrder(1);
132         return frb;
133     }
134
135     @Bean
136     @DependsOn("loadProperties")
137     public FilterRegistrationBean<Filter> cadiFilterRegistration() throws ServletException {
138         logger.info("cadiFilterRegistration called for cadi filter..");
139         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
140         frb.setFilter(cadiFilter());
141         if (MusicUtil.getIsCadi()) {
142             frb.addUrlPatterns(
143                 KEYSPACE_PATTERN,
144                 LOCKS_PATTERN,
145                 Q_PATTERN
146             );
147         } else {
148             frb.addUrlPatterns("/v0/test");
149         }
150         frb.setName("cadiFilter");
151         frb.setOrder(2);
152         return frb;
153     }
154
155     
156     /**
157      * Added for Authorization using CADI
158      * 
159      * sp931a
160      * 
161      * @return
162      * @throws ServletException
163      */
164     @Bean
165     @DependsOn("loadProperties")
166     public FilterRegistrationBean<Filter> cadiFilterRegistrationForAuth() throws ServletException {
167         logger.info("cadiFilterRegistrationForAuth called for cadi auth filter..");
168         FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
169         frb.setFilter(cadiMusicAuthFilter());
170
171         if (MusicUtil.getIsCadi()) {
172             frb.addUrlPatterns(
173                 KEYSPACE_PATTERN,
174                 LOCKS_PATTERN,
175                 Q_PATTERN
176                 );
177         } else {
178             frb.addUrlPatterns("/v0/test");
179         }
180         frb.setName("cadiMusicAuthFilter");
181         frb.setOrder(3);
182         return frb;
183     }
184
185     @Bean(name = "cadiMusicAuthFilter")
186     @DependsOn("loadProperties")
187     public Filter cadiMusicAuthFilter() throws ServletException {
188         propertyLoader.loadProperties();
189         if (MusicUtil.getIsCadi()) {
190             return new MusicAuthorizationFilter();
191         } else {
192             return (ServletRequest request, ServletResponse response, FilterChain chain) -> {
193                 // do nothing for now.
194             };
195         }
196     }
197
198     @Bean
199     @ConditionalOnMissingBean(RequestContextListener.class)
200     public RequestContextListener requestContextListener() {
201         return new RequestContextListener();
202     }
203 }