35e88f5fa6d7013b882abbde9478d7b17898ac6a
[aai/aai-common.git] / aai-aaf-auth / src / main / java / org / onap / aai / aaf / filters / CadiProps.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.aaf.filters;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.beans.factory.annotation.Value;
26 import org.springframework.context.annotation.Profile;
27 import org.springframework.stereotype.Component;
28
29 import javax.annotation.PostConstruct;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.Properties;
35
36 // This component will be created if and only if any of the following profiles are active
37 @Component
38 @Profile({
39     AafProfiles.AAF_CERT_AUTHENTICATION,
40     AafProfiles.AAF_AUTHENTICATION,
41     AafProfiles.TWO_WAY_SSL
42 })
43 public class CadiProps {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(CadiProps.class);
46
47     private String cadiFileName;
48
49     private Properties cadiProperties;
50
51     @Autowired
52     public CadiProps(@Value("${aaf.cadi.file:./resources/cadi.properties}") String filename){
53         cadiFileName   = filename;
54         cadiProperties = new Properties();
55     }
56
57     @PostConstruct
58     public void init() throws IOException {
59
60         File cadiFile  = new File(cadiFileName);
61
62         if(!cadiFile.exists()){
63             LOGGER.warn("Unable to find the cadi file in the given path {} so loading cadi.properties from classloader", cadiFileName);
64             InputStream is = this.getClass().getClassLoader().getResourceAsStream("cadi.properties");
65             cadiProperties.load(is);
66         } else {
67             LOGGER.info("Successfully found the file {} and started loading the properties from it", cadiFileName);
68             cadiFileName = cadiFile.getAbsolutePath();
69             try (InputStream inputStream = new FileInputStream(cadiFile)) {
70                 cadiProperties.load(inputStream);
71             }
72
73         }
74     }
75     public String getCadiFileName() {
76         return cadiFileName;
77     }
78     public Properties getCadiProperties(){
79         return cadiProperties;
80     }
81 }