Destroy65 extends BaseCommand { @Override public void cmdExecute(final Message clientMessage, final ServerConnection serverConnection, final SecurityService securityService, long start) throws IOException, InterruptedException { StringBuilder errMessage = new StringBuilder(); CacheServerStats stats = serverConnection.getCacheServerStats(); serverConnection.setAsTrue(REQUIRES_RESPONSE); long now = DistributionStats.getStatTime(); stats.incReadDestroyRequestTime(now - start); final Part regionNamePart = clientMessage.getPart(0); final Part keyPart = clientMessage.getPart(1); final Part expectedOldValuePart = clientMessage.getPart(2); final Operation operation; try { final Part operationPart = clientMessage.getPart(3); if (operationPart.isBytes()) { final byte[] bytes = operationPart.getSerializedForm(); if (null == bytes || 0 == bytes.length) { operation = Operation.DESTROY; } else { operation = Operation.fromOrdinal(bytes[0]); } } else { final Object operationObject = operationPart.getObject(); if (operationObject == null) { operation = Operation.DESTROY; } else if (operationObject instanceof Byte && (Byte) operationObject == OpType.DESTROY) { operation = Operation.REMOVE; } else { operation = (Operation) operationObject; } } } catch (Exception e) { writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } Object expectedOldValue = null; if (operation == Operation.REMOVE) { try { expectedOldValue = expectedOldValuePart.getObject(); } catch (Exception e) { writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } } final Part eventPart = clientMessage.getPart(4); Object callbackArg = null; if (clientMessage.getNumberOfParts() > 5) { final Part callbackArgPart = clientMessage.getPart(5); try { callbackArg = callbackArgPart.getObject(); } catch (Exception e) { writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } } final Object key; try { key = keyPart.getStringOrObject(); } catch (Exception e) { writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } final String regionName = regionNamePart.getCachedString(); if (logger.isDebugEnabled()) { logger.debug( "{}: Received destroy65 request ({} bytes; op={}) from {} for region {} key {}{} txId {}", serverConnection.getName(), clientMessage.getPayloadLength(), operation, serverConnection.getSocketString(), regionName, key, (operation == Operation.REMOVE ? " value=" + expectedOldValue : ""), clientMessage.getTransactionId()); } boolean entryNotFoundForRemove = false; if (key == null || regionName == null) { if (key == null) { logger.warn("{}: The input key for the destroy request is null", serverConnection.getName()); errMessage.append("The input key for the destroy request is null"); } if (regionName == null) { logger.warn("{}: The input region name for the destroy request is null", serverConnection.getName()); errMessage .append("The input region name for the destroy request is null"); } writeErrorResponse(clientMessage, MessageType.DESTROY_DATA_ERROR, errMessage.toString(), serverConnection); serverConnection.setAsTrue(RESPONDED); return; } final LocalRegion region = (LocalRegion) serverConnection.getCache().getRegion(regionName); if (region == null) { String reason = String.format("%s was not found during destroy request", regionName); writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } final ByteBuffer eventIdPartsBuffer = ByteBuffer.wrap(eventPart.getSerializedForm()); final long threadId = EventID.readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer); final long sequenceId = EventID.readEventIdPartsFromOptmizedByteArray(eventIdPartsBuffer); final EventID eventId = new EventID(serverConnection.getEventMemberIDByteArray(), threadId, sequenceId); final EventIDHolder clientEvent = new EventIDHolder(eventId); Breadcrumbs.setEventId(eventId); if (clientMessage.isRetry()) { clientEvent.setPossibleDuplicate(true); if (region.getAttributes().getConcurrencyChecksEnabled()) { clientEvent.setRegion(region); if (!recoverVersionTagForRetriedOperation(clientEvent)) { clientEvent.setPossibleDuplicate(false); } } } try { securityService.authorize(Resource.DATA, ResourcePermission.Operation.WRITE, regionName, key); AuthorizeRequest authzRequest = serverConnection.getAuthzRequest(); if (authzRequest != null) { if (DynamicRegionFactory.regionIsDynamicRegionList(regionName)) { RegionDestroyOperationContext destroyContext = authzRequest.destroyRegionAuthorize((String) key, callbackArg); callbackArg = destroyContext.getCallbackArg(); } else { DestroyOperationContext destroyContext = authzRequest.destroyAuthorize(regionName, key, callbackArg); callbackArg = destroyContext.getCallbackArg(); } } if (operation == null || operation == Operation.DESTROY) { region.basicBridgeDestroy(key, callbackArg, serverConnection.getProxyID(), true, clientEvent); } else { try { if (expectedOldValue == null) { expectedOldValue = Token.INVALID; } if (operation == Operation.REMOVE && clientMessage.isRetry() && clientEvent.getVersionTag() != null) { if (logger.isDebugEnabled()) { logger.debug("remove(k,v) operation was successful last time with version {}", clientEvent.getVersionTag()); } try { region.basicBridgeRemove(key, expectedOldValue, callbackArg, serverConnection.getProxyID(), true, clientEvent); } catch (EntryNotFoundException e) { } } else { region.basicBridgeRemove(key, expectedOldValue, callbackArg, serverConnection.getProxyID(), true, clientEvent); if (logger.isDebugEnabled()) { logger.debug("region.remove succeeded"); } } } catch (EntryNotFoundException e) { serverConnection.setModificationInfo(true, regionName, key); if (logger.isDebugEnabled()) { logger.debug("writing entryNotFound response"); } entryNotFoundForRemove = true; } } serverConnection.setModificationInfo(true, regionName, key); } catch (EntryNotFoundException e) { if (logger.isDebugEnabled()) { logger.debug("{}: during entry destroy no entry was found for key {}", serverConnection.getName(), key); } entryNotFoundForRemove = true; } catch (RegionDestroyedException rde) { writeException(clientMessage, rde, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } catch (Exception e) { checkForInterrupt(serverConnection, e); writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); if (e instanceof GemFireSecurityException) { if (logger.isDebugEnabled()) { logger.debug("{}: Unexpected Security exception", serverConnection.getName(), e); } } else { logger.warn(String.format("%s: Unexpected Exception", serverConnection.getName()), e); } return; } now = DistributionStats.getStatTime(); stats.incProcessDestroyTime(now - start); if (region instanceof PartitionedRegion) { PartitionedRegion pr = (PartitionedRegion) region; if (pr.getNetworkHopType() != PartitionedRegion.NETWORK_HOP_NONE) { writeReplyWithRefreshMetadata(clientMessage, serverConnection, pr, entryNotFoundForRemove, pr.getNetworkHopType(), clientEvent.getVersionTag()); pr.clearNetworkHopData(); } else { writeReply(clientMessage, serverConnection, entryNotFoundForRemove | clientEvent.getIsRedestroyedEntry(), clientEvent.getVersionTag()); } } else { writeReply(clientMessage, serverConnection, entryNotFoundForRemove | clientEvent.getIsRedestroyedEntry(), clientEvent.getVersionTag()); } serverConnection.setAsTrue(RESPONDED); if (logger.isDebugEnabled()) { logger.debug("{}: Sent destroy response for region {} key {}", serverConnection.getName(), regionName, key); } stats.incWriteDestroyResponseTime(DistributionStats.getStatTime() - start); }  static Command getCommand(); @Override void cmdExecute(final Message clientMessage, final ServerConnection serverConnection,
      final SecurityService securityService, long start);  }
