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