Get70 extends BaseCommand { @Override public void cmdExecute(final Message clientMessage, final ServerConnection serverConnection, final SecurityService securityService, long startparam) throws IOException { long start = startparam; Part regionNamePart = null, keyPart = null, valuePart = null; String regionName = null; Object callbackArg = null, key = null; CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper(); CacheServerStats stats = serverConnection.getCacheServerStats(); String errMessage = null; serverConnection.setAsTrue(REQUIRES_RESPONSE); { long oldStart = start; start = DistributionStats.getStatTime(); stats.incReadGetRequestTime(start - oldStart); } int parts = clientMessage.getNumberOfParts(); regionNamePart = clientMessage.getPart(0); keyPart = clientMessage.getPart(1); if (parts > 2) { valuePart = clientMessage.getPart(2); try { callbackArg = valuePart.getObject(); } catch (Exception e) { writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } } 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 7.0 get request ({} bytes) from {} for region {} key {} txId {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString(), regionName, key, clientMessage.getTransactionId()); } if (key == null || regionName == null) { if ((key == null) && (regionName == null)) { errMessage = "The input region name and key for the get request are null."; } else if (key == null) { errMessage = "The input key for the get request is null."; } else if (regionName == null) { errMessage = "The input region name for the get request is null."; } logger.warn("{}: {}", serverConnection.getName(), errMessage); writeErrorResponse(clientMessage, MessageType.REQUESTDATAERROR, errMessage, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } Region region = serverConnection.getCache().getRegion(regionName); if (region == null) { String reason = String.format("%s was not found during get request", regionName); writeRegionDestroyedEx(clientMessage, regionName, reason, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } GetOperationContext getContext = null; try { securityService.authorize(Resource.DATA, Operation.READ, regionName, key); AuthorizeRequest authzRequest = serverConnection.getAuthzRequest(); if (authzRequest != null) { getContext = authzRequest.getAuthorize(regionName, key, callbackArg); callbackArg = getContext.getCallbackArg(); } } catch (NotAuthorizedException ex) { writeException(clientMessage, ex, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } Entry entry; try { entry = getEntry(region, key, callbackArg, serverConnection); } catch (Exception e) { writeException(clientMessage, e, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } @Retained final Object originalData = entry.value; Object data = originalData; try { boolean isObject = entry.isObject; VersionTag versionTag = entry.versionTag; boolean keyNotPresent = entry.keyNotPresent; try { AuthorizeRequestPP postAuthzRequest = serverConnection.getPostAuthzRequest(); if (postAuthzRequest != null) { try { getContext = postAuthzRequest.getAuthorize(regionName, key, data, isObject, getContext); GetOperationContextImpl gci = (GetOperationContextImpl) getContext; Object newData = gci.getRawValue(); if (newData != data) { isObject = getContext.isObject(); data = newData; } } finally { if (getContext != null) { ((GetOperationContextImpl) getContext).release(); } } } } catch (NotAuthorizedException ex) { writeException(clientMessage, ex, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } data = securityService.postProcess(regionName, key, data, entry.isObject); long oldStart = start; start = DistributionStats.getStatTime(); stats.incProcessGetTime(start - oldStart); if (region instanceof PartitionedRegion) { PartitionedRegion pr = (PartitionedRegion) region; if (pr.getNetworkHopType() != PartitionedRegion.NETWORK_HOP_NONE) { writeResponseWithRefreshMetadata(data, callbackArg, clientMessage, isObject, serverConnection, pr, pr.getNetworkHopType(), versionTag, keyNotPresent); pr.clearNetworkHopData(); } else { writeResponse(data, callbackArg, clientMessage, isObject, versionTag, keyNotPresent, serverConnection); } } else { writeResponse(data, callbackArg, clientMessage, isObject, versionTag, keyNotPresent, serverConnection); } } finally { OffHeapHelper.release(originalData); } serverConnection.setAsTrue(RESPONDED); if (logger.isDebugEnabled()) { logger.debug("{}: Wrote get response back to {} for region {} {}", serverConnection.getName(), serverConnection.getSocketString(), regionName, entry); } stats.incWriteGetResponseTime(DistributionStats.getStatTime() - start); CachePerfStats regionPerfStats = ((InternalRegion) region).getRegionPerfStats(); if (regionPerfStats != null) { regionPerfStats.endGetForClient(startparam, entry.keyNotPresent); } }  static Command getCommand(); @Override void cmdExecute(final Message clientMessage, final ServerConnection serverConnection,
      final SecurityService securityService, long startparam); Entry getValueAndIsObject(Region region, Object key, Object callbackArg,
      ServerConnection servConn); @Retained Entry getEntryRetained(Region region, Object key, Object callbackArg,
      ServerConnection servConn);  }
@Test public void noSecurityShouldSucceed() throws Exception { when(securityService.isClientSecurityRequired()).thenReturn(false); get70.cmdExecute(message, serverConnection, securityService, 0); verify(responseMessage).send(serverConnection); }
@Test public void integratedSecurityShouldSucceedIfAuthorized() throws Exception { givenIntegratedSecurity(); get70.cmdExecute(message, serverConnection, securityService, 0); verify(securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME, KEY); verify(responseMessage).send(serverConnection); }
@Test public void integratedSecurityShouldFailIfNotAuthorized() throws Exception { givenIntegratedSecurity(); givenIntegratedSecurityNotAuthorized(); get70.cmdExecute(message, serverConnection, securityService, 0); verify(securityService).authorize(Resource.DATA, Operation.READ, REGION_NAME, KEY); verify(errorResponseMessage).send(eq(serverConnection)); }
@Test public void oldSecurityShouldSucceedIfAuthorized() throws Exception { givenOldSecurity(); get70.cmdExecute(message, serverConnection, securityService, 0); verify(authzRequest).getAuthorize(eq(REGION_NAME), eq(KEY), eq(CALLBACK_ARG)); verify(responseMessage).send(serverConnection); }
@Test public void oldSecurityShouldFailIfNotAuthorized() throws Exception { givenOldSecurity(); givenOldSecurityNotAuthorized(); get70.cmdExecute(message, serverConnection, securityService, 0); verify(authzRequest).getAuthorize(eq(REGION_NAME), eq(KEY), eq(CALLBACK_ARG)); verify(errorResponseMessage).send(eq(serverConnection)); }
@Test public void callsEndGetForClient_ifGetSucceedsWithHit() throws IOException { CachePerfStats regionPerfStats = mock(CachePerfStats.class); when(region.getRegionPerfStats()).thenReturn(regionPerfStats); when(region.getRetained(any(), any(), anyBoolean(), anyBoolean(), any(), any(), anyBoolean())) .thenReturn("data"); get70.cmdExecute(message, serverConnection, securityService, 0); verify(regionPerfStats).endGetForClient(0, false); }
@Test public void callsEndGetForClient_ifGetSucceedsWithMiss() throws IOException { CachePerfStats regionPerfStats = mock(CachePerfStats.class); when(region.getRegionPerfStats()).thenReturn(regionPerfStats); when(region.getRetained(any(), any(), anyBoolean(), anyBoolean(), any(), any(), anyBoolean())) .thenReturn(null); get70.cmdExecute(message, serverConnection, securityService, 0); verify(regionPerfStats).endGetForClient(0, true); }
@Test public void doesNotCallEndGetForClient_ifGetFails() throws IOException { givenIntegratedSecurity(); givenIntegratedSecurityNotAuthorized(); CachePerfStats regionPerfStats = mock(CachePerfStats.class); when(region.getRegionPerfStats()).thenReturn(regionPerfStats); get70.cmdExecute(message, serverConnection, securityService, 0); verify(regionPerfStats, never()).endGetForClient(anyLong(), anyBoolean()); }
@Test public void doesNotThrow_ifRegionPerfStatsIsNull() { when(region.getRegionPerfStats()).thenReturn(null); assertThatCode(() -> get70.cmdExecute(message, serverConnection, securityService, 0)) .doesNotThrowAnyException(); }