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 java.io.File; 027import java.util.HashSet; 028import java.util.Locale; 029import java.util.Set; 030 031import javax.servlet.ServletRequest; 032import javax.servlet.jsp.PageContext; 033 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037import dk.netarkivet.common.exceptions.ArgumentNotValid; 038import dk.netarkivet.common.exceptions.ForwardedToErrorPage; 039import dk.netarkivet.common.utils.I18n; 040import dk.netarkivet.common.webinterface.HTMLUtils; 041import dk.netarkivet.harvester.datamodel.HarvestDefinitionDAO; 042import dk.netarkivet.harvester.datamodel.PartialHarvest; 043import dk.netarkivet.harvester.datamodel.TemplateDAO; 044 045/** 046 * Contains utility methods for supporting event harvest GUI. 047 */ 048public final class EventHarvestUtil { 049 050 //static final Log log = LogFactory.getLog(EventHarvestUtil.class.getName()); 051 static final Logger log = LoggerFactory.getLogger(EventHarvestUtil.class); 052 053 /** 054 * Private Constructor. Instances are not meaningful. 055 */ 056 private EventHarvestUtil() { 057 } 058 059 /** 060 * Adds a bunch of configurations to a given PartialHarvest. For full definitions of the parameters, see 061 * Definitions-add-event-seeds.jsp. For each seed in the list, the following steps are taken: 1) The domain is 062 * parsed out of the seed. If no such domain is known, it is created with the usual defaults. 2) For each domain, a 063 * configuration with the name <harvestDefinition>_<orderTemplate>_<maxBytes>Bytes is created 064 * unless it already exists. The configuration uses orderTemplate, and the specified maxBytes. If maxBytes is 065 * unspecified, its default value is used. The configuration is added to the harvest specified by the 066 * harvestDefinition argument. 3) For each domain, a seedlist with the name 067 * <harvestDefinition>_<orderTemplate>_<maxBytes>Bytes is created if it does not already exist and 068 * the given url is added to it. This seedlist is the only seedlist associated with the configuration of the same 069 * name. 070 * 071 * @param context the current JSP context 072 * @param i18n the translation information to use in this context 073 * @param eventHarvestName The name of the partial harvest to which these seeds are to be added 074 * @throws ForwardedToErrorPage If maxBytes is not a number, or if any of the seeds is badly formatted such that no 075 * domain name can be parsed from it, or if orderTemplate is not given or unknown. 076 */ 077 public static void addConfigurations(PageContext context, I18n i18n, String eventHarvestName) { 078 ArgumentNotValid.checkNotNull(context, "PageContext context"); 079 ArgumentNotValid.checkNotNull(i18n, "I18n i18n"); 080 ArgumentNotValid.checkNotNull(eventHarvestName, "String eventHarvestName"); 081 082 HTMLUtils.forwardOnMissingParameter(context, Constants.SEEDS_PARAM); 083 ServletRequest request = context.getRequest(); 084 085 // If no seeds are specified, just return 086 String seeds = request.getParameter(Constants.SEEDS_PARAM); 087 if (seeds == null || seeds.trim().length() == 0) { 088 return; 089 } 090 // split the seeds up into individual seeds 091 // Note: Matches any sort of newline (unix/mac/dos), but won't get empty 092 // lines, which is fine for this purpose 093 094 Set<String> seedSet = new HashSet<String>(); 095 for (String seed : seeds.split("[\n\r]+")) { 096 seedSet.add(seed); 097 } 098 099 HTMLUtils.forwardOnEmptyParameter(context, Constants.ORDER_TEMPLATE_PARAM); 100 String orderTemplate = request.getParameter(Constants.ORDER_TEMPLATE_PARAM); 101 // Check that order template exists 102 if (!TemplateDAO.getInstance().exists(orderTemplate)) { 103 HTMLUtils.forwardWithErrorMessage(context, i18n, "errormsg;harvest.template.0.does.not.exist", 104 orderTemplate); 105 throw new ForwardedToErrorPage("The orderTemplate with name '" + orderTemplate + "' does not exist!"); 106 } 107 108 // Check that numerical parameters are meaningful and replace null or 109 // empty with default values 110 long maxBytes = HTMLUtils.parseOptionalLong(context, Constants.MAX_BYTES_PARAM, 111 dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_BYTES); 112 long maxObjectsL = HTMLUtils.parseOptionalLong(context, Constants.MAX_OBJECTS_PARAM, 113 dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_OBJECTS); 114 int maxObjects = (int) maxObjectsL; 115 // All parameters are valid, so call method 116 try { 117 PartialHarvest eventHarvest = (PartialHarvest) HarvestDefinitionDAO.getInstance().getHarvestDefinition( 118 eventHarvestName); 119 eventHarvest.addSeeds(seedSet, orderTemplate, maxBytes, maxObjects); 120 } catch (Exception e) { 121 HTMLUtils.forwardWithErrorMessage(context, i18n, "errormsg;error.adding.seeds.to.0", eventHarvestName, e); 122 throw new ForwardedToErrorPage("Error while adding seeds", e); 123 } 124 } 125 126 /** 127 * Add configurations to an existing selective harvest. 128 * 129 * @param context The current JSP context 130 * @param i18n The translation information to use in this context 131 * @param eventHarvestName The name of the partial harvest to which these seeds are to be added 132 * @param seeds The seeds as a file (each seed on a separate line) 133 * @param maxbytesString The given maxbytes as a string 134 * @param maxobjectsString The given maxobjects as a string 135 * @param maxrateString The given maxrate as a string (currently not used) 136 * @param ordertemplate The name of the ordertemplate to use 137 */ 138 public static void addConfigurationsFromSeedsFile(PageContext context, I18n i18n, String eventHarvestName, 139 File seeds, String maxbytesString, String maxobjectsString, String maxrateString, String ordertemplate) { 140 ArgumentNotValid.checkNotNull(context, "PageContext context"); 141 ArgumentNotValid.checkNotNull(i18n, "I18n i18n"); 142 ArgumentNotValid.checkNotNullOrEmpty(eventHarvestName, "String eventHarvestName"); 143 ArgumentNotValid.checkNotNull(seeds, "String seeds"); 144 ArgumentNotValid.checkNotNull(ordertemplate, "String ordertemplate"); 145 146 long maxBytes = 0L; 147 int maxObjects = 0; 148 149 try { 150 if (maxbytesString == null) { 151 maxBytes = dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_BYTES; 152 } else { 153 Locale loc = HTMLUtils.getLocaleObject(context); 154 maxBytes = HTMLUtils.parseLong(loc, maxbytesString, Constants.MAX_BYTES_PARAM, 155 dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_BYTES); 156 } 157 158 if (maxobjectsString == null) { 159 maxObjects = (int) dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_OBJECTS; 160 } else { 161 Locale loc = HTMLUtils.getLocaleObject(context); 162 long maxObjectsL = HTMLUtils.parseLong(loc, maxobjectsString, Constants.MAX_OBJECTS_PARAM, 163 dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_OBJECTS); 164 maxObjects = (int) maxObjectsL; 165 } 166 167 } catch (Exception e) { 168 HTMLUtils.forwardWithErrorMessage(context, i18n, "Exception.thrown.when.adding.seeds", e); 169 return; 170 } 171 // Check that order template exists 172 if (!TemplateDAO.getInstance().exists(ordertemplate)) { 173 HTMLUtils.forwardWithErrorMessage(context, i18n, "errormsg;harvest.template.0.does.not.exist", 174 ordertemplate); 175 throw new ForwardedToErrorPage("The orderTemplate with name '" + ordertemplate + "' does not exist!"); 176 } 177 178 // All parameters are valid, so call method 179 try { 180 PartialHarvest eventHarvest = (PartialHarvest) HarvestDefinitionDAO.getInstance().getHarvestDefinition( 181 eventHarvestName); 182 eventHarvest.addSeedsFromFile(seeds, ordertemplate, maxBytes, maxObjects); 183 } catch (Exception e) { 184 HTMLUtils 185 .forwardWithErrorMessage(context, i18n, "errormsg;error.adding.seeds.to.0", e, eventHarvestName, e); 186 throw new ForwardedToErrorPage("Error while adding seeds", e); 187 } 188 } 189}