blob: 07b5aa1479cfb3ea15e0ff755608f858102df6b5 [file] [log] [blame]
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +00001/*
Richard S. Hall435c20c2006-09-28 20:11:35 +00002 * 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
Richard S. Hallfe8e5602006-04-19 15:23:22 +00009 *
Richard S. Hall435c20c2006-09-28 20:11:35 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Richard S. Hallfe8e5602006-04-19 15:23:22 +000011 *
Richard S. Hall435c20c2006-09-28 20:11:35 +000012 * 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.
Richard S. Hallfe8e5602006-04-19 15:23:22 +000018 */
19package org.mortbay.jetty.servlet;
20
21
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000022import java.io.IOException;
Richard S. Hallfe8e5602006-04-19 15:23:22 +000023import java.util.Dictionary;
24import java.util.Enumeration;
25
26import javax.servlet.Servlet;
27import javax.servlet.ServletConfig;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000028import javax.servlet.ServletContext;
29import javax.servlet.ServletException;
30import javax.servlet.ServletRequest;
31import javax.servlet.ServletResponse;
Richard S. Hallfe8e5602006-04-19 15:23:22 +000032import javax.servlet.UnavailableException;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000033import javax.servlet.http.HttpServletRequest;
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000034import javax.servlet.http.HttpServletRequestWrapper;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000035import javax.servlet.http.HttpServletResponse;
Richard S. Hallfe8e5602006-04-19 15:23:22 +000036
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000037import org.apache.felix.http.jetty.ServletContextGroup;
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000038import org.osgi.service.http.HttpContext;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000039
40
41public class OsgiServletHolder extends ServletHolder
Richard S. Hallfe8e5602006-04-19 15:23:22 +000042{
43 private Servlet m_servlet;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000044 private ServletContextGroup m_servletContextGroup;
Richard S. Hallfe8e5602006-04-19 15:23:22 +000045 private ServletConfig m_config;
46
47
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000048 public OsgiServletHolder( ServletHandler handler, Servlet servlet, String name,
49 ServletContextGroup servletContextGroup, Dictionary params )
Richard S. Hallfe8e5602006-04-19 15:23:22 +000050 {
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000051 super(servlet);
52 setServletHandler( handler );
53 setName( name );
54
Richard S. Hallfe8e5602006-04-19 15:23:22 +000055 m_servlet = servlet;
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000056 m_servletContextGroup = servletContextGroup;
Richard S. Hallfe8e5602006-04-19 15:23:22 +000057
58 // Seemed safer to copy params into parent holder, rather than override
59 // the getInitxxx methods.
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000060 if ( params != null )
Richard S. Hallfe8e5602006-04-19 15:23:22 +000061 {
62 Enumeration e = params.keys();
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000063 while ( e.hasMoreElements() )
Richard S. Hallfe8e5602006-04-19 15:23:22 +000064 {
65 Object key = e.nextElement();
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000066 setInitParameter( String.valueOf( key ), String.valueOf( params.get( key ) ) );
Richard S. Hallfe8e5602006-04-19 15:23:22 +000067 }
68 }
69 }
70
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000071
Richard S. Hallfe8e5602006-04-19 15:23:22 +000072 public synchronized Servlet getServlet()
Richard S. Hallfe8e5602006-04-19 15:23:22 +000073 {
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000074 return m_servlet;
Richard S. Hallfe8e5602006-04-19 15:23:22 +000075 }
76
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000077
Richard S. Hallfe8e5602006-04-19 15:23:22 +000078 public Servlet getOsgiServlet()
79 {
80 return m_servlet;
81 }
82
83
84 // override "Holder" method to prevent instantiation
85 public synchronized Object newInstance()
Richard S. Hallfe8e5602006-04-19 15:23:22 +000086 {
87 return getOsgiServlet();
88 }
89
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +000090
91 public void handle( ServletRequest request, ServletResponse response ) throws ServletException,
92 UnavailableException, IOException
93 {
94 if ( m_servletContextGroup.getOsgiHttpContext().handleSecurity( ( HttpServletRequest ) request,
95 ( HttpServletResponse ) response ) )
96 {
Felix Meschbergerc7ee0152008-03-26 09:24:35 +000097 // wrap request to get auth type and remote user
98 request = new HttpServiceHttpServletRequest( ( HttpServletRequest ) request );
99
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000100 // service request
101 super.handle( request, response );
102 }
103 else
104 {
105 //TODO: any other error/auth handling we should do in here?
106
107 // response.flushBuffer() if available
108 try
109 {
110 response.getClass().getDeclaredMethod( "flushBuffer", null ).invoke( response, null );
111 }
112 catch ( Exception ex )
113 {
114 // else ignore
115 ex.printStackTrace();
116 }
117 }
118 }
119
120
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000121 // override "Holder" method to prevent attempt to load
122 // the servlet class.
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000123 public void doStart() throws Exception
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000124 {
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000125 _class = m_servlet.getClass();
126
127 m_config = new Config()
128 {
129 public ServletContext getServletContext()
130 {
131 return m_servletContextGroup;
132 }
133 };
134
135 m_servlet.init( m_config );
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000136 }
137
Felix Meschbergerb76cfdb2007-09-28 14:13:22 +0000138
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000139 // override "Holder" method to prevent destroy, which is only called
140 // when a bundle manually unregisters
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000141 public void doStop()
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000142 {
143 }
Felix Meschbergerc7ee0152008-03-26 09:24:35 +0000144
145 // Simple wrapper class returning the authentication type and remote user
146 // from the respective request attribute as specificed by the HttpService
147 // spec (step 4 in Section 102.7, Authentication)
148 private static class HttpServiceHttpServletRequest extends HttpServletRequestWrapper {
149
150 HttpServiceHttpServletRequest(HttpServletRequest request) {
151 super(request);
152 }
153
154 public String getAuthType()
155 {
156 // use the auth type attribute; should not be null, but
157 // better check and use wrapped result if missing
158 Object name = getAttribute( HttpContext.AUTHENTICATION_TYPE );
159 if (name != null) {
160 return name.toString();
161 }
162
163 return super.getAuthType();
164 }
165
166 public String getRemoteUser()
167 {
168 // use the remote user attribute; should not be null, but
169 // better check and use wrapped result if missing
170 Object name = getAttribute( HttpContext.REMOTE_USER );
171 if (name != null) {
172 return name.toString();
173 }
174
175 return super.getRemoteUser();
176 }
177 }
Richard S. Hallfe8e5602006-04-19 15:23:22 +0000178}