View Javadoc
1 package org.apache.turbine.services.servlet; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.InputStream; 58 import java.net.MalformedURLException; 59 import java.net.URL; 60 import javax.servlet.ServletConfig; 61 import javax.servlet.ServletContext; 62 import org.apache.turbine.Turbine; 63 import org.apache.turbine.services.TurbineBaseService; 64 import org.apache.turbine.util.Log; 65 import org.apache.turbine.util.ServletUtils; 66 67 /*** 68 * <p>This class provides a context service when the application 69 * is run in a ServletContainer. It is mainly a wrapper around 70 * the ServletContext API.</p> 71 * <p>This class requires Servlet API 2.1 or better.</p> 72 * 73 * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a> 74 * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a> 75 * @author <a href="mailto:ekkerbj@netscape.net">Jeff Brekke</a> 76 * @author <a href="mailto:sgala@hisitech.com">Santiago Gala</a> 77 * @author <a href="mailto:jvanzyl@periapt.com.com">Jason van Zyl</a> 78 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 79 * @version $Id: TurbineServletService.java,v 1.3 2002/07/11 16:53:24 mpoeschl Exp $ 80 */ 81 public class TurbineServletService 82 extends TurbineBaseService implements ServletService 83 { 84 private ServletContext servletContext = null; 85 private ServletConfig servletConfig = null; 86 87 /*** 88 * Called during Turbine.init() 89 * 90 * @param config A ServletConfig. 91 */ 92 public void init( ServletConfig servletConfig ) 93 { 94 try 95 { 96 this.servletConfig = servletConfig; 97 this.servletContext = servletConfig.getServletContext(); 98 99 Log.debug("[TurbineServletService] Initializing with ServletConfig"); 100 } 101 catch (Exception e) 102 { 103 Log.error ( "Cannot initialize TurbineServletService." ); 104 Log.error (e); 105 } 106 setInit(true); 107 } 108 109 /*** 110 * Returns an URL object for a given URI string. 111 * This URI is considered relative to the context. 112 * 113 * @see javax.servlet.ServletContext#getResource 114 * @param uri the URI to resolve as an URL 115 * @return an URL object or null is the uri is malformed or 116 * can't be resolved 117 */ 118 public URL getResource( String uri ) 119 { 120 if ( servletContext == null) 121 { 122 return null; 123 } 124 125 URL url = null; 126 127 try 128 { 129 url = getServletContext().getResource( uri ); 130 // work-around for Websphere 3.52 131 if (url != null && url.toString().startsWith("classloader:")) 132 { 133 url = new URL("file:" + url.toString().substring(12)); 134 } 135 else if (url == null) 136 { 137 url = new URL("file:" + getServletContext().getRealPath(uri)); 138 } 139 } 140 catch ( MalformedURLException e) 141 { 142 //if the URL is wrong, return null 143 } 144 145 return url; 146 } 147 148 /*** 149 * Same as getResource except that it returns an InputStream 150 * 151 * @see javax.servlet.ServletContext#getResourceAsStream 152 * @param uri the URI to resolve 153 * @return an InputStream on the URI content or null 154 */ 155 public InputStream getResourceAsStream( String uri ) 156 { 157 if ( servletContext == null) return null; 158 159 InputStream is = null; 160 161 is = servletContext.getResourceAsStream( uri ); 162 163 return is; 164 } 165 166 /*** 167 * Returns the complete filesystem path for a 168 * given URI 169 * 170 * @see javax.servlet.ServletContext#getRealPath 171 * @param uri the URI to resolve 172 * @return the full system path of this URI 173 */ 174 public String getRealPath( String uri ) 175 { 176 if ( getServletContext() == null || uri == null ) 177 { 178 return null; 179 } 180 else 181 { 182 return getServletContext().getRealPath( uri ); 183 } 184 } 185 186 /*** 187 * Returns the servlet config used by this 188 * Turbine web application. 189 * 190 * @return turbine servlet config 191 */ 192 public ServletConfig getServletConfig() 193 { 194 return servletConfig; 195 } 196 197 /*** 198 * Returns the servlet context used by this 199 * Turbine web application. 200 * 201 * @return turbine servlet context 202 */ 203 public ServletContext getServletContext() 204 { 205 return servletContext; 206 } 207 208 /*** 209 * Returns the server scheme for this 210 * Turbine application. This will either 211 * be http or https. 212 * 213 * @return String 214 */ 215 public String getServerScheme() 216 { 217 return Turbine.getServerScheme(); 218 } 219 220 /*** 221 * Returns the server name that this 222 * Turbine application is running 223 * on. 224 * 225 * @return String 226 */ 227 public String getServerName() 228 { 229 return Turbine.getServerName(); 230 } 231 232 /*** 233 * Returns the port that this Turbine 234 * application is running through 235 * on the server. 236 * 237 * @return String 238 */ 239 public String getServerPort() 240 { 241 return Turbine.getServerPort(); 242 } 243 244 /*** 245 * Returns the context path for this 246 * Turbine application. 247 * 248 * @return String 249 */ 250 public String getContextPath() 251 { 252 return Turbine.getContextPath(); 253 } 254 255 /*** 256 * Expands a string that points to a relative path or path list, 257 * leaving it as an absolute path based on the servlet context. 258 * It will return null if the text is empty or the config object 259 * is null. 260 * 261 * @param config The ServletConfig. 262 * @param text The String containing a path or path list. 263 * @return A String with the expanded path or path list. 264 */ 265 public String expandRelative( String path ) 266 { 267 return ServletUtils.expandRelative(getServletConfig(), path); 268 } 269 }

This page was automatically generated by Maven