
org.jmockring.webserver.jetty.ShutdownHandler Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2013, Pavel Lechev
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3) Neither the name of the Pavel Lechev nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jmockring.webserver.jetty;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ShutdownHandler extends AbstractHandler {
private static final Logger LOG = LoggerFactory.getLogger(ShutdownHandler.class);
private final Server server;
private final String secret;
public ShutdownHandler(Server server, String secret) {
this.server = server;
this.secret = secret;
}
/**
* @param target
* @param serverRequest
* @param request
* @param response
* @throws IOException
* @should return immediately if URL not shutdown
* @should return immediately if method not POST
* @should return immediately if secret key is invalid
* @should stop server by executing Jetty stop method asynchronously
*/
public void handle(String target, Request serverRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
if (!"/shutdown".equals(target) || !"POST".equals(request.getMethod()) || !secret.equals(request.getParameter("secret"))) {
return;
}
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = response.getWriter();
out.println("Shutting down");
out.close();
serverRequest.setHandled(true);
new Thread() {
@Override
public void run() {
try {
LOG.info("Shutting down the server...");
server.stop();
LOG.info("Server has stopped.");
} catch (Exception ex) {
LOG.error("Error when stopping Jetty server: " + ex.getMessage());
}
}
}.start();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy