Thu. Mar 28th, 2024
filters

Today, I’m going to share with you a command handler for the HTTP Server I presented previously.  This is a handler to extend the abilities of that HTTP Server in a Jar to provide you the ability to stop, restart and get status of it, if needed.

WARNING: This is NOT a robust, secure HTTP Server, this is a simple HTTP Server, for when you need one, without the hassle of installing one.  If you need robust, and secure, then go install Apache HTTP, Apache Tomcat or something else!

This provides a rest API to perform a few tasks quickly and easily.  Here are the commands implemented

PathTypeDescription
/statusGETGets the current status of the HTTP Server, providing back a text based response.
/stopPOSTInstructs the HTTP Server to shutdown, once all processing is complete.
/restartPOSTInstructs the HTTP Server to restart once all processing is complete.

These are pretty commands and you easily see how to add additional commands to these as needed.   Stay tuned for my next on this series of posts!

package name.mymiller.httpserver.handlers;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.sun.net.httpserver.Filter;
import com.sun.net.httpserver.HttpExchange;

import name.mymiller.extensions.log.LogManager;
import name.mymiller.httpserver.HttpConstants;
import name.mymiller.httpserver.HttpSystem;

/**
 * @author jmiller
 *
 */
@SuppressWarnings("restriction")
@HttpHandlerAnnotation(context = "/cmd")
public class CmdHandler extends AbstractContextHandler
{
	/**
	 * Error Text for unknown command
	 */
	private static final String UNKOWN_COMMAND = "Unkown Command";

	@Override
	public void doGet(final HttpExchange exchange, final String pathInfo, final Map<String, Object> parameters)
			throws IOException
	{
		switch (pathInfo) {
			case "/status":
				final String responseBody = "Listen Port: " + HttpSystem.getInstance().getAddress().getPort()
						+ "\nListen Address: " + HttpSystem.getInstance().getAddress().getHostString()
						+ "\nActive Thread Count: " + HttpSystem.getInstance().getActiveCount()
						+ "\nLargest Thread Pool Size: " + HttpSystem.getInstance().getLargestPoolSize()
						+ "\nMaximum Thread Pool Size: " + HttpSystem.getInstance().getMaximumPoolSize()
						+ "\nCurrent Task Count: " + HttpSystem.getInstance().getTaskCount()
						+ "\nCompleted Task Count: " + HttpSystem.getInstance().getCompletedTaskCount() + "\n";

				LogManager.getLogger(this.getClass()).info("Response: " + responseBody);

				this.sendResponse(exchange, HttpConstants.HTTP_OK_STATUS, responseBody);
				break;
			default:
				LogManager.getLogger(this.getClass()).severe("Unknown GET Command: " + pathInfo);

				this.sendResponse(exchange, HttpConstants.HTTP_NOT_ACCEPTABLE_STATUS, CmdHandler.UNKOWN_COMMAND);
				break;
		}
	}

	@Override
	public void doPost(final HttpExchange exchange, final String pathInfo, final Map<String, Object> parameters)
			throws IOException
	{
		switch (pathInfo) {
			case "/stop":
				final String stopResponse = "Stopping Server on Port:"
						+ HttpSystem.getInstance().getAddress().getPort();
				LogManager.getLogger(this.getClass()).info("Response: " + stopResponse);
				// Set the response header status and length

				this.sendResponse(exchange, HttpConstants.HTTP_OK_STATUS, stopResponse);

				HttpSystem.getInstance().shutdown(HttpConstants.DELAY_TIME);
				break;
			case "/restart":
				final String restartResponse = "Restarting Server on Port:"
						+ HttpSystem.getInstance().getAddress().getPort();
				LogManager.getLogger(this.getClass()).info("Response: " + restartResponse);

				this.sendResponse(exchange, HttpConstants.HTTP_OK_STATUS, restartResponse);

				HttpSystem.getInstance().setRestart(true);
				break;
			default:
				LogManager.getLogger(this.getClass()).severe("Unknown POST Command: " + pathInfo);

				this.sendResponse(exchange, HttpConstants.HTTP_NOT_ACCEPTABLE_STATUS, CmdHandler.UNKOWN_COMMAND);
				break;
		}

	}

	@Override
	public List<Filter> getFilters()
	{
		return null;
	}
}

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d