ContainsKey66 extends BaseCommand { @Override public void cmdExecute(final Message clientMessage, final ServerConnection serverConnection, final SecurityService securityService, long start) throws IOException { Part regionNamePart = null, keyPart = null; String regionName = null; Object key = null; ContainsKeyOp.MODE mode; CacheServerStats stats = serverConnection.getCacheServerStats(); serverConnection.setAsTrue(REQUIRES_RESPONSE); { long oldStart = start; start = DistributionStats.getStatTime(); stats.incReadContainsKeyRequestTime(start - oldStart); } regionNamePart = clientMessage.getPart(0); keyPart = clientMessage.getPart(1); mode = ContainsKeyOp.MODE.values()[(clientMessage.getPart(2).getInt())]; regionName = regionNamePart.getCachedString(); try { key = keyPart.getStringOrObject(); } catch (Exception e) { writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } if (logger.isDebugEnabled()) { logger.debug("{}: Received containsKey request ({} bytes) from {} for region {} key {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString(), regionName, key); } if (key == null || regionName == null) { String errMessage = ""; if (key == null) { logger.warn("{}: The input key for the containsKey request is null", serverConnection.getName()); errMessage = "The input key for the containsKey request is null"; } if (regionName == null) { logger.warn("{}: The input region name for the containsKey request is null", serverConnection.getName()); errMessage = "The input region name for the containsKey request is null"; } writeErrorResponse(clientMessage, MessageType.CONTAINS_KEY_DATA_ERROR, errMessage, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } LocalRegion region = (LocalRegion) serverConnection.getCache().getRegion(regionName); if (region == null) { String reason = "was not found during containsKey request"; writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } try { securityService.authorize(Resource.DATA, Operation.READ, regionName, key); } catch (NotAuthorizedException ex) { writeException(clientMessage, ex, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } AuthorizeRequest authzRequest = serverConnection.getAuthzRequest(); if (authzRequest != null) { try { authzRequest.containsKeyAuthorize(regionName, key); } catch (NotAuthorizedException ex) { writeException(clientMessage, ex, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } } boolean containsKey; switch (mode) { case KEY: containsKey = region.containsKey(key); break; case VALUE: containsKey = region.containsValue(key); break; case VALUE_FOR_KEY: containsKey = region.containsValueForKey(key); break; default: containsKey = false; break; } { long oldStart = start; start = DistributionStats.getStatTime(); stats.incProcessContainsKeyTime(start - oldStart); } writeContainsKeyResponse(containsKey, clientMessage, serverConnection); serverConnection.setAsTrue(RESPONDED); if (logger.isDebugEnabled()) { logger.debug("{}: Sent containsKey response for region {} key {}", serverConnection.getName(), regionName, key); } stats.incWriteContainsKeyResponseTime(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.containsKey66.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.containsKey66.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.securityService).authorize(Resource.DATA, Operation.READ, 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.READ, REGION_NAME, KEY); this.containsKey66.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.securityService).authorize(Resource.DATA, Operation.READ, 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.containsKey66.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest).containsKeyAuthorize(eq(REGION_NAME), eq(KEY)); 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) .containsKeyAuthorize(eq(REGION_NAME), eq(KEY)); this.containsKey66.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest).containsKeyAuthorize(eq(REGION_NAME), eq(KEY)); verify(this.errorResponseMessage).send(eq(this.serverConnection)); }