1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
12 * * http://www.apache.org/licenses/LICENSE-2.0
\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
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cadi.filter;
\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
33 import javax.servlet.ServletContext;
\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
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
45 // Properties derived from <pass> sources (could be property files, Valve Configurations, Filter
\r
47 protected Properties props;
\r
49 // Will we write Logs?
\r
50 protected Level willWrite = Level.INFO;
\r
52 protected ServletContext context;
\r
53 protected Get getter = Get.NULL; // replace with Derived Class getter
\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
62 props.put(es.getKey(), v.toString());
\r
65 Object keyfile = props.get(Config.CADI_KEYFILE);
\r
68 FileInputStream fis = new FileInputStream(keyfile.toString());
\r
69 symm = Symm.obtain(fis);
\r
70 } catch (Exception e) {
\r
77 public Level willWrite() {
\r
82 * @see com.att.cadi.Access#willLog(com.att.cadi.Access.Level)
\r
85 public boolean willLog(Level level) {
\r
86 return willWrite.compareTo(level)<=0;
\r
90 * Add the "Level" to the Buildline for Logging types that don't specify, or straight Streams, etc. Then buildline
\r
92 * Build a line of code onto a StringBuilder based on Objects. Analyze whether
\r
93 * spaces need including.
\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
106 * Build a line of code onto a StringBuilder based on Objects. Analyze whether
\r
107 * spaces need including.
\r
113 public final static StringBuilder buildLine(StringBuilder sb, Object[] elements) {
\r
116 boolean notFirst = false;
\r
117 for(Object o : elements) {
\r
119 str = o.toString();
\r
121 if(str.length()>0) {
\r
122 if(notFirst && shouldAddSpace(str,true) && shouldAddSpace(sb,false)) {
\r
134 private static boolean shouldAddSpace(CharSequence c,boolean start) {
\r
136 switch(c.charAt(start?0:c.length()-1)) {
\r
149 * Standard mechanism for logging, given being within a Servlet Context
\r
153 * if context exists, log to it, otherwise log to Std Out (The latter is usually for startup
\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
163 context.log(sb.toString());
\r
169 * Standard mechanism for logging an Exception, given being within a Servlet Context, etc
\r
171 * if context exists, log to it, otherwise log to Std Out (The latter is usually for startup
\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
179 if(context==null) {
\r
180 sb.append(e.toString());
\r
181 System.out.println(sb.toString());
\r
183 context.log(sb.toString(),e);
\r
188 public void setLogLevel(Level level) {
\r
193 * Pass back the classloader of the Servlet Context, if it exists. Otherwise, get the classloader
\r
196 public ClassLoader classLoader() { // Use the Classloader that Context was created with
\r
197 return (context==null?this:context).getClass().getClassLoader();
\r
201 * Get the Property from Context
\r
203 public String getProperty(String string, String def) {
\r
206 if ( props != null )
\r
207 rv = props.getProperty( string, def );
\r
210 rv = context.getInitParameter(string);
\r
212 return rv==null?def:rv;
\r
216 public void load(InputStream is) throws IOException {
\r
217 if(this.props==null) {
\r
218 this.props = new Properties();
\r
220 this.props.load(is);
\r
221 symm = Symm.obtain(this);
\r
224 public String decrypt(String encrypted, boolean anytext) throws IOException {
\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
233 return (symm!=null && encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
\r
234 ? symm.depass(encrypted)
\r
239 public void printf(Level level, String fmt, Object[] elements) {
\r
240 // TODO Auto-generated method stub
\r