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

File ServletEnvironment.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% 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 52 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  245 toggle public void setServletContext(ServletContext servletContext)
53    {
54  245 this.servletContext = servletContext;
55    }
56   
57    /**
58    * @return the Servlet Context
59    */
 
60  415890 toggle public ServletContext getServletContext()
61    {
62  415868 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  415876 return this.servletContext;
67    }
68   
 
69  86854 toggle @Override
70    public InputStream getResourceAsStream(String resourceName)
71    {
72  86857 return getServletContext().getResourceAsStream(resourceName);
73    }
74   
 
75  303210 toggle @Override
76    public URL getResource(String resourceName)
77    {
78  303201 URL url;
79  303208 try {
80  303209 url = getServletContext().getResource(resourceName);
81    } catch (MalformedURLException e) {
82  1 url = null;
83  1 this.logger.warn("Error getting resource [{}] because of invalid path format. Reason: [{}]",
84    resourceName, e.getMessage());
85    }
86  303217 return url;
87    }
88   
 
89  74 toggle @Override
90    protected String getTemporaryDirectoryName()
91    {
92  74 final String tmpDirectory = super.getTemporaryDirectoryName();
93  74 try {
94  74 if (tmpDirectory == null) {
95  74 File tempDir = (File) this.getServletContext().getAttribute(ServletContext.TEMPDIR);
96  73 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    }