001/* 002 * #%L 003 * Netarchivesuite - heritrix 3 monitor 004 * %% 005 * Copyright (C) 2005 - 2018 The Royal Danish Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023 024package dk.netarkivet.heritrix3.monitor.resources; 025 026import java.io.BufferedReader; 027import java.io.IOException; 028import java.io.StringReader; 029import java.util.LinkedList; 030import java.util.List; 031import java.util.Locale; 032 033import javax.servlet.ServletContext; 034import javax.servlet.ServletOutputStream; 035import javax.servlet.http.HttpServletRequest; 036import javax.servlet.http.HttpServletResponse; 037 038import com.antiaction.common.filter.Caching; 039import com.antiaction.common.templateengine.TemplateBuilderFactory; 040 041import dk.netarkivet.heritrix3.monitor.NASEnvironment; 042import dk.netarkivet.heritrix3.monitor.NASEnvironment.StringMatcher; 043import dk.netarkivet.heritrix3.monitor.NASUser; 044import dk.netarkivet.heritrix3.monitor.ResourceAbstract; 045import dk.netarkivet.heritrix3.monitor.ResourceManagerAbstract; 046import dk.netarkivet.heritrix3.monitor.HttpLocaleHandler.HttpLocale; 047 048public class ConfigResource implements ResourceAbstract { 049 050 private NASEnvironment environment; 051 052 protected int R_CONFIG = -1; 053 054 @Override 055 public void resources_init(NASEnvironment environment) { 056 this.environment = environment; 057 } 058 059 @Override 060 public void resources_add(ResourceManagerAbstract resourceManager) { 061 R_CONFIG = resourceManager.resource_add(this, "/config/", false); 062 } 063 064 @Override 065 public void resource_service(ServletContext servletContext, NASUser nas_user, HttpServletRequest req, HttpServletResponse resp, HttpLocale httpLocale, int resource_id, List<Integer> numerics, String pathInfo) throws IOException { 066 if (NASEnvironment.contextPath == null) { 067 NASEnvironment.contextPath = req.getContextPath(); 068 } 069 if (NASEnvironment.servicePath == null) { 070 NASEnvironment.servicePath = req.getContextPath() + req.getServletPath() + "/"; 071 } 072 String method = req.getMethod().toUpperCase(); 073 if (resource_id == R_CONFIG) { 074 if ("GET".equals(method) || "POST".equals(method)) { 075 config(req, resp, httpLocale, numerics); 076 } 077 } 078 } 079 080 public void config(HttpServletRequest req, HttpServletResponse resp, HttpLocale httpLocale, List<Integer> numerics) throws IOException { 081 Locale locale = httpLocale.locale; 082 resp.setContentType("text/html; charset=UTF-8"); 083 ServletOutputStream out = resp.getOutputStream(); 084 Caching.caching_disable_headers(resp); 085 086 TemplateBuilderFactory<ConfigTemplateBuilder> configTplBuilderFactory = TemplateBuilderFactory.getInstance(environment.templateMaster, "h3config.tpl", "UTF-8", ConfigTemplateBuilder.class); 087 ConfigTemplateBuilder configTplBuilder = configTplBuilderFactory.getTemplateBuilder(); 088 089 StringBuilder sb = new StringBuilder(); 090 StringBuilder enabledhostsSb = new StringBuilder(); 091 List<String> invalidPatternsList = new LinkedList<String>(); 092 093 String method = req.getMethod().toUpperCase(); 094 if ("POST".equals(method)) { 095 String enabledhostsStr = req.getParameter("enabledhosts"); 096 String tmpStr; 097 if (enabledhostsStr != null) { 098 BufferedReader reader = new BufferedReader(new StringReader(enabledhostsStr)); 099 List<String> enabledhostsList = new LinkedList<String>(); 100 while ((tmpStr = reader.readLine()) != null) { 101 enabledhostsList.add(tmpStr); 102 } 103 reader.close(); 104 environment.replaceH3HostnamePortRegexList(enabledhostsList, invalidPatternsList); 105 environment.h3JobMonitorThread.updateH3HostnamePortFilter(); 106 } 107 } 108 109 synchronized (environment.h3HostPortAllowRegexList) { 110 StringMatcher stringMatcher; 111 for (int i=0; i<environment.h3HostPortAllowRegexList.size(); ++i) { 112 stringMatcher = environment.h3HostPortAllowRegexList.get(i); 113 enabledhostsSb.append(stringMatcher.str); 114 enabledhostsSb.append("\n"); 115 } 116 } 117 118 if (invalidPatternsList.size() > 0) { 119 sb.append("<h5>"); 120 sb.append(environment.I18N.getString(locale, "h5.invalid.regexes")); 121 sb.append(":</h5>\n"); 122 for (int i=0; i<invalidPatternsList.size(); ++i) { 123 sb.append(invalidPatternsList.get(i)); 124 sb.append("<br />\n"); 125 } 126 } 127 128 synchronized (environment.h3JobMonitorThread.h3HostnamePortEnabledList) { 129 sb.append("<h5>"); 130 sb.append(environment.I18N.getString(locale, "running.jobs.monitor.crawllog.cache.enabled.for")); 131 sb.append(":</h5>\n"); 132 if (environment.h3JobMonitorThread.h3HostnamePortEnabledList.size() > 0) { 133 for (int i=0; i<environment.h3JobMonitorThread.h3HostnamePortEnabledList.size(); ++i) { 134 sb.append(environment.h3JobMonitorThread.h3HostnamePortEnabledList.get(i)); 135 sb.append("<br />\n"); 136 } 137 } else { 138 sb.append("<p>"); 139 sb.append(environment.I18N.getString(locale, "running.jobs.monitor.no.hosts.enabled")); 140 sb.append("</p>\n"); 141 } 142 } 143 sb.append("<br />\n"); 144 synchronized (environment.h3JobMonitorThread.h3HostnamePortDisabledList) { 145 sb.append("<h5>"); 146 sb.append(environment.I18N.getString(locale, "running.jobs.monitor.crawllog.cache.disabled.for")); 147 sb.append("</h5>\n"); 148 if (environment.h3JobMonitorThread.h3HostnamePortDisabledList.size() > 0) { 149 for (int i=0; i<environment.h3JobMonitorThread.h3HostnamePortDisabledList.size(); ++i) { 150 sb.append(environment.h3JobMonitorThread.h3HostnamePortDisabledList.get(i)); 151 sb.append("<br />\n"); 152 } 153 } else { 154 sb.append("<p>"); 155 sb.append(environment.I18N.getString(locale, "running.jobs.monitor.no.hosts.disable")); 156 sb.append("</p>\n"); 157 } 158 } 159 160 StringBuilder menuSb = configTplBuilder.buildMenu(new StringBuilder(), req, locale, null); 161 162 configTplBuilder.insertContent("H3 Remote Access Config", menuSb.toString(), httpLocale.generateLanguageLinks(), "H3 Remote Access Config", 163 enabledhostsSb.toString(), sb.toString(), "").write(out); 164 165 out.flush(); 166 out.close(); 167 } 168 169}