Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / dmaap / DmaapClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.so.client.dmaap;
22
23 import java.io.IOException;
24 import java.security.GeneralSecurityException;
25 import java.util.Base64;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.Properties;
29 import org.onap.so.client.defaultproperties.DefaultDmaapPropertiesImpl;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.core.io.ClassPathResource;
33 import org.springframework.core.io.Resource;
34 import org.onap.so.utils.CryptoUtils;
35
36 public abstract class DmaapClient {
37
38     protected static Logger logger = LoggerFactory.getLogger(DmaapClient.class);
39     protected final Map<String, String> msoProperties;
40     protected final Properties properties;
41
42     public DmaapClient(String filepath) throws IOException {
43         Resource resource = new ClassPathResource(filepath);
44         DmaapProperties dmaapProperties = DmaapPropertiesLoader.getInstance().getNewImpl();
45         if (dmaapProperties == null) {
46             logger.error("No RestProperty implementation found on classpath, loading default");
47             dmaapProperties = new DefaultDmaapPropertiesImpl();
48         }
49         this.msoProperties = dmaapProperties.getProperties();
50         this.properties = new Properties();
51         this.properties.load(resource.getInputStream());
52         try {
53             this.properties.put("auth", CryptoUtils.decrypt(this.getAuth(), this.getKey()).getBytes());
54         } catch (GeneralSecurityException e) {
55             logger.error(e.getMessage(), e);
56         }
57         this.properties.put("key", this.getKey());
58         this.properties.put("topic", this.getTopic());
59         Optional<String> host = this.getHost();
60         if (host.isPresent()) {
61             this.properties.put("host", host.get());
62         }
63     }
64
65     protected String deobfuscatePassword(String decrypted_key) {
66
67         try {
68             return new String(Base64.getDecoder().decode(decrypted_key.getBytes()));
69         } catch (IllegalArgumentException iae) {
70             logger.error("llegal Arguments", iae);
71             return decrypted_key;
72         }
73     }
74
75     public abstract String getKey();
76
77     public abstract String getAuth();
78
79     public abstract String getTopic();
80
81     public abstract Optional<String> getHost();
82 }