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

File ExtendedURLURLNormalizer.java

 

Coverage histogram

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

Code metrics

8
16
4
1
114
59
8
0.5
4
4
2

Classes

Class Line # Actions
ExtendedURLURLNormalizer 47 16 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 7 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.container;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.configuration.ConfigurationSource;
32    import org.xwiki.environment.Environment;
33    import org.xwiki.environment.internal.ServletEnvironment;
34    import org.xwiki.url.ExtendedURL;
35    import org.xwiki.url.URLNormalizer;
36   
37    /**
38    * Prefixes the passed Extended URL with the webapp's Servlet context. For example {@code /some/path} would be
39    * normalized into {@code /xwiki/some/path} if the webapp's context was {@code xwiki}.
40    *
41    * @version $Id: 181ecc5774c76266f6816e02038cc449967d7711 $
42    * @since 6.1M2
43    */
44    @Component
45    @Named("contextpath")
46    @Singleton
 
47    public class ExtendedURLURLNormalizer implements URLNormalizer<ExtendedURL>
48    {
49    private static final String URL_SEGMENT_DELIMITER = "/";
50   
51    @Inject
52    @Named("xwikicfg")
53    private ConfigurationSource configurationSource;
54   
55    /** Provides access to the application context configuration. */
56    @Inject
57    private Environment environment;
58   
 
59  25724 toggle @Override
60    public ExtendedURL normalize(ExtendedURL partialURL)
61    {
62  25723 String contextPath = StringUtils.strip(getContextPath(), URL_SEGMENT_DELIMITER);
63   
64  25724 if (contextPath == null) {
65  1 throw new RuntimeException(String.format("Failed to normalize the URL [%s] since the "
66    + "application's Servlet context couldn't be computed.", partialURL));
67    }
68   
69  25721 List<String> segments = new ArrayList<>();
70  25721 if (StringUtils.isNotEmpty(contextPath)) {
71  25720 segments.add(contextPath);
72    }
73  25721 segments.addAll(partialURL.getSegments());
74   
75  25723 return new ExtendedURL(segments, partialURL.getParameters());
76    }
77   
 
78  25723 toggle private String getContextPath()
79    {
80  25723 String contextPath = getContextPathFromConfiguration();
81   
82    // If the context path is not configured, try to extract it from the application context
83  25724 if (contextPath == null) {
84  25720 contextPath = getContextPathFromApplicationContext();
85    }
86   
87  25724 return contextPath;
88    }
89   
90    /**
91    * Look in the XWiki configuration for a hard-coded value. Currently, this is specified using the
92    * {@code xwiki.webapppath} setting in {@code xwiki.cfg}.
93    *
94    * @return the value specified in the settings, or {@code null} if not specified
95    */
 
96  25723 toggle private String getContextPathFromConfiguration()
97    {
98  25724 return this.configurationSource.getProperty("xwiki.webapppath");
99    }
100   
101    /**
102    * Look in the application context, if there is such a context.
103    *
104    * @return the context path taken from the application context, or {@code null} if this isn't running in a servlet
105    * environment
106    */
 
107  25719 toggle private String getContextPathFromApplicationContext()
108    {
109  25719 if (this.environment instanceof ServletEnvironment) {
110  25719 return ((ServletEnvironment) this.environment).getServletContext().getContextPath();
111    }
112  1 return null;
113    }
114    }