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

File DefaultEntityResourceActionLister.java

 

Coverage histogram

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

Code metrics

2
22
4
1
140
88
7
0.32
5.5
4
1.75

Classes

Class Line # Actions
DefaultEntityResourceActionLister 61 22 0% 7 2
0.928571492.9%
 

Contributing tests

This file is covered by 1 test. .

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.resource.internal.entity;
21   
22    import java.io.IOException;
23    import java.io.InputStream;
24    import java.io.StringReader;
25    import java.util.ArrayList;
26    import java.util.List;
27    import java.util.Map;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Provider;
32    import javax.inject.Singleton;
33   
34    import org.apache.commons.lang3.StringUtils;
35    import org.jdom2.Document;
36    import org.jdom2.Element;
37    import org.jdom2.JDOMException;
38    import org.jdom2.input.SAXBuilder;
39    import org.xml.sax.EntityResolver;
40    import org.xml.sax.InputSource;
41    import org.xml.sax.SAXException;
42    import org.xwiki.component.annotation.Component;
43    import org.xwiki.component.manager.ComponentLookupException;
44    import org.xwiki.component.manager.ComponentManager;
45    import org.xwiki.component.phase.Initializable;
46    import org.xwiki.component.phase.InitializationException;
47    import org.xwiki.component.util.DefaultParameterizedType;
48    import org.xwiki.environment.Environment;
49    import org.xwiki.resource.ResourceReferenceHandler;
50    import org.xwiki.resource.entity.EntityResourceAction;
51   
52    /**
53    * Takes into account both the old legacy way of defining Actions (in struts-config.xml) and the new way using
54    * {@link EntityResourceReferenceHandler}.
55    *
56    * @version $Id: 34b455c30f91b367cd1efe7c718ad9b041aebf78 $
57    * @since 7.2M1
58    */
59    @Component
60    @Singleton
 
61    public class DefaultEntityResourceActionLister implements EntityResourceActionLister, Initializable
62    {
63    private static final String STRUTS_CONFIG_RESOURCE = "/WEB-INF/struts-config.xml";
64   
65    private List<String> strutsActionNames;
66   
67    @Inject
68    private Environment environment;
69   
70    @Inject
71    @Named("context")
72    private Provider<ComponentManager> contextComponentManagerProvider;
73   
 
74  89 toggle @Override
75    public void initialize() throws InitializationException
76    {
77    // Parse the Struts config file (struts-config.xml) to extract all available actions
78  89 List<String> actionNames = new ArrayList<>();
79  89 SAXBuilder builder = new SAXBuilder();
80   
81    // Make sure we don't require an Internet Connection to parse the Struts config file!
82  89 builder.setEntityResolver(new EntityResolver() {
 
83  33 toggle @Override public InputSource resolveEntity(String publicId, String systemId)
84    throws SAXException, IOException
85    {
86  33 return new InputSource(new StringReader(""));
87    }
88    });
89   
90    // Step 1: Get a stream on the Struts config file if it exists
91  89 InputStream strutsConfigStream = this.environment.getResourceAsStream(getStrutsConfigResource());
92   
93  89 if (strutsConfigStream != null) {
94    // Step 2: Parse the Strust config file, looking for action names
95  33 Document document;
96  33 try {
97  33 document = builder.build(strutsConfigStream);
98    } catch (JDOMException | IOException e) {
99  0 throw new InitializationException(
100    String.format("Failed to parse Struts Config file [%s]", getStrutsConfigResource()), e);
101    }
102  33 Element mappingElement = document.getRootElement().getChild("action-mappings");
103  33 for (Element element : mappingElement.getChildren("action")) {
104    // We extract the action name from the path mapping. Note that we cannot use the "name" attribute since
105    // it's not reliable (it's not unique) and for example the sanveandcontinue action uses "save" as its
106    // "name" element value.
107  1781 actionNames.add(StringUtils.strip(element.getAttributeValue("path"), "/"));
108    }
109    }
110   
111  89 this.strutsActionNames = actionNames;
112    }
113   
 
114  18505 toggle @Override
115    public List<String> listActions()
116    {
117  18505 List<String> actionNames = new ArrayList<>(this.strutsActionNames);
118   
119    // Note: We don't cache the action names coming from Resource Reference Handlers as some extensions could
120    // contribute some.
121  18502 try {
122  18502 Map<String, ResourceReferenceHandler<EntityResourceAction>> componentMap =
123    this.contextComponentManagerProvider.get().getInstanceMap(
124    new DefaultParameterizedType(null, ResourceReferenceHandler.class, EntityResourceAction.class));
125  18507 for (String key : componentMap.keySet()) {
126  1 actionNames.add(key);
127    }
128    } catch (ComponentLookupException e) {
129    // This should not happen normally.
130  0 throw new RuntimeException("Failed to locate Resource Reference Handlers", e);
131    }
132   
133  18505 return actionNames;
134    }
135   
 
136  89 toggle protected String getStrutsConfigResource()
137    {
138  89 return STRUTS_CONFIG_RESOURCE;
139    }
140    }