1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimacro.internal

File WikiMacroExecutionEventListener.java

 

Coverage histogram

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

Code metrics

6
19
6
1
141
78
10
0.53
3.17
6
1.67

Classes

Class Line # Actions
WikiMacroExecutionEventListener 50 19 0% 10 2
0.935483993.5%
 

Contributing tests

This file is covered by 4 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.rendering.wikimacro.internal;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24    import java.util.Stack;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.slf4j.Logger;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.context.Execution;
33    import org.xwiki.context.ExecutionContext;
34    import org.xwiki.observation.EventListener;
35    import org.xwiki.observation.event.Event;
36    import org.xwiki.rendering.macro.wikibridge.WikiMacro;
37    import org.xwiki.rendering.macro.wikibridge.WikiMacroExecutionFinishedEvent;
38    import org.xwiki.rendering.macro.wikibridge.WikiMacroExecutionStartsEvent;
39    import org.xwiki.security.authorization.AuthorExecutor;
40   
41    /**
42    * Make sure to execute wiki macro with a properly configured context and especially which user programming right is
43    * tested on.
44    *
45    * @version $Id: 6b5f3333f2e3fd668143b0d57af23a688c72149a $
46    */
47    @Component
48    @Singleton
49    @Named("WikiMacroExecutionEventListener")
 
50    public class WikiMacroExecutionEventListener implements EventListener
51    {
52    /**
53    * The name of the listener.
54    */
55    private static final String NAME = "WikiMacroExecutionEventListener";
56   
57    private static final String SUCONTEXT_KEY = "wikimacro.backup.sucontext";
58   
59    /**
60    * The events to match.
61    */
62    private static final List<Event> EVENTS = new ArrayList<Event>()
63    {
 
64  20 toggle {
65  20 add(new WikiMacroExecutionStartsEvent());
66  20 add(new WikiMacroExecutionFinishedEvent());
67    }
68    };
69   
70    @Inject
71    private Execution execution;
72   
73    @Inject
74    private AuthorExecutor suExecutor;
75   
76    /**
77    * The logger to log.
78    */
79    @Inject
80    private Logger logger;
81   
 
82  23 toggle @Override
83    public List<Event> getEvents()
84    {
85  23 return EVENTS;
86    }
87   
 
88  92 toggle @Override
89    public String getName()
90    {
91  92 return NAME;
92    }
93   
 
94  292 toggle @Override
95    public void onEvent(Event event, Object source, Object data)
96    {
97  292 if (event instanceof WikiMacroExecutionStartsEvent) {
98  146 onWikiMacroExecutionStartsEvent((WikiMacro) source);
99    } else {
100  146 onWikiMacroExecutionFinishedEvent();
101    }
102    }
103   
104    /**
105    * Called when receiving a {@link WikiMacroExecutionStartsEvent} event.
106    *
107    * @param wikiMacro the wiki macro sending the event
108    */
 
109  146 toggle public void onWikiMacroExecutionStartsEvent(WikiMacro wikiMacro)
110    {
111    // Modify the context for that following code is executed with the right of wiki macro author
112  146 AutoCloseable sucontext = this.suExecutor.before(wikiMacro.getAuthorReference());
113   
114    // Put it in an hidden context property to restore it later
115  146 ExecutionContext econtext = this.execution.getContext();
116    // Use a stack in case a wiki macro calls another wiki macro
117  146 Stack<AutoCloseable> backup = (Stack<AutoCloseable>) econtext.getProperty(SUCONTEXT_KEY);
118  146 if (backup == null) {
119  93 backup = new Stack<AutoCloseable>();
120  93 econtext.setProperty(SUCONTEXT_KEY, backup);
121    }
122  146 backup.push(sucontext);
123    }
124   
125    /**
126    * Called when receiving a {@link WikiMacroExecutionFinishedEvent} event.
127    */
 
128  146 toggle public void onWikiMacroExecutionFinishedEvent()
129    {
130    // Get the su context to restore
131  146 ExecutionContext econtext = this.execution.getContext();
132    // Use a stack in case a wiki macro calls another wiki macro
133  146 Stack<AutoCloseable> backup = (Stack<AutoCloseable>) econtext.getProperty(SUCONTEXT_KEY);
134  146 if (backup != null && !backup.isEmpty()) {
135    // Restore the context execution rights
136  146 this.suExecutor.after(backup.pop());
137    } else {
138  0 this.logger.error("Can't find any backed up execution right information in the execution context");
139    }
140    }
141    }