comparison 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
comparison
equal deleted inserted replaced
999:74b9daf2826c 1000:32d4b569567c
19 package org.eclipse.jetty.server.handler; 19 package org.eclipse.jetty.server.handler;
20 20
21 import java.io.IOException; 21 import java.io.IOException;
22 22
23 import javax.servlet.ServletException; 23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse; 24 import javax.servlet.http.HttpServletResponse;
26 25
27 import org.eclipse.jetty.server.Request; 26 import org.eclipse.jetty.server.Request;
28 import org.eclipse.jetty.server.Server; 27 import org.eclipse.jetty.server.Server;
29 import org.slf4j.Logger; 28 import org.slf4j.Logger;
37 * This handler is a contribution from Johannes Brodwall: https://bugs.eclipse.org/bugs/show_bug.cgi?id=357687 36 * This handler is a contribution from Johannes Brodwall: https://bugs.eclipse.org/bugs/show_bug.cgi?id=357687
38 * 37 *
39 * Usage: 38 * Usage:
40 * 39 *
41 * <pre> 40 * <pre>
42 Server server = new Server(8080); 41 Server server = new Server(8080);
43 HandlerList handlers = new HandlerList(); 42 HandlerList handlers = new HandlerList();
44 handlers.setHandlers(new Handler[] 43 handlers.setHandlers(new Handler[]
45 { someOtherHandler, new ShutdownHandler(server,&quot;secret password&quot;) }); 44 { someOtherHandler, new ShutdownHandler(server,&quot;secret password&quot;) });
46 server.setHandler(handlers); 45 server.setHandler(handlers);
47 server.start(); 46 server.start();
48 </pre> 47 </pre>
49 * 48 *
50 <pre> 49 <pre>
51 public static void attemptShutdown(int port, String shutdownCookie) { 50 public static void attemptShutdown(int port, String shutdownCookie) {
52 try { 51 try {
53 URL url = new URL("http://localhost:" + port + "/shutdown?token=" + shutdownCookie); 52 URL url = new URL("http://localhost:" + port + "/shutdown?token=" + shutdownCookie);
54 HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 53 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
55 connection.setRequestMethod("POST"); 54 connection.setRequestMethod("POST");
56 connection.getResponseCode(); 55 connection.getResponseCode();
57 logger.info("Shutting down " + url + ": " + connection.getResponseMessage()); 56 logger.info("Shutting down " + url + ": " + connection.getResponseMessage());
58 } catch (SocketException e) { 57 } catch (SocketException e) {
59 logger.debug("Not running"); 58 logger.debug("Not running");
60 // Okay - the server is not running 59 // Okay - the server is not running
61 } catch (IOException e) { 60 } catch (IOException e) {
62 throw new RuntimeException(e); 61 throw new RuntimeException(e);
63 } 62 }
64 } 63 }
65 </pre> 64 </pre>
66 */ 65 */
67 public class ShutdownHandler extends AbstractHandler 66 public class ShutdownHandler extends AbstractHandler
68 { 67 {
69 private static final Logger LOG = LoggerFactory.getLogger(ShutdownHandler.class); 68 private static final Logger LOG = LoggerFactory.getLogger(ShutdownHandler.class);
70 69
71 private final String _shutdownToken; 70 private final String _shutdownToken;
72 71
73 private final Server _server; 72 private final Server _server;
74 73
75 private boolean _exitJvm = false; 74 private boolean _exitJvm = false;
76 75
77 76
78 77
79 /** 78 /**
80 * Creates a listener that lets the server be shut down remotely (but only from localhost). 79 * Creates a listener that lets the server be shut down remotely (but only from localhost).
81 * 80 *
82 * @param server 81 * @param server
83 * the Jetty instance that should be shut down 82 * the Jetty instance that should be shut down
84 * @param shutdownToken 83 * @param shutdownToken
85 * a secret password to avoid unauthorized shutdown attempts 84 * a secret password to avoid unauthorized shutdown attempts
86 */ 85 */
87 public ShutdownHandler(Server server, String shutdownToken) 86 public ShutdownHandler(Server server, String shutdownToken)
88 { 87 {
89 this._server = server; 88 this._server = server;
90 this._shutdownToken = shutdownToken; 89 this._shutdownToken = shutdownToken;
91 } 90 }
92 91
93 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException 92 @Override
94 { 93 public void handle(String target, Request request, HttpServletResponse response) throws IOException, ServletException
95 if (!target.equals("/shutdown")) 94 {
96 { 95 if (!target.equals("/shutdown"))
97 return; 96 {
98 } 97 return;
98 }
99 99
100 if (!request.getMethod().equals("POST")) 100 if (!request.getMethod().equals("POST"))
101 { 101 {
102 response.sendError(HttpServletResponse.SC_BAD_REQUEST); 102 response.sendError(HttpServletResponse.SC_BAD_REQUEST);
103 return; 103 return;
104 } 104 }
105 if (!hasCorrectSecurityToken(request)) 105 if (!hasCorrectSecurityToken(request))
106 { 106 {
107 LOG.warn("Unauthorized shutdown attempt from " + getRemoteAddr(request)); 107 LOG.warn("Unauthorized shutdown attempt from " + getRemoteAddr(request));
108 response.sendError(HttpServletResponse.SC_UNAUTHORIZED); 108 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
109 return; 109 return;
110 } 110 }
111 if (!requestFromLocalhost(request)) 111 if (!requestFromLocalhost(request))
112 { 112 {
113 LOG.warn("Unauthorized shutdown attempt from " + getRemoteAddr(request)); 113 LOG.warn("Unauthorized shutdown attempt from " + getRemoteAddr(request));
114 response.sendError(HttpServletResponse.SC_UNAUTHORIZED); 114 response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
115 return; 115 return;
116 } 116 }
117 117
118 LOG.info("Shutting down by request from " + getRemoteAddr(request)); 118 LOG.info("Shutting down by request from " + getRemoteAddr(request));
119 119
120 new Thread() 120 new Thread()
121 { 121 {
122 public void run () 122 public void run ()
123 { 123 {
124 try 124 try
125 { 125 {
126 shutdownServer(); 126 shutdownServer();
127 } 127 }
128 catch (InterruptedException e) 128 catch (InterruptedException e)
129 { 129 {
130 LOG.trace("",e); 130 LOG.trace("",e);
131 } 131 }
132 catch (Exception e) 132 catch (Exception e)
133 { 133 {
134 throw new RuntimeException("Shutting down server",e); 134 throw new RuntimeException("Shutting down server",e);
135 } 135 }
136 } 136 }
137 }.start(); 137 }.start();
138 } 138 }
139 139
140 private boolean requestFromLocalhost(HttpServletRequest request) 140 private boolean requestFromLocalhost(Request request)
141 { 141 {
142 return "127.0.0.1".equals(getRemoteAddr(request)); 142 return "127.0.0.1".equals(getRemoteAddr(request));
143 } 143 }
144 144
145 protected String getRemoteAddr(HttpServletRequest request) 145 protected String getRemoteAddr(Request request)
146 { 146 {
147 return request.getRemoteAddr(); 147 return request.getRemoteAddr();
148 } 148 }
149 149
150 private boolean hasCorrectSecurityToken(HttpServletRequest request) 150 private boolean hasCorrectSecurityToken(Request request)
151 { 151 {
152 return _shutdownToken.equals(request.getParameter("token")); 152 return _shutdownToken.equals(request.getParameter("token"));
153 } 153 }
154 154
155 private void shutdownServer() throws Exception 155 private void shutdownServer() throws Exception
156 { 156 {
157 _server.stop(); 157 _server.stop();
158 158
159 if (_exitJvm) 159 if (_exitJvm)
160 { 160 {
161 System.exit(0); 161 System.exit(0);
162 } 162 }
163 } 163 }
164 164
165 public void setExitJvm(boolean exitJvm) 165 public void setExitJvm(boolean exitJvm)
166 { 166 {
167 this._exitJvm = exitJvm; 167 this._exitJvm = exitJvm;
168 } 168 }
169 169
170 } 170 }