Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 435c20c | 2006-09-28 20:11:35 +0000 | [diff] [blame] | 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 |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 9 | * |
Richard S. Hall | 435c20c | 2006-09-28 20:11:35 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 11 | * |
Richard S. Hall | 435c20c | 2006-09-28 20:11:35 +0000 | [diff] [blame] | 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. |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 18 | */ |
| 19 | package org.mortbay.jetty.servlet; |
| 20 | |
| 21 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 22 | import java.io.IOException; |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 23 | import java.util.Dictionary; |
| 24 | import java.util.Enumeration; |
| 25 | |
| 26 | import javax.servlet.Servlet; |
| 27 | import javax.servlet.ServletConfig; |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 28 | import javax.servlet.ServletContext; |
| 29 | import javax.servlet.ServletException; |
| 30 | import javax.servlet.ServletRequest; |
| 31 | import javax.servlet.ServletResponse; |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 32 | import javax.servlet.UnavailableException; |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 33 | import javax.servlet.http.HttpServletRequest; |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 34 | import javax.servlet.http.HttpServletRequestWrapper; |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 35 | import javax.servlet.http.HttpServletResponse; |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 36 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 37 | import org.apache.felix.http.jetty.ServletContextGroup; |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 38 | import org.osgi.service.http.HttpContext; |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | public class OsgiServletHolder extends ServletHolder |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 42 | { |
| 43 | private Servlet m_servlet; |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 44 | private ServletContextGroup m_servletContextGroup; |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 45 | private ServletConfig m_config; |
| 46 | |
| 47 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 48 | public OsgiServletHolder( ServletHandler handler, Servlet servlet, String name, |
| 49 | ServletContextGroup servletContextGroup, Dictionary params ) |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 50 | { |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 51 | super(servlet); |
| 52 | setServletHandler( handler ); |
| 53 | setName( name ); |
| 54 | |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 55 | m_servlet = servlet; |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 56 | m_servletContextGroup = servletContextGroup; |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 57 | |
| 58 | // Seemed safer to copy params into parent holder, rather than override |
| 59 | // the getInitxxx methods. |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 60 | if ( params != null ) |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 61 | { |
| 62 | Enumeration e = params.keys(); |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 63 | while ( e.hasMoreElements() ) |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 64 | { |
| 65 | Object key = e.nextElement(); |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 66 | setInitParameter( String.valueOf( key ), String.valueOf( params.get( key ) ) ); |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 71 | |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 72 | public synchronized Servlet getServlet() |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 73 | { |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 74 | return m_servlet; |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 77 | |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 78 | 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. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 86 | { |
| 87 | return getOsgiServlet(); |
| 88 | } |
| 89 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 90 | |
| 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 Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 97 | // wrap request to get auth type and remote user |
| 98 | request = new HttpServiceHttpServletRequest( ( HttpServletRequest ) request ); |
| 99 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 100 | // 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. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 121 | // override "Holder" method to prevent attempt to load |
| 122 | // the servlet class. |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 123 | public void doStart() throws Exception |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 124 | { |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 125 | _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. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Felix Meschberger | b76cfdb | 2007-09-28 14:13:22 +0000 | [diff] [blame] | 138 | |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 139 | // override "Holder" method to prevent destroy, which is only called |
| 140 | // when a bundle manually unregisters |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 141 | public void doStop() |
Richard S. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 142 | { |
| 143 | } |
Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 144 | |
| 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. Hall | fe8e560 | 2006-04-19 15:23:22 +0000 | [diff] [blame] | 178 | } |