Skip to content

Commit

Permalink
Simplified BoltRequestMessageHandler interface
Browse files Browse the repository at this point in the history
By removing exception type parameter. It was always `RuntimeException`
for production code. Only test code used checked `IOException`.

This commit makes type simpler and converts exceptions to unchecked
in tests.
  • Loading branch information
lutovich committed Mar 1, 2018
1 parent 9a3c928 commit 83aecc3
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 66 deletions.
Expand Up @@ -35,7 +35,7 @@
* This class is responsible for routing incoming request messages to a worker
* as well as handling outgoing response messages via appropriate handlers.
*/
public class BoltMessageRouter implements BoltRequestMessageHandler<RuntimeException>
public class BoltMessageRouter implements BoltRequestMessageHandler
{
private final BoltMessageLogger messageLogger;

Expand Down Expand Up @@ -63,22 +63,21 @@ public BoltMessageRouter( Log internalLog, BoltMessageLogger messageLogger,
}

@Override
public void onInit( String userAgent, Map<String,Object> authToken ) throws RuntimeException
public void onInit( String userAgent, Map<String,Object> authToken )
{
// TODO: make the client transmit the version for now it is hardcoded to -1 to ensure current behaviour
messageLogger.logInit(userAgent );
worker.enqueue( session -> session.init( userAgent, authToken, initHandler ) );
}

@Override
public void onAckFailure() throws RuntimeException
public void onAckFailure()
{
messageLogger.logAckFailure();
worker.enqueue( session -> session.ackFailure( defaultHandler ) );
}

@Override
public void onReset() throws RuntimeException
public void onReset()
{
messageLogger.clientEvent("INTERRUPT");
messageLogger.logReset();
Expand Down
Expand Up @@ -25,25 +25,21 @@
import org.neo4j.values.virtual.MapValue;

/**
* Interface defining simple handler methods for each defined
* Bolt request message.
*
* @param <E> an exception that may be thrown by each handler method
* Interface defining simple handler methods for each defined Bolt request message.
*/
public interface BoltRequestMessageHandler<E extends Exception>
public interface BoltRequestMessageHandler
{
void onInit( String userAgent, Map<String,Object> authToken ) throws E;

void onAckFailure() throws E;
void onInit( String userAgent, Map<String,Object> authToken );

void onReset() throws E;
void onAckFailure();

void onRun( String statement, MapValue params ) throws E;
void onReset();

void onDiscardAll() throws E;
void onRun( String statement, MapValue params );

void onPullAll() throws E;
void onDiscardAll();

void onExternalError( Neo4jError error ) throws E;
void onPullAll();

void onExternalError( Neo4jError error );
}
Expand Up @@ -47,7 +47,7 @@ public BoltRequestMessageReader( Neo4jPack.Unpacker unpacker )
*
* @param handler handler for request messages
*/
public <E extends Exception> void read( BoltRequestMessageHandler<E> handler ) throws IOException, E
public void read( BoltRequestMessageHandler handler ) throws IOException
{
try
{
Expand Down
Expand Up @@ -31,7 +31,7 @@ public class BoltV1Dechunker
{
private final ChunkedInput input;
private final BoltRequestMessageReader unpacker;
private final BoltRequestMessageHandler<RuntimeException> onMessage;
private final BoltRequestMessageHandler onMessage;
private final Runnable onMessageStarted;

public enum State
Expand All @@ -45,8 +45,7 @@ public enum State
private State state = State.AWAITING_CHUNK;
private int chunkSize;

public BoltV1Dechunker( Neo4jPack neo4jPack, BoltRequestMessageHandler<RuntimeException> messageHandler,
Runnable onMessageStarted )
public BoltV1Dechunker( Neo4jPack neo4jPack, BoltRequestMessageHandler messageHandler, Runnable onMessageStarted )
{
this.onMessage = messageHandler;
this.onMessageStarted = onMessageStarted;
Expand Down
Expand Up @@ -32,22 +32,22 @@
import static org.neo4j.bolt.v1.messaging.message.ResetMessage.reset;
import static org.neo4j.bolt.v1.messaging.message.RunMessage.run;

public class BoltRequestMessageRecorder extends MessageRecorder<RequestMessage> implements BoltRequestMessageHandler<RuntimeException>
public class BoltRequestMessageRecorder extends MessageRecorder<RequestMessage> implements BoltRequestMessageHandler
{
@Override
public void onInit( String clientName, Map<String, Object> credentials ) throws RuntimeException
public void onInit( String clientName, Map<String,Object> credentials )
{
messages.add( init( clientName, credentials ) );
}

@Override
public void onAckFailure() throws RuntimeException
public void onAckFailure()
{
messages.add( ackFailure() );
}

@Override
public void onReset() throws RuntimeException
public void onReset()
{
messages.add( reset() );
}
Expand All @@ -71,7 +71,7 @@ public void onPullAll()
}

@Override
public void onExternalError( Neo4jError error ) throws RuntimeException
public void onExternalError( Neo4jError error )
{
//ignore
}
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.bolt.v1.messaging;

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

import org.neo4j.bolt.v1.messaging.message.RequestMessage;
Expand All @@ -34,10 +35,8 @@
import static org.neo4j.bolt.v1.messaging.BoltRequestMessage.RESET;
import static org.neo4j.bolt.v1.messaging.BoltRequestMessage.RUN;


public class BoltRequestMessageWriter implements BoltRequestMessageHandler<IOException>
public class BoltRequestMessageWriter implements BoltRequestMessageHandler
{

private final Neo4jPack.Packer packer;
private final BoltResponseMessageBoundaryHook onMessageComplete;

Expand All @@ -54,57 +53,106 @@ public BoltRequestMessageWriter write( RequestMessage message ) throws IOExcepti
}

@Override
public void onInit( String clientName, Map<String,Object> credentials ) throws IOException
public void onInit( String clientName, Map<String,Object> credentials )
{
packer.packStructHeader( 2, INIT.signature() );
packer.pack( clientName );
packer.pack( ValueUtils.asMapValue( credentials ) );
onMessageComplete.onMessageComplete();
try
{
packer.packStructHeader( 2, INIT.signature() );
packer.pack( clientName );
packer.pack( ValueUtils.asMapValue( credentials ) );
onMessageComplete.onMessageComplete();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}

@Override
public void onAckFailure() throws IOException
public void onAckFailure()
{
packer.packStructHeader( 0, ACK_FAILURE.signature() );
onMessageComplete.onMessageComplete();
try
{
packer.packStructHeader( 0, ACK_FAILURE.signature() );
onMessageComplete.onMessageComplete();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}

@Override
public void onReset() throws IOException
public void onReset()
{
packer.packStructHeader( 0, RESET.signature() );
onMessageComplete.onMessageComplete();
try
{
packer.packStructHeader( 0, RESET.signature() );
onMessageComplete.onMessageComplete();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}

@Override
public void onRun( String statement, MapValue params )
throws IOException

{
packer.packStructHeader( 2, RUN.signature() );
packer.pack( statement );
packer.pack( params );
onMessageComplete.onMessageComplete();
try
{
packer.packStructHeader( 2, RUN.signature() );
packer.pack( statement );
packer.pack( params );
onMessageComplete.onMessageComplete();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}

@Override
public void onDiscardAll()
throws IOException

{
packer.packStructHeader( 0, DISCARD_ALL.signature() );
onMessageComplete.onMessageComplete();
try
{
packer.packStructHeader( 0, DISCARD_ALL.signature() );
onMessageComplete.onMessageComplete();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}

@Override
public void onPullAll()
throws IOException

{
packer.packStructHeader( 0, PULL_ALL.signature() );
onMessageComplete.onMessageComplete();
try
{
packer.packStructHeader( 0, PULL_ALL.signature() );
onMessageComplete.onMessageComplete();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}

public void flush() throws IOException
public void flush()
{
packer.flush();
try
{
packer.flush();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}

@Override
Expand Down
Expand Up @@ -23,7 +23,7 @@

public class AckFailureMessage implements RequestMessage
{
private static AckFailureMessage INSTANCE = new AckFailureMessage();
private static final AckFailureMessage INSTANCE = new AckFailureMessage();

/**
* Factory method for obtaining ACK_FAILURE messages.
Expand All @@ -38,7 +38,7 @@ private AckFailureMessage()
}

@Override
public <E extends Exception> void dispatch( BoltRequestMessageHandler<E> consumer ) throws E
public void dispatch( BoltRequestMessageHandler consumer )
{
consumer.onAckFailure();
}
Expand Down
Expand Up @@ -23,7 +23,7 @@

public class DiscardAllMessage implements RequestMessage
{
private static DiscardAllMessage INSTANCE = new DiscardAllMessage();
private static final DiscardAllMessage INSTANCE = new DiscardAllMessage();

/**
* Factory method for obtaining DISCARD_ALL messages.
Expand All @@ -38,7 +38,7 @@ private DiscardAllMessage()
}

@Override
public <E extends Exception> void dispatch( BoltRequestMessageHandler<E> consumer ) throws E
public void dispatch( BoltRequestMessageHandler consumer )
{
consumer.onDiscardAll();
}
Expand Down
Expand Up @@ -48,7 +48,7 @@ public String userAgent()
}

@Override
public <E extends Exception> void dispatch( BoltRequestMessageHandler<E> consumer ) throws E
public void dispatch( BoltRequestMessageHandler consumer )
{
consumer.onInit( userAgent, authToken );
}
Expand Down
Expand Up @@ -23,7 +23,7 @@

public class PullAllMessage implements RequestMessage
{
private static PullAllMessage INSTANCE = new PullAllMessage();
private static final PullAllMessage INSTANCE = new PullAllMessage();

public static PullAllMessage pullAll()
{
Expand All @@ -35,7 +35,7 @@ private PullAllMessage()
}

@Override
public <E extends Exception> void dispatch( BoltRequestMessageHandler<E> consumer ) throws E
public void dispatch( BoltRequestMessageHandler consumer )
{
consumer.onPullAll();
}
Expand Down
Expand Up @@ -23,5 +23,5 @@

public interface RequestMessage
{
<E extends Exception> void dispatch( BoltRequestMessageHandler<E> consumer ) throws E;
void dispatch( BoltRequestMessageHandler consumer );
}
Expand Up @@ -23,7 +23,7 @@

public class ResetMessage implements RequestMessage
{
private static ResetMessage INSTANCE = new ResetMessage();
private static final ResetMessage INSTANCE = new ResetMessage();

/**
* Factory method for obtaining RESET messages.
Expand All @@ -38,7 +38,7 @@ private ResetMessage()
}

@Override
public <E extends Exception> void dispatch( BoltRequestMessageHandler<E> consumer ) throws E
public void dispatch( BoltRequestMessageHandler consumer )
{
consumer.onReset();
}
Expand Down
Expand Up @@ -25,7 +25,7 @@

public class RunMessage implements RequestMessage
{
private static MapValue EMPTY_PARAMETERS = VirtualValues.EMPTY_MAP;
private static final MapValue EMPTY_PARAMETERS = VirtualValues.EMPTY_MAP;

/**
* Factory method for obtaining RUN messages.
Expand Down Expand Up @@ -64,7 +64,7 @@ public String statement()
}

@Override
public <E extends Exception> void dispatch( BoltRequestMessageHandler<E> consumer ) throws E
public void dispatch( BoltRequestMessageHandler consumer )
{
consumer.onRun( statement, params );
}
Expand Down

0 comments on commit 83aecc3

Please sign in to comment.