EmbeddedServlet.java

/***************************************************************************
   Copyright 2008 Emily Estes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***************************************************************************/
package net.metanotion.web.servlets;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.metanotion.web.HttpMethod;

/** Servlet for use in embedded containers like Jetty. This implementation
assumes you can control instantation of the Servlet object and have
initialized your app server prior to creating this instance. */
public final class EmbeddedServlet extends HttpServlet {
	private final transient RequestHandler handler;
	/**  Constructor for your already initialized RequestHandler object.
		@param handler RequestHandler instance to dispatch to.
	*/
	public EmbeddedServlet(final RequestHandler handler) {
		super();
		this.handler = handler;
	}

	@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
			throws ServletException, IOException {
		handler.request(HttpMethod.GET, req, resp);
	}

	@Override protected void doDelete(final HttpServletRequest req, final HttpServletResponse resp)
			throws ServletException, IOException {
		handler.request(HttpMethod.DELETE, req, resp);
	}

	@Override protected void doHead(final HttpServletRequest req, final HttpServletResponse resp)
			throws ServletException, IOException {
		handler.request(HttpMethod.HEAD, req, resp);
	}

	@Override protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
			throws ServletException, IOException {
		handler.request(HttpMethod.POST, req, resp);
	}

	@Override protected void doPut(final HttpServletRequest req, final HttpServletResponse resp)
			throws ServletException, IOException {
		handler.request(HttpMethod.PUT, req, resp);
	}
}