View Javadoc

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