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 026import javax.servlet.http.HttpServletRequest; 027import javax.servlet.jsp.PageContext; 028 029import org.apache.commons.fileupload.servlet.ServletFileUpload; 030 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034import dk.netarkivet.common.exceptions.ArgumentNotValid; 035import dk.netarkivet.common.exceptions.ForwardedToErrorPage; 036import dk.netarkivet.common.utils.I18n; 037import dk.netarkivet.common.webinterface.HTMLUtils; 038 039/** 040 * Abstract class representing an action to take on the collection of global crawler traps. 041 */ 042 043public abstract class TrapAction { 044 045 protected static final Logger log = LoggerFactory.getLogger(TrapAction.class); 046 /** 047 * This method processes the request to determine which action it corresponds to and passes the request along 048 * accordingly. If it is a multipart post then it is passed along to a create-or-update instance. Otherwise if no 049 * action is specified, none is taken. Otherwise the request is passed on to a specific concrete instance of this 050 * class for further processing. 051 * 052 * @param context the original servlet context of the request. 053 * @param i18n the internationalisation to be used. 054 * @throws ForwardedToErrorPage if an exception is thrown while carrying out the action. 055 */ 056 public static void processRequest(PageContext context, I18n i18n) throws ForwardedToErrorPage { 057 ArgumentNotValid.checkNotNull(context, "PageContext context"); 058 ArgumentNotValid.checkNotNull(i18n, "I18n i18n"); 059 log.debug("Processing request"); 060 HttpServletRequest request = (HttpServletRequest) context.getRequest(); 061 062 try { 063 if (ServletFileUpload.isMultipartContent(request)) { 064 TrapActionEnum.CREATE_OR_UPDATE.getTrapAction().doAction(context, i18n); 065 } else { 066 String requestType = request.getParameter(Constants.TRAP_ACTION); 067 if (requestType == null || requestType.isEmpty()) { 068 TrapActionEnum.NULL_ACTION.getTrapAction().doAction(context, i18n); 069 } else { 070 TrapActionEnum actionType = TrapActionEnum.valueOf(requestType); 071 actionType.getTrapAction().doAction(context, i18n); 072 } 073 } 074 } catch (Throwable e) { 075 log.warn("Error in Global Crawler Traps", e); 076 HTMLUtils.forwardWithErrorMessage(context, i18n, e, "errormsg;crawlertrap.action.error"); 077 throw new ForwardedToErrorPage("Error in Global Crawler Traps", e); 078 } 079 } 080 081 /** 082 * Method implementing the specific action to take. 083 * 084 * @param context the context of the servlet request triggering this action. 085 * @param i18n the internationalisation to use for presenting the results. 086 */ 087 protected abstract void doAction(PageContext context, I18n i18n); 088 089}