blob: f0161e262b8285286c20525f57c7a1467cdcc012 [file] [log] [blame]
Ken Gilmer2c29a972011-10-13 05:01:42 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.http.lightweight.servlet;
20
21import java.io.IOException;
22import java.io.InputStream;
23import java.net.MalformedURLException;
24import java.net.URL;
25import java.util.Collections;
26import java.util.Dictionary;
27import java.util.Enumeration;
28import java.util.HashMap;
29import java.util.Map;
30import java.util.Set;
31
32import javax.servlet.RequestDispatcher;
33import javax.servlet.Servlet;
34import javax.servlet.ServletContext;
35import javax.servlet.ServletException;
36
37import org.apache.felix.http.lightweight.osgi.Logger;
38import org.osgi.service.http.HttpContext;
39
40/**
41 * ServletContext implementation.
42 *
43 */
44public class ServletContextImpl implements ServletContext
45{
46
47 private final HttpContext m_httpContext;
48 private final Logger m_logger;
49 private final Dictionary m_initparams;
50 private Map m_attributes;
51 private final String m_name;
52
53 /**
54 * @param name Name of Servlet Context
55 * @param httpContext HttpContext
56 * @param initparams Dictionary
57 * @param logger Logger
58 */
59 public ServletContextImpl(final String name, final HttpContext httpContext, final Dictionary initparams, final Logger logger)
60 {
61 this.m_name = name;
62 this.m_httpContext = httpContext;
63 this.m_initparams = initparams;
64 this.m_logger = logger;
65 }
66
67 /* (non-Javadoc)
68 * @see javax.servlet.ServletContext#getContext(java.lang.String)
69 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +000070 @Override
71 public ServletContext getContext(String uripath)
Ken Gilmer2c29a972011-10-13 05:01:42 +000072 {
73 return null;
74 }
75
76 /* (non-Javadoc)
77 * @see javax.servlet.ServletContext#getMajorVersion()
78 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +000079 @Override
80 public int getMajorVersion()
Ken Gilmer2c29a972011-10-13 05:01:42 +000081 {
82 return 2;
83 }
84
85 /* (non-Javadoc)
86 * @see javax.servlet.ServletContext#getMinorVersion()
87 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +000088 @Override
89 public int getMinorVersion()
Ken Gilmer2c29a972011-10-13 05:01:42 +000090 {
91 return 4;
92 }
93
94 /* (non-Javadoc)
95 * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
96 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +000097 @Override
98 public String getMimeType(String file)
Ken Gilmer2c29a972011-10-13 05:01:42 +000099 {
100 return m_httpContext.getMimeType(file);
101 }
102
103 /* (non-Javadoc)
104 * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
105 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000106 @Override
107 public Set getResourcePaths(String path)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000108 {
109 return null;
110 }
111
112 /* (non-Javadoc)
113 * @see javax.servlet.ServletContext#getResource(java.lang.String)
114 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000115 @Override
116 public URL getResource(String path) throws MalformedURLException
Ken Gilmer2c29a972011-10-13 05:01:42 +0000117 {
118 return m_httpContext.getResource(path);
119 }
120
121 /* (non-Javadoc)
122 * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
123 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000124 @Override
125 public InputStream getResourceAsStream(String path)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000126 {
127 try
128 {
129 return m_httpContext.getResource(path).openStream();
130 }
131 catch (IOException e)
132 {
133 m_logger.log(Logger.LOG_ERROR, "Unable to open stream on resource: " + path,
134 e);
135 return null;
136 }
137 }
138
139 /* (non-Javadoc)
140 * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
141 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000142 @Override
143 public RequestDispatcher getRequestDispatcher(String path)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000144 {
145 return null;
146 }
147
148 /* (non-Javadoc)
149 * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
150 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000151 @Override
152 public RequestDispatcher getNamedDispatcher(String name)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000153 {
154 return null;
155 }
156
157 /* (non-Javadoc)
158 * @see javax.servlet.ServletContext#getServlet(java.lang.String)
159 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000160 @Override
161 public Servlet getServlet(String name) throws ServletException
Ken Gilmer2c29a972011-10-13 05:01:42 +0000162 {
163 return null;
164 }
165
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000166 @Override
167 public Enumeration getServlets()
Ken Gilmer2c29a972011-10-13 05:01:42 +0000168 {
169 return null;
170 }
171
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000172 @Override
173 public Enumeration getServletNames()
Ken Gilmer2c29a972011-10-13 05:01:42 +0000174 {
175 return null;
176 }
177
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000178 @Override
179 public void log(String msg)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000180 {
181 m_logger.log(Logger.LOG_INFO, msg);
182 }
183
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000184 @Override
185 public void log(Exception exception, String msg)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000186 {
187 m_logger.log(Logger.LOG_ERROR, msg, exception);
188 }
189
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000190 @Override
191 public void log(String message, Throwable throwable)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000192 {
193 m_logger.log(Logger.LOG_ERROR, message, throwable);
194 }
195
196 /* (non-Javadoc)
197 * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
198 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000199 @Override
200 public String getRealPath(String path)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000201 {
202 return path;
203 }
204
205 /* (non-Javadoc)
206 * @see javax.servlet.ServletContext#getServerInfo()
207 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000208 @Override
209 public String getServerInfo()
Ken Gilmer2c29a972011-10-13 05:01:42 +0000210 {
211 return HttpConstants.SERVER_INFO;
212 }
213
214 /* (non-Javadoc)
215 * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
216 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000217 @Override
218 public String getInitParameter(String name)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000219 {
220 if (m_initparams != null)
221 {
222 Object o = m_initparams.get(name);
223
224 if (o != null)
225 {
226 return o.toString();
227 }
228 }
229
230 return null;
231 }
232
233 /* (non-Javadoc)
234 * @see javax.servlet.ServletContext#getInitParameterNames()
235 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000236 @Override
237 public Enumeration getInitParameterNames()
Ken Gilmer2c29a972011-10-13 05:01:42 +0000238 {
239 if (m_initparams != null)
240 {
241 return m_initparams.keys();
242 }
243
244 return HttpConstants.EMPTY_ENUMERATION;
245 }
246
247 /* (non-Javadoc)
248 * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
249 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000250 @Override
251 public Object getAttribute(String name)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000252 {
253 if (m_attributes != null)
254 {
255 return m_attributes.get(name);
256 }
257
258 return null;
259 }
260
261 /* (non-Javadoc)
262 * @see javax.servlet.ServletContext#getAttributeNames()
263 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000264 @Override
265 public Enumeration getAttributeNames()
Ken Gilmer2c29a972011-10-13 05:01:42 +0000266 {
267 if (m_attributes != null)
268 {
269 return Collections.enumeration(m_attributes.keySet());
270 }
271
272 return HttpConstants.EMPTY_ENUMERATION;
273 }
274
275 /* (non-Javadoc)
276 * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
277 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000278 @Override
279 public void setAttribute(String name, Object object)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000280 {
281 if (m_attributes == null)
282 {
283 m_attributes = new HashMap();
284 }
285
286 m_attributes.put(name, object);
287 }
288
289 /* (non-Javadoc)
290 * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
291 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000292 @Override
293 public void removeAttribute(String name)
Ken Gilmer2c29a972011-10-13 05:01:42 +0000294 {
295 if (m_attributes != null)
296 {
297 m_attributes.remove(name);
298 }
299 }
300
301 /* (non-Javadoc)
302 * @see javax.servlet.ServletContext#getServletContextName()
303 */
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000304 @Override
305 public String getServletContextName()
Ken Gilmer2c29a972011-10-13 05:01:42 +0000306 {
307 return m_name;
308 }
Ken Gilmerc65a28c2011-10-13 05:36:47 +0000309
310 /* (non-Javadoc)
311 * @see javax.servlet.ServletContext#getContextPath()
312 */
313 @Override
314 public String getContextPath() {
315 return m_name;
316 }
Ken Gilmer2c29a972011-10-13 05:01:42 +0000317}