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

File ResourceReferenceHandlerAuthenticatorListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

2
6
2
1
83
42
4
0.67
3
2
2

Classes

Class Line # Actions
ResourceReferenceHandlerAuthenticatorListener 50 6 0% 4 1
0.990%
 

Contributing tests

This file is covered by 69 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.resource;
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.xwiki.component.annotation.Component;
28    import org.xwiki.context.Execution;
29    import org.xwiki.context.ExecutionContext;
30    import org.xwiki.observation.AbstractEventListener;
31    import org.xwiki.observation.event.Event;
32    import org.xwiki.resource.ResourceReferenceHandler;
33    import org.xwiki.resource.annotations.Authenticate;
34    import org.xwiki.resource.events.ResourceReferenceHandlingEvent;
35   
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.XWikiContextInitializer;
38    import com.xpn.xwiki.XWikiException;
39   
40    /**
41    * Automatically initialize and authenticate a {@link XWikiContext} around {@link ResourceReferenceHandler} execution
42    * requiring it.
43    *
44    * @version $Id: f1d21c194bb80c024b6d3ec96c5a4c75baae45eb $
45    * @since 9.11RC1
46    */
47    @Component
48    @Named("ResourceReferenceHandlerAuthenticatorListener")
49    @Singleton
 
50    public class ResourceReferenceHandlerAuthenticatorListener extends AbstractEventListener
51    {
52    @Inject
53    private Provider<XWikiContextInitializer> xcontextInitializeProvider;
54   
55    @Inject
56    private Execution execution;
57   
58    /**
59    * The default constructor.
60    */
 
61  216 toggle public ResourceReferenceHandlerAuthenticatorListener()
62    {
63  216 super("ResourceReferenceHandlerAuthenticatorListener", new ResourceReferenceHandlingEvent());
64    }
65   
 
66  7704 toggle @Override
67    public void onEvent(Event event, Object source, Object data)
68    {
69  7718 if (source.getClass().getAnnotation(Authenticate.class) != null) {
70  1 ExecutionContext econtext = this.execution.getContext();
71   
72    // Initialize a new XWikiContext no matter one since the one in place is generally a stub context
73    // (automatically initialized with the ExecutionContext)
74    // TODO: find a way to avoid hacks like this one
75  1 try {
76    // Initialize and authenticate the XWikiContext
77  1 this.xcontextInitializeProvider.get().authenticate().initialize(econtext);
78    } catch (XWikiException e) {
79  0 throw new RuntimeException("Failed to initialize and authenticate the XWiki context", e);
80    }
81    }
82    }
83    }