summaryrefslogtreecommitdiffstats
path: root/core/src/main/java/org/onap/aaf/cadi/filter/CadiAccess.java
blob: 2ccd29a7c8afaf545078f828f0b6a75a582964bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package org.onap.aaf.cadi.filter;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.onap.aaf.cadi.Access;
import org.onap.aaf.cadi.Symm;
import org.onap.aaf.cadi.config.Config;
import org.onap.aaf.cadi.config.Get;

public class CadiAccess implements Access {
	// constants for a couple of very commonly used strings.
	protected static final String FROM = "from";
	protected static final String FOR = "for";

	// Properties derived from <pass> sources (could be property files, Valve Configurations, Filter
	// configs, etc. 
	protected Properties props;

	// Will we write Logs?
	protected Level willWrite = Level.INFO;
	
	protected ServletContext context;
	protected Get getter = Get.NULL; // replace with Derived Class getter
	private Symm symm;

	public CadiAccess(Map<String, Object> map) {
		if(map!=null && !map.isEmpty()) {
			props = new Properties();
			for(Entry<String, Object> es : map.entrySet()) {
				Object v = es.getValue();
				if(v!=null) {
					props.put(es.getKey(), v.toString());
				}
			}
			Object keyfile = props.get(Config.CADI_KEYFILE);
			if(keyfile!=null) {
				try {
					FileInputStream fis = new FileInputStream(keyfile.toString());
					symm = Symm.obtain(fis);
				} catch (Exception e) {
				}
			}

		}
	}
	
	public Level willWrite() {
		return willWrite;
	}

	/* (non-Javadoc)
	 * @see com.att.cadi.Access#willLog(com.att.cadi.Access.Level)
	 */
	@Override
	public boolean willLog(Level level) {
		return willWrite.compareTo(level)<=0;
	}

	/**
	 * Add the "Level" to the Buildline for Logging types that don't specify, or straight Streams, etc.  Then buildline
	 * 
	 * Build a line of code onto a StringBuilder based on Objects.  Analyze whether 
	 * spaces need including.
	 *
	 * @param level
	 * @param sb
	 * @param elements
	 * @return
	 */
	public final static StringBuilder buildLine(Level level, StringBuilder sb, Object[] elements) {
		sb.append(level.name());
		return buildLine(sb,elements);
	}
	
	/*
	 * Build a line of code onto a StringBuilder based on Objects.  Analyze whether 
	 * spaces need including.
	 * 
	 * @param sb
	 * @param elements
	 * @return
	 */
	public final static StringBuilder buildLine(StringBuilder sb, Object[] elements) {
		sb.append(' ');
		String str;
		boolean notFirst = false;
		for(Object o : elements) {
			if(o!=null) {
				str = o.toString();
				
				if(str.length()>0) {
					if(notFirst && shouldAddSpace(str,true) && shouldAddSpace(sb,false)) {
						sb.append(' ');
					} else {
						notFirst=true;
					}
					sb.append(str);
				}
			}
		}
		return sb;
	}
	
	private static boolean shouldAddSpace(CharSequence c,boolean start) {
		if(c.length()>0)
			switch(c.charAt(start?0:c.length()-1)) {
				case ' ':
				case '\t':
				case '\n':
				case '\'':
				case '"':
				case '|':
					return false;
			}
		return true;
	}

	/**
	 * Standard mechanism for logging, given being within a Servlet Context
	 * 
	 * Here, we treat
	 * 
	 * if context exists, log to it, otherwise log to Std Out (The latter is usually for startup 
	 * scenarios)
	 *
	 */
	public void log(Level level, Object... elements) {
		if(willWrite.compareTo(level)<=0) {
			StringBuilder sb = buildLine(level, new StringBuilder(),elements);
			if(context==null) {
				System.out.println(sb.toString());
			} else {
				context.log(sb.toString());
			}
		}
	}

	/**
	 * Standard mechanism for logging an Exception, given being within a Servlet Context, etc
	 * 
	 * if context exists, log to it, otherwise log to Std Out (The latter is usually for startup 
	 * scenarios)
	 *
	 */
	public void log(Exception e, Object... elements) {
		if(willWrite.compareTo(Level.ERROR)<=0) {
			StringBuilder sb = buildLine(Level.ERROR, new StringBuilder(),elements);
		
			if(context==null) {
				sb.append(e.toString());
				System.out.println(sb.toString());
			} else {
				context.log(sb.toString(),e);
			}
		}
	}
	
	public void setLogLevel(Level level) {
		willWrite = level;
	}
	
	/**
	 * Pass back the classloader of the Servlet Context, if it exists. Otherwise, get the classloader
	 * of this object.
	 */
	public ClassLoader classLoader() { // Use the Classloader that Context was created with
		return (context==null?this:context).getClass().getClassLoader();
	}

	/**
	 * Get the Property from Context
	 */
	public String getProperty(String string, String def) {
		String rv = null;

        if ( props != null )
            rv = props.getProperty( string, def );
        
		if(rv==null) {
            rv = context.getInitParameter(string);
		}
		return rv==null?def:rv;

	}

	public void load(InputStream is) throws IOException {
		if(this.props==null) {
			this.props = new Properties();
		}
		this.props.load(is);
		symm = Symm.obtain(this);
	}

	public String decrypt(String encrypted, boolean anytext) throws IOException {
		if(symm==null) {
			String keyfile = getter.get(Config.CADI_KEYFILE, null, true);
			if(keyfile!=null) {
				FileInputStream fis = new FileInputStream(keyfile);
				symm=Symm.obtain(fis);
				fis.close();
			}
		}
		return (symm!=null && encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
			? symm.depass(encrypted)
			: encrypted;
	}

	@Override
	public void printf(Level level, String fmt, Object[] elements) {
		// TODO Auto-generated method stub
		
	}

}