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.IOException; 027import java.util.List; 028import java.util.Locale; 029 030import javax.servlet.ServletContext; 031import javax.servlet.ServletOutputStream; 032import javax.servlet.http.HttpServletRequest; 033import javax.servlet.http.HttpServletResponse; 034 035import org.apache.commons.lang.StringEscapeUtils; 036import org.netarchivesuite.heritrix3wrapper.ScriptResult; 037 038import com.antiaction.common.filter.Caching; 039import com.antiaction.common.templateengine.TemplateBuilderFactory; 040 041import dk.netarkivet.heritrix3.monitor.Heritrix3JobMonitor; 042import dk.netarkivet.heritrix3.monitor.NASEnvironment; 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 H3ScriptResource implements ResourceAbstract { 049 050 private NASEnvironment environment; 051 052 protected int R_SCRIPT = -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_SCRIPT = resourceManager.resource_add(this, "/job/<numeric>/script/", 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_SCRIPT) { 074 if ("GET".equals(method) || "POST".equals(method)) { 075 script(req, resp, httpLocale, numerics); 076 } 077 } 078 } 079 080 public void script(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<H3ScriptTemplateBuilder> scriptTplBuilderFactory = TemplateBuilderFactory.getInstance(environment.templateMaster, "h3script.tpl", "UTF-8", H3ScriptTemplateBuilder.class); 087 H3ScriptTemplateBuilder scriptTplBuilder = scriptTplBuilderFactory.getTemplateBuilder(); 088 089 String engineStr = req.getParameter("engine"); 090 String scriptStr = req.getParameter("script"); 091 if (scriptStr == null) { 092 scriptStr = ""; 093 } 094 095 StringBuilder sb = new StringBuilder(); 096 097 long jobId = numerics.get(0); 098 Heritrix3JobMonitor h3Job = environment.h3JobMonitorThread.getRunningH3Job(jobId); 099 100 if (h3Job != null && h3Job.isReady()) { 101 if (engineStr != null && engineStr.length() > 0 && scriptStr != null && scriptStr.length() > 0) { 102 ScriptResult scriptResult = h3Job.h3wrapper.ExecuteShellScriptInJob(h3Job.jobResult.job.shortName, engineStr, scriptStr); 103 //System.out.println(new String(scriptResult.response, "UTF-8")); 104 if (scriptResult != null && scriptResult.script != null) { 105 if (scriptResult.script.failure) { 106 if (scriptResult.script.stackTrace != null) { 107 sb.append("<h5>Script failed with the following stacktrace:</h5>\n"); 108 sb.append("<pre>\n"); 109 sb.append(StringEscapeUtils.escapeHtml(scriptResult.script.stackTrace)); 110 sb.append("</pre>\n"); 111 } else if (scriptResult.script.exception != null) { 112 sb.append("<h5>Script failed with the following message:</h5>\n"); 113 sb.append("<pre>\n"); 114 sb.append(StringEscapeUtils.escapeHtml(scriptResult.script.exception)); 115 sb.append("</pre>\n"); 116 } else { 117 sb.append("<b>Unknown script failure!</b></br>\n"); 118 } 119 sb.append("<h5>Raw script result Xml:</h5>\n"); 120 sb.append("<pre>"); 121 sb.append(StringEscapeUtils.escapeHtml(new String(scriptResult.response, "UTF-8"))); 122 sb.append("</pre>"); 123 } else { 124 if (scriptResult.script.htmlOutput != null) { 125 sb.append(scriptResult.script.htmlOutput); 126 } 127 if (scriptResult.script.rawOutput != null) { 128 sb.append("<pre>"); 129 sb.append(scriptResult.script.rawOutput); 130 sb.append("</pre>"); 131 } 132 sb.append("<pre>"); 133 sb.append(new String(scriptResult.response, "UTF-8")); 134 sb.append("</pre>"); 135 } 136 } else { 137 sb.append("<b>Script did not return any response!</b><br/>\n"); 138 } 139 } 140 } 141 142 StringBuilder menuSb = scriptTplBuilder.buildMenu(new StringBuilder(), req, locale, h3Job); 143 144 scriptTplBuilder.insertContent("Scripting console", menuSb.toString(), httpLocale.generateLanguageLinks(), "Scripting console", scriptStr, sb.toString(), "").write(out); 145 146 out.flush(); 147 out.close(); 148 } 149 150}