KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > servlet > internal > ProxyServlet


1 /*******************************************************************************
2  * Copyright (c) 2005-2007 Cognos Incorporated, IBM Corporation and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Cognos Incorporated - initial API and implementation
10  * IBM Corporation - bug fixes and enhancements
11  *******************************************************************************/

12 package org.eclipse.equinox.http.servlet.internal;
13
14 import java.io.IOException JavaDoc;
15 import java.security.AccessController JavaDoc;
16 import java.util.*;
17 import javax.servlet.*;
18 import javax.servlet.http.*;
19 import org.osgi.framework.Bundle;
20 import org.osgi.service.http.HttpContext;
21 import org.osgi.service.http.NamespaceException;
22
23 /**
24  * The ProxyServlet is the private side of a Servlet that when registered (and init() called) in a servlet container
25  * will in-turn register and provide an OSGi Http Service implementation.
26  * This class is not meant for extending or even using directly and is purely meant for registering
27  * in a servlet container.
28  */

29 public class ProxyServlet extends HttpServlet {
30
31     private static final long serialVersionUID = 4117456123807468871L;
32     private Map registrations = new HashMap(); //alias --> registration
33
private Set servlets = new HashSet(); //All the servlets objects that have been registered
34
private ProxyContext proxyContext;
35
36     public void init(ServletConfig config) throws ServletException {
37         super.init(config);
38         proxyContext = new ProxyContext(config.getServletContext());
39         Activator.addProxyServlet(this);
40     }
41
42     public void destroy() {
43         Activator.removeProxyServlet(this);
44         proxyContext.destroy();
45         proxyContext = null;
46         super.destroy();
47     }
48
49     /**
50      * @see HttpServlet#service(ServletRequest, ServletResponse)
51      */

52     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException JavaDoc {
53         proxyContext.initializeServletPath(req);
54         String JavaDoc alias = HttpServletRequestAdaptor.getDispatchPathInfo(req);
55         if (alias == null)
56             alias = "/"; //$NON-NLS-1$
57

58         // perfect match
59
if (processAlias(req, resp, alias, null))
60             return;
61
62         String JavaDoc extensionAlias = findExtensionAlias(alias);
63         alias = alias.substring(0, alias.lastIndexOf('/'));
64
65         // longest path match
66
while (alias.length() != 0) {
67             if (processAlias(req, resp, alias, extensionAlias))
68                 return;
69             alias = alias.substring(0, alias.lastIndexOf('/'));
70         }
71
72         // default handler match
73
if (extensionAlias != null)
74             extensionAlias = extensionAlias.substring(1); // remove the leading '/'
75
if (processAlias(req, resp, "/", extensionAlias)) //Handle '/' aliases //$NON-NLS-1$
76
return;
77         resp.sendError(HttpServletResponse.SC_NOT_FOUND, "ProxyServlet: " + req.getRequestURI()); //$NON-NLS-1$
78
}
79
80     private String JavaDoc findExtensionAlias(String JavaDoc alias) {
81         String JavaDoc lastSegment = alias.substring(alias.lastIndexOf('/') + 1);
82         int dot = lastSegment.indexOf('.');
83         if (dot == -1)
84             return null;
85         String JavaDoc extension = lastSegment.substring(dot + 1);
86         if (extension.length() == 0)
87             return null;
88         return "/*." + extension; //$NON-NLS-1$
89
}
90
91     private boolean processAlias(HttpServletRequest req, HttpServletResponse resp, String JavaDoc alias, String JavaDoc extensionAlias) throws ServletException, IOException JavaDoc {
92         Registration registration = null;
93         synchronized (this) {
94             if (extensionAlias == null)
95                 registration = (Registration) registrations.get(alias);
96             else {
97                 registration = (Registration) registrations.get(alias + extensionAlias);
98                 if (registration != null) {
99                     // for ServletRegistrations extensions should be handled on the full alias
100
if (registration instanceof ServletRegistration)
101                         alias = HttpServletRequestAdaptor.getDispatchPathInfo(req);
102                 } else
103                     registration = (Registration) registrations.get(alias);
104             }
105
106             if (registration != null)
107                 registration.addReference();
108         }
109         if (registration != null) {
110             try {
111                 if (registration.handleRequest(req, resp, alias))
112                     return true;
113             } finally {
114                 registration.removeReference();
115             }
116         }
117         return false;
118     }
119
120     //Effective unregistration of servlet and resources as defined in HttpService#unregister()
121
synchronized void unregister(String JavaDoc alias, boolean destroy) {
122         Registration removedRegistration = (Registration) registrations.remove(alias);
123         if (removedRegistration != null) {
124             if (destroy)
125                 removedRegistration.destroy();
126             removedRegistration.close();
127         }
128     }
129
130     //Effective registration of the servlet as defined HttpService#registerServlet()
131
synchronized void registerServlet(String JavaDoc alias, Servlet servlet, Dictionary initparams, HttpContext context, Bundle bundle) throws ServletException, NamespaceException {
132         checkAlias(alias);
133         if (servlet == null)
134             throw new IllegalArgumentException JavaDoc("Servlet cannot be null"); //$NON-NLS-1$
135

136         ServletRegistration registration = new ServletRegistration(servlet, proxyContext, context, bundle, servlets);
137         registration.checkServletRegistration();
138
139         ServletContext wrappedServletContext = new ServletContextAdaptor(proxyContext, getServletContext(), context, AccessController.getContext());
140         ServletConfig servletConfig = new ServletConfigImpl(servlet, initparams, wrappedServletContext);
141
142         registration.init(servletConfig);
143         registrations.put(alias, registration);
144     }
145
146     //Effective registration of the resources as defined HttpService#registerResources()
147
synchronized void registerResources(String JavaDoc alias, String JavaDoc name, HttpContext context) throws NamespaceException {
148         checkAlias(alias);
149         checkName(name);
150         registrations.put(alias, new ResourceRegistration(name, context, getServletContext(), AccessController.getContext()));
151     }
152
153     private void checkName(String JavaDoc name) {
154         if (name == null)
155             throw new IllegalArgumentException JavaDoc("Name cannot be null"); //$NON-NLS-1$
156

157         if (name.endsWith("/") && !name.equals("/")) //$NON-NLS-1$ //$NON-NLS-2$
158
throw new IllegalArgumentException JavaDoc("Invalid Name '" + name + "'"); //$NON-NLS-1$//$NON-NLS-2$
159
}
160
161     private void checkAlias(String JavaDoc alias) throws NamespaceException {
162         if (alias == null)
163             throw new IllegalArgumentException JavaDoc("Alias cannot be null"); //$NON-NLS-1$
164

165         if (!alias.startsWith("/") || (alias.endsWith("/") && !alias.equals("/"))) //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
166
throw new IllegalArgumentException JavaDoc("Invalid alias '" + alias + "'"); //$NON-NLS-1$//$NON-NLS-2$
167

168         if (registrations.containsKey(alias))
169             throw new NamespaceException("The alias '" + alias + "' is already in use."); //$NON-NLS-1$//$NON-NLS-2$
170
}
171 }
172
Popular Tags