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

File ContextAndActionURLNormalizer.java

 

Coverage histogram

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

Code metrics

10
22
4
1
137
75
9
0.41
5.5
4
2.25

Classes

Class Line # Actions
ContextAndActionURLNormalizer 49 22 0% 9 1
0.972222297.2%
 

Contributing tests

This file is covered by 9 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 java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.List;
26   
27    import javax.inject.Inject;
28    import javax.inject.Named;
29    import javax.inject.Singleton;
30   
31    import org.apache.commons.lang3.StringUtils;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.phase.Initializable;
34    import org.xwiki.configuration.ConfigurationSource;
35    import org.xwiki.model.ModelContext;
36    import org.xwiki.url.ExtendedURL;
37   
38    /**
39    * Prefixes the passed Extended URL with the webapp's Servlet context and the Struts "action" servlet, usually mapped as
40    * {@code /bin}. For example {@code /some/path} would be normalized into {@code /xwiki/bin/some/path} if the webapp's
41    * context was {@code xwiki} and the main mapping for the action servlet is {@code /bin}.
42    *
43    * @version $Id: 2682db53dc17f7fdd3a1bce2638fbee5693e7b54 $
44    * @since 7.4M1
45    */
46    @Component
47    @Named("contextpath+actionservletpath")
48    @Singleton
 
49    public class ContextAndActionURLNormalizer extends org.xwiki.url.internal.container.ContextAndActionURLNormalizer
50    implements Initializable
51    {
52    /** This will be removed from the context and servlet paths. */
53    private static final String URL_SEGMENT_DELIMITER = "/";
54   
55    /** The old configuration, where the context path can be configured. */
56    @Inject
57    @Named("xwikicfg")
58    private ConfigurationSource configurationSource;
59   
60    /** The new configuration for URLs. */
61    @Inject
62    private StandardURLConfiguration urlConfiguration;
63   
64    @Inject
65    private ModelContext context;
66   
67    /** The mapping used for virtual wikis in path-based wiki access. */
68    private String virtualWikiServletMapping;
69   
 
70  9 toggle @Override
71    public void initialize()
72    {
73  9 super.initialize();
74  9 this.virtualWikiServletMapping = this.urlConfiguration.getWikiPathPrefix();
75    }
76   
 
77  9 toggle @Override
78    public ExtendedURL normalize(ExtendedURL partialURL)
79    {
80  9 String contextPath = StringUtils.strip(getContextPath(), URL_SEGMENT_DELIMITER);
81  9 if (contextPath == null) {
82  1 throw new RuntimeException(String.format("Failed to normalize the URL [%s] since the "
83    + "application's Servlet context couldn't be computed.", partialURL));
84    }
85  8 List<String> segments = new ArrayList<>();
86  8 if (StringUtils.isNotEmpty(contextPath)) {
87  5 segments.add(contextPath);
88    }
89   
90  8 List<String> servletPath = getActionAndWikiServletMapping();
91  8 for (String segment : servletPath) {
92  9 if (StringUtils.isNotEmpty(segment)) {
93  9 segments.add(segment);
94    }
95    }
96   
97  8 segments.addAll(partialURL.getSegments());
98   
99  8 return new ExtendedURL(segments, partialURL.getParameters());
100    }
101   
 
102  9 toggle @Override
103    protected String getContextPath()
104    {
105    // Look in the XWiki configuration for a hard-coded value. Currently, this is specified using the
106    // {@code xwiki.webapppath} setting in {@code xwiki.cfg}.
107  9 String contextPath = this.configurationSource.getProperty("xwiki.webapppath");
108   
109    // If the context path is not configured, try to extract it from the application context
110  9 if (contextPath == null) {
111  3 contextPath = super.getContextPath();
112    }
113   
114  9 return contextPath;
115    }
116   
117    /**
118    * Get the path prefix used for the Struts Action Servlet, either a prefix similar to the one used in the current
119    * request if it also passes through the Action servlet, or using the default path configured for it. In case the
120    * current request is for a virtual wiki identified through the path, the return value also includes the wiki
121    * identifier.
122    *
123    * @return a list of segments containing the path used for triggering the Struts Action Servlet (may be the empty
124    * string), and optionally a wiki identifier if the first segment corresponds to virtual wiki access
125    */
 
126  8 toggle protected List<String> getActionAndWikiServletMapping()
127    {
128  8 String result = super.getActionServletMapping();
129  8 if (StringUtils.equals(this.virtualWikiServletMapping, result)) {
130    // Virtual wiki, also include the wiki identifier
131  1 return Arrays.asList(this.virtualWikiServletMapping,
132    this.context.getCurrentEntityReference().getRoot().getName());
133    }
134   
135  7 return Collections.singletonList(result);
136    }
137    }