KeySet extends BaseCommand { @Override public void cmdExecute(final Message clientMessage, final ServerConnection serverConnection, final SecurityService securityService, long start) throws IOException, InterruptedException { Part regionNamePart = null; String regionName = null; serverConnection.setAsTrue(REQUIRES_RESPONSE); serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE); regionNamePart = clientMessage.getPart(0); regionName = regionNamePart.getCachedString(); ChunkedMessage chunkedResponseMsg = serverConnection.getChunkedResponseMessage(); final boolean isDebugEnabled = logger.isDebugEnabled(); if (isDebugEnabled) { logger.debug("{}: Received key set request ({} bytes) from {} for region {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString(), regionName); } if (regionName == null) { String message = null; { message = String.format("%s: The input region name for the key set request is null", serverConnection.getName()); logger.warn("{}: The input region name for the key set request is null", serverConnection.getName()); } writeKeySetErrorResponse(clientMessage, MessageType.KEY_SET_DATA_ERROR, message, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } LocalRegion region = (LocalRegion) serverConnection.getCache().getRegion(regionName); if (region == null) { String reason = String.format("%s was not found during key set request", regionName); writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } if (isInTransaction() && region.getPartitionAttributes() != null) { if (clientMessage.isRetry()) { keySetWriteChunkedException(clientMessage, new TransactionException( "Failover on a set operation of a partitioned region is not allowed in a transaction."), serverConnection); serverConnection.setAsTrue(RESPONDED); return; } } try { securityService.authorize(Resource.DATA, Operation.READ, regionName); } catch (NotAuthorizedException ex) { keySetWriteChunkedException(clientMessage, ex, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } KeySetOperationContext keySetContext = null; AuthorizeRequest authzRequest = serverConnection.getAuthzRequest(); if (authzRequest != null) { try { keySetContext = authzRequest.keySetAuthorize(regionName); } catch (NotAuthorizedException ex) { writeChunkedException(clientMessage, ex, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } } chunkedResponseMsg.setMessageType(MessageType.RESPONSE); chunkedResponseMsg.setTransactionId(clientMessage.getTransactionId()); chunkedResponseMsg.sendHeader(); try { fillAndSendKeySetResponseChunks(region, regionName, keySetContext, serverConnection); serverConnection.setAsTrue(RESPONDED); } catch (Exception e) { checkForInterrupt(serverConnection, e); writeChunkedException(clientMessage, e, serverConnection, serverConnection.getChunkedResponseMessage()); serverConnection.setAsTrue(RESPONDED); return; } if (isDebugEnabled) { logger.debug("{}: Sent key set response for the region {}", serverConnection.getName(), regionName); } }  static Command getCommand(); @Override void cmdExecute(final Message clientMessage, final ServerConnection serverConnection,
      final SecurityService securityService, long start);  }
@Test public void retryKeySet_doesNotWriteTransactionException_ifIsNotInTransaction() throws Exception { long startTime = 0; TestableKeySet keySet = new TestableKeySet(); keySet.setIsInTransaction(false); when(message.isRetry()).thenReturn(true); when(region.getPartitionAttributes()).thenReturn(mock(PartitionAttributes.class)); keySet.cmdExecute(message, serverConnection, securityService, startTime); assertThat(keySet.exceptionSentToClient).isNull(); }
@Test public void nonRetryKeySet_doesNotWriteTransactionException() throws Exception { long startTime = 0; TestableKeySet keySet = new TestableKeySet(); keySet.setIsInTransaction(true); when(message.isRetry()).thenReturn(false); when(region.getPartitionAttributes()).thenReturn(mock(PartitionAttributes.class)); keySet.cmdExecute(message, serverConnection, securityService, startTime); assertThat(keySet.exceptionSentToClient).isNull(); }
@Test public void retryKeySet_doesNotWriteTransactionException_ifIsInTransactionAndIsNotPartitionedRegion() throws Exception { long startTime = 0; TestableKeySet keySet = new TestableKeySet(); keySet.setIsInTransaction(true); when(message.isRetry()).thenReturn(true); when(region.getPartitionAttributes()).thenReturn(null); keySet.cmdExecute(message, serverConnection, securityService, startTime); assertThat(keySet.exceptionSentToClient).isNull(); }
@Test public void retryKeySet_writesTransactionException_ifIsInTransactionAndIsPartitionedRegion() throws Exception { long startTime = 0; TestableKeySet keySet = new TestableKeySet(); keySet.setIsInTransaction(true); when(message.isRetry()).thenReturn(true); when(region.getPartitionAttributes()).thenReturn(mock(PartitionAttributes.class)); keySet.cmdExecute(message, serverConnection, securityService, startTime); assertThat(keySet.exceptionSentToClient).isInstanceOf(TransactionException.class).hasMessage( "Failover on a set operation of a partitioned region is not allowed in a transaction."); }
@Test public void noSecurityShouldSucceed() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(false); this.keySet.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); }
@Test public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(true); this.keySet.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME); verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); }
@Test public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(true); doThrow(new NotAuthorizedException("")).when(this.securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME); this.keySet.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME); verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); }
@Test public void oldSecurityShouldSucceedIfAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(false); this.keySet.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest).keySetAuthorize(eq(REGION_NAME)); verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); }
@Test public void oldSecurityShouldFailIfNotAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(false); doThrow(new NotAuthorizedException("")).when(this.authzRequest) .keySetAuthorize(eq(REGION_NAME)); this.keySet.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest).keySetAuthorize(eq(REGION_NAME)); ArgumentCaptor<NotAuthorizedException> argument = ArgumentCaptor.forClass(NotAuthorizedException.class); verify(this.chunkedResponseMessage).addObjPart(argument.capture()); assertThat(argument.getValue()).isExactlyInstanceOf(NotAuthorizedException.class); verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); }