/
EventHarvestUtil.java
202 lines (179 loc) · 9.71 KB
/
EventHarvestUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* #%L
* Netarchivesuite - harvester
* %%
* Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
* the National Library of France and the Austrian National Library.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package dk.netarkivet.harvester.webinterface;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.PageContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import dk.netarkivet.common.exceptions.ArgumentNotValid;
import dk.netarkivet.common.exceptions.ForwardedToErrorPage;
import dk.netarkivet.common.utils.I18n;
import dk.netarkivet.common.webinterface.HTMLUtils;
import dk.netarkivet.harvester.datamodel.HarvestDefinitionDAO;
import dk.netarkivet.harvester.datamodel.PartialHarvest;
import dk.netarkivet.harvester.datamodel.TemplateDAO;
import dk.netarkivet.harvester.datamodel.eav.EAV;
/**
* Contains utility methods for supporting event harvest GUI.
*/
public final class EventHarvestUtil {
static final Logger log = LoggerFactory.getLogger(EventHarvestUtil.class);
/**
* Private Constructor. Instances are not meaningful.
*/
private EventHarvestUtil() {
}
/**
* Adds a bunch of configurations to a given PartialHarvest. For full definitions of the parameters, see
* Definitions-add-event-seeds.jsp. For each seed in the list, the following steps are taken: 1) The domain is
* parsed out of the seed. If no such domain is known, it is created with the usual defaults. 2) For each domain, a
* configuration with the name <harvestDefinition>_<orderTemplate>_<maxBytes>Bytes is created
* unless it already exists. The configuration uses orderTemplate, and the specified maxBytes. If maxBytes is
* unspecified, its default value is used. The configuration is added to the harvest specified by the
* harvestDefinition argument. 3) For each domain, a seedlist with the name
* <harvestDefinition>_<orderTemplate>_<maxBytes>Bytes is created if it does not already exist and
* the given url is added to it. This seedlist is the only seedlist associated with the configuration of the same
* name.
*
* @param context the current JSP context
* @param i18n the translation information to use in this context
* @param eventHarvestName The name of the partial harvest to which these seeds are to be added
* @throws ForwardedToErrorPage If maxBytes is not a number, or if any of the seeds is badly formatted such that no
* domain name can be parsed from it, or if orderTemplate is not given or unknown.
*/
public static void addConfigurations(PageContext context, I18n i18n, String eventHarvestName) {
ArgumentNotValid.checkNotNull(context, "PageContext context");
ArgumentNotValid.checkNotNull(i18n, "I18n i18n");
ArgumentNotValid.checkNotNull(eventHarvestName, "String eventHarvestName");
HTMLUtils.forwardOnMissingParameter(context, Constants.SEEDS_PARAM);
ServletRequest request = context.getRequest();
// If no seeds are specified, just return
String seeds = request.getParameter(Constants.SEEDS_PARAM);
if (seeds == null || seeds.trim().length() == 0) {
return;
}
// split the seeds up into individual seeds
// Note: Matches any sort of newline (unix/mac/dos), but won't get empty
// lines, which is fine for this purpose
Set<String> seedSet = new HashSet<String>();
for (String seed : seeds.split("[\n\r]+")) {
seedSet.add(seed);
}
HTMLUtils.forwardOnEmptyParameter(context, Constants.ORDER_TEMPLATE_PARAM);
String orderTemplate = request.getParameter(Constants.ORDER_TEMPLATE_PARAM);
// Check that order template exists
if (!TemplateDAO.getInstance().exists(orderTemplate)) {
HTMLUtils.forwardWithErrorMessage(context, i18n, "errormsg;harvest.template.0.does.not.exist",
orderTemplate);
throw new ForwardedToErrorPage("The orderTemplate with name '" + orderTemplate + "' does not exist!");
}
// Check that numerical parameters are meaningful and replace null or
// empty with default values
long maxBytes = HTMLUtils.parseOptionalLong(context, Constants.MAX_BYTES_PARAM,
dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_BYTES);
long maxObjectsL = HTMLUtils.parseOptionalLong(context, Constants.MAX_OBJECTS_PARAM,
dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_OBJECTS);
int maxObjects = (int) maxObjectsL;
Map<String,String> attributeValues = new HashMap<String,String>();
// Fetch all attributes from context to be used later
for (String attrParam: EAV.getAttributeNames(EAV.DOMAIN_TREE_ID)){
String paramValue = context.getRequest().getParameter(attrParam);
log.debug("Read attribute {}. The value in form: {}", attrParam, paramValue);
attributeValues.put(attrParam, paramValue);
}
// All parameters are valid, so call method
try {
PartialHarvest eventHarvest = (PartialHarvest) HarvestDefinitionDAO.getInstance().getHarvestDefinition(
eventHarvestName);
eventHarvest.addSeeds(seedSet, orderTemplate, maxBytes, maxObjects, attributeValues);
} catch (Exception e) {
HTMLUtils.forwardWithErrorMessage(context, i18n, "errormsg;error.adding.seeds.to.0", eventHarvestName, e);
throw new ForwardedToErrorPage("Error while adding seeds", e);
}
}
/**
* Add configurations to an existing selective harvest.
*
* @param context The current JSP context
* @param i18n The translation information to use in this context
* @param eventHarvestName The name of the partial harvest to which these seeds are to be added
* @param seeds The seeds as a file (each seed on a separate line)
* @param maxbytesString The given maxbytes as a string
* @param maxobjectsString The given maxobjects as a string
* @param maxrateString The given maxrate as a string (currently not used)
* @param ordertemplate The name of the ordertemplate to use
* @param attributes A list of attributes and form values
*/
public static void addConfigurationsFromSeedsFile(PageContext context, I18n i18n, String eventHarvestName,
File seeds, String maxbytesString, String maxobjectsString, String maxrateString, String ordertemplate, Map<String,String> attributes) {
ArgumentNotValid.checkNotNull(context, "PageContext context");
ArgumentNotValid.checkNotNull(i18n, "I18n i18n");
ArgumentNotValid.checkNotNullOrEmpty(eventHarvestName, "String eventHarvestName");
ArgumentNotValid.checkNotNull(seeds, "String seeds");
ArgumentNotValid.checkNotNull(ordertemplate, "String ordertemplate");
long maxBytes = 0L;
int maxObjects = 0;
try {
if (maxbytesString == null) {
maxBytes = dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_BYTES;
} else {
Locale loc = HTMLUtils.getLocaleObject(context);
maxBytes = HTMLUtils.parseLong(loc, maxbytesString, Constants.MAX_BYTES_PARAM,
dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_BYTES);
}
if (maxobjectsString == null) {
maxObjects = (int) dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_OBJECTS;
} else {
Locale loc = HTMLUtils.getLocaleObject(context);
long maxObjectsL = HTMLUtils.parseLong(loc, maxobjectsString, Constants.MAX_OBJECTS_PARAM,
dk.netarkivet.harvester.datamodel.Constants.DEFAULT_MAX_OBJECTS);
maxObjects = (int) maxObjectsL;
}
} catch (Exception e) {
HTMLUtils.forwardWithErrorMessage(context, i18n, "Exception.thrown.when.adding.seeds", e);
return;
}
// Check that order template exists
if (!TemplateDAO.getInstance().exists(ordertemplate)) {
HTMLUtils.forwardWithErrorMessage(context, i18n, "errormsg;harvest.template.0.does.not.exist",
ordertemplate);
throw new ForwardedToErrorPage("The orderTemplate with name '" + ordertemplate + "' does not exist!");
}
// All parameters are valid, so call method
try {
PartialHarvest eventHarvest = (PartialHarvest) HarvestDefinitionDAO.getInstance().getHarvestDefinition(
eventHarvestName);
eventHarvest.addSeedsFromFile(seeds, ordertemplate, maxBytes, maxObjects, attributes);
} catch (Exception e) {
HTMLUtils
.forwardWithErrorMessage(context, i18n, "errormsg;error.adding.seeds.to.0", e, eventHarvestName, e);
throw new ForwardedToErrorPage("Error while adding seeds", e);
}
}
}