##### Coverage Report - org.apache.turbine.services.servlet.TurbineServletService

Classes in this File | Line Coverage | Branch Coverage | Complexity
TurbineServletService |
26% | 10/38
0% | 0/14
| 2.090909090909091;2,091

1 |  |
     package org.apache.turbine.services.servlet;
2 |  |
3 |  |
4 |  |
     /*
5 |  |
      * Licensed to the Apache Software Foundation (ASF) under one
6 |  |
      * or more contributor license agreements.  See the NOTICE file
7 |  |
      * distributed with this work for additional information
8 |  |
      * regarding copyright ownership.  The ASF licenses this file
9 |  |
      * to you under the Apache License, Version 2.0 (the
10 |  |
      * "License"); you may not use this file except in compliance
11 |  |
      * with the License.  You may obtain a copy of the License at
12 |  |
      *
13 |  |
      *   http://www.apache.org/licenses/LICENSE-2.0
14 |  |
      *
15 |  |
      * Unless required by applicable law or agreed to in writing,
16 |  |
      * software distributed under the License is distributed on an
17 |  |
      * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 |  |
      * KIND, either express or implied.  See the License for the
19 |  |
      * specific language governing permissions and limitations
20 |  |
      * under the License.
21 |  |
      */
22 |  |
23 |  |
24 |  |
     import java.io.InputStream;
25 |  |
     import java.net.MalformedURLException;
26 |  |
     import java.net.URL;
27 |  |
28 |  |
     import javax.servlet.ServletConfig;
29 |  |
     import javax.servlet.ServletContext;
30 |  |
31 |  |
     import org.apache.commons.logging.Log;
32 |  |
     import org.apache.commons.logging.LogFactory;
33 |  |
     import org.apache.turbine.Turbine;
34 |  |
     import org.apache.turbine.services.TurbineBaseService;
35 |  |
     import org.apache.turbine.util.ServletUtils;
36 |  |
37 |  |
     /**
38 |  |
      * <p>This class provides a context service when the application
39 |  |
      * is run in a ServletContainer. It is mainly a wrapper around
40 |  |
      * the ServletContext API.</p>
41 |  |
      * <p>This class requires Servlet API 2.1 or better.</p>
42 |  |
      *
43 |  |
      * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
44 |  |
      * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
45 |  |
      * @author <a href="mailto:ekkerbj@netscape.net">Jeff Brekke</a>
46 |  |
      * @author <a href="mailto:sgala@hisitech.com">Santiago Gala</a>
47 |  |
      * @author <a href="mailto:jvanzyl@periapt.com.com">Jason van Zyl</a>
48 |  |
      * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
49 |  |
      * @version $Id: TurbineServletService.java 1071044 2011-02-15 20:47:31Z tv $
50 |  |
      */
51 | 30 |
     public class TurbineServletService
52 |  |
             extends TurbineBaseService implements ServletService
