CreateRegion extends BaseCommand { @Override public void cmdExecute(final Message clientMessage, final ServerConnection serverConnection, final SecurityService securityService, long start) throws IOException { Part regionNamePart = null; String regionName = null; serverConnection.setAsTrue(REQUIRES_RESPONSE); Part parentRegionNamePart = clientMessage.getPart(0); String parentRegionName = parentRegionNamePart.getCachedString(); regionNamePart = clientMessage.getPart(1); regionName = regionNamePart.getCachedString(); if (logger.isDebugEnabled()) { logger.debug( "{}: Received create region request ({} bytes) from {} for parent region {} region {}", serverConnection.getName(), clientMessage.getPayloadLength(), serverConnection.getSocketString(), parentRegionName, regionName); } if (parentRegionName == null || regionName == null) { String errMessage = ""; if (parentRegionName == null) { logger.warn("{}: The input parent region name for the create region request is null", serverConnection.getName()); errMessage = "The input parent region name for the create region request is null"; } if (regionName == null) { logger.warn("{}: The input region name for the create region request is null", serverConnection.getName()); errMessage = "The input region name for the create region request is null"; } writeErrorResponse(clientMessage, MessageType.CREATE_REGION_DATA_ERROR, errMessage, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } Region parentRegion = serverConnection.getCache().getRegion(parentRegionName); if (parentRegion == null) { String reason = String.format("%s was not found during subregion creation request", parentRegionName); writeRegionDestroyedEx(clientMessage, parentRegionName, reason, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } try { securityService.authorize(Resource.DATA, Operation.MANAGE); } catch (NotAuthorizedException ex) { writeException(clientMessage, ex, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } AuthorizeRequest authzRequest = serverConnection.getAuthzRequest(); if (authzRequest != null) { try { authzRequest.createRegionAuthorize(parentRegionName + SEPARATOR + regionName); } catch (NotAuthorizedException ex) { writeException(clientMessage, ex, false, serverConnection); serverConnection.setAsTrue(RESPONDED); return; } } Region region = parentRegion.getSubregion(regionName); if (region == null) { AttributesFactory factory = new AttributesFactory(parentRegion.getAttributes()); region = parentRegion.createSubregion(regionName, factory.create()); if (logger.isDebugEnabled()) { logger.debug("{}: Created region {}", serverConnection.getName(), region); } } else { if (logger.isDebugEnabled()) { logger.debug("{}: Retrieved region {}", serverConnection.getName(), region); } } writeReply(clientMessage, serverConnection); serverConnection.setAsTrue(RESPONDED); if (logger.isDebugEnabled()) { logger.debug("{}: Sent create region response for parent region {} region {}", serverConnection.getName(), parentRegionName, regionName); } }  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.createRegion.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.createRegion.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.securityService).authorize(Resource.DATA, Operation.MANAGE); 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.MANAGE); this.createRegion.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.securityService).authorize(Resource.DATA, Operation.MANAGE); 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.createRegion.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest) .createRegionAuthorize(eq(PARENT_REGION_NAME + SEPARATOR + REGION_NAME)); 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) .createRegionAuthorize(eq(PARENT_REGION_NAME + SEPARATOR + REGION_NAME)); this.createRegion.cmdExecute(this.message, this.serverConnection, this.securityService, 0); verify(this.authzRequest) .createRegionAuthorize(eq(PARENT_REGION_NAME + SEPARATOR + REGION_NAME)); verify(this.errorResponseMessage).send(eq(this.serverConnection)); }