Skip to content

Commit

Permalink
Throw ProtocolException when QueryType is unknown or missing
Browse files Browse the repository at this point in the history
This update ensures that `ProtocolException` is thrown when server provides an unknown `QueryType` or when it is missing on `SUCCESS` response that expects it.
  • Loading branch information
injectives committed Mar 21, 2022
1 parent df8b781 commit a8bc968
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.neo4j.driver.Query;
import org.neo4j.driver.Record;
import org.neo4j.driver.Value;
import org.neo4j.driver.exceptions.Neo4jException;
import org.neo4j.driver.internal.InternalRecord;
import org.neo4j.driver.internal.messaging.request.PullAllMessage;
import org.neo4j.driver.internal.spi.Connection;
Expand Down Expand Up @@ -92,19 +93,34 @@ public boolean canManageAutoRead()
public synchronized void onSuccess( Map<String,Value> metadata )
{
finished = true;
summary = extractResultSummary( metadata );
Neo4jException exception = null;
try
{
summary = extractResultSummary( metadata, true );
}
catch ( Neo4jException e )
{
exception = e;
}

completionListener.afterSuccess( metadata );
if ( exception == null )
{
completionListener.afterSuccess( metadata );

completeRecordFuture( null );
completeFailureFuture( null );
completeRecordFuture( null );
completeFailureFuture( null );
}
else
{
onFailure( exception );
}
}

@Override
public synchronized void onFailure( Throwable error )
{
finished = true;
summary = extractResultSummary( emptyMap() );
summary = extractResultSummary( emptyMap(), false );

completionListener.afterFailure( error );

Expand Down Expand Up @@ -332,10 +348,10 @@ private boolean completeFailureFuture( Throwable error )
return false;
}

private ResultSummary extractResultSummary( Map<String,Value> metadata )
private ResultSummary extractResultSummary( Map<String,Value> metadata, boolean enforceQueryType )
{
long resultAvailableAfter = runResponseHandler.resultAvailableAfter();
return metadataExtractor.extractSummary(query, connection, resultAvailableAfter, metadata );
return metadataExtractor.extractSummary( query, connection, resultAvailableAfter, metadata, enforceQueryType );
}

private void enableAutoRead()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.neo4j.driver.Query;
import org.neo4j.driver.Record;
import org.neo4j.driver.Value;
import org.neo4j.driver.exceptions.Neo4jException;
import org.neo4j.driver.internal.InternalRecord;
import org.neo4j.driver.internal.handlers.PullResponseCompletionListener;
import org.neo4j.driver.internal.handlers.RunResponseHandler;
Expand Down Expand Up @@ -106,15 +107,24 @@ public synchronized void cancel()
protected void completeWithFailure( Throwable error )
{
completionListener.afterFailure( error );
complete( extractResultSummary( emptyMap() ), error );
complete( extractResultSummary( emptyMap(), false ), error );
}

protected void completeWithSuccess( Map<String,Value> metadata )
protected void completeWithSuccess( Map<String,Value> metadata, boolean enforceQueryType )
{
completionListener.afterSuccess( metadata );
ResultSummary summary = extractResultSummary( metadata );

complete( summary, null );
ResultSummary summary;
Neo4jException exception = null;
try
{
summary = extractResultSummary( metadata, enforceQueryType );
}
catch ( Neo4jException e )
{
summary = extractResultSummary( emptyMap(), false );
exception = e;
}
complete( summary, exception );
}

protected void successHasMore()
Expand Down Expand Up @@ -169,10 +179,10 @@ protected boolean isDone()
return state.equals( State.SUCCEEDED_STATE ) || state.equals( State.FAILURE_STATE );
}

private ResultSummary extractResultSummary( Map<String,Value> metadata )
private ResultSummary extractResultSummary( Map<String,Value> metadata, boolean enforceQueryType )
{
long resultAvailableAfter = runResponseHandler.resultAvailableAfter();
return metadataExtractor.extractSummary( query, connection, resultAvailableAfter, metadata );
return metadataExtractor.extractSummary( query, connection, resultAvailableAfter, metadata, enforceQueryType );
}

