X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Frserv%2FCachingFileAccess.java;h=cdda50db48eba07714994cd2a0299a7721a01c09;hb=HEAD;hp=53511b403a649fc9cecfd6b7b8f9f9df8aa86a0a;hpb=4b5a7d721d994a49057e9bfb403c7bff1b376660;p=aaf%2Fauthz.git diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/rserv/CachingFileAccess.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/rserv/CachingFileAccess.java index 53511b40..cdda50db 100644 --- a/auth/auth-core/src/main/java/org/onap/aaf/auth/rserv/CachingFileAccess.java +++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/rserv/CachingFileAccess.java @@ -7,9 +7,9 @@ * 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. @@ -24,15 +24,12 @@ package org.onap.aaf.auth.rserv; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -52,45 +49,43 @@ import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.onap.aaf.misc.env.Env; import org.onap.aaf.misc.env.EnvJAXB; import org.onap.aaf.misc.env.LogTarget; import org.onap.aaf.misc.env.Store; -import org.onap.aaf.misc.env.TimeTaken; import org.onap.aaf.misc.env.Trans; /* * CachingFileAccess - * + * * Author: Jonathan Gathman, Gathsys 2010 - * + * */ public class CachingFileAccess extends HttpCode { public static void setEnv(Store store, String[] args) { - for(int i=0;i typeMap; private final NavigableMap content; private final Set attachOnly; - public final static String CFA_WEB_PATH = "aaf_cfa_web_path"; + public static final String CFA_WEB_PATH = "aaf_cfa_web_path"; // when to re-validate from file // Re validating means comparing the Timestamp on the disk, and seeing it has changed. Cache is not marked - // dirty unless file has changed, but it still makes File IO, which for some kinds of cached data, i.e. + // dirty unless file has changed, but it still makes File IO, which for some kinds of cached data, i.e. // deployed GUI elements is unnecessary, and wastes time. // This parameter exists to cover the cases where data can be more volatile, so the user can choose how often the // File IO will be accessed, based on probability of change. "0", of course, means, check every time. - private final static String CFA_CACHE_CHECK_INTERVAL = "aaf_cfa_cache_check_interval"; - private final static String CFA_MAX_SIZE = "aaf_cfa_max_size"; // Cache size limit - private final static String CFA_CLEAR_COMMAND = "aaf_cfa_clear_command"; + private static final String CFA_CACHE_CHECK_INTERVAL = "aaf_cfa_cache_check_interval"; + private static final String CFA_MAX_SIZE = "aaf_cfa_max_size"; // Cache size limit + private static final String CFA_CLEAR_COMMAND = "aaf_cfa_clear_command"; // Note: can be null without a problem, but included // to tie in with existing Logging. @@ -98,13 +93,15 @@ public class CachingFileAccess extends HttpCode extends HttpCode extends HttpCode2 && slash>=0 && key.substring(0,slash).equals(clearCommand)) { resp.setHeader("Content-Type",typeMap.get("txt")); - if("clear".equals(cmd)) { + if ("clear".equals(key.substring(slash+1))) { content.clear(); resp.setStatus(200/*HttpStatus.OK_200*/); } else { @@ -170,8 +179,8 @@ public class CachingFileAccess extends HttpCode0?key+'/'+cmd:key, null, checkInterval); - if(c.attachmentOnly) { + Content c = load(logT , webPath,key, null, checkInterval); + if (c.attachmentOnly) { resp.setHeader("Content-disposition", "attachment"); } c.setHeader(resp); @@ -181,14 +190,14 @@ public class CachingFileAccess extends HttpCode extends HttpCode extends HttpCode extends HttpCodec.date) { + if (f.lastModified()>c.date) { c=null; } } } - if(c==null) { - if(logTarget!=null) { + if (c==null) { + if (logTarget!=null) { logTarget.log("File Read: ",key); } - - if(f==null){ + + if (f==null){ f = new File(fileName); } boolean cacheMe; - if(f.exists()) { - if(f.isDirectory()) { + if (f.exists()) { + if (f.isDirectory()) { cacheMe = false; c = new DirectoryContent(f,isRoot); } else { - if(f.length() > maxItemSize) { + if (f.length() > maxItemSize) { c = new DirectFileContent(f); cacheMe = false; } else { c = new CachedContent(f); cacheMe = checkInterval>0; } - - if(mediaType==null) { // determine from file Ending + + if (mediaType==null) { // determine from file Ending int idx = key.lastIndexOf('.'); String subkey = key.substring(++idx); - if((c.contentType = idx<0?null:typeMap.get(subkey))==null) { + if ((c.contentType = idx<0?null:typeMap.get(subkey))==null) { // if nothing else, just set to default type... c.contentType = "application/octet-stream"; } @@ -272,10 +281,10 @@ public class CachingFileAccess extends HttpCode extends HttpCode extends HttpCode=0) { + while ((read = fr.read(buff,0,1024))>=0) { writer.write(buff,0,read); } } finally { @@ -387,7 +369,7 @@ public class CachingFileAccess extends HttpCode=0) { + while ((read = fis.read(buff,0,1024))>=0) { os.write(buff,0,read); } } finally { @@ -407,7 +389,7 @@ public class CachingFileAccess extends HttpCode() { @Override @@ -415,27 +397,27 @@ public class CachingFileAccess extends HttpCode extends HttpCode extends HttpCode extends HttpCode extends HttpCode content; - + public Cleanup(NavigableMap content, int size) { maxSize = size; this.content = content; } - + private class Comp implements Comparable { public Map.Entry entry; - + public Comp(Map.Entry en) { entry = en; } - + @Override public int compareTo(Comp o) { return (int)(entry.getValue().access-o.entry.getValue().access); } - + } @SuppressWarnings("unchecked") @Override public void run() { int size = content.size(); - if(size>maxSize) { + if (size>maxSize) { ArrayList scont = new ArrayList<>(size); Object[] entries = content.entrySet().toArray(); - for(int i=0;i)entries[i])); } Collections.sort(scont); int end = size - ((maxSize/4)*3); // reduce to 3/4 of max size - //System.out.println("------ Cleanup Cycle ------ " + new Date().toString() + " -------"); - for(int i=0;i entry = scont.get(i).entry; content.remove(entry.getKey()); - //System.out.println("removed Cache Item " + entry.getKey() + "/" + new Date(entry.getValue().access).toString()); } -// for(int i=end;i entry = scont.get(i).entry; -// //System.out.println("remaining Cache Item " + entry.getKey() + "/" + new Date(entry.getValue().access).toString()); -// } } } }