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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.application.provider;
import com.yahoo.collections.Tuple2;
import com.yahoo.component.Version;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.path.Path;
import com.yahoo.io.reader.NamedReader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;
/**
* Validation of xml files in application package against RELAX NG schemas.
*
* @author hmusum
*/
public class ApplicationPackageXmlFilesValidator {
private final AppSubDirs appDirs;
private final SchemaValidators validators;
private static final FilenameFilter xmlFilter = (dir, name) -> name.endsWith(".xml");
private ApplicationPackageXmlFilesValidator(AppSubDirs appDirs, Version vespaVersion) {
this.appDirs = appDirs;
this.validators = new SchemaValidators(vespaVersion);
}
public static ApplicationPackageXmlFilesValidator create(File appDir, Version vespaVersion) {
return new ApplicationPackageXmlFilesValidator(new AppSubDirs(appDir), vespaVersion);
}
@SuppressWarnings("deprecation")
public void checkApplication() throws IOException {
validate(validators.servicesXmlValidator(), servicesFileName());
validateOptional(validators.hostsXmlValidator(), FilesApplicationPackage.HOSTS);
validateOptional(validators.deploymentXmlValidator(), FilesApplicationPackage.DEPLOYMENT_FILE.getName());
validateOptional(validators.validationOverridesXmlValidator(), FilesApplicationPackage.VALIDATION_OVERRIDES.getName());
if (appDirs.searchdefinitions().exists()) {
if (FilesApplicationPackage.getSearchDefinitionFiles(appDirs.root()).isEmpty()) {
throw new IllegalArgumentException("Application package in " + appDirs.root() +
" must contain at least one search definition (.sd) file when directory searchdefinitions/ exists.");
}
}
validateRouting(appDirs.routingtables);
}
// For testing
public void checkIncludedDirs(ApplicationPackage app) throws IOException {
for (String includedDir : app.getUserIncludeDirs()) {
List<NamedReader> includedFiles = app.getFiles(Path.fromString(includedDir), ".xml", true);
for (NamedReader file : includedFiles) {
validators.containerIncludeXmlValidator().validate(file);
}
}
}
private void validateOptional(SchemaValidator validator, String file) throws IOException {
if ( ! appDirs.file(file).exists()) return;
validate(validator, file);
}
private void validate(SchemaValidator validator, String filename) throws IOException {
validator.validate(appDirs.file(filename));
}
@SuppressWarnings("deprecation")
private String servicesFileName() {
String servicesFile = FilesApplicationPackage.SERVICES;
if ( ! appDirs.file(servicesFile).exists()) {
throw new IllegalArgumentException("Application package in " + appDirs.root() +
" must contain " + FilesApplicationPackage.SERVICES);
}
return servicesFile;
}
private void validateRouting(Tuple2<File, String> directory) throws IOException {
if ( ! directory.first.isDirectory()) return;
validateRouting(validators.routingStandaloneXmlValidator(), directory);
}
private void validateRouting(SchemaValidator validator, Tuple2<File, String> directory) throws IOException {
File dir = directory.first;
if ( ! dir.isDirectory()) return;
String directoryName = directory.second;
for (File f : dir.listFiles(xmlFilter)) {
if (f.isDirectory())
validateRouting(validator, new Tuple2<>(f, directoryName + File.separator + f.getName()));
else
validator.validate(f, directoryName + File.separator + f.getName());
}
}
}
|