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