53 |  |
     {
54 |  |
         /** Logging */
55 | 30 |
         private static Log log = LogFactory.getLog(TurbineServletService.class);
56 |  |
57 |  |
         /** The servlet context for this servlet */
58 | 30 |
         private ServletContext servletContext = null;
59 |  |
60 |  |
         /** The servlet configuration for this servlet */
61 | 30 |
         private ServletConfig servletConfig = null;
62 |  |
63 |  |
         /**
64 |  |
          * Load all configured components and initialize them. This is
65 |  |
          * a zero parameter variant which queries the Turbine Servlet
66 |  |
          * for its config.
67 |  |
          */
68 |  |
         @Override
69 |  |
         public void init()
70 |  |
         {
71 | 1 |
             this.servletConfig = Turbine.getTurbineServletConfig();
72 |  |
             try
73 |  |
             {
74 | 1 |
                 this.servletContext = servletConfig.getServletContext();
75 |  |
76 | 1 |
                 log.debug("Initializing with ServletConfig");
77 |  |
             }
78 | 0 |
             catch (Exception e)
79 |  |
             {
80 | 0 |
                 log.error("Cannot initialize TurbineServletService.", e);
81 | 1 |
             }
82 | 1 |
             setInit(true);
83 | 1 |
         }
84 |  |
85 |  |
         /**
86 |  |
          * Returns an URL object for a given URI string.
87 |  |
          * This URI is considered relative to the context.
88 |  |
          *
89 |  |
          * @see javax.servlet.ServletContext#getResource
90 |  |
          * @param uri the URI to resolve as an URL
91 |  |
          * @return an URL object or null is the uri is malformed or
92 |  |
          * can't be resolved
93 |  |
          */
94 |  |
         public URL getResource(String uri)
95 |  |
         {
96 | 0 |
             if (servletContext == null)
97 |  |
             {
98 | 0 |
                 return null;
99 |  |
             }
100 |  |
101 | 0 |
             URL url = null;
102 |  |
103 |  |
             try
104 |  |
             {
105 | 0 |
                 url = getServletContext().getResource(uri);
106 |  |
                 // work-around for Websphere 3.52
107 | 0 |
                 if (url != null && url.toString().startsWith("classloader:"))
108 |  |
                 {
109 | 0 |
                     url = new URL("file:" + url.toString().substring(12));
110 |  |
                 }
111 | 0 |
                 else if (url == null)
112 |  |
                 {
113 | 0 |
                     url = new URL("file:" + getServletContext().getRealPath(uri));
114 |  |
                 }
115 |  |
             }
116 | 0 |
             catch (MalformedURLException e)
117 |  |
             {
118 |  |
                 //if the URL is wrong, return null
119 | 0 |
             }
120 |  |
121 | 0 |
             return url;
122 |  |
         }
123 |  |
124 |  |
         /**
125 |  |
          * Same as getResource except that it returns an InputStream
126 |  |
          *
127 |  |
          * @see javax.servlet.ServletContext#getResourceAsStream
128 |  |
          * @param uri the URI to resolve
129 |  |
          * @return an InputStream on the URI content or null
130 |  |
          */
131 |  |
         public InputStream getResourceAsStream(String uri)
132 |  |
         {
133 | 0 |
             if (servletContext == null)
134 |  |
             {
135 | 0 |
                 return null;
136 |  |
             }
137 |  |
138 | 0 |
             InputStream is = null;
139 | 0 |
             is = servletContext.getResourceAsStream(uri);
140 | 0 |
             return is;
141 |  |
         }
142 |  |
143 |  |
         /**
144 |  |
          * Returns the complete filesystem path for a
145 |  |
          * given URI
146 |  |
          *
147 |  |
          * @see javax.servlet.ServletContext#getRealPath
148 |  |
          * @param uri the URI to resolve
149 |  |
          * @return the full system path of this URI
150 |  |
          */
151 |  |
         public String getRealPath(String uri)
152 |  |
         {
153 | 0 |
             if (getServletContext() == null || uri == null)
154 |  |
             {
155 | 0 |
                 return null;
156 |  |
             }
157 |  |
             else
158 |  |
             {
159 | 0 |
                 return getServletContext().getRealPath(uri);
160 |  |
             }
161 |  |
         }
162 |  |
163 |  |
         /**
164 |  |
          * Returns the servlet config used by this
165 |  |
          * Turbine web application.
166 |  |
          *
167 |  |
          * @return turbine servlet config
168 |  |
          */
169 |  |
         public ServletConfig getServletConfig()
170 |  |
         {
171 | 0 |
             return servletConfig;
172 |  |
         }
173 |  |
174 |  |
         /**
175 |  |
          * Returns the servlet context used by this
176 |  |
          * Turbine web application.
177 |  |
          *
178 |  |
          * @return turbine servlet context
179 |  |
          */
180 |  |
         public ServletContext getServletContext()
181 |  |
         {
182 | 0 |
             return servletContext;
183 |  |
         }
184 |  |
185 |  |
         /**
186 |  |
          * Returns the server scheme for this
187 |  |
          * Turbine application. This will either
188 |  |
          * be http or https.
189 |  |
          *
190 |  |
          * @return String
191 |  |
          */
192 |  |
         public String getServerScheme()
193 |  |
         {
194 | 0 |
             return Turbine.getServerScheme();
195 |  |
         }
196 |  |
197 |  |
         /**
198 |  |
          * Returns the server name that this
199 |  |
          * Turbine application is running
200 |  |
          * on.
201 |  |
          *
202 |  |
          * @return String
203 |  |
          */
204 |  |
         public String getServerName()
205 |  |
         {
206 | 0 |
             return Turbine.getServerName();
207 |  |
         }
208 |  |
209 |  |
         /**
210 |  |
          * Returns the port that this Turbine
211 |  |
          * application is running through
212 |  |
          * on the server.
213 |  |
          *
214 |  |
          * @return String
215 |  |
          */
216 |  |
         public String getServerPort()
217 |  |
         {
218 | 0 |
             return Turbine.getServerPort();
219 |  |
         }
220 |  |
221 |  |
         /**
222 |  |
          * Returns the context path for this
223 |  |
          * Turbine application.
224 |  |
          *
225 |  |
          * @return String
226 |  |
          */
227 |  |
         public String getContextPath()
228 |  |
         {
229 | 0 |
             return Turbine.getContextPath();
230 |  |
         }
231 |  |
232 |  |
         /**
233 |  |
          * Expands a string that points to a relative path or path list,
234 |  |
          * leaving it as an absolute path based on the servlet context.
235 |  |
          * It will return null if the text is empty or the config object
236 |  |
          * is null.
237 |  |
          *
238 |  |
          * @param path The String containing a path or path list.
239 |  |
          * @return A String with the expanded path or path list.
240 |  |
          */
241 |  |
         public String expandRelative(String path)
242 |  |
         {
243 | 0 |
             return ServletUtils.expandRelative(getServletConfig(), path);
244 |  |
         }
245 |  |
     }