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

File JobRequestContextInitializer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

22
32
4
1
140
88
17
0.53
8
4
4.25

Classes

Class Line # Actions
JobRequestContextInitializer 51 32 0% 17 14
0.758620775.9%
 

Contributing tests

This file is covered by 91 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.job;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Provider;
25    import javax.inject.Singleton;
26   
27    import org.slf4j.Logger;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.context.concurrent.ContextStore;
30    import org.xwiki.job.event.JobStartedEvent;
31    import org.xwiki.observation.AbstractEventListener;
32    import org.xwiki.observation.event.Event;
33   
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.XWikiException;
36    import com.xpn.xwiki.doc.XWikiDocument;
37    import com.xpn.xwiki.job.JobRequestContext;
38    import com.xpn.xwiki.web.XWikiServletRequestStub;
39   
40    /**
41    * Automatically set various context values before the actual job start.
42    *
43    * @version $Id: 432ffc80733ce431f5da84e62e35c8f66dbd7721 $
44    * @since 8.4RC1
45    * @deprecated since 10.9RC1, use {@link ContextStore} instead
46    */
47    @Component
48    @Named("com.xpn.xwiki.internal.job.JobRequestContextInitializer")
49    @Singleton
50    @Deprecated
 
51    public class JobRequestContextInitializer extends AbstractEventListener
52    {
53    @Inject
54    private Provider<XWikiContext> xcontextProvider;
55   
56    @Inject
57    private Logger logger;
58   
59    /**
60    * The default constructor.
61    */
 
62  217 toggle public JobRequestContextInitializer()
63    {
64  217 super(JobRequestContextInitializer.class.getName(), new JobStartedEvent());
65    }
66   
 
67  21624 toggle @Override
68    public void onEvent(Event event, Object source, Object data)
69    {
70  21628 JobStartedEvent jobStartedEvent = (JobStartedEvent) event;
71   
72  21627 JobRequestContext jobRequestContext = jobStartedEvent.getRequest().getProperty(JobRequestContext.KEY);
73   
74  21629 if (jobRequestContext != null) {
75  52 XWikiContext xcontext = this.xcontextProvider.get();
76   
77  52 if (xcontext != null) {
78    // Wiki id
79  52 if (jobRequestContext.isWikiIdSet()) {
80  52 xcontext.setWikiId(jobRequestContext.getWikiId());
81    }
82   
83    // User
84  52 if (jobRequestContext.isUserReferenceSet()) {
85  52 xcontext.setUserReference(jobRequestContext.getUserReference());
86    }
87   
88    // Document
89  52 if (jobRequestContext.isDocumentSet()) {
90  52 XWikiDocument document = getDocument(jobRequestContext, xcontext);
91  52 xcontext.setDoc(document);
92    }
93   
94    // Secure document
95  52 if (jobRequestContext.isSDocumentSet()) {
96  52 XWikiDocument sdocument = getSDocument(jobRequestContext, xcontext);
97  52 xcontext.put(XWikiDocument.CKEY_SDOC, sdocument);
98    }
99   
100    // Request
101  52 if (jobRequestContext.isRequestSet()) {
102  52 xcontext.setRequest(new XWikiServletRequestStub(jobRequestContext.getRequestURL(),
103    jobRequestContext.getRequestContextPath(), jobRequestContext.getRequestParameters()));
104    }
105    }
106    }
107    }
108   
 
109  52 toggle private XWikiDocument getDocument(JobRequestContext jobRequestContext, XWikiContext xcontext)
110    {
111  52 if (jobRequestContext.getDocument() != null) {
112  39 return jobRequestContext.getDocument();
113  13 } else if (jobRequestContext.getDocumentReference() != null) {
114  0 try {
115  0 return xcontext.getWiki().getDocument(jobRequestContext.getDocumentReference(), xcontext);
116    } catch (XWikiException e) {
117  0 this.logger.error("Failed to get document with reference [{}]",
118    jobRequestContext.getDocumentReference(), e);
119    }
120    }
121   
122  13 return null;
123    }
124   
 
125  52 toggle private XWikiDocument getSDocument(JobRequestContext jobRequestContext, XWikiContext xcontext)
126    {
127  52 if (jobRequestContext.getSDocument() != null) {
128  1 return jobRequestContext.getSDocument();
129  51 } else if (jobRequestContext.getSDocumentReference() != null) {
130  0 try {
131  0 return xcontext.getWiki().getDocument(jobRequestContext.getSDocumentReference(), xcontext);
132    } catch (XWikiException e) {
133  0 this.logger.error("Failed to get secure document with reference [{}]",
134    jobRequestContext.getSDocumentReference(), e);
135    }
136    }
137   
138  51 return null;
139    }
140    }