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.netarchivesuite.heritrix3wrapper.StreamResult; 036import org.netarchivesuite.heritrix3wrapper.jaxb.Job; 037import org.netarchivesuite.heritrix3wrapper.jaxb.Report; 038 039import com.antiaction.common.filter.Caching; 040import com.antiaction.common.templateengine.TemplateBuilderFactory; 041 042import dk.netarkivet.common.utils.Settings; 043import dk.netarkivet.harvester.HarvesterSettings; 044import dk.netarkivet.heritrix3.monitor.Heritrix3JobMonitor; 045import dk.netarkivet.heritrix3.monitor.NASEnvironment; 046import dk.netarkivet.heritrix3.monitor.NASUser; 047import dk.netarkivet.heritrix3.monitor.ResourceAbstract; 048import dk.netarkivet.heritrix3.monitor.ResourceManagerAbstract; 049import dk.netarkivet.heritrix3.monitor.HttpLocaleHandler.HttpLocale; 050 051public class H3ReportResource implements ResourceAbstract { 052 053 private NASEnvironment environment; 054 055 protected int R_REPORT = -1; 056 057 @Override 058 public void resources_init(NASEnvironment environment) { 059 this.environment = environment; 060 } 061 062 @Override 063 public void resources_add(ResourceManagerAbstract resourceManager) { 064 R_REPORT = resourceManager.resource_add(this, "/job/<numeric>/report/", false); 065 } 066 067 @Override 068 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 { 069 if (NASEnvironment.contextPath == null) { 070 NASEnvironment.contextPath = req.getContextPath(); 071 } 072 if (NASEnvironment.servicePath == null) { 073 NASEnvironment.servicePath = req.getContextPath() + req.getServletPath() + "/"; 074 } 075 String method = req.getMethod().toUpperCase(); 076 if (resource_id == R_REPORT) { 077 if ("GET".equals(method)) { 078 report(req, resp, httpLocale, numerics); 079 } 080 } 081 } 082 083 public void report(HttpServletRequest req, HttpServletResponse resp, HttpLocale httpLocale, List<Integer> numerics) throws IOException { 084 Locale locale = httpLocale.locale; 085 resp.setContentType("text/html; charset=UTF-8"); 086 ServletOutputStream out = resp.getOutputStream(); 087 Caching.caching_disable_headers(resp); 088 089 TemplateBuilderFactory<MasterTemplateBuilder> masterTplBuilderFactory = TemplateBuilderFactory.getInstance(environment.templateMaster, "master.tpl", "UTF-8", MasterTemplateBuilder.class); 090 MasterTemplateBuilder masterTplBuilder = masterTplBuilderFactory.getTemplateBuilder(); 091 092 StringBuilder sb = new StringBuilder(); 093 094 String reportStr = req.getParameter("report"); 095 096 long jobId = numerics.get(0); 097 Heritrix3JobMonitor h3Job = environment.h3JobMonitorThread.getRunningH3Job(jobId); 098 Job job; 099 100 if (h3Job != null && h3Job.isReady()) { 101 if (h3Job.jobResult != null && h3Job.jobResult.job != null) { 102 job = h3Job.jobResult.job; 103 Report report; 104 for (int i=0; i<job.reports.size(); ++i) { 105 report = job.reports.get(i); 106 if (i > 0) { 107 sb.append(" "); 108 } 109 sb.append("<a href=\""); 110 sb.append(NASEnvironment.servicePath); 111 sb.append("job/"); 112 sb.append(h3Job.jobId); 113 sb.append("/report/?report="); 114 sb.append(report.className); 115 sb.append("\" class=\"btn btn-default\">"); 116 sb.append(report.shortName); 117 sb.append("</a>"); 118 } 119 if (reportStr != null && reportStr.length() > 0) { 120 sb.append("<br />\n"); 121 sb.append("<h5>"); 122 sb.append(reportStr); 123 sb.append("</h5>"); 124 sb.append("<pre>"); 125 StreamResult anypathResult = h3Job.h3wrapper.path("job/" + h3Job.jobname + "/report/" + reportStr, null, null); 126 byte[] tmpBuf = new byte[8192]; 127 int read; 128 try { 129 while ((read = anypathResult.in.read(tmpBuf)) != -1) { 130 sb.append(new String(tmpBuf, 0, read)); 131 } 132 anypathResult.close(); 133 } catch (IOException e) { 134 e.printStackTrace(); 135 } 136 sb.append("</pre>"); 137 } 138 } 139 } 140 141 StringBuilder menuSb = masterTplBuilder.buildMenu(new StringBuilder(), req, locale, h3Job); 142 143 masterTplBuilder.insertContent("Job "+ jobId + " Reports", menuSb.toString(), httpLocale.generateLanguageLinks(), "Job " + jobId + " Reports", sb.toString(), 144 "<meta http-equiv=\"refresh\" content=\""+Settings.get(HarvesterSettings.HARVEST_MONITOR_REFRESH_INTERVAL)+"\"/>\n").write(out); 145 146 out.flush(); 147 out.close(); 148 } 149 150}