| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.wysiwyg.server.filter; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
import java.lang.reflect.Type; |
| 24 |
|
|
| 25 |
|
import javax.servlet.Filter; |
| 26 |
|
import javax.servlet.FilterChain; |
| 27 |
|
import javax.servlet.FilterConfig; |
| 28 |
|
import javax.servlet.ServletException; |
| 29 |
|
import javax.servlet.ServletRequest; |
| 30 |
|
import javax.servlet.ServletResponse; |
| 31 |
|
import javax.servlet.http.HttpServletRequest; |
| 32 |
|
import javax.servlet.http.HttpServletResponse; |
| 33 |
|
|
| 34 |
|
import org.xwiki.container.Container; |
| 35 |
|
import org.xwiki.container.servlet.ServletContainerException; |
| 36 |
|
import org.xwiki.container.servlet.ServletContainerInitializer; |
| 37 |
|
import org.xwiki.context.Execution; |
| 38 |
|
import org.xwiki.model.reference.DocumentReference; |
| 39 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 40 |
|
import org.xwiki.model.reference.SpaceReference; |
| 41 |
|
import org.xwiki.model.reference.WikiReference; |
| 42 |
|
|
| 43 |
|
import com.xpn.xwiki.XWiki; |
| 44 |
|
import com.xpn.xwiki.XWikiContext; |
| 45 |
|
import com.xpn.xwiki.XWikiException; |
| 46 |
|
import com.xpn.xwiki.user.api.XWikiRightService; |
| 47 |
|
import com.xpn.xwiki.user.api.XWikiUser; |
| 48 |
|
import com.xpn.xwiki.web.Utils; |
| 49 |
|
import com.xpn.xwiki.web.XWikiServletContext; |
| 50 |
|
import com.xpn.xwiki.web.XWikiServletRequest; |
| 51 |
|
import com.xpn.xwiki.web.XWikiServletResponse; |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
@version |
| 57 |
|
|
| |
|
| 91.4% |
Uncovered Elements: 5 (58) |
Complexity: 14 |
Complexity Density: 0.33 |
|
| 58 |
|
public class XWikiContextInitializationFilter implements Filter |
| 59 |
|
{ |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
private FilterConfig filterConfig; |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
private int mode; |
| 69 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 70 |
64 |
@Override... |
| 71 |
|
public void destroy() |
| 72 |
|
{ |
| 73 |
64 |
this.filterConfig = null; |
| 74 |
|
} |
| 75 |
|
|
| |
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 76 |
1712 |
@Override... |
| 77 |
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, |
| 78 |
|
ServletException |
| 79 |
|
{ |
| 80 |
1712 |
try { |
| 81 |
|
|
| 82 |
1712 |
if (request instanceof HttpServletRequest) { |
| 83 |
1712 |
initializeXWikiContext(request, response); |
| 84 |
|
} |
| 85 |
1712 |
chain.doFilter(request, response); |
| 86 |
|
} finally { |
| 87 |
1712 |
if (request instanceof HttpServletRequest) { |
| 88 |
1712 |
cleanupComponents(); |
| 89 |
|
} |
| 90 |
|
} |
| 91 |
|
} |
| 92 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 93 |
64 |
@Override... |
| 94 |
|
public void init(FilterConfig filterConfig) throws ServletException |
| 95 |
|
{ |
| 96 |
64 |
this.filterConfig = filterConfig; |
| 97 |
|
|
| 98 |
64 |
try { |
| 99 |
64 |
this.mode = Integer.parseInt(filterConfig.getInitParameter("mode")); |
| 100 |
|
} catch (Exception e) { |
| 101 |
32 |
this.mode = -1; |
| 102 |
|
} |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
@param |
| 109 |
|
@param |
| 110 |
|
@throws |
| 111 |
|
|
| |
|
| 92% |
Uncovered Elements: 2 (25) |
Complexity: 5 |
Complexity Density: 0.26 |
|
| 112 |
1712 |
protected void initializeXWikiContext(ServletRequest request, ServletResponse response) throws ServletException... |
| 113 |
|
{ |
| 114 |
1712 |
try { |
| 115 |
|
|
| 116 |
1712 |
String action = ""; |
| 117 |
1712 |
XWikiServletContext xwikiEngine = new XWikiServletContext(this.filterConfig.getServletContext()); |
| 118 |
1712 |
XWikiServletRequest xwikiRequest = new XWikiServletRequest((HttpServletRequest) request); |
| 119 |
1712 |
XWikiServletResponse xwikiResponse = new XWikiServletResponse((HttpServletResponse) response); |
| 120 |
|
|
| 121 |
|
|
| 122 |
1712 |
XWikiContext context = Utils.prepareContext(action, xwikiRequest, xwikiResponse, xwikiEngine); |
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
1712 |
if (this.mode >= 0) { |
| 127 |
1709 |
context.setMode(this.mode); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
1712 |
initializeContainerComponent(context); |
| 133 |
|
|
| 134 |
|
|
| 135 |
1712 |
XWiki xwiki = XWiki.getXWiki(context); |
| 136 |
|
|
| 137 |
|
|
| 138 |
1712 |
context.setURLFactory(xwiki.getURLFactoryService().createURLFactory(context.getMode(), context)); |
| 139 |
|
|
| 140 |
|
|
| 141 |
1712 |
xwiki.prepareResources(context); |
| 142 |
|
|
| 143 |
|
|
| 144 |
1712 |
XWikiUser user = context.getWiki().checkAuth(context); |
| 145 |
1712 |
if (user != null) { |
| 146 |
773 |
DocumentReferenceResolver<String> documentReferenceResolver = |
| 147 |
|
Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "explicit"); |
| 148 |
773 |
SpaceReference defaultUserSpace = |
| 149 |
|
new SpaceReference(XWiki.SYSTEM_SPACE, new WikiReference(context.getWikiId())); |
| 150 |
773 |
DocumentReference userReference = documentReferenceResolver.resolve(user.getUser(), defaultUserSpace); |
| 151 |
773 |
context.setUserReference(XWikiRightService.GUEST_USER.equals(userReference.getName()) ? null |
| 152 |
|
: userReference); |
| 153 |
|
} |
| 154 |
|
} catch (XWikiException e) { |
| 155 |
0 |
throw new ServletException("Failed to initialize the XWiki context.", e); |
| 156 |
|
} |
| 157 |
|
} |
| 158 |
|
|
| 159 |
|
|
| 160 |
|
@param |
| 161 |
|
@throws |
| 162 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 163 |
1712 |
protected void initializeContainerComponent(XWikiContext context) throws ServletException... |
| 164 |
|
{ |
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
1712 |
ServletContainerInitializer containerInitializer = Utils.getComponent((Type) ServletContainerInitializer.class); |
| 169 |
|
|
| 170 |
1712 |
try { |
| 171 |
1712 |
containerInitializer.initializeRequest(context.getRequest().getHttpServletRequest(), context); |
| 172 |
1712 |
containerInitializer.initializeResponse(context.getResponse()); |
| 173 |
1712 |
containerInitializer.initializeSession(context.getRequest().getHttpServletRequest()); |
| 174 |
|
} catch (ServletContainerException e) { |
| 175 |
0 |
throw new ServletException("Failed to initialize Request/Response or Session", e); |
| 176 |
|
} |
| 177 |
|
} |
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 183 |
1712 |
protected void cleanupComponents()... |
| 184 |
|
{ |
| 185 |
1712 |
Container container = Utils.getComponent((Type) Container.class); |
| 186 |
1712 |
container.removeRequest(); |
| 187 |
1712 |
container.removeResponse(); |
| 188 |
1712 |
container.removeSession(); |
| 189 |
|
|
| 190 |
1712 |
Execution execution = Utils.getComponent((Type) Execution.class); |
| 191 |
1712 |
execution.removeContext(); |
| 192 |
|
} |
| 193 |
|
} |