7d836253849cf4a8cf223f269ac583d47f1be4ae
[aaf/cadi.git] / aaf / src / test / java / com / att / cadi / lur / aaf / test / TestAccess.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.lur.aaf.test;\r
24 \r
25 import java.io.FileInputStream;\r
26 import java.io.IOException;\r
27 import java.io.InputStream;\r
28 import java.io.PrintStream;\r
29 \r
30 import com.att.cadi.Access;\r
31 import com.att.cadi.Symm;\r
32 import com.att.cadi.config.Config;\r
33 \r
34 public class TestAccess implements Access {\r
35         private Symm symm;\r
36         private PrintStream out;\r
37 \r
38         public TestAccess(PrintStream out) {\r
39                 this.out = out;\r
40                 InputStream is = ClassLoader.getSystemResourceAsStream("cadi.properties");\r
41                 try {\r
42                         System.getProperties().load(is);\r
43                 } catch (IOException e) {\r
44                         e.printStackTrace(out);\r
45                 } finally {\r
46                         try {\r
47                                 is.close();\r
48                         } catch (IOException e) {\r
49                                 e.printStackTrace(out);\r
50                         }\r
51                 }\r
52                 \r
53                 String keyfile = System.getProperty(Config.CADI_KEYFILE);\r
54                 if(keyfile==null) {\r
55                         System.err.println("No " + Config.CADI_KEYFILE + " in Classpath");\r
56                 } else {\r
57                         try {\r
58                                 is = new FileInputStream(keyfile);\r
59                                 try {\r
60                                         symm = Symm.obtain(is);\r
61                                 } finally {\r
62                                         is.close();\r
63                                 }\r
64                         } catch (IOException e) {\r
65                                 e.printStackTrace(out);\r
66                         }\r
67                 }\r
68                 \r
69 \r
70 \r
71         }\r
72         \r
73         public void log(Level level, Object... elements) {\r
74                 boolean first = true;\r
75                 for(int i=0;i<elements.length;++i) {\r
76                         if(first)first = false;\r
77                         else out.print(' ');\r
78                         out.print(elements[i].toString());\r
79                 }\r
80                 out.println();\r
81         }\r
82 \r
83         public void log(Exception e, Object... elements) {\r
84                 e.printStackTrace(out);\r
85                 log(Level.ERROR,elements);\r
86         }\r
87 \r
88         public void setLogLevel(Level level) {\r
89                 \r
90         }\r
91 \r
92         @Override\r
93         public boolean willLog(Level level) {\r
94                 return true;\r
95         }\r
96 \r
97         public ClassLoader classLoader() {\r
98                 return ClassLoader.getSystemClassLoader();\r
99         }\r
100 \r
101         public String getProperty(String string, String def) {\r
102                 String rv = System.getProperty(string);\r
103                 return rv==null?def:rv;\r
104         }\r
105 \r
106         public void load(InputStream is) throws IOException {\r
107                 \r
108         }\r
109 \r
110         public String decrypt(String encrypted, boolean anytext) throws IOException {\r
111                 return (encrypted!=null && (anytext==true || encrypted.startsWith(Symm.ENC)))\r
112                         ? symm.depass(encrypted)\r
113                         : encrypted;\r
114         }\r
115 \r
116         @Override\r
117         public void printf(Level level, String fmt, Object... elements) {\r
118                 // TODO Auto-generated method stub\r
119                 \r
120         }\r
121 \r
122 }\r