1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal

File DefaultXWikiStubContextProvider.java

 

Coverage histogram

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

Code metrics

12
33
2
1
140
74
8
0.24
16.5
2
4

Classes

Class Line # Actions
DefaultXWikiStubContextProvider 50 33 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 37 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 com.xpn.xwiki.internal;
21   
22    import javax.inject.Inject;
23    import javax.inject.Provider;
24    import javax.inject.Singleton;
25   
26    import org.slf4j.Logger;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.model.reference.DocumentReference;
29   
30    import com.xpn.xwiki.XWikiContext;
31    import com.xpn.xwiki.doc.XWikiDocument;
32    import com.xpn.xwiki.util.XWikiStubContextProvider;
33    import com.xpn.xwiki.web.XWikiServletRequest;
34    import com.xpn.xwiki.web.XWikiServletRequestStub;
35    import com.xpn.xwiki.web.XWikiServletResponse;
36    import com.xpn.xwiki.web.XWikiServletResponseStub;
37    import com.xpn.xwiki.web.XWikiURLFactory;
38   
39    /**
40    * Default implementation of XWikiStubContextProvider.
41    *
42    * @todo make DefaultXWikiStubContextProvider able to generate a stub context from scratch some way, it will need some
43    * refactor around XWiki class for this to be possible. The current limitation is that without a first request
44    * this provider is unusable.
45    * @version $Id: 717e4d576e305cdba85eae4971aad1f20e92e031 $
46    * @since 2.0M3
47    */
48    @Component
49    @Singleton
 
50    public class DefaultXWikiStubContextProvider implements XWikiStubContextProvider
51    {
52    /**
53    * The logger to log.
54    */
55    @Inject
56    private Logger logger;
57   
58    @Inject
59    private Provider<DocumentReference> defaultDocumentReferenceProvider;
60   
61    /**
62    * The initial stub XWikiContext.
63    */
64    private XWikiContext initialContext;
65   
 
66  528 toggle @Override
67    public void initialize(XWikiContext context)
68    {
69  528 XWikiContext newContext = context.clone();
70   
71  528 newContext.setCacheDuration(0);
72   
73  528 newContext.setUserReference(null);
74  528 newContext.setLocale(null);
75  528 newContext.setWikiId(context.getMainXWiki());
76   
77    // Cleanup
78  528 newContext.flushClassCache();
79   
80    // We are sure the context request is a real servlet request
81    // So we force the dummy request with the current host
82  528 if (newContext.getRequest() != null) {
83  290 XWikiServletRequestStub initialRequest = new XWikiServletRequestStub(context.getRequest());
84  290 XWikiServletRequest request = new XWikiServletRequest(initialRequest);
85  290 newContext.setRequest(request);
86    }
87   
88    // Get rid of the real response
89  528 if (newContext.getResponse() != null) {
90  413 XWikiServletResponseStub initialResponse = new XWikiServletResponseStub();
91    // anything to keep ?
92  413 XWikiServletResponse response = new XWikiServletResponse(initialResponse);
93  413 newContext.setResponse(response);
94    }
95   
96    // Set the URL Factory so that the URL Factory doesn't depend of the mode of the first action that entered
97    // the system (Servlet, PDF, Portlet, etc).
98    // We decide arbitrarily to use the Servlet URL Factory since it's the "standard" factory.
99  528 if (newContext.getURLFactory() != null) {
100  39 XWikiURLFactory urlf =
101    newContext.getWiki().getURLFactoryService().createURLFactory(XWikiContext.MODE_SERVLET, context);
102  39 newContext.setURLFactory(urlf);
103    }
104   
105  528 this.initialContext = newContext;
106   
107  528 this.logger.debug("Stub context initialized.");
108    }
109   
 
110  138503 toggle @Override
111    public XWikiContext createStubContext()
112    {
113  138550 XWikiContext stubContext;
114   
115  138567 if (this.initialContext != null) {
116  138052 stubContext = this.initialContext.clone();
117   
118    // We make sure to not share the same Request instance with several threads
119  138075 if (this.initialContext.getRequest() != null) {
120  138028 XWikiServletRequestStub stubRequest = new XWikiServletRequestStub(this.initialContext.getRequest());
121  137999 XWikiServletRequest request = new XWikiServletRequest(stubRequest);
122  138009 stubContext.setRequest(request);
123    }
124   
125    // We make sure to not share the same Response instance with several threads
126  138034 if (this.initialContext.getResponse() != null) {
127  137992 XWikiServletResponseStub stubResponse = new XWikiServletResponseStub();
128  137973 XWikiServletResponse response = new XWikiServletResponse(stubResponse);
129  138006 stubContext.setResponse(response);
130    }
131   
132    // We make sure to not share the same document instance with several threads
133  138056 stubContext.setDoc(new XWikiDocument(this.defaultDocumentReferenceProvider.get()));
134    } else {
135  511 stubContext = null;
136    }
137   
138  138581 return stubContext;
139    }
140    }