001package org.apache.turbine.services.servlet;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import java.io.InputStream;
025import java.net.MalformedURLException;
026import java.net.URL;
027
028import javax.servlet.ServletConfig;
029import javax.servlet.ServletContext;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.apache.turbine.Turbine;
034import org.apache.turbine.services.TurbineBaseService;
035import org.apache.turbine.util.ServletUtils;
036
037/**
038 * <p>This class provides a context service when the application
039 * is run in a ServletContainer. It is mainly a wrapper around
040 * the ServletContext API.</p>
041 * <p>This class requires Servlet API 2.1 or better.</p>
042 *
043 * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
044 * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a>
045 * @author <a href="mailto:ekkerbj@netscape.net">Jeff Brekke</a>
046 * @author <a href="mailto:sgala@hisitech.com">Santiago Gala</a>
047 * @author <a href="mailto:jvanzyl@periapt.com.com">Jason van Zyl</a>
048 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
049 * @version $Id: TurbineServletService.java 1071044 2011-02-15 20:47:31Z tv $
050 */
051public class TurbineServletService
052        extends TurbineBaseService implements ServletService
053{
054    /** Logging */
055    private static Log log = LogFactory.getLog(TurbineServletService.class);
056
057    /** The servlet context for this servlet */
058    private ServletContext servletContext = null;
059
060    /** The servlet configuration for this servlet */
061    private ServletConfig servletConfig = null;
062
063    /**
064     * Load all configured components and initialize them. This is
065     * a zero parameter variant which queries the Turbine Servlet
066     * for its config.
067     */
068    @Override
069    public void init()
070    {
071        this.servletConfig = Turbine.getTurbineServletConfig();
072        try
073        {
074            this.servletContext = servletConfig.getServletContext();
075
076            log.debug("Initializing with ServletConfig");
077        }
078        catch (Exception e)
079        {
080            log.error("Cannot initialize TurbineServletService.", e);
081        }
082        setInit(true);
083    }
084
085    /**
086     * Returns an URL object for a given URI string.
087     * This URI is considered relative to the context.
088     *
089     * @see javax.servlet.ServletContext#getResource
090     * @param uri the URI to resolve as an URL
091     * @return an URL object or null is the uri is malformed or
092     * can't be resolved
093     */
094    public URL getResource(String uri)
095    {
096        if (servletContext == null)
097        {
098            return null;
099        }
100
101        URL url = null;
102
103        try
104        {
105            url = getServletContext().getResource(uri);
106            // work-around for Websphere 3.52
107            if (url != null && url.toString().startsWith("classloader:"))
108            {
109                url = new URL("file:" + url.toString().substring(12));
110            }
111            else if (url == null)
112            {
113                url = new URL("file:" + getServletContext().getRealPath(uri));
114            }
115        }
116        catch (MalformedURLException e)
117        {
118            //if the URL is wrong, return null
119        }
120
121        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        if (servletContext == null)
134        {
135            return null;
136        }
137
138        InputStream is = null;
139        is = servletContext.getResourceAsStream(uri);
140        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        if (getServletContext() == null || uri == null)
154        {
155            return null;
156        }
157        else
158        {
159            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        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        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        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        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        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        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        return ServletUtils.expandRelative(getServletConfig(), path);
244    }
245}