1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.script

File AbstractExtensionScriptService.java

 

Coverage histogram

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

Code metrics

6
18
8
1
186
91
12
0.67
2.25
8
1.5

Classes

Class Line # Actions
AbstractExtensionScriptService 48 18 0% 12 0
1.0100%
 

Contributing tests

This file is covered by 12 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 org.xwiki.extension.script;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Provider;
27   
28    import org.xwiki.bridge.DocumentAccessBridge;
29    import org.xwiki.context.Execution;
30    import org.xwiki.extension.internal.validator.AbstractExtensionValidator;
31    import org.xwiki.job.AbstractRequest;
32    import org.xwiki.job.JobExecutor;
33    import org.xwiki.job.event.status.JobStatus;
34    import org.xwiki.job.script.JobScriptService;
35    import org.xwiki.script.internal.safe.ScriptSafeProvider;
36    import org.xwiki.script.service.ScriptService;
37    import org.xwiki.security.authorization.ContextualAuthorizationManager;
38   
39    import com.xpn.xwiki.XWikiContext;
40    import com.xpn.xwiki.doc.XWikiDocument;
41   
42    /**
43    * Base class for all extension related script services.
44    *
45    * @version $Id: 26363f4e79f0188334b49e4813f0403022584e08 $
46    * @since 5.3M1
47    */
 
48    public abstract class AbstractExtensionScriptService implements ScriptService
49    {
50    /**
51    * The key under which the last encountered error is stored in the current execution context.
52    */
53    public static final String EXTENSIONERROR_KEY = "scriptservice.extension.error";
54   
55    /**
56    * Extension request property that specifies from which wiki the job was started.
57    */
58    protected static final String PROPERTY_CONTEXT_WIKI = "context.wiki";
59   
60    /**
61    * Extension request property that specifies from which document action the job was started.
62    */
63    protected static final String PROPERTY_CONTEXT_ACTION = "context.action";
64   
65    protected static final String PROPERTY_USERREFERENCE = AbstractExtensionValidator.PROPERTY_USERREFERENCE;
66   
67    protected static final String PROPERTY_CALLERREFERENCE = AbstractExtensionValidator.PROPERTY_CALLERREFERENCE;
68   
69    protected static final String PROPERTY_CHECKRIGHTS = AbstractExtensionValidator.PROPERTY_CHECKRIGHTS;
70   
71    /**
72    * The prefix used for wiki namespace id.
73    */
74    protected static final String WIKI_NAMESPACE_PREFIX = "wiki:";
75   
76    @Inject
77    @SuppressWarnings("rawtypes")
78    protected ScriptSafeProvider scriptProvider;
79   
80    /**
81    * Provides access to the current context.
82    */
83    @Inject
84    protected Execution execution;
85   
86    /**
87    * Needed for getting the current user reference.
88    */
89    @Inject
90    protected DocumentAccessBridge documentAccessBridge;
91   
92    @Inject
93    protected Provider<XWikiContext> xcontextProvider;
94   
95    @Inject
96    protected JobExecutor jobExecutor;
97   
98    @Inject
99    protected ContextualAuthorizationManager authorization;
100   
101    @Inject
102    @Named("job")
103    private ScriptService jobScriptService;
104   
105    /**
106    * @param <T> the type of the object
107    * @param unsafe the unsafe object
108    * @return the safe version of the passed object
109    */
 
110  1900 toggle @SuppressWarnings("unchecked")
111    protected <T> T safe(T unsafe)
112    {
113  1900 return (T) this.scriptProvider.get(unsafe);
114    }
115   
 
116  58 toggle protected <T extends AbstractRequest> void setRightsProperties(T extensionRequest)
117    {
118  58 extensionRequest.setProperty(AbstractExtensionValidator.PROPERTY_CHECKRIGHTS, true);
119  58 extensionRequest.setProperty(AbstractExtensionValidator.PROPERTY_USERREFERENCE,
120    this.documentAccessBridge.getCurrentUserReference());
121  58 XWikiDocument callerDocument = getCallerDocument();
122  58 if (callerDocument != null) {
123  36 extensionRequest.setProperty(AbstractExtensionValidator.PROPERTY_CALLERREFERENCE,
124    callerDocument.getContentAuthorReference());
125    }
126    }
127   
 
128  58 toggle protected XWikiDocument getCallerDocument()
129    {
130  58 XWikiContext xcontext = this.xcontextProvider.get();
131  58 XWikiDocument sdoc = (XWikiDocument) xcontext.get("sdoc");
132  58 if (sdoc == null) {
133  22 sdoc = xcontext.getDoc();
134    }
135   
136  58 return sdoc;
137    }
138   
 
139  4060 toggle protected JobStatus getJobStatus(List<String> jobId)
140    {
141  4060 return ((JobScriptService) jobScriptService).getJobStatus(jobId);
142    }
143   
144    /**
145    * @since 8.1M1
146    */
 
147  41 toggle protected String toWikiId(String namespace)
148    {
149  41 if (namespace != null && namespace.startsWith(WIKI_NAMESPACE_PREFIX)) {
150  36 return namespace.substring(WIKI_NAMESPACE_PREFIX.length());
151    }
152   
153  5 return null;
154    }
155   
156    /**
157    * @since 8.1M1
158    */
 
159  15 toggle protected String fromWikitoNamespace(String wiki)
160    {
161  15 return WIKI_NAMESPACE_PREFIX + wiki;
162    }
163   
164    // Error management
165   
166    /**
167    * Get the error generated while performing the previously called action.
168    *
169    * @return an eventual exception or {@code null} if no exception was thrown
170    */
 
171  21 toggle public Exception getLastError()
172    {
173  21 return (Exception) this.execution.getContext().getProperty(EXTENSIONERROR_KEY);
174    }
175   
176    /**
177    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
178    *
179    * @param e the exception to store, can be {@code null} to clear the previously stored exception
180    * @see #getLastError()
181    */
 
182  431 toggle protected void setError(Exception e)
183    {
184  431 this.execution.getContext().setProperty(EXTENSIONERROR_KEY, e);
185    }
186    }