private void addToRequest( long toAdd )
Expand Down Expand Up @@ -247,7 +257,7 @@ enum State
void onSuccess( BasicPullResponseHandler context, Map<String,Value> metadata )
{
context.state( SUCCEEDED_STATE );
context.completeWithSuccess( metadata );
context.completeWithSuccess( metadata, false );
}

@Override
Expand Down Expand Up @@ -290,7 +300,7 @@ void onSuccess( BasicPullResponseHandler context, Map<String,Value> metadata )
else
{
context.state( SUCCEEDED_STATE );
context.completeWithSuccess( metadata );
context.completeWithSuccess( metadata, true );
}
}

Expand Down Expand Up @@ -334,7 +344,7 @@ void onSuccess( BasicPullResponseHandler context, Map<String,Value> metadata )
else
{
context.state( SUCCEEDED_STATE );
context.completeWithSuccess( metadata );
context.completeWithSuccess( metadata, true );
}
}

Expand Down Expand Up @@ -369,7 +379,7 @@ void cancel( BasicPullResponseHandler context )
void onSuccess( BasicPullResponseHandler context, Map<String,Value> metadata )
{
context.state( SUCCEEDED_STATE );
context.completeWithSuccess( metadata );
context.completeWithSuccess( metadata, false );
}

@Override
Expand Down Expand Up @@ -403,7 +413,7 @@ void cancel( BasicPullResponseHandler context )
void onSuccess( BasicPullResponseHandler context, Map<String,Value> metadata )
{
context.state( SUCCEEDED_STATE );
context.completeWithSuccess( metadata );
context.completeWithSuccess( metadata, false );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.neo4j.driver.Bookmark;
import org.neo4j.driver.Query;
import org.neo4j.driver.Value;
import org.neo4j.driver.exceptions.ProtocolException;
import org.neo4j.driver.exceptions.UntrustedServerException;
import org.neo4j.driver.internal.InternalBookmark;
import org.neo4j.driver.internal.spi.Connection;
Expand All @@ -49,6 +51,10 @@
public class MetadataExtractor
{
public static final int ABSENT_QUERY_ID = -1;
public static final String MISSING_TYPE_MSG = "No query type has been provided, consider updating the driver";
private static final String UNEXPECTED_TYPE_MSG_FMT = "Unexpected query type '%s', consider updating the driver";
private static final Function<String,ProtocolException> UNEXPECTED_TYPE_EXCEPTION_SUPPLIER =
( type ) -> new ProtocolException( String.format( UNEXPECTED_TYPE_MSG_FMT, type ) );
private final String resultAvailableAfterMetadataKey;
private final String resultConsumedAfterMetadataKey;

Expand Down Expand Up @@ -98,14 +104,15 @@ public long extractResultAvailableAfter( Map<String,Value> metadata )
return -1;
}

public ResultSummary extractSummary(Query query, Connection connection, long resultAvailableAfter, Map<String,Value> metadata )
public ResultSummary extractSummary( Query query, Connection connection, long resultAvailableAfter, Map<String,Value> metadata, boolean enforceQueryType )
{
ServerInfo serverInfo =
new InternalServerInfo( connection.serverAgent(), connection.serverAddress(), connection.protocol().version() );
DatabaseInfo dbInfo = extractDatabaseInfo( metadata );
return new InternalResultSummary(query, serverInfo, dbInfo, extractQueryType( metadata ), extractCounters( metadata ), extractPlan( metadata ),
extractProfiledPlan( metadata ), extractNotifications( metadata ), resultAvailableAfter,
extractResultConsumedAfter( metadata, resultConsumedAfterMetadataKey ) );
QueryType queryType = extractQueryType( metadata, enforceQueryType );
return new InternalResultSummary( query, serverInfo, dbInfo, queryType, extractCounters( metadata ), extractPlan( metadata ),
extractProfiledPlan( metadata ), extractNotifications( metadata ), resultAvailableAfter,
extractResultConsumedAfter( metadata, resultConsumedAfterMetadataKey ) );
}

public static DatabaseInfo extractDatabaseInfo( Map<String,Value> metadata )
Expand Down Expand Up @@ -146,12 +153,16 @@ public static Value extractServer( Map<String,Value> metadata )
return versionValue;
}

