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