1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.internal.namespace

File DefaultNamespaceValidator.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
20
4
1
111
67
11
0.55
5
4
2.75

Classes

Class Line # Actions
DefaultNamespaceValidator 47 20 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 100 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.component.internal.namespace;
21   
22    import java.util.Collection;
23    import java.util.HashSet;
24    import java.util.Set;
25    import java.util.regex.Matcher;
26    import java.util.regex.Pattern;
27   
28    import javax.inject.Inject;
29    import javax.inject.Named;
30    import javax.inject.Provider;
31    import javax.inject.Singleton;
32   
33    import org.apache.commons.lang3.StringUtils;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.manager.ComponentManager;
36    import org.xwiki.component.namespace.NamespaceNotAllowedException;
37    import org.xwiki.component.namespace.NamespaceValidator;
38   
39    /**
40    * Default implementation of {@link NamespaceValidator}.
41    *
42    * @version $Id: 059f5ba8a3f7d99dbcdfd4004b9e8ada415f0eea $
43    * @since 8.0M1
44    */
45    @Component
46    @Singleton
 
47    public class DefaultNamespaceValidator implements NamespaceValidator
48    {
49    private static final Pattern REGEXP_PATTERN = Pattern.compile("\\[(.*)\\]");
50   
51    private static final Set<String> ROOT_NAMESPACES = new HashSet<>();
52   
 
53  44 toggle static {
54  44 ROOT_NAMESPACES.add("{root}");
55  44 ROOT_NAMESPACES.add("{}");
56    };
57   
58    @Inject
59    @Named("context")
60    private Provider<ComponentManager> componentManager;
61   
 
62  1365 toggle @Override
63    public boolean isAllowed(Collection<String> allowedNamespaces, String namespace)
64    {
65    // Null list means any namespace
66  1365 if (allowedNamespaces == null) {
67  1169 return true;
68    }
69   
70  196 for (String allowedNamespace : allowedNamespaces) {
71  196 if (isAllowed(allowedNamespace, namespace)) {
72  84 return true;
73    }
74    }
75   
76  112 return false;
77    }
78   
 
79  196 toggle private boolean isAllowed(String allowedNamespace, String namespace)
80    {
81    // Check if it's the same namespace
82  196 if (StringUtils.equals(allowedNamespace, namespace)) {
83  46 return true;
84    }
85   
86    // Check root namespaces aliases
87  150 if (namespace == null) {
88  66 return ROOT_NAMESPACES.contains(allowedNamespace);
89    }
90   
91    // Check if it's a regexp
92  84 if (allowedNamespace != null) {
93  74 Matcher matcher = REGEXP_PATTERN.matcher(allowedNamespace);
94  74 if (matcher.matches()) {
95  5 String pattern = matcher.group(1);
96  5 return namespace.matches(pattern);
97    }
98    }
99   
100  79 return false;
101    }
102   
 
103  2 toggle @Override
104    public void checkAllowed(Collection<String> allowedNamespaces, String namespace) throws NamespaceNotAllowedException
105    {
106  2 if (!isAllowed(allowedNamespaces, namespace)) {
107  1 throw new NamespaceNotAllowedException(
108    "Allowed namespace list [" + allowedNamespaces + "] does not matches namespace [" + namespace + "]");
109    }
110    }
111    }