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

File FilterScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

2
47
17
1
288
171
22
0.47
2.76
17
1.29

Classes

Class Line # Actions
FilterScriptService 66 47 0% 22 26
0.606060660.6%
 

Contributing tests

No tests hitting this source file were found.

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.filter.script;
21   
22    import java.io.OutputStream;
23    import java.lang.reflect.Type;
24    import java.util.ArrayList;
25    import java.util.Collection;
26    import java.util.Collections;
27    import java.util.List;
28    import java.util.Map;
29   
30    import javax.inject.Inject;
31    import javax.inject.Named;
32    import javax.inject.Provider;
33    import javax.inject.Singleton;
34   
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.component.descriptor.ComponentDescriptor;
37    import org.xwiki.component.manager.ComponentManager;
38    import org.xwiki.filter.FilterStreamFactory;
39    import org.xwiki.filter.descriptor.FilterStreamDescriptor;
40    import org.xwiki.filter.input.InputFilterStreamFactory;
41    import org.xwiki.filter.internal.job.FilterStreamConverterJob;
42    import org.xwiki.filter.job.FilterStreamConverterJobRequest;
43    import org.xwiki.filter.output.DefaultOutputStreamOutputTarget;
44    import org.xwiki.filter.output.OutputFilterStreamFactory;
45    import org.xwiki.filter.output.OutputStreamOutputTarget;
46    import org.xwiki.filter.type.FilterStreamType;
47    import org.xwiki.job.Job;
48    import org.xwiki.job.JobExecutor;
49    import org.xwiki.script.service.ScriptService;
50    import org.xwiki.script.service.ScriptServiceManager;
51    import org.xwiki.security.authorization.ContextualAuthorizationManager;
52    import org.xwiki.security.authorization.Right;
53   
54    import com.xpn.xwiki.XWikiContext;
55    import com.xpn.xwiki.job.JobRequestContext;
56   
57    /**
58    * Expose various FilterStream related APIs to scripts.
59    *
60    * @version $Id: bedccd645354ebf3bfb534c20adbac9ede917532 $
61    * @since 6.2M1
62    */
63    @Component
64    @Named(FilterScriptService.ROLEHINT)
65    @Singleton
 
