diff src/org/eclipse/jetty/server/handler/ShutdownHandler.java @ 1000:32d4b569567c

simplify handle()
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 19 Oct 2016 04:22:51 -0600
parents 8e9db0bbf4f9
children
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/handler/ShutdownHandler.java	Wed Oct 19 00:59:46 2016 -0600
+++ b/src/org/eclipse/jetty/server/handler/ShutdownHandler.java	Wed Oct 19 04:22:51 2016 -0600
@@ -21,7 +21,6 @@
 import java.io.IOException;
 
 import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.eclipse.jetty.server.Request;
@@ -39,132 +38,133 @@
  * Usage:
  *
  * <pre>
-    Server server = new Server(8080);
-    HandlerList handlers = new HandlerList();
-    handlers.setHandlers(new Handler[]
-    { someOtherHandler, new ShutdownHandler(server,&quot;secret password&quot;) });
-    server.setHandler(handlers);
-    server.start();
+	Server server = new Server(8080);
+	HandlerList handlers = new HandlerList();
+	handlers.setHandlers(new Handler[]
+	{ someOtherHandler, new ShutdownHandler(server,&quot;secret password&quot;) });
+	server.setHandler(handlers);
+	server.start();
    </pre>
  * 
    <pre>
    public static void attemptShutdown(int port, String shutdownCookie) {
-        try {
-            URL url = new URL("http://localhost:" + port + "/shutdown?token=" + shutdownCookie);
-            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
-            connection.setRequestMethod("POST");
-            connection.getResponseCode();
-            logger.info("Shutting down " + url + ": " + connection.getResponseMessage());
-        } catch (SocketException e) {
-            logger.debug("Not running");
-            // Okay - the server is not running
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
-    }
+		try {
+			URL url = new URL("http://localhost:" + port + "/shutdown?token=" + shutdownCookie);
+			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
+			connection.setRequestMethod("POST");
+			connection.getResponseCode();
+			logger.info("Shutting down " + url + ": " + connection.getResponseMessage());
+		} catch (SocketException e) {
+			logger.debug("Not running");
+			// Okay - the server is not running
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
   </pre>
  */
 public class ShutdownHandler extends AbstractHandler
 {
-    private static final Logger LOG = LoggerFactory.getLogger(ShutdownHandler.class);
+	private static final Logger LOG = LoggerFactory.getLogger(ShutdownHandler.class);
 
-    private final String _shutdownToken;
+	private final String _shutdownToken;
 
-    private final Server _server;
+	private final Server _server;
 
-    private boolean _exitJvm = false;
+	private boolean _exitJvm = false;
   
-    
+	
 
-    /**
-     * Creates a listener that lets the server be shut down remotely (but only from localhost).
-     *
-     * @param server
-     *            the Jetty instance that should be shut down
-     * @param shutdownToken
-     *            a secret password to avoid unauthorized shutdown attempts
-     */
-    public ShutdownHandler(Server server, String shutdownToken)
-    {
-        this._server = server;
-        this._shutdownToken = shutdownToken;
-    }
+	/**
+	 * Creates a listener that lets the server be shut down remotely (but only from localhost).
+	 *
+	 * @param server
+	 *            the Jetty instance that should be shut down
+	 * @param shutdownToken
+	 *            a secret password to avoid unauthorized shutdown attempts
+	 */
+	public ShutdownHandler(Server server, String shutdownToken)
+	{
+		this._server = server;
+		this._shutdownToken = shutdownToken;
+	}
 
-    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
-    {
-        if (!target.equals("/shutdown"))
-        {
-            return;
-        }
+	@Override
+	public void handle(String target, Request request, HttpServletResponse response) throws IOException, ServletException
+	{
+		if (!target.equals("/shutdown"))
+		{
+			return;
+		}
 
-        if (!request.getMethod().equals("POST"))
-        {
-            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
-            return;
-        }
-        if (!hasCorrectSecurityToken(request))
-        {
-            LOG.warn("Unauthorized shutdown attempt from " + getRemoteAddr(request));
-            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
-            return;
-        }
-        if (!requestFromLocalhost(request))
-        {
-            LOG.warn("Unauthorized shutdown attempt from " + getRemoteAddr(request));
-            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
-            return;
-        }
+		if (!request.getMethod().equals("POST"))
+		{
+			response.sendError(HttpServletResponse.SC_BAD_REQUEST);
+			return;
+		}
+		if (!hasCorrectSecurityToken(request))
+		{
+			LOG.warn("Unauthorized shutdown attempt from " + getRemoteAddr(request));
+			response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
+			return;
+		}
+		if (!requestFromLocalhost(request))
+		{
+			LOG.warn("Unauthorized shutdown attempt from " + getRemoteAddr(request));
+			response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
+			return;
+		}
 
-        LOG.info("Shutting down by request from " + getRemoteAddr(request));
-        
-        new Thread()
-        {
-            public void run ()
-            {
-                try
-                {
-                    shutdownServer();
-                }
-                catch (InterruptedException e)
-                {
-                    LOG.trace("",e);
-                }
-                catch (Exception e)
-                {
-                    throw new RuntimeException("Shutting down server",e);
-                }
-            }
-        }.start();
-    }
+		LOG.info("Shutting down by request from " + getRemoteAddr(request));
+		
+		new Thread()
+		{
+			public void run ()
+			{
+				try
+				{
+					shutdownServer();
+				}
+				catch (InterruptedException e)
+				{
+					LOG.trace("",e);
+				}
+				catch (Exception e)
+				{
+					throw new RuntimeException("Shutting down server",e);
+				}
+			}
+		}.start();
+	}
 
-    private boolean requestFromLocalhost(HttpServletRequest request)
-    {
-        return "127.0.0.1".equals(getRemoteAddr(request));
-    }
+	private boolean requestFromLocalhost(Request request)
+	{
+		return "127.0.0.1".equals(getRemoteAddr(request));
+	}
 
-    protected String getRemoteAddr(HttpServletRequest request)
-    {
-        return request.getRemoteAddr();
-    }
+	protected String getRemoteAddr(Request request)
+	{
+		return request.getRemoteAddr();
+	}
 
-    private boolean hasCorrectSecurityToken(HttpServletRequest request)
-    {
-        return _shutdownToken.equals(request.getParameter("token"));
-    }
+	private boolean hasCorrectSecurityToken(Request request)
+	{
+		return _shutdownToken.equals(request.getParameter("token"));
+	}
 
-    private void shutdownServer() throws Exception
-    {
-        _server.stop();
-        
-        if (_exitJvm)
-        {
-            System.exit(0);
-        }
-    }
+	private void shutdownServer() throws Exception
+	{
+		_server.stop();
+		
+		if (_exitJvm)
+		{
+			System.exit(0);
+		}
+	}
 
-    public void setExitJvm(boolean exitJvm)
-    {
-        this._exitJvm = exitJvm;
-    }
+	public void setExitJvm(boolean exitJvm)
+	{
+		this._exitJvm = exitJvm;
+	}
 
 }