GetAllWithCallback extends BaseCommand { @Override public void cmdExecute(final Message clientMessage, final ServerConnection serverConnection, final SecurityService securityService, long start) throws IOException, InterruptedException { Part regionNamePart = null, keysPart = null, callbackPart = null; String regionName = null; Object[] keys = null; Object callback = null; serverConnection.setAsTrue(REQUIRES_RESPONSE); serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE); int partIdx = 0; regionNamePart = clientMessage.getPart(partIdx++); regionName = regionNamePart.getCachedString(); keysPart = clientMessage.getPart(partIdx++); try { keys = (Object[]) keysPart.getObject(); } catch (Exception e) { writeChunkedException(clientMessage, e, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } callbackPart = clientMessage.getPart(partIdx++); try { callback = callbackPart.getObject(); } catch (Exception e) { writeChunkedException(clientMessage, e, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } if (logger.isDebugEnabled()) { StringBuilder buffer = new StringBuilder(); buffer.append(serverConnection.getName()).append(": Received getAll request (") .append(clientMessage.getPayloadLength()).append(" bytes) from ") .append(serverConnection.getSocketString()).append(" for region ").append(regionName) .append(" with callback ").append(callback).append(" keys "); if (keys != null) { for (int i = 0; i < keys.length; i++) { buffer.append(keys[i]).append(" "); } } else { buffer.append("NULL"); } logger.debug(buffer.toString()); } if (regionName == null) { String message = null; { message = "The input region name for the getAll request is null"; } logger.warn("{} : {}", new Object[] {serverConnection.getName(), message}); writeChunkedErrorResponse(clientMessage, MessageType.GET_ALL_DATA_ERROR, message, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } LocalRegion region = (LocalRegion) serverConnection.getCache().getRegion(regionName); if (region == null) { String reason = " was not found during getAll request"; writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } ChunkedMessage chunkedResponseMsg = serverConnection.getChunkedResponseMessage(); chunkedResponseMsg.setMessageType(MessageType.RESPONSE); chunkedResponseMsg.setTransactionId(clientMessage.getTransactionId()); chunkedResponseMsg.sendHeader(); try { fillAndSendGetAllResponseChunks(region, regionName, keys, serverConnection, callback, securityService); serverConnection.setAsTrue(RESPONDED); } catch (Exception e) { checkForInterrupt(serverConnection, e); writeChunkedException(clientMessage, e, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } }  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.getAll70.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); }
@Test public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(true); this.getAll70.cmdExecute(this.message, this.serverConnection, this.securityService, 0); ArgumentCaptor<ObjectPartList> argument = ArgumentCaptor.forClass(ObjectPartList.class); verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); for (Object key : argument.getValue().getKeys()) { assertThat(key).isIn(KEYS); } for (Object key : KEYS) { verify(this.securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME, key.toString()); } verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); }
@Test public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(true); for (Object key : KEYS) { doThrow(new NotAuthorizedException("")).when(this.securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME, key.toString()); } this.getAll70.cmdExecute(this.message, this.serverConnection, this.securityService, 0); for (Object key : KEYS) { verify(this.securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME, key.toString()); } ArgumentCaptor<ObjectPartList> argument = ArgumentCaptor.forClass(ObjectPartList.class); verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); for (Object key : argument.getValue().getObjects()) { assertThat(key).isExactlyInstanceOf(NotAuthorizedException.class); } verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); }
@Test public void oldSecurityShouldSucceedIfAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(false); this.getAll70.cmdExecute(this.message, this.serverConnection, this.securityService, 0); ArgumentCaptor<ObjectPartList> argument = ArgumentCaptor.forClass(ObjectPartList.class); verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); for (Object key : argument.getValue().getKeys()) { assertThat(key).isIn(KEYS); } for (Object key : KEYS) { verify(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); } verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); }
@Test public void oldSecurityShouldFailIfNotAuthorized() throws Exception { when(this.securityService.isClientSecurityRequired()).thenReturn(true); when(this.securityService.isIntegratedSecurity()).thenReturn(false); for (Object key : KEYS) { doThrow(new NotAuthorizedException("")).when(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); } this.getAll70.cmdExecute(this.message, this.serverConnection, this.securityService, 0); ArgumentCaptor<ObjectPartList> argument = ArgumentCaptor.forClass(ObjectPartList.class); verify(this.chunkedResponseMessage).addObjPartNoCopying(argument.capture()); assertThat(argument.getValue().getObjects()).hasSize(KEYS.length); for (Object o : argument.getValue().getObjects()) { assertThat(o).isExactlyInstanceOf(NotAuthorizedException.class); } for (Object key : KEYS) { verify(this.authzRequest).getAuthorize(eq(REGION_NAME), eq(key.toString()), eq(null)); } verify(this.chunkedResponseMessage).sendChunk(eq(this.serverConnection)); }