Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // This software is released into the Public Domain.  See copying.txt for details.
  package org.openstreetmap.osmosis.replicationhttp.v0_6.impl;
  
  
Netty handler for sending replication sequence numbers to clients.

Author(s):
Brett Henderson
 
 public abstract class SequenceServerHandler extends SimpleChannelHandler {
 
 	private static final Logger LOG = Logger.getLogger(SequenceServerHandler.class.getName());
 
 	private long currentSequenceNumber;


Creates a new instance.

Parameters:
control Provides the Netty handlers with access to the controller.
 
 		this. = control;
 	}


Gets the central control object.

Returns:
The controller.
 
 		return ;
 	}
 
 
 	}


Writes a HTTP 404 response to the client.

Parameters:
ctx The Netty context.
requestedUri The URI requested by the client.
 
 	private void writeResourceNotFound(final ChannelHandlerContext ctxString requestedUri) {
 		// Write the HTTP header to the client.
 		response.addHeader("Content-Type""text/plain");
 
 		// Send the 404 message to the client.
 		ChannelBuffer buffer = ChannelBuffers.copiedBuffer("The requested resource does not exist: " + requestedUri,
 		response.setContent(buffer);
 
 		// Write the header. Use a new future because the future we've been
 		// passed is for upstream.
 		ChannelFuture headerFuture = Channels.future(ctx.getChannel());
 		Channels.write(ctxheaderFutureresponse);
 
 		// Wait for the previous operation to finish and then close the channel.
 		headerFuture.addListener(new ChannelFutureListener() {
 			public void operationComplete(ChannelFuture future) {
 				ctx.getChannel().close();
 			}
 		});
 	}


Writes a HTTP 410 response to the client.

Parameters:
ctx The Netty context.
requestedUri The URI requested by the client.
	private void writeResourceGone(final ChannelHandlerContext ctxString requestedUri) {
		// Write the HTTP header to the client.
		response.addHeader("Content-Type""text/plain");
		// Send the 410 message to the client.
		ChannelBuffer buffer = ChannelBuffers.copiedBuffer("The requested resource is no longer available: "
requestedUri.);
		response.setContent(buffer);
		// Write the header. Use a new future because the future we've been
		// passed is for upstream.
		ChannelFuture headerFuture = Channels.future(ctx.getChannel());
		Channels.write(ctxheaderFutureresponse);
		// Wait for the previous operation to finish and then close the channel.
		headerFuture.addListener(new ChannelFutureListener() {
			public void operationComplete(ChannelFuture future) {
			}
		});
	}


Writes a HTTP 400 response to the client.

Parameters:
ctx The Netty context.
requestedUri The URI requested by the client.
errorMessage Further information about why the request is bad.
	private void writeBadRequest(final ChannelHandlerContext ctxString requestedUri,
			String errorMessage) {
		final String newLine = "\r\n";
		// Write the HTTP header to the client.
		response.addHeader("Content-Type""text/plain");
		// Send the 400 message to the client.
		StringBuilder messageBuilder = new StringBuilder();
		messageBuilder.append("Bad Request").append(newLine);
		messageBuilder.append("Message: ").append(errorMessage).append(newLine);
		messageBuilder.append("Requested URI: ").append(requestedUri).append(newLine);
		ChannelBuffer buffer = ChannelBuffers.copiedBuffer(messageBuilder.toString(), .);
		response.setContent(buffer);
		// Write the header. Use a new future because the future we've been
		// passed is for upstream.
		ChannelFuture headerFuture = Channels.future(ctx.getChannel());
		Channels.write(ctxheaderFutureresponse);
		// Wait for the previous operation to finish and then close the channel.
		headerFuture.addListener(new ChannelFutureListener() {
			public void operationComplete(ChannelFuture future) {
			}
		});
	}


Writes server statistics to the client.

Parameters:
ctx The Netty context.
	private void writeStatistics(final ChannelHandlerContext ctx) {
		final String newLine = "\r\n";
		// Write the HTTP header to the client.
		response.addHeader("Content-Type""text/plain");
		// Send the statistics message to the client.
		StringBuilder messageBuilder = new StringBuilder();
		messageBuilder.append("Server Statistics").append(newLine);
		messageBuilder.append("Total Requests: ").append(statistics.getTotalRequests()).append(newLine);
		messageBuilder.append("Active Connections: ").append(statistics.getActiveConnections()).append(newLine);
		ChannelBuffer buffer = ChannelBuffers.copiedBuffer(messageBuilder.toString(), .);
		response.setContent(buffer);
		// Write the header. Use a new future because the future we've been
		// passed is for upstream.
		ChannelFuture headerFuture = Channels.future(ctx.getChannel());
		Channels.write(ctxheaderFutureresponse);
		// Wait for the previous operation to finish and then close the channel.
		headerFuture.addListener(new ChannelFutureListener() {
			public void operationComplete(ChannelFuture future) {
			}
		});
	}


Writes sequence data to the client. If follow is set, it allows continuous updates to be streamed to the client.

Parameters:
ctx The Netty context.
contentType The content type to set on the HTTP response.
requestedSequenceNumber The requested sequence number. Sending will start from this number.
follow If true, continuous updates will be sent to the client.
	protected void initiateSequenceWriting(final ChannelHandlerContext ctx,
			String contentTypefinal long requestedSequenceNumberfinal boolean follow) {
		// Create the HTTP header to send to the client.
		response.addHeader("Content-Type"contentType);
		response.setChunked(true);
		// Write the header. We must use a new future because the future we've
		// been passed is for upstream.
		ChannelFuture headerFuture = Channels.future(ctx.getChannel());
		Channels.write(ctxheaderFutureresponse);
		// Wait for the previous operation to finish and then start sending
		// sequence numbers to this channel.
		headerFuture.addListener(new ChannelFutureListener() {
			public void operationComplete(ChannelFuture futurethrows Exception {
				if (future.isSuccess()) {
					.determineNextChannelAction(ctx.getChannel(), requestedSequenceNumberfollow);
				}
			}
		});
	}


Parses the request and initialises the response processing, typically by calling the writeSequence method.

Parameters:
ctx The Netty context.
request The client request.
	protected abstract void handleRequest(ChannelHandlerContext ctxHttpRequest request);
	public void messageReceived(ChannelHandlerContext ctxMessageEvent ethrows Exception {
		// We have received a message from the client which is a HTTP request.
		HttpRequest request = (HttpRequeste.getMessage();
			.fine("Received new request from " + remoteAddress.getAddress().getHostAddress() + ":"
remoteAddress.getPort());
		}
		// Process the HTTP request.
		try {
			// Check if this is a request to a generic URL. If it isn't
			// something we support then delegate to the specific handler.
			if (request.getUri().equals("/statistics")) {
else {
				handleRequest(ctxrequest);
			}
catch (ResourceNotFoundException ex) {
			writeResourceNotFound(ctxrequest.getUri());
catch (ResourceGoneException ex) {
			writeResourceGone(ctxrequest.getUri());
catch (BadRequestException ex) {
			writeBadRequest(ctxrequest.getUri(), ex.getMessage());
		}
	}


Convert the sequence number to sequence data and write to the channel.

Parameters:
ctx The channel handler context.
future The future for current processing.
sequenceNumber The sequence number to be written.
	protected abstract void writeSequence(ChannelHandlerContext ctxChannelFuture futurelong sequenceNumber);
	public void writeRequested(ChannelHandlerContext ctxMessageEvent ethrows Exception {
		// The message event is a Long containing the sequence number.
		// Call the concrete implementation to convert the sequence to writable
		// data.
	}
		// Get the cause of the exception.
		Throwable t = e.getCause();
		// A ClosedChannelException occurs if the client disconnects and is not
		// an error scenario.
		if (!(t instanceof ClosedChannelException)) {
			.log(."Error during processing for channel " + ctx.getChannel() + "."t);
		}
		// We must stop sending to this client if any errors occur during
		// processing.
	}

Used during request parsing to notify that the requested URI could not be found.
	protected static class ResourceNotFoundException extends RuntimeException {
		private static final long serialVersionUID = -1L;
	}

Used during request parsing to notify that the request is invalid in some way.
	protected static class BadRequestException extends RuntimeException {
		private static final long serialVersionUID = -1L;


Creates a new instance.

Parameters:
message The error message.
		public BadRequestException(String message) {
			super(message);
		}
	}

Used during request parsing to notify that the requested URI is no longer available.
	protected static class ResourceGoneException extends RuntimeException {
		private static final long serialVersionUID = -1L;
	}
New to GrepCode? Check out our FAQ X