Click here to Skip to main content
15,897,704 members
Articles / Programming Languages / Java

Ajaxion - Standalone AJAX - Part 2 of 2 - C# and Java Example

Rate me:
Please Sign up or sign in to vote.
4.97/5 (34 votes)
22 Jan 2013CPOL5 min read 55.3K   1.4K   42  
An article about how to keep AJAX simple as it is and get the most out of it.
package pw.ajaxion;

import java.io.IOException;
import java.io.PrintWriter;

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

import pw.log.FileLog;


public final class AjaxionEventConsumer
{
	private String URL_EVENT = "e";
	private String URL_PARAM = "p";
	//private String URL_DATE = "d";

	public AjaxionEventConsumer(HttpServletRequest request, 
										 HttpServletResponse response)
										 throws Exception
	{
		init(request, response, 0);
	}

	public AjaxionEventConsumer(HttpServletRequest request, 
										 HttpServletResponse response, long sleep)
										 throws Exception
	{
		init(request, response, sleep);
	}

	private HttpServletResponse response;
	private PrintWriter responsePrinter;

	public String getEventId()
	{
		return eventId;
	}
	private String eventId;

	public String getParameters()
	{
		return param;
	}
	private String param;

	public void init(HttpServletRequest request, HttpServletResponse response, long sleep)
		throws Exception
	{
		try
		{
			this.response = response;
			eventId = request.getParameter(URL_EVENT);

			param = request.getParameter(URL_PARAM);
			if (param == null)
				param = "";

			if (sleep > 0)
				Thread.sleep(sleep);
		}
		catch (Exception ex)
		{						 
		   String message = "AjaxionEventHandler.init exception : " + ex.getMessage();
		   FileLog.WriteLn(new FileLog().getFileName(), message);
		   throw new Exception(message);
		}
	}

	public void init(HttpServletRequest request, HttpServletResponse response)
		throws Exception
	{
		init(request, response, 0);
	}
	
	public void consumeEvent(String responseData, String responseContentType)
		throws Exception
	{
		beginResponse(responseContentType);
		responsePrinter.print(responseData);
		endResponse();
	}
	public void consumeEvent(byte[] responseData, String responseContentType)
		throws Exception
	{
		beginResponse(responseContentType);
		responsePrinter.print(responseData);
		endResponse();
	}

	public void beginResponse(String contentType)
		throws IOException
	{
	   if (responsePrinter == null)
	      responsePrinter = response.getWriter();

	   response.setContentType(contentType);
	   responsePrinter.flush();
	}
	public void respondString(String responseData)
		throws Exception
	{
	   if (response.getContentType().indexOf("text") > -1)
	   {
	      responsePrinter.print(responseData);
	      responsePrinter.flush();
	   }
	   else
	      throw new Exception("Unexpected content type...");
	}
	public void respondBytes(byte[] responseData)
		throws Exception
	{
		if (response.getContentType().indexOf("text") == -1)
		{
		   responsePrinter. print(responseData);
		   responsePrinter.flush();
		}
		else
			throw new Exception("Unexpected content type...");
	}

	public void endResponse() throws Exception
	{
		try
		{
			if (responsePrinter != null)
			{
				responsePrinter.flush();
				responsePrinter.close();
				responsePrinter = null;
			}
		}
		catch (Exception ex)
		{
		   String message = "AjaxionEventHandler.endResponse exception : " + ex.getMessage();
		   FileLog.WriteLn(new FileLog().getFileName(), message);
		   throw new Exception(message);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
New Zealand New Zealand
Coder

Comments and Discussions