1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cadi.filter;
\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
32 import javax.servlet.ServletContext;
\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
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
44 // Properties derived from <pass> sources (could be property files, Valve Configurations, Filter
\r
46 protected Properties props;
\r
48 // Will we write Logs?
\r
49 protected Level willWrite = Level.INFO;
\r
51 protected ServletContext context;
\r
52 protected Get getter = Get.NULL; // replace with Derived Class getter
\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
61 props.put(es.getKey(), v.toString());
\r
64 Object keyfile = props.get(Config.CADI_KEYFILE);
\r
67 FileInputStream fis = new FileInputStream(keyfile.toString());
\r
68 symm = Symm.obtain(fis);
\r
69 } catch (Exception e) {
\r
76 public Level willWrite() {
\r
81 * @see com.att.cadi.Access#willLog(com.att.cadi.Access.Level)
\r
84 public boolean willLog(Level level) {
\r
85 return willWrite.compareTo(level)<=0;
\r
89 * Add the "Level" to the Buildline for Logging types that don't specify, or straight Streams, etc. Then buildline
\r
91 * Build a line of code onto a StringBuilder based on Objects. Analyze whether
\r
92 * spaces need including.
\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
105 * Build a line of code onto a StringBuilder based on Objects. Analyze whether
\r
106 * spaces need including.
\r
112 public final static StringBuilder buildLine(StringBuilder sb, Object[] elements) {
\r
115 boolean notFirst = false;
\r
116 for(Object o : elements) {
\r
118 str = o.toString();
\r
120 if(str.length()>0) {
\r
121 if(notFirst && shouldAddSpace(str,true) && shouldAddSpace(sb,false)) {
\r
133 private static boolean shouldAddSpace(CharSequence c,boolean start) {
\r
135 switch(c.charAt(start?0:c.length()-1)) {
\r
148 * Standard mechanism for logging, given being within a Servlet Context
\r
152 * if context exists, log to it, otherwise log to Std Out (The latter is usually for startup
\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
162 context.log(sb.toString());
\r
168 * Standard mechanism for logging an Exception, given being within a Servlet Context, etc
\r
170 * if context exists, log to it, otherwise log to Std Out (The latter is usually for startup
\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
178 if(context==null) {
\r
179 sb.append(e.toString());
\r
180 System.out.println(sb.toString());
\r
182 context.log(sb.toString(),e);
\r
187 public void setLogLevel(Level level) {
\r
192 * Pass back the classloader of the Servlet Context, if it exists. Otherwise, get the classloader
\r
195 public ClassLoader classLoader() { // Use the Classloader that Context was created with
\r
196 return (context==null?this:context).getClass().getClassLoader();
\r
200 * Get the Property from Context
\r
202 public String getProperty(String string, String def) {
\r
205 if ( props != null )
\r
206 rv = props.getProperty( string, def );
\r
209 rv = context.getInitParameter(string);
\r
211 return rv==null?def:rv;
\r
215 public void load(InputStream is) throws IOException {
\r
216 if(this.props==null) {
\r
217 this.props = new Properties();
\r
219 this.props.load(is);
\r
220 symm = Symm.obtain(this);
\r
223 public String decrypt(String encrypted, boolean anytext) throws IOException {
\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
232 return (symm!=null && encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
\r
233 ? symm.depass(encrypted)
\r
238 public void printf(Level level, String fmt, Object[] elements) {
\r
239 // TODO Auto-generated method stub
\r