66    public class FilterScriptService extends AbstractFilterScriptService
67    {
68    public static final String ROLEHINT = "filter";
69   
70    @Inject
71    private ScriptServiceManager scriptServiceManager;
72   
73    @Inject
74    @Named("context")
75    private Provider<ComponentManager> componentManagerProvider;
76   
77    @Inject
78    private ContextualAuthorizationManager authorization;
79   
80    @Inject
81    private JobExecutor jobExecutor;
82   
83    @Inject
84    private Provider<XWikiContext> xcontextProvider;
85   
86    @Inject
87    @Named(FilterStreamConverterJob.JOBTYPE)
88    private Provider<Job> jobProvider;
89   
90    private Job lastJob;
91   
 
92  0 toggle public ScriptService get(String id)
93    {
94  0 return this.scriptServiceManager.get(ROLEHINT + '.' + id);
95    }
96   
97    /**
98    * @since 6.2M1
99    */
 
100  1 toggle public Job startConvert(FilterStreamType inputType, Map<String, Object> inputProperties,
101    FilterStreamType outputType, Map<String, Object> outputProperties)
102    {
103  1 return startConvert(inputType, inputProperties, outputType, true, outputProperties);
104    }
105   
106    /**
107    * @since 8.2RC1
108    */
 
109  1 toggle public Job startConvert(FilterStreamType inputType, Map<String, Object> inputProperties,
110    FilterStreamType outputType, boolean folded, Map<String, Object> outputProperties)
111    {
112  1 return convert(inputType, inputProperties, outputType, folded, outputProperties, true);
113    }
114   
115    /**
116    * @since 6.2M1
117    */
 
118  0 toggle public Job convert(FilterStreamType inputType, Map<String, Object> inputProperties, FilterStreamType outputType,
119    Map<String, Object> outputProperties)
120    {
121  0 return convert(inputType, inputProperties, outputType, true, outputProperties);
122    }
123   
124    /**
125    * @since 8.2RC1
126    */
 
127  0 toggle public Job convert(FilterStreamType inputType, Map<String, Object> inputProperties, FilterStreamType outputType,
128    boolean folded, Map<String, Object> outputProperties)
129    {
130  0 return convert(inputType, inputProperties, outputType, folded, outputProperties, false);
131    }
132   
 
133  1 toggle private Job convert(FilterStreamType inputType, Map<String, Object> inputProperties, FilterStreamType outputType,
134    boolean folded, Map<String, Object> outputProperties, boolean async)
135    {
136  1 resetError();
137   
138  1 this.lastJob = null;
139   
140  1 try {
141  1 this.authorization.checkAccess(Right.PROGRAM);
142   
143  1 FilterStreamConverterJobRequest request =
144    new FilterStreamConverterJobRequest(inputType, inputProperties, outputType, folded, outputProperties);
145   
146  1 if (async) {
147    // Give a few context related values to the job
148  1 JobRequestContext.set(request, this.xcontextProvider.get());
149   
150  1 this.lastJob = this.jobExecutor.execute(FilterStreamConverterJob.JOBTYPE, request);
151    } else {
152    // Not using the job executor to make sure to be executed in the current thread
153  0 this.lastJob = this.jobProvider.get();
154  0 this.lastJob.initialize(request);
155  0 this.lastJob.run();
156    }
157    } catch (Exception e) {
158  0 setError(e);
159    }
160   
161  1 return this.lastJob;
162    }
163   
164    /**
165    * @since 6.2M1
166    */
 
167  3 toggle public Job getCurrentJob()
168    {
169  3 return this.lastJob;
170    }
171   
172    /**
173    * @since 6.2M1
174    */
 
175  4 toggle private Collection<FilterStreamType> getAvailableStreams(Type factoryType)
176    {
177  4 resetError();
178   
179  4 try {
180  4 List<ComponentDescriptor<FilterStreamFactory>> descriptors =
181    this.componentManagerProvider.get().<FilterStreamFactory>getComponentDescriptorList(factoryType);
182   
183  4 List<FilterStreamType> types = new ArrayList<FilterStreamType>(descriptors.size());
184  4 for (ComponentDescriptor<FilterStreamFactory> descriptor : descriptors) {
185  24 types.add(FilterStreamType.unserialize(descriptor.getRoleHint()));
186    }
187   
188  4 Collections.sort(types);
189   
190  4 return types;
191    } catch (Exception e) {
192  0 setError(e);
193    }
194   
195  0 return null;
196    }
197   
198    /**
199    * @since 6.2M1
200    */
 
201  2 toggle public Collection<FilterStreamType> getAvailableInputStreams()
202    {
203  2 return getAvailableStreams(InputFilterStreamFactory.class);
204    }
205   
206    /**
207    * @since 6.2M1
208    */
 
209  2 toggle public Collection<FilterStreamType> getAvailableOutputStreams()
210    {
211  2 return getAvailableStreams(OutputFilterStreamFactory.class);
212    }
213   
214    /**
215    * @since 6.2M1
216    */
 
217  34 toggle private FilterStreamDescriptor getFilterStreamDescriptor(Type factoryType, FilterStreamType inputType)
218    {
219  34 resetError();
220   
221  34 try {
222  34 return this.componentManagerProvider.get()
223    .<FilterStreamFactory>getInstance(factoryType, inputType.serialize()).getDescriptor();
224    } catch (Exception e) {
225  2 setError(e);
226    }
227   
228  2 return null;
229    }
230   
231    /**
232    * @since 6.2M1
233    */
 
234  15 toggle public FilterStreamDescriptor getInputFilterStreamDescriptor(FilterStreamType inputType)
235    {
236  15 return getFilterStreamDescriptor(InputFilterStreamFactory.class, inputType);
237    }
238   
239    /**
240    * @since 6.2M1
241    */
 
242  19 toggle public FilterStreamDescriptor getOutputFilterStreamDescriptor(FilterStreamType inputType)
243    {
244  19 return getFilterStreamDescriptor(OutputFilterStreamFactory.class, inputType);
245    }
246   
247    /**
248    * @since 6.2M1
249    */
 
250  0 toggle private <F extends FilterStreamFactory> F getInputFilterStreamFactory(Type factoryType, FilterStreamType inputType)
251    {
252  0 resetError();
253   
254  0 try {
255  0 this.authorization.checkAccess(Right.PROGRAM);
256   
257  0 return this.componentManagerProvider.get().getInstance(factoryType, inputType.serialize());
258    } catch (Exception e) {
259  0 setError(e);
260    }
261   
262  0 return null;
263    }
264   
265    /**
266    * @since 6.2M1
267    */
 
268  0 toggle public InputFilterStreamFactory getInputFilterStreamFactory(FilterStreamType inputType)
269    {
270  0 return getInputFilterStreamFactory(InputFilterStreamFactory.class, inputType);
271    }
272   
273    /**
274    * @since 6.2M1
275    */
 
276  0 toggle public OutputFilterStreamFactory getOutputFilterStreamFactory(FilterStreamType outputType)
277    {
278  0 return getInputFilterStreamFactory(OutputFilterStreamFactory.class, outputType);
279    }
280   
281    /**
282    * @since 6.2M1
283    */
 
284  0 toggle public OutputStreamOutputTarget createOutputStreamOutputTarget(OutputStream stream, boolean autoclose)
285    {
286  0 return new DefaultOutputStreamOutputTarget(stream, autoclose);
287    }
288    }