View Javadoc

1   // ========================================================================
2   // Copyright (c) 2010-2011 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.osgi.httpservice;
14  
15  import java.io.IOException;
16  
17  import javax.servlet.ServletException;
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.eclipse.jetty.server.Request;
22  import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
23  
24  /**
25   * Extended error page handler.
26   * Makes it easy to plug a servlet to handle errors thrown by the HttpService or
27   * to use Jetty's ErrorPageErrorHandler API to plug custom error pages.
28   */
29  public class HttpServiceErrorPageErrorHandler extends ErrorPageErrorHandler
30  {
31  
32  	private static HttpServiceErrorPageErrorHandler INSTANCE;
33  	
34  	public static HttpServiceErrorPageErrorHandler getInstance()
35  	{
36  		return INSTANCE;
37  	}
38  	
39  	public HttpServiceErrorPageErrorHandler()
40  	{
41  		INSTANCE = this;
42  	}
43  
44  	@Override
45  	public void handle(String target, Request baseRequest,
46  			HttpServletRequest request, HttpServletResponse response)
47  			throws IOException {
48  		if (HttpServiceErrorHandlerHelper.getCustomErrorHandler() != null)
49  		{
50  			try
51  			{
52  				HttpServiceErrorHandlerHelper.getCustomErrorHandler().service(request, response);
53  			}
54  			catch (ServletException e)
55  			{
56  				//well
57  			}
58  		}
59  		if (!response.isCommitted())
60  		{
61  			super.handle(target, baseRequest, request, response);
62  		}
63  	}
64  
65  	@Override
66  	protected void doStop() throws Exception
67  	{
68  		INSTANCE = null;
69  		super.doStop();
70  	}
71  	
72  	
73  	
74  }