1 package org.apache.turbine.services.servlet;
2
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23
24 import java.io.InputStream;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27
28 import javax.servlet.ServletConfig;
29 import javax.servlet.ServletContext;
30
31 import org.apache.logging.log4j.LogManager;
32 import org.apache.logging.log4j.Logger;
33 import org.apache.turbine.Turbine;
34 import org.apache.turbine.services.TurbineBaseService;
35 import org.apache.turbine.util.ServletUtils;
36
37 /**
38 * <p>This class provides a context service when the application
39 * is run in a ServletContainer. It is mainly a wrapper around
40 * the ServletContext API.</p>
41 * <p>This class requires Servlet API 2.1 or better.</p>
42 *
43 * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
44 * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a>
45 * @author <a href="mailto:ekkerbj@netscape.net">Jeff Brekke</a>
46 * @author <a href="mailto:sgala@hisitech.com">Santiago Gala</a>
47 * @author <a href="mailto:jvanzyl@periapt.com.com">Jason van Zyl</a>
48 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
49 * @version $Id$
50 */
51 public class TurbineServletService
52 extends TurbineBaseService implements ServletService
53 {
54 /** Logging */
55 private static final Logger log = LogManager.getLogger(TurbineServletService.class);
56
57 /** The servlet context for this servlet */
58 private ServletContext servletContext = null;
59
60 /** The servlet configuration for this servlet */
61 private ServletConfig servletConfig = null;
62
63 /**
64 * Load all configured components and initialize them. This is
65 * a zero parameter variant which queries the Turbine Servlet
66 * for its config.
67 */
68 @Override
69 public void init()
70 {
71 this.servletConfig = Turbine.getTurbineServletConfig();
72 try
73 {
74 this.servletContext = servletConfig.getServletContext();
75
76 log.debug("Initializing with ServletConfig");
77 }
78 catch (Exception e)
79 {
80 log.error("Cannot initialize TurbineServletService.", e);
81 }
82 setInit(true);
83 }
84
85 /**
86 * Returns an URL object for a given URI string.
87 * This URI is considered relative to the context.
88 *
89 * @see javax.servlet.ServletContext#getResource
90 * @param uri the URI to resolve as an URL
91 * @return an URL object or null is the uri is malformed or
92 * can't be resolved
93 */
94 @Override
95 public URL getResource(String uri)
96 {
97 if (servletContext == null)
98 {
99 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 }