Skip to content

Commit

Permalink
JCRVLT-486 introduce new configuration property "validNameSpaces" all…
Browse files Browse the repository at this point in the history
…owing to configure list of allowed namespaces in the format: prefix1=ns-uri1,prefix2=nsuri2,...
  • Loading branch information
stefanseifert committed Dec 3, 2020
1 parent 2d62076 commit 428814c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
import java.net.JarURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.Manifest;

import javax.jcr.RepositoryException;
Expand All @@ -50,6 +56,8 @@ public class NodeTypeValidatorFactory implements ValidatorFactory {
/** The default node type to assume if no other node type is given */
public static final String OPTION_DEFAULT_NODE_TYPES = "defaultNodeType";
public static final String OPTION_SEVERITY_FOR_UNKNOWN_NODETYPES = "severityForUnknownNodetypes";
/** Comma-separated list of name spaces that are known as valid (even if not defined in the CND files). Use syntax "prefix1=ns-uri1,prefix2=nsuri2,...". */
public static final String OPTION_VALID_NAMESPACES = "validNameSpaces";

static final @NotNull String DEFAULT_DEFAULT_NODE_TYPE = JcrConstants.NT_FOLDER;

Expand Down Expand Up @@ -82,6 +90,14 @@ public class NodeTypeValidatorFactory implements ValidatorFactory {
severityForUnknownNodetypes = DEFAULT_SEVERITY_FOR_UNKNOWN_NODETYPE;
}

Map<String,String> validNameSpaces;
if (settings.getOptions().containsKey(OPTION_VALID_NAMESPACES)) {
validNameSpaces = parseNamespaces(settings.getOptions().get(OPTION_VALID_NAMESPACES));
}
else {
validNameSpaces = Collections.emptyMap();
}

try {
NodeTypeManagerProvider ntManagerProvider = null;
ntManagerProvider = new NodeTypeManagerProvider();
Expand All @@ -93,6 +109,9 @@ public class NodeTypeValidatorFactory implements ValidatorFactory {
throw new IllegalArgumentException("Error loading node types from CND at " + cndUrl, e);
}
}
for (Map.Entry<String, String> entry : validNameSpaces.entrySet()) {
ntManagerProvider.registerNamespace(entry.getKey(), entry.getValue());
}
return new NodeTypeValidator(context.getFilter(), ntManagerProvider, ntManagerProvider.getNameResolver().getQName(defaultNodeType), settings.getDefaultSeverity(),
severityForUnknownNodetypes);
} catch (IOException | RepositoryException | ParseException e) {
Expand Down Expand Up @@ -134,6 +153,18 @@ static List<String> resolveJarUrls(String... urls) {
return resolvedUrls;
}

static Map<String,String> parseNamespaces(String optionValue) {
Map<String,String> result = new HashMap<>();
String[] namespaces = optionValue.split("\\s*,\\s*");
for (String namespace : namespaces) {
String[] namespaceParts = namespace.split("\\s*=\\s*");
if (namespaceParts.length == 2) {
result.put(namespaceParts[0], namespaceParts[1]);
}
}
return result;
}

@Override
public boolean shouldValidateSubpackages() {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.jackrabbit.vault.validation.spi.impl.nodetype;

import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class NodeTypeValidatorFactoryTest {

@Test
public void testParseNamespaces() {
String optionValue = "ns1=http://uri1,\n"
+ " ns2 = http://uri2,ns3=http://uri3";
Map<String,String> actual = NodeTypeValidatorFactory.parseNamespaces(optionValue);

Map<String,String> expected = new HashMap<>();
expected.put("ns1", "http://uri1");
expected.put("ns2", "http://uri2");
expected.put("ns3", "http://uri3");
assertEquals(expected, actual);
}

}

0 comments on commit 428814c

Please sign in to comment.