/
DomainDefinition.java
393 lines (352 loc) · 17 KB
/
DomainDefinition.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* #%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.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
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.exceptions.IOFailure;
import dk.netarkivet.common.utils.DomainUtils;
import dk.netarkivet.common.utils.I18n;
import dk.netarkivet.common.webinterface.HTMLUtils;
import dk.netarkivet.harvester.datamodel.Domain;
import dk.netarkivet.harvester.datamodel.DomainConfiguration;
import dk.netarkivet.harvester.datamodel.DomainDAO;
import dk.netarkivet.harvester.datamodel.NamedUtils;
import dk.netarkivet.harvester.datamodel.SeedList;
import dk.netarkivet.harvester.datamodel.extendedfield.ExtendedFieldTypes;
/**
* Utility class for handling update of domain from the domain jsp page.
*/
@SuppressWarnings("unused")
public class DomainDefinition {
//private static Log log = LogFactory.getLog(DomainDefinition.class.getName());
protected static final Logger log = LoggerFactory.getLogger(DomainDefinition.class);
/** Private constructor to prevent public construction of this class. */
private DomainDefinition() {
}
/**
* Extracts all required parameters from the request, checks for any inconsistencies, and passes the requisite data
* to the updateDomain method for processing.
* <p>
* For reference, the parameters for this page look something like
* http://localhost:8076/HarvestDefinition/Definitions-edit-domain.jsp?
* update=1&name=netarkivet.dk&default=defaultconfig&configName=&order_xml=&
* load=&maxObjects=&urlListName=&seedList=+&passwordName=&passwordDomain=& passwordRealm=&userName=&password=&
* crawlertraps=%2Fcgi-bin%2F*%0D%0A%2Ftrap%2F*%0D%0A
* <p>
* update: This method throws an exception if update is not set
* <p>
* name: must be the name of a known domain
* <p>
* comments: optional user-entered comments about the domain
* <p>
* default: the defaultconfig is set to this value. Must be non-null and a known configuration of this domain.
* <p>
* crawlertraps: a newline-separated list of urls to be ignored. May be empty or null
* <p>
* alias: If set, this domain is an alias of the set domain renewAlias: If set, the alias date should be renewed
*
* @param context The context of this request
* @param i18n I18n information
* @throws IOFailure on updateerrors in the DAO
* @throws ForwardedToErrorPage if domain is not found, if the edition is out-of-date, or if parameters are missing
* or invalid
*/
public static void processRequest(PageContext context, I18n i18n) {
ArgumentNotValid.checkNotNull(context, "PageContext context");
ArgumentNotValid.checkNotNull(i18n, "I18n i18n");
HTMLUtils.forwardOnEmptyParameter(context, Constants.DOMAIN_PARAM, Constants.DEFAULT_PARAM);
ServletRequest request = context.getRequest();
String name = request.getParameter(Constants.DOMAIN_PARAM).trim();
if (!DomainDAO.getInstance().exists(name)) {
HTMLUtils.forwardWithErrorMessage(context, i18n, "errormsg;unknown.domain.0", name);
throw new ForwardedToErrorPage("Unknown domain '" + name + "'");
}
Domain domain = DomainDAO.getInstance().read(name);
// check the edition number before updating
long edition = HTMLUtils.parseOptionalLong(context, Constants.EDITION_PARAM, -1L);
if (domain.getEdition() != edition) {
HTMLUtils.forwardWithRawErrorMessage(
context,
i18n,
"errormsg;domain.definition.changed.0.retry.1",
"<br/><a href=\"Definitions-edit-domain.jsp?" + Constants.DOMAIN_PARAM + "="
+ HTMLUtils.escapeHtmlValues(HTMLUtils.encode(name)) + "\">", "</a>");
throw new ForwardedToErrorPage("Domain '" + name + "' has changed");
}
// default configuration
String defaultConf = request.getParameter(Constants.DEFAULT_PARAM);
if (!domain.hasConfiguration(defaultConf)) {
HTMLUtils.forwardWithErrorMessage(context, i18n, "errormsg;unknown.default.configuration.0.for.1",
defaultConf, name);
throw new ForwardedToErrorPage("Unknown default configuration '" + defaultConf + "'");
}
String crawlertraps = request.getParameter(Constants.CRAWLERTRAPS_PARAM);
if (crawlertraps == null) {
crawlertraps = "";
}
String comments = request.getParameter(Constants.COMMENTS_PARAM);
if (comments == null) {
comments = "";
}
String alias = request.getParameter(Constants.ALIAS_PARAM);
if (alias == null) {
alias = "";
}
String aliasRenew = request.getParameter(Constants.RENEW_ALIAS_PARAM);
if (aliasRenew == null) {
aliasRenew = "no";
}
boolean renewAlias = aliasRenew.equals("yes");
ExtendedFieldValueDefinition.processRequest(context, i18n, domain, ExtendedFieldTypes.DOMAIN);
updateDomain(domain, defaultConf, crawlertraps, comments, alias, renewAlias);
}
/**
* This updates the given domain in the database.
*
* @param domain the given domain
* @param defaultConfig the name of the default configuration
* @param crawlertraps the current crawlertraps stated for the domain
* @param comments User-defined comments for the domain
* @param alias if this is non-null, this domain is an alias of 'alias'.
* @param renewAlias true, if alias is to be updated even if it is not changed
*/
private static void updateDomain(Domain domain, String defaultConfig, String crawlertraps, String comments,
String alias, boolean renewAlias) {
// Set default configuration
domain.setDefaultConfiguration(defaultConfig);
domain.setComments(comments);
// Update crawlertraps
List<String> trapList = new ArrayList<String>();
if (crawlertraps.length() > 0) {
String[] traps = crawlertraps.split("[\\r\\n]+");
for (String trap : traps) {
if (trap.trim().length() > 0) {
trapList.add(trap);
}
}
}
domain.setCrawlerTraps(trapList, true);
// Update alias information
// If alias is empty string, do not regard this domain as an alias any
// more.
// If alias is not-empty, update only if alias is different from
// oldAlias or
// This only updates alias if it is required: See javadoc below for
// needToUpdateAlias()
String oldAlias = null;
if (domain.getAliasInfo() != null) {
oldAlias = domain.getAliasInfo().getAliasOf();
}
String newAlias;
// If alias is empty string, this domain is or should not be an alias.
if (alias.trim().isEmpty()) {
newAlias = null;
} else {
newAlias = alias.trim();
}
if (needToUpdateAlias(oldAlias, newAlias, renewAlias)) {
domain.updateAlias(newAlias);
}
DomainDAO.getInstance().update(domain);
}
/**
* Define the cases where we want to update the alias information. 1. The alias information is updated, if the new
* alias is null, and the old alias is different from null 2. The alias information is updated, if the new alias is
* different from null, and old alias is null 3. The alias information is updated, if the new alias is different
* from null, and the old alias is different from null, and they are not either not equal, or renewAlias is true
*
* @param oldAlias the old alias (could be null)
* @param newAlias the new alias (could be null)
* @param renewAlias should we renew alias, if the alias is unchanged?
* @return true, if we want to update the alias information, false otherwise
*/
private static boolean needToUpdateAlias(String oldAlias, String newAlias, boolean renewAlias) {
boolean needToUpdate = false;
// If new alias is null: update if old alias is different from null
if (newAlias == null) {
if (oldAlias != null) {
needToUpdate = true;
}
} else { // newAlias is not null
if (oldAlias == null) {
needToUpdate = true;
} else {
if (oldAlias.equals(newAlias)) {
if (renewAlias) {
needToUpdate = true;
}
} else {
needToUpdate = true;
}
}
}
return needToUpdate;
}
/**
* Creates domains with default attributes.
*
* @param domains a list of domain names
* @return List of the non-empty domain names that were not legal domain names or already exist.
*/
public static List<String> createDomains(String... domains) {
DomainDAO ddao = DomainDAO.getInstance();
List<String> illegals = new ArrayList<String>();
List<Domain> domainsToCreate = new ArrayList<Domain>();
for (String domain : domains) {
if (DomainUtils.isValidDomainName(domain) && !ddao.exists(domain)) {
domainsToCreate.add(Domain.getDefaultDomain(domain));
} else {
if (domain.trim().length() > 0) {
illegals.add(domain);
}
}
}
ddao.create(domainsToCreate);
return illegals;
}
/**
* Creates a link to the domain edit page.
*
* @param domain The domain to show with a link
* @return HTML code with the link and the domain name shown
*/
public static String makeDomainLink(String domain) {
ArgumentNotValid.checkNotNullOrEmpty(domain, "domain");
String url = "/HarvestDefinition/Definitions-edit-domain.jsp?" + Constants.DOMAIN_PARAM + "="
+ HTMLUtils.encode(domain);
return "<a href=\"" + url + "\">" + HTMLUtils.escapeHtmlValues(domain) + "</a>";
}
/**
* Creates a url based on the supplied request where all the parameters are the same, except the
* <code>ShowUnusedConfigurations</code> boolean, which is flipped.
*
* @param request The original 'create domain' request to based the new url on.
* @return The new url with the <code>ShowUnusedConfigurations</code> boolean switched.
*/
public static String createDomainUrlWithFlippedShowConfigurations(ServletRequest request) {
boolean showUnusedConfigurationsParam = Boolean.parseBoolean(request
.getParameter(Constants.SHOW_UNUSED_CONFIGURATIONS_PARAM));
boolean showUnusedSeedsParam = Boolean.parseBoolean(request.getParameter(Constants.SHOW_UNUSED_SEEDS_PARAM));
StringBuilder urlBuilder = new StringBuilder("/HarvestDefinition/Definitions-edit-domain.jsp?");
urlBuilder
.append(Constants.DOMAIN_PARAM + "=" + HTMLUtils.encode(request.getParameter(Constants.DOMAIN_PARAM)));
urlBuilder.append("&" + Constants.SHOW_UNUSED_CONFIGURATIONS_PARAM + "="
+ Boolean.toString(!showUnusedConfigurationsParam));
urlBuilder.append("&" + Constants.SHOW_UNUSED_SEEDS_PARAM + "=" + Boolean.toString(showUnusedSeedsParam));
return urlBuilder.toString();
}
/**
* Creates a url based on the supplied request where all the parameters are the same, except the
* <code>ShowUnusedSeedLists</code> boolean, which is flipped.
*
* @param request The original 'create domain' request to based the new url on.
* @return The new url with the <code>ShowUnusedSeedLists</code> boolean switched.
*/
public static String createDomainUrlWithFlippedShowSeeds(ServletRequest request) {
boolean showUnusedConfigurationsParam = Boolean.parseBoolean(request
.getParameter(Constants.SHOW_UNUSED_CONFIGURATIONS_PARAM));
boolean showUnusedSeedsParam = Boolean.parseBoolean(request.getParameter(Constants.SHOW_UNUSED_SEEDS_PARAM));
StringBuilder urlBuilder = new StringBuilder("/HarvestDefinition/Definitions-edit-domain.jsp?");
urlBuilder
.append(Constants.DOMAIN_PARAM + "=" + HTMLUtils.encode(request.getParameter(Constants.DOMAIN_PARAM)));
urlBuilder.append("&" + Constants.SHOW_UNUSED_CONFIGURATIONS_PARAM + "="
+ Boolean.toString(showUnusedConfigurationsParam));
urlBuilder.append("&" + Constants.SHOW_UNUSED_SEEDS_PARAM + "=" + Boolean.toString(!showUnusedSeedsParam));
return urlBuilder.toString();
}
/**
* Search for domains matching the following criteria.
* TODO Should we allow more than one criteria?
* TODO use Enum instead for searchType
*
* @param context the context of the JSP page calling
* @param i18n The translation properties file used
* @param searchQuery The given searchQuery for searching for among the domains.
* @param searchType The given searchCriteria
* @return the set of domain-names matching the given criteria.
*/
public static List<String> getDomains(PageContext context, I18n i18n, String searchQuery, String searchType) {
List<String> resultSet = new ArrayList<String>();
ArgumentNotValid.checkNotNullOrEmpty(searchQuery, "String searchQuery");
ArgumentNotValid.checkNotNullOrEmpty(searchType, "String searchType");
try {
DomainSearchType.parse(searchType);
} catch (ArgumentNotValid e) {
HTMLUtils.forwardWithErrorMessage(context, i18n, "errormsg;invalid.domain.search.criteria.0", searchType);
throw new ForwardedToErrorPage("Unknown domain search criteria '" + searchType + "'");
}
log.debug("SearchQuery '" + searchQuery + "', searchType: " + searchType);
resultSet = DomainDAO.getInstance().getDomains(searchQuery, searchType);
return resultSet;
}
/**
* Returns the list of domain configurations which are either used in a concrete harvest or is a 'default
* configuration'.
* <p>
* The list is sorted alphabetically by name according to the supplied locale.
*
* @param domain The domain to find the used configurations for.
* @param locale The locale to base the sorting on
* @return A sorted list of used configurations for the supplied domain.
*/
public static List<DomainConfiguration> getUsedConfiguration(Domain domain, Locale locale) {
List<Long> usedConfigurationIDs = DomainDAO.getInstance().findUsedConfigurations(domain.getID());
List<DomainConfiguration> usedConfigurations = new LinkedList<DomainConfiguration>();
for (DomainConfiguration configuration : domain.getAllConfigurationsAsSortedList(locale)) {
if (usedConfigurationIDs.contains(new Long(configuration.getID()))
|| configuration.getID() == domain.getDefaultConfiguration().getID()) {
usedConfigurations.add(configuration);
}
}
NamedUtils.sortNamedObjectList(locale, usedConfigurations);
return usedConfigurations;
}
/**
* Returns the seed lists associated with the supplied configurations.
*
* @param configurations The configurations to find seed lists for
* @return The seed lists used in the supplied configurations.
*/
public static List<SeedList> getSeedLists(List<DomainConfiguration> configurations) {
List<SeedList> seedsLists = new LinkedList<SeedList>();
for (DomainConfiguration configuration : configurations) {
Iterator<SeedList> seedListIterator = configuration.getSeedLists();
while (seedListIterator.hasNext()) {
SeedList seedList = seedListIterator.next();
if (!seedsLists.contains(seedList)) {
seedsLists.add(seedList);
}
}
}
return seedsLists;
}
}