Update AAF Version 1.0.0
[aaf/cadi.git] / core / src / main / java / org / onap / aaf / cadi / filter / CadiAccess.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 org.onap.aaf.cadi.filter;\r
24 \r
25 import java.io.FileInputStream;\r
26 import java.io.IOException;\r
27 import java.io.InputStream;\r
28 import java.util.Map;\r
29 import java.util.Map.Entry;\r
30 import java.util.Properties;\r
31 \r
32 import javax.servlet.ServletContext;\r
33 \r
34 import org.onap.aaf.cadi.Access;\r
35 import org.onap.aaf.cadi.Symm;\r
36 import org.onap.aaf.cadi.config.Config;\r
37 import org.onap.aaf.cadi.config.Get;\r
38 \r
39 public class CadiAccess implements Access {\r
40         // constants for a couple of very commonly used strings.\r
41         protected static final String FROM = "from";\r
42         protected static final String FOR = "for";\r
43 \r
44         // Properties derived from <pass> sources (could be property files, Valve Configurations, Filter\r
45         // configs, etc. \r
46         protected Properties props;\r
47 \r
48         // Will we write Logs?\r
49         protected Level willWrite = Level.INFO;\r
50         \r
51         protected ServletContext context;\r
52         protected Get getter = Get.NULL; // replace with Derived Class getter\r
53         private Symm symm;\r
54 \r
55         public CadiAccess(Map<String, Object> map) {\r
56                 if(map!=null && !map.isEmpty()) {\r
57                         props = new Properties();\r
58                         for(Entry<String, Object> es : map.entrySet()) {\r
59                                 Object v = es.getValue();\r
60                                 if(v!=null) {\r
61                                         props.put(es.getKey(), v.toString());\r
62                                 }\r
63                         }\r
64                         Object keyfile = props.get(Config.CADI_KEYFILE);\r
65                         if(keyfile!=null) {\r
66                                 try {\r
67                                         FileInputStream fis = new FileInputStream(keyfile.toString());\r
68                                         symm = Symm.obtain(fis);\r
69                                 } catch (Exception e) {\r
70                                 }\r
71                         }\r
72 \r
73                 }\r
74         }\r
75         \r
76         public Level willWrite() {\r
77                 return willWrite;\r
78         }\r
79 \r
80         /* (non-Javadoc)\r
81          * @see com.att.cadi.Access#willLog(com.att.cadi.Access.Level)\r
82          */\r
83         @Override\r
84         public boolean willLog(Level level) {\r
85                 return willWrite.compareTo(level)<=0;\r
86         }\r
87 \r
88         /**\r
89          * Add the "Level" to the Buildline for Logging types that don't specify, or straight Streams, etc.  Then buildline\r
90          * \r
91          * Build a line of code onto a StringBuilder based on Objects.  Analyze whether \r
92          * spaces need including.\r
93          *\r
94          * @param level\r
95          * @param sb\r
96          * @param elements\r
97          * @return\r
98          */\r
99         public final static StringBuilder buildLine(Level level, StringBuilder sb, Object[] elements) {\r
100                 sb.append(level.name());\r
101                 return buildLine(sb,elements);\r
102         }\r
103         \r
104         /*\r
105          * Build a line of code onto a StringBuilder based on Objects.  Analyze whether \r
106          * spaces need including.\r
107          * \r
108          * @param sb\r
109          * @param elements\r
110          * @return\r
111          */\r
112         public final static StringBuilder buildLine(StringBuilder sb, Object[] elements) {\r
113                 sb.append(' ');\r
114                 String str;\r
115                 boolean notFirst = false;\r
116                 for(Object o : elements) {\r
117                         if(o!=null) {\r
118                                 str = o.toString();\r
119                                 \r
120                                 if(str.length()>0) {\r
121                                         if(notFirst && shouldAddSpace(str,true) && shouldAddSpace(sb,false)) {\r
122                                                 sb.append(' ');\r
123                                         } else {\r
124                                                 notFirst=true;\r
125                                         }\r
126                                         sb.append(str);\r
127                                 }\r
128                         }\r
129                 }\r
130                 return sb;\r
131         }\r
132         \r
133         private static boolean shouldAddSpace(CharSequence c,boolean start) {\r
134                 if(c.length()>0)\r
135                         switch(c.charAt(start?0:c.length()-1)) {\r
136                                 case ' ':\r
137                                 case '\t':\r
138                                 case '\n':\r
139                                 case '\'':\r
140                                 case '"':\r
141                                 case '|':\r
142                                         return false;\r
143                         }\r
144                 return true;\r
145         }\r
146 \r
147         /**\r
148          * Standard mechanism for logging, given being within a Servlet Context\r
149          * \r
150          * Here, we treat\r
151          * \r
152          * if context exists, log to it, otherwise log to Std Out (The latter is usually for startup \r
153          * scenarios)\r
154          *\r
155          */\r
156         public void log(Level level, Object... elements) {\r
157                 if(willWrite.compareTo(level)<=0) {\r
158                         StringBuilder sb = buildLine(level, new StringBuilder(),elements);\r
159                         if(context==null) {\r
160                                 System.out.println(sb.toString());\r
161                         } else {\r
162                                 context.log(sb.toString());\r
163                         }\r
164                 }\r
165         }\r
166 \r
167         /**\r
168          * Standard mechanism for logging an Exception, given being within a Servlet Context, etc\r
169          * \r
170          * if context exists, log to it, otherwise log to Std Out (The latter is usually for startup \r
171          * scenarios)\r
172          *\r
173          */\r
174         public void log(Exception e, Object... elements) {\r
175                 if(willWrite.compareTo(Level.ERROR)<=0) {\r
176                         StringBuilder sb = buildLine(Level.ERROR, new StringBuilder(),elements);\r
177                 \r
178                         if(context==null) {\r
179                                 sb.append(e.toString());\r
180                                 System.out.println(sb.toString());\r
181                         } else {\r
182                                 context.log(sb.toString(),e);\r
183                         }\r
184                 }\r
185         }\r
186         \r
187         public void setLogLevel(Level level) {\r
188                 willWrite = level;\r
189         }\r
190         \r
191         /**\r
192          * Pass back the classloader of the Servlet Context, if it exists. Otherwise, get the classloader\r
193          * of this object.\r
194          */\r
195         public ClassLoader classLoader() { // Use the Classloader that Context was created with\r
196                 return (context==null?this:context).getClass().getClassLoader();\r
197         }\r
198 \r
199         /**\r
200          * Get the Property from Context\r
201          */\r
202         public String getProperty(String string, String def) {\r
203                 String rv = null;\r
204 \r
205         if ( props != null )\r
206             rv = props.getProperty( string, def );\r
207         \r
208                 if(rv==null) {\r
209             rv = context.getInitParameter(string);\r
210                 }\r
211                 return rv==null?def:rv;\r
212 \r
213         }\r
214 \r
215         public void load(InputStream is) throws IOException {\r
216                 if(this.props==null) {\r
217                         this.props = new Properties();\r
218                 }\r
219                 this.props.load(is);\r
220                 symm = Symm.obtain(this);\r
221         }\r
222 \r
223         public String decrypt(String encrypted, boolean anytext) throws IOException {\r
224                 if(symm==null) {\r
225                         String keyfile = getter.get(Config.CADI_KEYFILE, null, true);\r
226                         if(keyfile!=null) {\r
227                                 FileInputStream fis = new FileInputStream(keyfile);\r
228                                 symm=Symm.obtain(fis);\r
229                                 fis.close();\r
230                         }\r
231                 }\r
232                 return (symm!=null && encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))\r
233                         ? symm.depass(encrypted)\r
234                         : encrypted;\r
235         }\r
236 \r
237         @Override\r
238         public void printf(Level level, String fmt, Object[] elements) {\r
239                 // TODO Auto-generated method stub\r
240                 \r
241         }\r
242 \r
243 }\r