001/* 002 * #%L 003 * Netarchivesuite - harvester 004 * %% 005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University 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.harvester.webinterface; 025 026//import java.io.IOException; 027import java.io.OutputStream; 028import java.util.Set; 029 030import javax.servlet.http.HttpServletRequest; 031import javax.servlet.http.HttpServletResponse; 032import javax.servlet.jsp.PageContext; 033 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037import dk.netarkivet.common.exceptions.ForwardedToErrorPage; 038import dk.netarkivet.common.utils.I18n; 039import dk.netarkivet.common.webinterface.HTMLUtils; 040import dk.netarkivet.harvester.datamodel.GlobalCrawlerTrapList; 041import dk.netarkivet.harvester.datamodel.GlobalCrawlerTrapListDAO; 042import dk.netarkivet.harvester.datamodel.GlobalCrawlerTrapListDBDAO; 043 044/** 045 * Class to read and return a global crawler trap list to a web request. 046 */ 047 048public class TrapReadAction extends TrapAction { 049 050 protected static final Logger log = LoggerFactory.getLogger(TrapReadAction.class); 051 052 @Override 053 protected void doAction(PageContext context, I18n i18n) { 054 HttpServletRequest request = (HttpServletRequest) context.getRequest(); 055 int trapId = Integer.parseInt(request.getParameter(Constants.TRAP_ID)); 056 String contentType = request.getParameter(Constants.TRAP_CONTENT_TYPE); 057 HttpServletResponse response = (HttpServletResponse) context.getResponse(); 058 GlobalCrawlerTrapListDAO dao = GlobalCrawlerTrapListDBDAO.getInstance(); 059 GlobalCrawlerTrapList trapList = dao.read(trapId); 060 response.setHeader("Content-Type", contentType); 061 if (contentType.startsWith("binary")) { 062 response.setHeader("Content-Disposition", "Attachment; filename=" + trapList.getName()); 063 } 064 OutputStream out = null; 065 Set<String> traps= trapList.getTraps(); 066 int trapSize = traps.size(); 067 try { 068 out = response.getOutputStream(); 069 int count=0; 070 for (String trap : trapList.getTraps()) { 071 count++; 072 log.trace("Writing trap {}/{} to output destination", count, trapSize); 073 out.write((trap + "\n").getBytes()); 074 } 075 out.close(); 076 } catch (Throwable e) { 077 log.warn("error occurred", e); 078 HTMLUtils.forwardWithErrorMessage(context, i18n, e, ""); 079 throw new ForwardedToErrorPage("Error in retrieving trap list", e); 080 } 081 log.info("All {} traps in list {} written to output destination successfully", trapSize, trapList.getName()); 082 } 083}