AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / lur / aaf / test / JU_TestAccess.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 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  ******************************************************************************/
22 package org.onap.aaf.cadi.lur.aaf.test;
23
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.PrintStream;
28 import java.util.Properties;
29
30 import org.onap.aaf.cadi.Access;
31 import org.onap.aaf.cadi.Symm;
32 import org.onap.aaf.cadi.config.Config;
33
34 public class JU_TestAccess implements Access {
35         private Symm symm;
36         private PrintStream out;
37
38         public JU_TestAccess(PrintStream out) {
39                 this.out = out;
40                 InputStream is = ClassLoader.getSystemResourceAsStream("cadi.properties");
41                 try {
42                         System.getProperties().load(is);
43                 } catch (IOException e) {
44                         e.printStackTrace(out);
45                 } finally {
46                         try {
47                                 is.close();
48                         } catch (IOException e) {
49                                 e.printStackTrace(out);
50                         }
51                 }
52                 
53                 String keyfile = System.getProperty(Config.CADI_KEYFILE);
54                 if(keyfile==null) {
55                         System.err.println("No " + Config.CADI_KEYFILE + " in Classpath");
56                 } else {
57                         try {
58                                 is = new FileInputStream(keyfile);
59                                 try {
60                                         symm = Symm.obtain(is);
61                                 } finally {
62                                         is.close();
63                                 }
64                         } catch (IOException e) {
65                                 e.printStackTrace(out);
66                         }
67                 }
68                 
69
70
71         }
72         
73         public void log(Level level, Object... elements) {
74                 boolean first = true;
75                 for(int i=0;i<elements.length;++i) {
76                         if(first)first = false;
77                         else out.print(' ');
78                         out.print(elements[i].toString());
79                 }
80                 out.println();
81         }
82
83         public void log(Exception e, Object... elements) {
84                 e.printStackTrace(out);
85                 log(Level.ERROR,elements);
86         }
87
88         public void setLogLevel(Level level) {
89                 
90         }
91
92         @Override
93         public boolean willLog(Level level) {
94                 return true;
95         }
96
97         public ClassLoader classLoader() {
98                 return ClassLoader.getSystemClassLoader();
99         }
100
101         public String getProperty(String string, String def) {
102                 String rv = System.getProperty(string);
103                 return rv==null?def:rv;
104         }
105
106         @Override
107         public Properties getProperties() {
108                 return System.getProperties();
109         }
110
111         public void load(InputStream is) throws IOException {
112                 
113         }
114
115         public String decrypt(String encrypted, boolean anytext) throws IOException {
116                 return (encrypted!=null && (anytext==true || encrypted.startsWith(Symm.ENC)))
117                         ? symm.depass(encrypted)
118                         : encrypted;
119         }
120
121         @Override
122         public void printf(Level level, String fmt, Object... elements) {
123                 // TODO Auto-generated method stub
124                 
125         }
126
127 }