RegisterInterestList61 extends BaseCommand { @Override public void cmdExecute(Message clientMessage, ServerConnection serverConnection, SecurityService securityService, long start) throws IOException, InterruptedException { Part regionNamePart, keyPart, numberOfKeysPart; String regionName; Object key = null; InterestResultPolicy policy; List<Object> keys; int numberOfKeys, partNumber; serverConnection.setAsTrue(REQUIRES_RESPONSE); serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE); ChunkedMessage chunkedResponseMsg = serverConnection.getRegisterInterestResponseMessage(); regionNamePart = clientMessage.getPart(0); regionName = regionNamePart.getCachedString(); try { policy = (InterestResultPolicy) clientMessage.getPart(1).getObject(); } catch (Exception e) { writeChunkedException(clientMessage, e, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } boolean isDurable; try { Part durablePart = clientMessage.getPart(2); byte[] durablePartBytes = (byte[]) durablePart.getObject(); isDurable = durablePartBytes[0] == 0x01; } catch (Exception e) { writeChunkedException(clientMessage, e, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } byte[] regionDataPolicyPartBytes; try { Part regionDataPolicyPart = clientMessage.getPart(clientMessage.getNumberOfParts() - 1); regionDataPolicyPartBytes = (byte[]) regionDataPolicyPart.getObject(); } catch (Exception e) { writeChunkedException(clientMessage, e, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } numberOfKeysPart = clientMessage.getPart(3); numberOfKeys = numberOfKeysPart.getInt(); partNumber = 4; keys = new ArrayList<>(); for (int i = 0; i < numberOfKeys; i++) { keyPart = clientMessage.getPart(partNumber + i); try { key = keyPart.getStringOrObject(); } catch (Exception e) { writeChunkedException(clientMessage, e, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } keys.add(key); } boolean sendUpdatesAsInvalidates = false; if (clientMessage.getNumberOfParts() > (numberOfKeys + partNumber)) { try { Part notifyPart = clientMessage.getPart(numberOfKeys + partNumber); byte[] notifyPartBytes = (byte[]) notifyPart.getObject(); sendUpdatesAsInvalidates = notifyPartBytes[0] == 0x01; } catch (Exception e) { writeChunkedException(clientMessage, e, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } } if (logger.isDebugEnabled()) { logger.debug( "{}: Received register interest 61 request ({} bytes) from {} for the following {} keys in region {}: {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString(), numberOfKeys, regionName, keys); } if (keys.isEmpty() || regionName == null) { String errMessage = null; if (keys.isEmpty() && regionName == null) { errMessage = "The input list of keys is empty and the input region name is null for the register interest request."; } else if (keys.isEmpty()) { errMessage = "The input list of keys for the register interest request is empty."; } else if (regionName == null) { errMessage = "The input region name for the register interest request is null."; } logger.warn("{}: {}", serverConnection.getName(), errMessage); writeChunkedErrorResponse(clientMessage, MessageType.REGISTER_INTEREST_DATA_ERROR, errMessage, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } LocalRegion region = (LocalRegion) serverConnection.getCache().getRegion(regionName); if (region == null) { logger.info("{}: Region named {} was not found during register interest list request.", new Object[] {serverConnection.getName(), regionName}); } try { securityService.authorize(Resource.DATA, Operation.READ, regionName); AuthorizeRequest authorizeRequest = serverConnection.getAuthzRequest(); if (authorizeRequest != null) { if (!DynamicRegionFactory.regionIsDynamicRegionList(regionName)) { RegisterInterestOperationContext registerContext = authorizeRequest.registerInterestListAuthorize(regionName, keys, policy); keys = (List<Object>) registerContext.getKey(); } } serverConnection.getAcceptor().getCacheClientNotifier().registerClientInterest(regionName, keys, serverConnection.getProxyID(), isDurable, sendUpdatesAsInvalidates, true, regionDataPolicyPartBytes[0], true); } catch (Exception ex) { checkForInterrupt(serverConnection, ex); writeChunkedException(clientMessage, ex, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } CacheClientProxy ccp = serverConnection.getAcceptor().getCacheClientNotifier() .getClientProxy(serverConnection.getProxyID()); if (ccp == null) { IOException ioException = new IOException( "CacheClientProxy for this client is no longer on the server"); writeChunkedException(clientMessage, ioException, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } boolean isPrimary = ccp.isPrimary(); if (!isPrimary) { chunkedResponseMsg.setMessageType(MessageType.RESPONSE_FROM_SECONDARY); chunkedResponseMsg.setTransactionId(clientMessage.getTransactionId()); chunkedResponseMsg.sendHeader(); chunkedResponseMsg.setLastChunk(true); if (logger.isDebugEnabled()) { logger.debug( "{}: Sending register interest response chunk from secondary for region: {} for key: {} chunk=<{}>", serverConnection.getName(), regionName, key, chunkedResponseMsg); } chunkedResponseMsg.sendChunk(serverConnection); } else { chunkedResponseMsg.setMessageType(MessageType.RESPONSE_FROM_PRIMARY); chunkedResponseMsg.setTransactionId(clientMessage.getTransactionId()); chunkedResponseMsg.sendHeader(); try { fillAndSendRegisterInterestResponseChunks(region, keys, InterestType.KEY, policy, serverConnection); serverConnection.setAsTrue(RESPONDED); } catch (Exception e) { checkForInterrupt(serverConnection, e); writeChunkedException(clientMessage, e, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } if (logger.isDebugEnabled()) { logger.debug( "{}: Sent register interest response for the following {} keys in region {}: {}", serverConnection.getName(), numberOfKeys, regionName, keys); } } } RegisterInterestList61(); static Command getCommand(); @Override void cmdExecute(Message clientMessage, ServerConnection serverConnection,
      SecurityService securityService, long start);  }
@Test public void noSecurityShouldSucceed() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(false); this.registerInterestList61.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.registerInterestList61.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 integratedSecurityShouldThrowIfNotAuthorized() 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.registerInterestList61.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.registerInterestList61.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest).registerInterestListAuthorize(eq(REGION_NAME), any(), any()); 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) .registerInterestListAuthorize(eq(REGION_NAME), any(), any()); this.registerInterestList61.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest).registerInterestListAuthorize(eq(REGION_NAME), any(), any()); ArgumentCaptor<NotAuthorizedException> argument = ArgumentCaptor.forClass(NotAuthorizedException.class); verify(this.chunkedResponseMessage).addObjPart(argument.capture()); assertThat(argument.getValue()).isExactlyInstanceOf(NotAuthorizedException.class); verify(this.chunkedResponseMessage).sendChunk(this.serverConnection); }