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

File ContextAndActionURLNormalizer.java

 

Coverage histogram

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

Code metrics

16
26
4
1
144
83
12
0.46
6.5
4
3

Classes

Class Line # Actions
ContextAndActionURLNormalizer 53 26 0% 12 0
1.0100%
 

Contributing tests

This file is covered by 16 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.Collection;
24    import java.util.LinkedHashSet;
25    import java.util.List;
26   
27    import javax.inject.Inject;
28    import javax.inject.Named;
29    import javax.inject.Singleton;
30    import javax.servlet.http.HttpServletRequest;
31   
32    import org.apache.commons.lang3.StringUtils;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.component.phase.Initializable;
35    import org.xwiki.container.Container;
36    import org.xwiki.container.servlet.ServletRequest;
37    import org.xwiki.environment.Environment;
38    import org.xwiki.environment.internal.ServletEnvironment;
39    import org.xwiki.url.ExtendedURL;
40    import org.xwiki.url.URLNormalizer;
41   
42    /**
43    * Prefixes the passed Extended URL with the webapp's Servlet context and the Struts "action" servlet, usually mapped as
44    * {@code /bin}. For example {@code /some/path} would be normalized into {@code /xwiki/bin/some/path} if the webapp's
45    * context was {@code xwiki} and the main mapping for the action servlet is {@code /bin}.
46    *
47    * @version $Id: 3090fafd9027824d97cbef89fbf011d976fa36c2 $
48    * @since 7.4M1
49    */
50    @Component
51    @Named("contextpath+actionservletpath")
52    @Singleton
 
53    public class ContextAndActionURLNormalizer implements URLNormalizer<ExtendedURL>, Initializable
54    {
55    /** This will be removed from the context and servlet paths. */
56    private static final String URL_SEGMENT_DELIMITER = "/";
57   
58    /** These will be removed from the configured action servlet mappings. */
59    private static final String IGNORED_MAPPING_CHARACTERS = "/*";
60   
61    /** Provides access to the current request, if any. */
62    @Inject
63    private Container container;
64   
65    /** Provides access to the application context configuration. */
66    @Inject
67    private Environment environment;
68   
69    /** The default mapping for the action servlet. */
70    private String defaultServletMapping = "bin";
71   
72    /**
73    * Valid mappings for the action servlet. If a request doesn't use one of these (for example a REST request), then
74    * the default mapping will be used.
75    */
76    private Collection<String> validServletMappings = new LinkedHashSet<>();
77   
 
78  16 toggle @Override
79    public void initialize()
80    {
81  16 if (this.environment instanceof ServletEnvironment) {
82  14 for (String mapping : ((ServletEnvironment) this.environment).getServletContext()
83    .getServletRegistration("action").getMappings()) {
84  43 this.validServletMappings.add(StringUtils.strip(mapping, IGNORED_MAPPING_CHARACTERS));
85    }
86    }
87  16 if (!this.validServletMappings.isEmpty()) {
88  14 this.defaultServletMapping = this.validServletMappings.iterator().next();
89    }
90    }
91   
 
92  7 toggle @Override
93    public ExtendedURL normalize(ExtendedURL partialURL)
94    {
95  7 String contextPath = StringUtils.strip(getContextPath(), URL_SEGMENT_DELIMITER);
96  7 if (contextPath == null) {
97  1 throw new RuntimeException(String.format("Failed to normalize the URL [%s] since the "
98    + "application's Servlet context couldn't be computed.", partialURL));
99    }
100  6 List<String> segments = new ArrayList<>();
101  6 if (StringUtils.isNotEmpty(contextPath)) {
102  4 segments.add(contextPath);
103    }
104   
105  6 String servletPath = getActionServletMapping();
106  6 if (StringUtils.isNotEmpty(servletPath)) {
107  5 segments.add(servletPath);
108    }
109   
110  6 segments.addAll(partialURL.getSegments());
111   
112  6 return new ExtendedURL(segments, partialURL.getParameters());
113    }
114   
 
115  10 toggle protected String getContextPath()
116    {
117  10 if (this.environment instanceof ServletEnvironment) {
118  8 return ((ServletEnvironment) this.environment).getServletContext().getContextPath();
119    }
120  2 return null;
121    }
122   
123    /**
124    * Get the path prefix used for the Struts Action Servlet, either a prefix similar to the one used in the current
125    * request if it also passes through the Action servlet, or using the default path configured for it.
126    *
127    * @return a path used for triggering the Struts Action Servlet (may be the empty string)
128    */
 
129  14 toggle protected String getActionServletMapping()
130    {
131  14 String result = this.defaultServletMapping;
132  14 if (this.container.getRequest() instanceof ServletRequest) {
133  4 HttpServletRequest hsRequest = ((ServletRequest) this.container.getRequest()).getHttpServletRequest();
134  4 result = StringUtils.strip(hsRequest.getServletPath(), IGNORED_MAPPING_CHARACTERS);
135   
136  4 if (!this.validServletMappings.contains(result)) {
137    // The current request doesn't pass through the Action servlet, don't reuse the path prefix
138  1 result = this.defaultServletMapping;
139    }
140    }
141   
142  14 return result;
143    }
144    }