+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.filter;\r
-\r
-import java.io.FileInputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.util.Map;\r
-import java.util.Map.Entry;\r
-import java.util.Properties;\r
-\r
-import javax.servlet.ServletContext;\r
-\r
-import org.onap.aaf.cadi.Access;\r
-import org.onap.aaf.cadi.Symm;\r
-import org.onap.aaf.cadi.config.Config;\r
-import org.onap.aaf.cadi.config.Get;\r
-\r
-public class CadiAccess implements Access {\r
- // constants for a couple of very commonly used strings.\r
- protected static final String FROM = "from";\r
- protected static final String FOR = "for";\r
-\r
- // Properties derived from <pass> sources (could be property files, Valve Configurations, Filter\r
- // configs, etc. \r
- protected Properties props;\r
-\r
- // Will we write Logs?\r
- protected Level willWrite = Level.INFO;\r
- \r
- protected ServletContext context;\r
- protected Get getter = Get.NULL; // replace with Derived Class getter\r
- private Symm symm;\r
-\r
- public CadiAccess(Map<String, Object> map) {\r
- if(map!=null && !map.isEmpty()) {\r
- props = new Properties();\r
- for(Entry<String, Object> es : map.entrySet()) {\r
- Object v = es.getValue();\r
- if(v!=null) {\r
- props.put(es.getKey(), v.toString());\r
- }\r
- }\r
- Object keyfile = props.get(Config.CADI_KEYFILE);\r
- if(keyfile!=null) {\r
- try {\r
- FileInputStream fis = new FileInputStream(keyfile.toString());\r
- symm = Symm.obtain(fis);\r
- } catch (Exception e) {\r
- }\r
- }\r
-\r
- }\r
- }\r
- \r
- public Level willWrite() {\r
- return willWrite;\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see com.att.cadi.Access#willLog(com.att.cadi.Access.Level)\r
- */\r
- @Override\r
- public boolean willLog(Level level) {\r
- return willWrite.compareTo(level)<=0;\r
- }\r
-\r
- /**\r
- * Add the "Level" to the Buildline for Logging types that don't specify, or straight Streams, etc. Then buildline\r
- * \r
- * Build a line of code onto a StringBuilder based on Objects. Analyze whether \r
- * spaces need including.\r
- *\r
- * @param level\r
- * @param sb\r
- * @param elements\r
- * @return\r
- */\r
- public final static StringBuilder buildLine(Level level, StringBuilder sb, Object[] elements) {\r
- sb.append(level.name());\r
- return buildLine(sb,elements);\r
- }\r
- \r
- /*\r
- * Build a line of code onto a StringBuilder based on Objects. Analyze whether \r
- * spaces need including.\r
- * \r
- * @param sb\r
- * @param elements\r
- * @return\r
- */\r
- public final static StringBuilder buildLine(StringBuilder sb, Object[] elements) {\r
- sb.append(' ');\r
- String str;\r
- boolean notFirst = false;\r
- for(Object o : elements) {\r
- if(o!=null) {\r
- str = o.toString();\r
- \r
- if(str.length()>0) {\r
- if(notFirst && shouldAddSpace(str,true) && shouldAddSpace(sb,false)) {\r
- sb.append(' ');\r
- } else {\r
- notFirst=true;\r
- }\r
- sb.append(str);\r
- }\r
- }\r
- }\r
- return sb;\r
- }\r
- \r
- private static boolean shouldAddSpace(CharSequence c,boolean start) {\r
- if(c.length()>0)\r
- switch(c.charAt(start?0:c.length()-1)) {\r
- case ' ':\r
- case '\t':\r
- case '\n':\r
- case '\'':\r
- case '"':\r
- case '|':\r
- return false;\r
- }\r
- return true;\r
- }\r
-\r
- /**\r
- * Standard mechanism for logging, given being within a Servlet Context\r
- * \r
- * Here, we treat\r
- * \r
- * if context exists, log to it, otherwise log to Std Out (The latter is usually for startup \r
- * scenarios)\r
- *\r
- */\r
- public void log(Level level, Object... elements) {\r
- if(willWrite.compareTo(level)<=0) {\r
- StringBuilder sb = buildLine(level, new StringBuilder(),elements);\r
- if(context==null) {\r
- System.out.println(sb.toString());\r
- } else {\r
- context.log(sb.toString());\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Standard mechanism for logging an Exception, given being within a Servlet Context, etc\r
- * \r
- * if context exists, log to it, otherwise log to Std Out (The latter is usually for startup \r
- * scenarios)\r
- *\r
- */\r
- public void log(Exception e, Object... elements) {\r
- if(willWrite.compareTo(Level.ERROR)<=0) {\r
- StringBuilder sb = buildLine(Level.ERROR, new StringBuilder(),elements);\r
- \r
- if(context==null) {\r
- sb.append(e.toString());\r
- System.out.println(sb.toString());\r
- } else {\r
- context.log(sb.toString(),e);\r
- }\r
- }\r
- }\r
- \r
- public void setLogLevel(Level level) {\r
- willWrite = level;\r
- }\r
- \r
- /**\r
- * Pass back the classloader of the Servlet Context, if it exists. Otherwise, get the classloader\r
- * of this object.\r
- */\r
- public ClassLoader classLoader() { // Use the Classloader that Context was created with\r
- return (context==null?this:context).getClass().getClassLoader();\r
- }\r
-\r
- /**\r
- * Get the Property from Context\r
- */\r
- public String getProperty(String string, String def) {\r
- String rv = null;\r
-\r
- if ( props != null )\r
- rv = props.getProperty( string, def );\r
- \r
- if(rv==null) {\r
- rv = context.getInitParameter(string);\r
- }\r
- return rv==null?def:rv;\r
-\r
- }\r
-\r
- public void load(InputStream is) throws IOException {\r
- if(this.props==null) {\r
- this.props = new Properties();\r
- }\r
- this.props.load(is);\r
- symm = Symm.obtain(this);\r
- }\r
-\r
- public String decrypt(String encrypted, boolean anytext) throws IOException {\r
- if(symm==null) {\r
- String keyfile = getter.get(Config.CADI_KEYFILE, null, true);\r
- if(keyfile!=null) {\r
- FileInputStream fis = new FileInputStream(keyfile);\r
- symm=Symm.obtain(fis);\r
- fis.close();\r
- }\r
- }\r
- return (symm!=null && encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))\r
- ? symm.depass(encrypted)\r
- : encrypted;\r
- }\r
-\r
- @Override\r
- public void printf(Level level, String fmt, Object[] elements) {\r
- // TODO Auto-generated method stub\r
- \r
- }\r
-\r
-}\r