001package dk.netarkivet.harvester.webinterface.servlet; 002 003import java.io.File; 004import java.util.ArrayList; 005import java.util.List; 006import java.util.regex.Matcher; 007import java.util.regex.Pattern; 008 009import javax.servlet.ServletConfig; 010import javax.servlet.ServletContext; 011import javax.servlet.ServletException; 012 013import com.antiaction.common.templateengine.TemplateMaster; 014import com.antiaction.common.templateengine.login.LoginTemplateHandler; 015import com.antiaction.common.templateengine.storage.TemplateFileStorageManager; 016 017import dk.netarkivet.common.utils.Settings; 018import dk.netarkivet.harvester.HarvesterSettings; 019 020public class NASEnvironment { 021 022 /** servletConfig. */ 023 protected ServletConfig servletConfig = null; 024 025 protected TemplateMaster templateMaster = null; 026 027 protected String login_template_name = null; 028 029 protected LoginTemplateHandler<NASUser> loginHandler = null; 030 031 public File tempPath; 032 033 public String h3AdminName; 034 035 public String h3AdminPassword; 036 037 protected Heritrix3JobMonitorThread h3JobMonitorThread; 038 039 public static String contextPath; 040 041 public static String servicePath; 042 043 public static class StringMatcher { 044 public String str; 045 public Pattern p; 046 public Matcher m; 047 } 048 049 public List<StringMatcher> h3HostPortAllowRegexList = new ArrayList<StringMatcher>(); 050 051 public NASEnvironment(ServletContext servletContext, ServletConfig theServletConfig) throws ServletException { 052 login_template_name = "login.html"; 053 054 templateMaster = TemplateMaster.getInstance("default"); 055 templateMaster.addTemplateStorage(TemplateFileStorageManager.getInstance(servletContext.getRealPath("/"), "UTF-8")); 056 057 loginHandler = new LoginTemplateHandler<NASUser>(); 058 loginHandler.templateMaster = templateMaster; 059 loginHandler.templateName = login_template_name; 060 loginHandler.title = "Webdanica - Login"; 061 loginHandler.adminPath = "/"; 062 063 try { 064 tempPath = Settings.getFile(HarvesterSettings.HERITRIX3_MONITOR_TEMP_PATH); 065 } catch (Exception e) { 066 //This is normal if tempPath is unset, so system directory is used. 067 tempPath = new File(System.getProperty("java.io.tmpdir")); 068 } 069 if (tempPath == null || !tempPath.isDirectory() || !tempPath.isDirectory()) { 070 tempPath = new File(System.getProperty("java.io.tmpdir")); 071 } 072 073 h3AdminName = Settings.get(HarvesterSettings.HERITRIX_ADMIN_NAME); 074 h3AdminPassword = Settings.get(HarvesterSettings.HERITRIX_ADMIN_PASSWORD); 075 076 this.servletConfig = theServletConfig; 077 h3JobMonitorThread = new Heritrix3JobMonitorThread(this); 078 } 079 080 public void start() { 081 h3JobMonitorThread.start(); 082 } 083 084 /** 085 * Do some cleanup. This waits for the different workflow threads to stop running. 086 */ 087 public void cleanup() { 088 servletConfig = null; 089 } 090 091 public void replaceH3HostnamePortRegexList(List<String> h3HostnamePortRegexList) { 092 String regex; 093 StringMatcher stringMatcher; 094 synchronized (h3HostPortAllowRegexList) { 095 h3HostPortAllowRegexList.clear(); 096 for (int i=0; i<h3HostnamePortRegexList.size(); ++i) { 097 regex = h3HostnamePortRegexList.get(i); 098 stringMatcher = new StringMatcher(); 099 stringMatcher.str = regex; 100 stringMatcher.p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 101 stringMatcher.m = stringMatcher.p.matcher("42"); 102 h3HostPortAllowRegexList.add(stringMatcher); 103 } 104 } 105 } 106 107 public boolean isH3HostnamePortEnabled(String h3HostnamePort) { 108 boolean bAllowed = false; 109 synchronized (h3HostPortAllowRegexList) { 110 StringMatcher stringMatcher; 111 int idx = 0; 112 while (!bAllowed && idx < h3HostPortAllowRegexList.size()) { 113 stringMatcher = h3HostPortAllowRegexList.get(idx++); 114 stringMatcher.m.reset(h3HostnamePort); 115 bAllowed = stringMatcher.m.matches(); 116 } 117 } 118 return bAllowed; 119 } 120 121}