1. Project Clover database Tue Dec 20 2016 21:24:09 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 93 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  9 toggle static {
54  9 ROOT_NAMESPACES.add("{root}");
55  9 ROOT_NAMESPACES.add("{}");
56    };
57   
58    @Inject
59    @Named("context")
60    private Provider<ComponentManager> componentManager;
61   
 
62  315 toggle @Override
63    public boolean isAllowed(Collection<String> allowedNamespaces, String namespace)
64    {
65    // Null list means any namespace
66  315 if (allowedNamespaces == null) {
67  296 return true;
68    }
69   
70  19 for (String allowedNamespace : allowedNamespaces) {
71  19 if (isAllowed(allowedNamespace, namespace)) {
72  11 return true;
73    }
74    }
75   
76  8 return false;
77    }
78   
 
79  19 toggle private boolean isAllowed(String allowedNamespace, String namespace)
80    {
81    // Check if it's the same namespace
82  19 if (StringUtils.equals(allowedNamespace, namespace)) {
83  3 return true;
84    }
85   
86    // Check root namespaces aliases
87  16 if (namespace == null) {
88  6 return ROOT_NAMESPACES.contains(allowedNamespace);
89    }
90   
91    // Check if it's a regexp
92  10 if (allowedNamespace != null) {
93  9 Matcher matcher = REGEXP_PATTERN.matcher(allowedNamespace);
94  9 if (matcher.matches()) {
95  3 String pattern = matcher.group(1);
96  3 return namespace.matches(pattern);
97    }
98    }
99   
100  7 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    }