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.logging.log4j.LogManager;
032import org.apache.logging.log4j.Logger;
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$
050 */
051public class TurbineServletService
052        extends TurbineBaseService implements ServletService
053{
054    /** Logging */
055    private static final Logger log = LogManager.getLogger(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    @Override
095    public URL getResource(String uri)
096    {
097        if (servletContext == null)
098        {
099            return null;
100        }
101
102        URL url = null;
103
104        try
105        {
106            url = getServletContext().getResource(uri);
107            // work-around for Websphere 3.52
108            if (url != null && url.toString().startsWith("classloader:"))
109            {
110                url = new URL("file:" + url.toString().substring(12));
111            }
112            else if (url == null)
113            {
114                url = new URL("file:" + getServletContext().getRealPath(uri));
115            }
116        }
117        catch (MalformedURLException e)
118        {
119            //if the URL is wrong, return null
120        }
121
122        return url;
123    }
124
125    /**
126     * Same as getResource except that it returns an InputStream
127     *
128     * @see javax.servlet.ServletContext#getResourceAsStream
129     * @param uri the URI to resolve
130     * @return an InputStream on the URI content or null
131     */
132    @Override
133    public InputStream getResourceAsStream(String uri)
134    {
135        if (servletContext == null)
136        {
137            return null;
138        }
139
140        InputStream is = null;
141        is = servletContext.getResourceAsStream(uri);
142        return is;
143    }
144
145    /**
146     * Returns the complete filesystem path for a
147     * given URI
148     *
149     * @see javax.servlet.ServletContext#getRealPath
150     * @param uri the URI to resolve
151     * @return the full system path of this URI
152     */
153    @Override
154    public String getRealPath(String uri)
155    {
156        if (getServletContext() == null || uri == null)
157        {
158            return null;
159        }
160        else
161        {
162            return getServletContext().getRealPath(uri);
163        }
164    }
165
166    /**
167     * Returns the servlet config used by this
168     * Turbine web application.
169     *
170     * @return turbine servlet config
171     */
172    @Override
173    public ServletConfig getServletConfig()
174    {
175        return servletConfig;
176    }
177
178    /**
179     * Returns the servlet context used by this
180     * Turbine web application.
181     *
182     * @return turbine servlet context
183     */
184    @Override
185    public ServletContext getServletContext()
186    {
187        return servletContext;
188    }
189
190    /**
191     * Returns the server scheme for this
192     * Turbine application. This will either
193     * be http or https.
194     *
195     * @return String
196     */
197    @Override
198    public String getServerScheme()
199    {
200        return Turbine.getServerScheme();
201    }
202
203    /**
204     * Returns the server name that this
205     * Turbine application is running
206     * on.
207     *
208     * @return String
209     */
210    @Override
211    public String getServerName()
212    {
213        return Turbine.getServerName();
214    }
215
216    /**
217     * Returns the port that this Turbine
218     * application is running through
219     * on the server.
220     *
221     * @return String
222     */
223    @Override
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    @Override
236    public String getContextPath()
237    {
238        return Turbine.getContextPath();
239    }
240
241    /**
242     * Expands a string that points to a relative path or path list,
243     * leaving it as an absolute path based on the servlet context.
244     * It will return null if the text is empty or the config object
245     * is null.
246     *
247     * @param path The String containing a path or path list.
248     * @return A String with the expanded path or path list.
249     */
250    public String expandRelative(String path)
251    {
252        return ServletUtils.expandRelative(getServletConfig(), path);
253    }
254}