2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.auth.rserv;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.FileOutputStream;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.io.OutputStreamWriter;
33 import java.io.Writer;
34 import java.nio.ByteBuffer;
35 import java.nio.channels.FileChannel;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.Comparator;
40 import java.util.HashSet;
42 import java.util.Map.Entry;
43 import java.util.NavigableMap;
45 import java.util.Timer;
46 import java.util.TimerTask;
47 import java.util.TreeMap;
48 import java.util.concurrent.ConcurrentSkipListMap;
49 import java.util.regex.Matcher;
50 import java.util.regex.Pattern;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
55 import org.onap.aaf.misc.env.Env;
56 import org.onap.aaf.misc.env.EnvJAXB;
57 import org.onap.aaf.misc.env.LogTarget;
58 import org.onap.aaf.misc.env.Store;
59 import org.onap.aaf.misc.env.TimeTaken;
60 import org.onap.aaf.misc.env.Trans;
64 * Author: Jonathan Gathman, Gathsys 2010
67 public class CachingFileAccess<TRANS extends Trans> extends HttpCode<TRANS, Void> {
68 public static void setEnv(Store store, String[] args) {
69 for (int i=0;i<args.length-1;i+=2) { // cover two parms required for each
70 if (CFA_WEB_PATH.equals(args[i])) {
71 store.put(store.staticSlot(CFA_WEB_PATH), args[i+1]);
72 } else if (CFA_CACHE_CHECK_INTERVAL.equals(args[i])) {
73 store.put(store.staticSlot(CFA_CACHE_CHECK_INTERVAL), Long.parseLong(args[i+1]));
74 } else if (CFA_MAX_SIZE.equals(args[i])) {
75 store.put(store.staticSlot(CFA_MAX_SIZE), Integer.parseInt(args[i+1]));
80 private static String MAX_AGE = "max-age=3600"; // 1 hour Caching
81 private final Map<String,String> typeMap;
82 private final NavigableMap<String,Content> content;
83 private final Set<String> attachOnly;
84 public final static String CFA_WEB_PATH = "aaf_cfa_web_path";
85 // when to re-validate from file
86 // Re validating means comparing the Timestamp on the disk, and seeing it has changed. Cache is not marked
87 // dirty unless file has changed, but it still makes File IO, which for some kinds of cached data, i.e.
88 // deployed GUI elements is unnecessary, and wastes time.
89 // This parameter exists to cover the cases where data can be more volatile, so the user can choose how often the
90 // File IO will be accessed, based on probability of change. "0", of course, means, check every time.
91 private final static String CFA_CACHE_CHECK_INTERVAL = "aaf_cfa_cache_check_interval";
92 private final static String CFA_MAX_SIZE = "aaf_cfa_max_size"; // Cache size limit
93 private final static String CFA_CLEAR_COMMAND = "aaf_cfa_clear_command";
95 // Note: can be null without a problem, but included
96 // to tie in with existing Logging.
97 public LogTarget logT = null;
98 public long checkInterval; // = 600000L; // only check if not hit in 10 mins by default
99 public int maxItemSize; // = 512000; // max file 500k
101 private String web_path;
102 // A command key is set in the Properties, preferably changed on deployment.
103 // it is compared at the beginning of the path, and if so, it is assumed to issue certain commands
104 // It's purpose is to protect, to some degree the command, even though it is HTTP, allowing
105 // local batch files to, for instance, clear caches on resetting of files.
106 private String clear_command;
108 public CachingFileAccess(EnvJAXB env, String ... args) throws IOException {
109 super(null,"Caching File Access");
111 content = new ConcurrentSkipListMap<>(); // multi-thread changes possible
113 attachOnly = new HashSet<>(); // short, unchanged
115 typeMap = new TreeMap<>(); // Structure unchanged after Construction
116 typeMap.put("ico","image/icon");
117 typeMap.put("html","text/html");
118 typeMap.put("css","text/css");
119 typeMap.put("js","text/javascript");
120 typeMap.put("txt","text/plain");
121 typeMap.put("xml","text/xml");
122 typeMap.put("xsd","text/xml");
123 attachOnly.add("xsd");
124 typeMap.put("crl", "application/x-pkcs7-crl");
125 typeMap.put("appcache","text/cache-manifest");
127 typeMap.put("json","text/json");
128 typeMap.put("ogg", "audio/ogg");
129 typeMap.put("jpg","image/jpeg");
130 typeMap.put("gif","image/gif");
131 typeMap.put("png","image/png");
132 typeMap.put("svg","image/svg+xml");
133 typeMap.put("jar","application/x-java-applet");
134 typeMap.put("jnlp", "application/x-java-jnlp-file");
135 typeMap.put("class", "application/java");
136 typeMap.put("props", "text/plain");
137 typeMap.put("jks", "application/octet-stream");
139 timer = new Timer("Caching Cleanup",true);
140 timer.schedule(new Cleanup(content,500),60000,60000);
143 web_path = env.get(env.staticSlot(CFA_WEB_PATH));
144 env.init().log("CachingFileAccess path: " + new File(web_path).getCanonicalPath());
146 obj = env.get(env.staticSlot(CFA_CACHE_CHECK_INTERVAL),600000L); // Default is 10 mins
147 if (obj instanceof Long) {checkInterval=(Long)obj;
148 } else {checkInterval=Long.parseLong((String)obj);}
150 obj = env.get(env.staticSlot(CFA_MAX_SIZE), 512000); // Default is max file 500k
151 if (obj instanceof Integer) {maxItemSize=(Integer)obj;
152 } else {maxItemSize =Integer.parseInt((String)obj);}
154 clear_command = env.getProperty(CFA_CLEAR_COMMAND,null);
160 public void handle(TRANS trans, HttpServletRequest req, HttpServletResponse resp) throws IOException {
161 String key = pathParam(req, ":key");
162 String cmd = pathParam(req,":cmd");
163 if (key.equals(clear_command)) {
164 resp.setHeader("Content-Type",typeMap.get("txt"));
165 if ("clear".equals(cmd)) {
167 resp.setStatus(200/*HttpStatus.OK_200*/);
169 resp.setStatus(400/*HttpStatus.BAD_REQUEST_400 */);
173 Content c = load(logT , web_path,cmd!=null && cmd.length()>0?key+'/'+cmd:key, null, checkInterval);
174 if (c.attachmentOnly) {
175 resp.setHeader("Content-disposition", "attachment");
178 c.write(resp.getOutputStream());
179 trans.checkpoint(req.getPathInfo());
183 public String webPath() {
188 * Reset the Cleanup size and interval
190 * The size and interval when started are 500 items (memory size unknown) checked every minute in a background thread.
195 public void cleanupParams(int size, long interval) {
198 timer.schedule(new Cleanup(content,size), interval, interval);
204 * Load a file, first checking cache
207 * @param logTarget - logTarget can be null (won't log)
208 * @param dataRoot - data root storage directory
209 * @param key - relative File Path
210 * @param mediaType - what kind of file is it. If null, will check via file extension
211 * @param timeCheck - "-1" will take system default - Otherwise, will compare "now" + timeCheck(Millis) before looking at File mod
213 * @throws IOException
215 public Content load(LogTarget logTarget, String dataRoot, String key, String mediaType, long _timeCheck) throws IOException {
216 long timeCheck = _timeCheck;
218 timeCheck=checkInterval; // if time < 0, then use default
222 if ("-".equals(key)) {
226 fileName=dataRoot + '/' + key;
229 Content c = content.get(key);
230 long systime = System.currentTimeMillis();
233 // Don't check every hit... only after certain time value
234 if (c.date < systime + timeCheck) {
235 f = new File(fileName);
236 if (f.lastModified()>c.date) {
242 if (logTarget!=null) {
243 logTarget.log("File Read: ",key);
247 f = new File(fileName);
251 if (f.isDirectory()) {
253 c = new DirectoryContent(f,isRoot);
255 if (f.length() > maxItemSize) {
256 c = new DirectFileContent(f);
259 c = new CachedContent(f);
260 cacheMe = checkInterval>0;
263 if (mediaType==null) { // determine from file Ending
264 int idx = key.lastIndexOf('.');
265 String subkey = key.substring(++idx);
266 if ((c.contentType = idx<0?null:typeMap.get(subkey))==null) {
267 // if nothing else, just set to default type...
268 c.contentType = "application/octet-stream";
270 c.attachmentOnly = attachOnly.contains(subkey);
272 c.contentType=mediaType;
273 c.attachmentOnly = false;
276 c.date = f.lastModified();
286 if (logTarget!=null)logTarget.log("Cache Read: ",key);
295 public void invalidate(String key) {
299 private static final Content NULL=new Content() {
302 public void setHeader(HttpServletResponse resp) {
303 resp.setStatus(404/*NOT_FOUND_404*/);
304 resp.setHeader("Content-type","text/plain");
308 public void write(Writer writer) throws IOException {
312 public void write(OutputStream os) throws IOException {
317 private static abstract class Content {
318 private long date; // date of the actual artifact (i.e. File modified date)
319 private long access; // last accessed
321 protected String contentType;
322 protected boolean attachmentOnly;
324 public void setHeader(HttpServletResponse resp) {
325 resp.setStatus(200/*OK_200*/);
326 resp.setHeader("Content-Type",contentType);
327 resp.setHeader("Cache-Control", MAX_AGE);
330 public abstract void write(Writer writer) throws IOException;
331 public abstract void write(OutputStream os) throws IOException;
335 private static class DirectFileContent extends Content {
337 public DirectFileContent(File f) {
341 public String toString() {
342 return file.getName();
345 public void write(Writer writer) throws IOException {
346 FileReader fr = new FileReader(file);
347 char[] buff = new char[1024];
350 while ((read = fr.read(buff,0,1024))>=0) {
351 writer.write(buff,0,read);
358 public void write(OutputStream os) throws IOException {
359 FileInputStream fis = new FileInputStream(file);
360 byte[] buff = new byte[1024];
363 while ((read = fis.read(buff,0,1024))>=0) {
364 os.write(buff,0,read);
372 private static class DirectoryContent extends Content {
373 private static final Pattern A_NUMBER = Pattern.compile("\\d");
374 private static final String H1 = "<html><head><title>AAF Fileserver</title></head><body><h1>AAF Fileserver</h1><h2>";
375 private static final String H2 = "</h2><ul>\n";
376 private static final String F = "\n</ul></body></html>";
377 private File[] files;
379 private boolean notRoot;
381 public DirectoryContent(File directory, boolean isRoot) {
384 files = directory.listFiles();
385 Arrays.sort(files,new Comparator<File>() {
387 public int compare(File f1, File f2) {
388 // See if there are Numbers in the name
389 Matcher m1 = A_NUMBER.matcher(f1.getName());
390 Matcher m2 = A_NUMBER.matcher(f2.getName());
391 if (m1.find() && m2.find()) {
392 // if numbers, are the numbers in the same start position
396 // If same start position and the text is the same, then reverse sort
397 if (i1==i2 && f1.getName().startsWith(f2.getName().substring(0,i1))) {
398 // reverse sort files that start similarly, but have numbers in them
399 return f2.compareTo(f1);
402 return f1.compareTo(f2);
406 name = directory.getName();
407 attachmentOnly = false;
408 contentType = "text/html";
413 public void write(Writer w) throws IOException {
417 for (File f : files) {
418 w.append("<li><a href=\"");
423 w.append(f.getName());
425 w.append(f.getName());
426 w.append("</a></li>\n");
433 public void write(OutputStream os) throws IOException {
434 write(new OutputStreamWriter(os));
439 private static class CachedContent extends Content {
442 private char[] cdata;
444 public CachedContent(File f) throws IOException {
446 ByteBuffer bb = ByteBuffer.allocate((int)f.length());
447 FileInputStream fis = new FileInputStream(f);
449 fis.getChannel().read(bb);
459 public String toString() {
460 return Arrays.toString(data);
463 public void write(Writer writer) throws IOException {
465 // do the String Transformation once, and only if actually used
467 cdata = new char[end];
468 new String(data).getChars(0, end, cdata, 0);
471 writer.write(cdata,0,end);
473 public void write(OutputStream os) throws IOException {
474 os.write(data,0,end);
479 public void setEnv(LogTarget env) {
484 * Cleanup thread to remove older items if max Cache is reached.
488 private static class Cleanup extends TimerTask {
490 private NavigableMap<String, Content> content;
492 public Cleanup(NavigableMap<String, Content> content, int size) {
494 this.content = content;
497 private class Comp implements Comparable<Comp> {
498 public Map.Entry<String, Content> entry;
500 public Comp(Map.Entry<String, Content> en) {
505 public int compareTo(Comp o) {
506 return (int)(entry.getValue().access-o.entry.getValue().access);
510 @SuppressWarnings("unchecked")
513 int size = content.size();
515 ArrayList<Comp> scont = new ArrayList<>(size);
516 Object[] entries = content.entrySet().toArray();
517 for (int i=0;i<size;++i) {
518 scont.add(i, new Comp((Map.Entry<String,Content>)entries[i]));
520 Collections.sort(scont);
521 int end = size - ((maxSize/4)*3); // reduce to 3/4 of max size
522 //System.out.println("------ Cleanup Cycle ------ " + new Date().toString() + " -------");
523 for (int i=0;i<end;++i) {
524 Entry<String, Content> entry = scont.get(i).entry;
525 content.remove(entry.getKey());
526 //System.out.println("removed Cache Item " + entry.getKey() + "/" + new Date(entry.getValue().access).toString());
528 // for (int i=end;i<size;++i) {
529 // Entry<String, Content> entry = scont.get(i).entry;
530 // //System.out.println("remaining Cache Item " + entry.getKey() + "/" + new Date(entry.getValue().access).toString());