1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal

File DefaultXWikiStubContextProvider.java

 

Coverage histogram

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

Code metrics

12
37
2
1
144
78
8
0.22
18.5
2
4

Classes

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

Contributing tests

This file is covered by 34 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: 02413562ef4095c503f1c52968e8ce6678991577 $
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  474 toggle @Override
67    public void initialize(XWikiContext context)
68    {
69  474 XWikiContext newContext = context.clone();
70   
71  474 newContext.setCacheDuration(0);
72   
73  474 newContext.setUserReference(null);
74  474 newContext.setLanguage(null);
75  474 newContext.setWikiId(context.getMainXWiki());
76   
77    // Cleanup
78  474 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  474 if (newContext.getRequest() != null) {
83  238 XWikiServletRequestStub initialRequest = new XWikiServletRequestStub();
84  238 initialRequest.setHost(newContext.getRequest().getHeader("x-forwarded-host"));
85  238 initialRequest.setScheme(newContext.getRequest().getScheme());
86  238 XWikiServletRequest request = new XWikiServletRequest(initialRequest);
87  238 newContext.setRequest(request);
88    }
89   
90    // Get rid of the real response
91  474 if (newContext.getResponse() != null) {
92  371 XWikiServletResponseStub initialResponse = new XWikiServletResponseStub();
93    // anything to keep ?
94  371 XWikiServletResponse response = new XWikiServletResponse(initialResponse);
95  371 newContext.setResponse(response);
96    }
97   
98    // Set the URL Factory so that the URL Factory doesn't depend of the mode of the first action that entered
99    // the system (Servlet, PDF, GWT, Portlet, etc).
100    // We decide arbitrarily to use the Servlet URL Factory since it's the "standard" factory.
101  474 if (newContext.getURLFactory() != null) {
102  32 XWikiURLFactory urlf =
103    newContext.getWiki().getURLFactoryService().createURLFactory(XWikiContext.MODE_SERVLET, context);
104  32 newContext.setURLFactory(urlf);
105    }
106   
107  474 this.initialContext = newContext;
108   
109  474 this.logger.debug("Stub context initialized.");
110    }
111   
 
112  7276 toggle @Override
113    public XWikiContext createStubContext()
114    {
115  7273 XWikiContext stubContext;
116   
117  7274 if (this.initialContext != null) {
118  6805 stubContext = this.initialContext.clone();
119   
120    // We make sure to not share the same Request instance with several threads
121  6808 if (this.initialContext.getRequest() != null) {
122  6763 XWikiServletRequestStub stubRequest = new XWikiServletRequestStub();
123  6763 stubRequest.setHost(this.initialContext.getRequest().getHeader("x-forwarded-host"));
124  6763 stubRequest.setScheme(this.initialContext.getRequest().getScheme());
125  6763 XWikiServletRequest request = new XWikiServletRequest(stubRequest);
126  6763 stubContext.setRequest(request);
127    }
128   
129    // We make sure to not share the same Response instance with several threads
130  6808 if (this.initialContext.getResponse() != null) {
131  6763 XWikiServletResponseStub stubResponse = new XWikiServletResponseStub();
132  6763 XWikiServletResponse response = new XWikiServletResponse(stubResponse);
133  6763 stubContext.setResponse(response);
134    }
135   
136    // We make sure to not share the same document instance with several threads
137  6808 stubContext.setDoc(new XWikiDocument(this.defaultDocumentReferenceProvider.get()));
138    } else {
139  471 stubContext = null;
140    }
141   
142  7275 return stubContext;
143    }
144    }