private static QueryType extractQueryType( Map<String,Value> metadata )
private static QueryType extractQueryType( Map<String,Value> metadata, boolean enforce )
{
Value typeValue = metadata.get( "type" );
if ( typeValue != null )
{
return QueryType.fromCode( typeValue.asString() );
return QueryType.fromCode( typeValue.asString(), UNEXPECTED_TYPE_EXCEPTION_SUPPLIER );
}
else if ( enforce )
{
throw new ProtocolException( MISSING_TYPE_MSG );
}
return null;
}
Expand Down
24 changes: 22 additions & 2 deletions driver/src/main/java/org/neo4j/driver/summary/QueryType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
*/
package org.neo4j.driver.summary;

import java.util.function.Function;

import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.exceptions.Neo4jException;

/**
* The type of query executed.
*
* @since 1.0
*/
public enum QueryType
Expand All @@ -31,7 +35,16 @@ public enum QueryType
WRITE_ONLY,
SCHEMA_WRITE;

public static QueryType fromCode(String type )
private static final String UNEXPECTED_TYPE_MSG_FMT = "Unknown query type: `%s`.";
private static final Function<String,ClientException> UNEXPECTED_TYPE_EXCEPTION_SUPPLIER =
( type ) -> new ClientException( String.format( UNEXPECTED_TYPE_MSG_FMT, type ) );

public static QueryType fromCode( String type )
{
return fromCode( type, UNEXPECTED_TYPE_EXCEPTION_SUPPLIER );
}

public static QueryType fromCode( String type, Function<String,? extends Neo4jException> exceptionFunction )
{
switch ( type )
{
Expand All @@ -44,7 +57,14 @@ public static QueryType fromCode(String type )
case "s":
return QueryType.SCHEMA_WRITE;
default:
throw new ClientException( "Unknown query type: `" + type + "`." );
if ( exceptionFunction != null )
{
throw exceptionFunction.apply( type );
}
else
{
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

Expand All @@ -45,7 +46,6 @@
import org.neo4j.driver.util.Pair;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
Expand Down Expand Up @@ -369,7 +369,7 @@ private Result createResult(int numberOfRecords )
{
pullAllHandler.onRecord( new Value[]{value( "v1-" + i ), value( "v2-" + i )} );
}
pullAllHandler.onSuccess( emptyMap() );
pullAllHandler.onSuccess( Collections.singletonMap( "type", value( "rw" ) ) );

AsyncResultCursor cursor = new AsyncResultCursorImpl( null, runHandler, pullAllHandler );
return new InternalResult( connection, new DisposableAsyncResultCursor( cursor ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -107,7 +106,7 @@ void shouldNotDisableAutoReadWhenSummaryRequested()

verify( connection, never() ).disableAutoRead();

handler.onSuccess( emptyMap() );
handler.onSuccess( metadataWithType() );
assertTrue( summaryFuture.isDone() );

ResultSummary summary = await( summaryFuture );
Expand Down Expand Up @@ -228,7 +227,7 @@ void shouldEnableAutoReadOnConnectionWhenSummaryRequestedButNotAvailable() throw

assertNull( await( handler.nextAsync() ) );

handler.onSuccess( emptyMap() );
handler.onSuccess( metadataWithType() );

assertTrue( summaryFuture.isDone() );
assertNotNull( summaryFuture.get() );
Expand Down
Loading

0 comments on commit a8bc968

Please sign in to comment.