001    package 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    
024    import java.io.InputStream;
025    import java.net.URL;
026    
027    import javax.servlet.ServletConfig;
028    import javax.servlet.ServletContext;
029    
030    import org.apache.turbine.services.TurbineServices;
031    
032    /**
033     * Simple static accessor to the EngineContextService
034     *
035     * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
036     * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a>
037     * @author <a href="mailto:ekkerbj@netscape.net">Jeff Brekke</a>
038     * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
039     * @version $Id: TurbineServlet.java 1071044 2011-02-15 20:47:31Z tv $
040     */
041    public class TurbineServlet
042    {
043        /**
044         * Utility method for accessing the service
045         * implementation
046         *
047         * @return a ServletService implementation instance
048         */
049        protected static ServletService getService()
050        {
051            return (ServletService) TurbineServices
052                    .getInstance().getService(ServletService.SERVICE_NAME);
053        }
054    
055        /**
056         * Returns an URL object for a given URI string.
057         * This URI is considered relative to the context.
058         *
059         * @param uri the URI to resolve as an URL
060         * @return an URL object or null is the uri is malformed or can't be resolved
061         */
062        public static URL getResource(String uri)
063        {
064            return getService().getResource(uri);
065        }
066    
067        /**
068         * Same as getResource except that it returns an InputStream
069         *
070         * @see javax.servlet.ServletContext#getResourceAsStream
071         * @param uri the URI to resolve
072         * @return an InputStream on the URI content or null
073         */
074        public static InputStream getResourceAsStream(String uri)
075        {
076            return getService().getResourceAsStream(uri);
077        }
078    
079        /**
080         * Returns the complete filesystem path for a
081         * given URI
082         *
083         * @see javax.servlet.ServletContext#getRealPath
084         * @param uri the URI to resolve
085         * @return the full system path of this URI
086         */
087        public static String getRealPath(String path)
088        {
089            return getService().getRealPath(path);
090        }
091    
092        /**
093         * Returns the servlet config used by this
094         * Turbine web application.
095         *
096         * @return turbine servlet config
097         */
098        public static ServletConfig getServletConfig()
099        {
100            return getService().getServletConfig();
101        }
102    
103        /**
104         * Returns the servlet context used by this
105         * Turbine web application.
106         *
107         * @return turbine servlet context
108         */
109        public static ServletContext getServletContext()
110        {
111            return getService().getServletContext();
112        }
113    
114        /**
115         * Returns the server scheme for this
116         * Turbine application. This will either
117         * be http or https.
118         *
119         * @return String
120         */
121        public static String getServerScheme()
122        {
123            return getService().getServerScheme();
124        }
125    
126        /**
127         * Returns the server name that this
128         * Turbine application is running
129         * on.
130         *
131         * @return String
132         */
133        public static String getServerName()
134        {
135            return getService().getServerName();
136        }
137    
138        /**
139         * Returns the port that this Turbine
140         * application is running through
141         * on the server.
142         *
143         * @return String
144         */
145        public static String getServerPort()
146        {
147            return getService().getServerPort();
148        }
149    
150        /**
151         * Returns the context path for this
152         * Turbine application.
153         *
154         * @return String
155         */
156        public static String getContextPath()
157        {
158            return getService().getContextPath();
159        }
160    }