1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.environment.internal

File ServletEnvironment.java

 

Coverage histogram

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

Code metrics

6
18
5
1
104
61
10
0.56
3.6
5
2

Classes

Class Line # Actions
ServletEnvironment 42 18 0% 10 3
0.896551789.7%
 

Contributing tests

This file is covered by 55 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.environment.internal;
21   
22    import java.io.File;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.net.MalformedURLException;
26    import java.net.URL;
27   
28    import javax.inject.Singleton;
29    import javax.servlet.ServletContext;
30   
31    import org.apache.commons.lang3.exception.ExceptionUtils;
32    import org.xwiki.component.annotation.Component;
33   
34    /**
35    * Defines what an Environment means in a Servlet environment.
36    *
37    * @version $Id: 4f399b7d46a1b95081930b6f3ee2cc6a826c4ee3 $
38    * @since 3.5M1
39    */
40    @Component
41    @Singleton
 
42    public class ServletEnvironment extends AbstractEnvironment
43    {
44    /**
45    * @see #getServletContext()
46    */
47    private ServletContext servletContext;
48   
49    /**
50    * @param servletContext see {@link #getServletContext()}
51    */
 
52  253 toggle public void setServletContext(ServletContext servletContext)
53    {
54  253 this.servletContext = servletContext;
55    }
56   
57    /**
58    * @return the Servlet Context
59    */
 
60  4218742 toggle public ServletContext getServletContext()
61    {
62  4218694 if (this.servletContext == null) {
63  2 throw new RuntimeException("The Servlet Environment has not been properly initialized "
64    + "(The Servlet Context is not set)");
65    }
66  4218571 return this.servletContext;
67    }
68   
 
69  833569 toggle @Override
70    public InputStream getResourceAsStream(String resourceName)
71    {
72  834175 return getServletContext().getResourceAsStream(resourceName);
73    }
74   
 
75  3156611 toggle @Override
76    public URL getResource(String resourceName)
77    {
78  3158076 URL url;
79  3158803 try {
80  3159490 url = getServletContext().getResource(resourceName);
81    } catch (MalformedURLException e) {
82  5 url = null;
83  5 this.logger.warn("Error getting resource [{}] because of invalid path format. Reason: [{}]",
84    resourceName, e.getMessage());
85    }
86  3158777 return url;
87    }
88   
 
89  86 toggle @Override
90    protected String getTemporaryDirectoryName()
91    {
92  86 final String tmpDirectory = super.getTemporaryDirectoryName();
93  86 try {
94  86 if (tmpDirectory == null) {
95  86 File tempDir = (File) this.getServletContext().getAttribute(ServletContext.TEMPDIR);
96  85 return tempDir == null ? null : tempDir.getCanonicalPath();
97    }
98    } catch (IOException e) {
99  0 this.logger.warn("Unable to get Servlet temporary directory due to error [{}], "
100    + "falling back on the default System temporary directory.", ExceptionUtils.getMessage(e));
101    }
102  0 return tmpDirectory;
103    }
104    }