1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.activeinstalls.internal.client.data

File ServletContainerPingDataProvider.java

 

Coverage histogram

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

Code metrics

2
17
2
1
106
62
4
0.24
8.5
2
2

Classes

Class Line # Actions
ServletContainerPingDataProvider 47 17 0% 4 2
0.904761990.5%
 

Contributing tests

This file is covered by 2 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.activeinstalls.internal.client.data;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28    import javax.servlet.ServletContext;
29   
30    import org.apache.commons.lang.StringUtils;
31    import org.apache.commons.lang.exception.ExceptionUtils;
32    import org.slf4j.Logger;
33    import org.xwiki.activeinstalls.internal.client.PingDataProvider;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.environment.Environment;
36    import org.xwiki.environment.internal.ServletEnvironment;
37   
38    /**
39    * Provide the Servlet Container's name and version.
40    *
41    * @version $Id: 8d0446675a56cb24215711687b951488ce1ba69f $
42    * @since 6.1M1
43    */
44    @Component
45    @Named("servlet")
46    @Singleton
 
47    public class ServletContainerPingDataProvider implements PingDataProvider
48    {
49    private static final String SERVLET_INFO_VERSION_SEPARATOR = "/";
50   
51    private static final String SERVLET_INFO_OPTIONALSEPARATOR = "(";
52   
53    private static final String PROPERTY_SERVLET_CONTAINER_NAME = "servletContainerName";
54   
55    private static final String PROPERTY_SERVLET_CONTAINER_VERSION = "servletContainerVersion";
56   
57    /**
58    * Used to access the Servlet Context.
59    */
60    @Inject
61    private Environment environment;
62   
63    @Inject
64    private Logger logger;
65   
 
66  2 toggle @Override
67    public Map<String, Object> provideMapping()
68    {
69  2 Map<String, Object> map = new HashMap<>();
70  2 map.put("type", "string");
71  2 map.put("index", "not_analyzed");
72   
73  2 Map<String, Object> propertiesMap = new HashMap<>();
74  2 propertiesMap.put(PROPERTY_SERVLET_CONTAINER_NAME, map);
75  2 propertiesMap.put(PROPERTY_SERVLET_CONTAINER_VERSION, map);
76   
77  2 return propertiesMap;
78    }
79   
 
80  2 toggle @Override
81    public Map<String, Object> provideData()
82    {
83  2 Map<String, Object> jsonMap = new HashMap<>();
84  2 if (this.environment instanceof ServletEnvironment) {
85  2 ServletEnvironment servletEnvironment = (ServletEnvironment) this.environment;
86  2 try {
87  2 ServletContext servletContext = servletEnvironment.getServletContext();
88    // Format of getServerInfo() is "name/version (text)" where " (text)" is optional.
89  2 String serverInfo = servletContext.getServerInfo();
90  2 jsonMap.put(PROPERTY_SERVLET_CONTAINER_NAME,
91    StringUtils.trim(StringUtils.substringBefore(serverInfo, SERVLET_INFO_VERSION_SEPARATOR)));
92  2 jsonMap.put(PROPERTY_SERVLET_CONTAINER_VERSION, StringUtils.trim(StringUtils.substringBefore(
93    StringUtils.substringAfter(serverInfo, SERVLET_INFO_VERSION_SEPARATOR),
94    SERVLET_INFO_OPTIONALSEPARATOR)));
95    } catch (Throwable e) {
96    // Ignore, we just don't save that information...
97    // However we log a warning since it's a problem that needs to be seen and looked at.
98  0 this.logger.warn("Failed to compute Servlet container information. "
99    + "This information has not been added to the Active Installs ping data. Reason [{}]",
100    ExceptionUtils.getRootCauseMessage(e)
101    );
102    }
103    }
104  2 return jsonMap;
105    }
106    }