@Test public void noSecurityShouldSucceed() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(false); this.destroy65.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.responseMessage).send(this.serverConnection); }
@Test public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(true); this.destroy65.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.securityService).authorize(Resource.DATA, Operation.WRITE, REGION_NAME, KEY); verify(this.responseMessage).send(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.WRITE, REGION_NAME, KEY); this.destroy65.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.securityService).authorize(Resource.DATA, Operation.WRITE, REGION_NAME, KEY); verify(this.errorResponseMessage).send(eq(this.serverConnection)); }
@Test public void oldSecurityShouldSucceedIfAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(false); this.destroy65.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest).destroyAuthorize(eq(REGION_NAME), eq(KEY), eq(CALLBACK_ARG)); verify(this.responseMessage).send(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) .destroyAuthorize(eq(REGION_NAME), eq(KEY), eq(CALLBACK_ARG)); this.destroy65.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest).destroyAuthorize(eq(REGION_NAME), eq(KEY), eq(CALLBACK_ARG)); verify(this.errorResponseMessage).send(eq(this.serverConnection)); }
@Test public void destroyThrowsAndHandlesEntryNotFoundExceptionOnServer() { doThrow(new EntryNotFoundException("")).when(region).basicBridgeDestroy(any(), any(), any(), anyBoolean(), any()); assertThatCode(() -> destroy65.cmdExecute(message, serverConnection, securityService, 0)) .doesNotThrowAnyException(); }
@Test public void cmdExecuteWhenOperationPartIsNotBytes_whenOperationPartGetObjectIsNull_thenCallsRegionBasicBridgeDestroy() throws Exception { when(operationPart.getObject()).thenReturn(null); destroy65.cmdExecute(message, serverConnection, securityService, 0); verify(region).basicBridgeDestroy(any(), any(), any(), anyBoolean(), any()); }
@Test public void cmdExecuteWhenOperationPartIsNotBytes_whenOperationPartIsNotDestroyOrRemove_thenCallsRegionBasicBridgeRemove() throws Exception { when(operationPart.getObject()).thenReturn(org.apache.geode.cache.Operation.CREATE); destroy65.cmdExecute(message, serverConnection, securityService, 0); verify(region).basicBridgeRemove(any(), any(), any(), any(), anyBoolean(), any()); }
@Test public void cmdExecuteWhenOperationPartIsBytes_whenOperationSerializedFormIsNull_thenCallsRegionBasicBridgeDestroy() throws Exception { when(operationPart.isBytes()).thenReturn(true); when(operationPart.getSerializedForm()).thenReturn(null); destroy65.cmdExecute(message, serverConnection, securityService, 0); verify(region).basicBridgeDestroy(any(), any(), any(), anyBoolean(), any()); }
@Test public void cmdExecuteWhenOperationPartIsBytes_whenOperationSerializedFormIsLengthZero_thenCallsRegionBasicBridgeDestroy() throws Exception { when(operationPart.isBytes()).thenReturn(true); when(operationPart.getSerializedForm()).thenReturn(new byte[0]); destroy65.cmdExecute(message, serverConnection, securityService, 0); verify(region).basicBridgeDestroy(any(), any(), any(), anyBoolean(), any()); }
@Test public void cmdExecuteWhenIsBytesIsTrue_whenOperationSerializedFormIsValidAndIsNotDestroyOrRemove_thenCallsRegionBasicBridgeRemove() throws Exception { when(operationPart.isBytes()).thenReturn(true); byte[] serializedForm = {OpType.CREATE}; when(operationPart.getSerializedForm()).thenReturn(serializedForm); destroy65.cmdExecute(message, serverConnection, securityService, 0); verify(region).basicBridgeRemove(any(), any(), any(), any(), anyBoolean(), any()); }
@Test public void cmdExecuteWhenIsBytesIsFalse_whenOperationPartIsByteAndOpTypeDestroy_thenCallsRegionBasicBridgeRemove() throws Exception { when(message.getPart(2)).thenReturn(mock(Part.class)); when(operationPart.getObject()).thenReturn(OpType.DESTROY); destroy65.cmdExecute(message, serverConnection, securityService, 0); verify(region).basicBridgeRemove(any(), any(), any(), any(), anyBoolean(), any()); }