1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.url.internal.standard

File AbstractWikiReferenceExtractor.java

 

Coverage histogram

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

Code metrics

8
17
4
1
104
54
11
0.65
4.25
4
2.75

Classes

Class Line # Actions
AbstractWikiReferenceExtractor 35 17 0% 11 2
0.931034593.1%
 

Contributing tests

This file is covered by 17 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.url.internal.standard;
21   
22    import javax.inject.Inject;
23   
24    import org.xwiki.context.Execution;
25    import org.xwiki.wiki.descriptor.WikiDescriptor;
26    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
27    import org.xwiki.wiki.manager.WikiManagerException;
28   
29    /**
30    * Commons code for Wiki Reference Extractors.
31    *
32    * @version $Id: 97e7058b481b76dd04177b558f8e1f5fde591da9 $
33    * @since 6.3M1
34    */
 
35    public abstract class AbstractWikiReferenceExtractor implements WikiReferenceExtractor
36    {
37    @Inject
38    private StandardURLConfiguration configuration;
39   
40    /**
41    * Used to get wiki descriptors based on alias or wiki id.
42    */
43    @Inject
44    private WikiDescriptorManager wikiDescriptorManager;
45   
46    @Inject
47    private Execution execution;
48   
49    /**
50    * Check if there's a descriptor for the passed wiki and if not and the configuration option to redirect to
51    * the main wiki is enabled then return the main wiki.
52    */
 
53  75 toggle protected String normalizeWikiIdForNonExistentWikiDescriptor(String alias)
54    {
55  74 String normalizedWikiId = alias;
56  73 String mainWiki = getMainWikiId();
57  76 if (!mainWiki.equals(normalizedWikiId)
58    && this.configuration.getWikiNotFoundBehavior() == WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI)
59    {
60  70 if (getWikiDescriptorById(normalizedWikiId) == null) {
61    // Fallback on main wiki
62  68 normalizedWikiId = mainWiki;
63    }
64    }
65  75 return normalizedWikiId;
66    }
67   
 
68  21118 toggle protected WikiDescriptor getWikiDescriptorByAlias(String alias)
69    {
70    // Note: We also support not having an Execution Context available. This allows this code to work at request
71    // initialization time, when no Context has been set up yet. In the future, we need to move the Context init
72    // as the first thing along with Database initialization.
73  21113 if (this.execution.getContext() == null) {
74  9480 return null;
75    }
76   
77  11603 try {
78  11602 return this.wikiDescriptorManager.getByAlias(alias);
79    } catch (WikiManagerException e) {
80  0 throw new RuntimeException(String.format("Failed to locate wiki descriptor for alias [%s]", alias), e);
81    }
82    }
83   
 
84  69 toggle protected WikiDescriptor getWikiDescriptorById(String wikiId)
85    {
86    // Note: We also support not having an Execution Context available. This allows this code to work at request
87    // initialization time, when no Context has been set up yet. In the future, we need to move the Context init
88    // as the first thing along with Database initialization.
89  70 if (this.execution.getContext() == null) {
90  65 return null;
91    }
92   
93  5 try {
94  5 return this.wikiDescriptorManager.getById(wikiId);
95    } catch (WikiManagerException e) {
96  0 throw new RuntimeException(String.format("Failed to locate wiki descriptor for wiki [%s]", wikiId), e);
97    }
98    }
99   
 
100  9493 toggle protected String getMainWikiId()
101    {
102  9505 return this.wikiDescriptorManager.getMainWikiId();
103    }
104    }