| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.web; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
import java.util.Enumeration; |
| 24 |
|
|
| 25 |
|
import javax.servlet.Filter; |
| 26 |
|
import javax.servlet.FilterChain; |
| 27 |
|
import javax.servlet.FilterConfig; |
| 28 |
|
import javax.servlet.RequestDispatcher; |
| 29 |
|
import javax.servlet.ServletException; |
| 30 |
|
import javax.servlet.ServletRequest; |
| 31 |
|
import javax.servlet.ServletResponse; |
| 32 |
|
import javax.servlet.http.HttpServletRequest; |
| 33 |
|
|
| 34 |
|
import org.slf4j.Logger; |
| 35 |
|
import org.slf4j.LoggerFactory; |
| 36 |
|
import org.xwiki.configuration.ConfigurationSource; |
| 37 |
|
|
| 38 |
|
import com.xpn.xwiki.XWiki; |
| 39 |
|
import com.xpn.xwiki.internal.XWikiCfgConfigurationSource; |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
@link |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
@version |
| 59 |
|
@since |
| 60 |
|
|
| |
|
| 95.5% |
Uncovered Elements: 2 (44) |
Complexity: 11 |
Complexity Density: 0.39 |
|
| 61 |
|
public class ActionFilter implements Filter |
| 62 |
|
{ |
| 63 |
|
|
| 64 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(ActionFilter.class); |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
private static final String ACTION_PREFIX = "action_"; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
private static final String PATH_SEPARATOR = "/"; |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@link |
| 76 |
|
|
| 77 |
|
private static final String ATTRIBUTE_ACTION_DISPATCHED = ActionFilter.class.getName() + ".actionDispatched"; |
| 78 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 79 |
32 |
@Override... |
| 80 |
|
public void init(FilterConfig filterConfig) throws ServletException |
| 81 |
|
{ |
| 82 |
|
} |
| 83 |
|
|
| |
|
| 95.7% |
Uncovered Elements: 1 (23) |
Complexity: 6 |
Complexity Density: 0.4 |
|
| 84 |
9706 |
@SuppressWarnings("unchecked")... |
| 85 |
|
@Override |
| 86 |
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, |
| 87 |
|
ServletException |
| 88 |
|
{ |
| 89 |
|
|
| 90 |
9690 |
if (request instanceof HttpServletRequest |
| 91 |
|
&& !Boolean.valueOf((String) request.getAttribute(ATTRIBUTE_ACTION_DISPATCHED))) { |
| 92 |
9626 |
HttpServletRequest hrequest = (HttpServletRequest) request; |
| 93 |
9636 |
Enumeration<String> parameterNames = hrequest.getParameterNames(); |
| 94 |
23995 |
while (parameterNames.hasMoreElements()) { |
| 95 |
14419 |
String parameter = parameterNames.nextElement(); |
| 96 |
14430 |
if (parameter.startsWith(ACTION_PREFIX)) { |
| 97 |
59 |
String targetURL = getTargetURL(hrequest, parameter); |
| 98 |
59 |
RequestDispatcher dispatcher = hrequest.getRequestDispatcher(targetURL); |
| 99 |
59 |
if (dispatcher != null) { |
| 100 |
59 |
LOGGER.debug("Forwarding request to " + targetURL); |
| 101 |
59 |
request.setAttribute(ATTRIBUTE_ACTION_DISPATCHED, "true"); |
| 102 |
59 |
dispatcher.forward(hrequest, response); |
| 103 |
|
|
| 104 |
59 |
request.removeAttribute(ATTRIBUTE_ACTION_DISPATCHED); |
| 105 |
|
|
| 106 |
59 |
return; |
| 107 |
|
} |
| 108 |
|
} |
| 109 |
|
} |
| 110 |
|
} |
| 111 |
|
|
| 112 |
9644 |
chain.doFilter(request, response); |
| 113 |
|
} |
| 114 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 115 |
32 |
@Override... |
| 116 |
|
public void destroy() |
| 117 |
|
{ |
| 118 |
|
|
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
@link |
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
@param |
| 128 |
|
@param |
| 129 |
|
@return |
| 130 |
|
|
| 131 |
|
|
| |
|
| 94.1% |
Uncovered Elements: 1 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
| 132 |
59 |
private String getTargetURL(HttpServletRequest request, String action)... |
| 133 |
|
{ |
| 134 |
59 |
String newAction = PATH_SEPARATOR + action.substring(ACTION_PREFIX.length()); |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
59 |
String path = request.getRequestURI(); |
| 139 |
|
|
| 140 |
|
|
| 141 |
59 |
path = XWiki.stripSegmentFromPath(path, request.getContextPath()); |
| 142 |
|
|
| 143 |
|
|
| 144 |
59 |
String servletPath = request.getServletPath(); |
| 145 |
59 |
path = XWiki.stripSegmentFromPath(path, servletPath); |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
59 |
int index = path.indexOf(PATH_SEPARATOR, 1); |
| 151 |
|
|
| 152 |
|
|
| 153 |
59 |
ConfigurationSource configuration = |
| 154 |
|
Utils.getComponent(ConfigurationSource.class, XWikiCfgConfigurationSource.ROLEHINT); |
| 155 |
59 |
if ("1".equals(configuration.getProperty("xwiki.virtual.usepath", "1"))) { |
| 156 |
59 |
if (servletPath.equals(PATH_SEPARATOR |
| 157 |
|
+ configuration.getProperty("xwiki.virtual.usepath.servletpath", "wiki"))) { |
| 158 |
|
|
| 159 |
1 |
servletPath += path.substring(0, index); |
| 160 |
1 |
index = path.indexOf(PATH_SEPARATOR, index + 1); |
| 161 |
|
} |
| 162 |
|
} |
| 163 |
|
|
| 164 |
59 |
String document = path.substring(index); |
| 165 |
|
|
| 166 |
|
|
| 167 |
59 |
return servletPath + newAction + document; |
| 168 |
|
} |
| 169 |
|
} |