@Test public void getUserMetadataFromByteArrayComplexTest() throws Exception { Map<String, String> userMetadataMap = null; byte[] userMetadataByteArray = new byte[1]; userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertNull("UserMetadata should have been null ", userMetadataMap); userMetadataByteArray = new byte[4]; ByteBuffer byteBuffer = ByteBuffer.wrap(userMetadataByteArray); byteBuffer.putShort((short) 1); userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertNull("UserMetadata should have been null ", userMetadataMap); userMetadataByteArray = new byte[4]; byteBuffer = ByteBuffer.wrap(userMetadataByteArray); byteBuffer.putShort((short) 3); userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertNull("UserMetadata should have been null ", userMetadataMap); userMetadataByteArray = new byte[12]; byteBuffer = ByteBuffer.wrap(userMetadataByteArray); byteBuffer.putShort((short) 1); byteBuffer.putInt(4); byteBuffer.putInt(0); userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertNull("UserMetadata should have been null ", userMetadataMap); userMetadataByteArray = new byte[36]; byteBuffer = ByteBuffer.wrap(userMetadataByteArray); byteBuffer.putShort((short) 1); String key = "key1"; byte[] keyInBytes = key.getBytes(StandardCharsets.US_ASCII); int keyLength = keyInBytes.length; byteBuffer.putInt(21); byteBuffer.putInt(1); byteBuffer.putInt(keyLength); byteBuffer.put(keyInBytes); String value = "value1"; byte[] valueInBytes = value.getBytes(StandardCharsets.US_ASCII); int valueLength = valueInBytes.length; byteBuffer.putInt(valueLength); byteBuffer.put(valueInBytes); Crc32 crc32 = new Crc32(); crc32.update(userMetadataByteArray, 0, userMetadataByteArray.length - 8); byteBuffer.putLong(crc32.getValue()); userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertNull("UserMetadata should have been null ", userMetadataMap); userMetadataByteArray = new byte[36]; byteBuffer = ByteBuffer.wrap(userMetadataByteArray); byteBuffer.putShort((short) 1); byteBuffer.putInt(22); byteBuffer.putInt(2); byteBuffer.putInt(keyLength); byteBuffer.put(keyInBytes); byteBuffer.putInt(valueLength); byteBuffer.put(valueInBytes); crc32 = new Crc32(); crc32.update(userMetadataByteArray, 0, userMetadataByteArray.length - 8); byteBuffer.putLong(crc32.getValue()); userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertNull("UserMetadata should have been null ", userMetadataMap); userMetadataByteArray = new byte[36]; byteBuffer = ByteBuffer.wrap(userMetadataByteArray); byteBuffer.putShort((short) 1); byteBuffer.putInt(22); byteBuffer.putInt(1); byteBuffer.putInt(keyLength + 1); byteBuffer.put(keyInBytes); byteBuffer.putInt(valueLength); byteBuffer.put(valueInBytes); crc32 = new Crc32(); crc32.update(userMetadataByteArray, 0, userMetadataByteArray.length - 8); byteBuffer.putLong(crc32.getValue()); userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertNull("UserMetadata should have been null ", userMetadataMap); userMetadataByteArray = new byte[36]; byteBuffer = ByteBuffer.wrap(userMetadataByteArray); byteBuffer.putShort((short) 1); byteBuffer.putInt(22); byteBuffer.putInt(1); byteBuffer.putInt(keyLength); byteBuffer.put(keyInBytes); byteBuffer.putInt(valueLength + 1); byteBuffer.put(valueInBytes); crc32 = new Crc32(); crc32.update(userMetadataByteArray, 0, userMetadataByteArray.length - 8); byteBuffer.putLong(crc32.getValue()); userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertNull("UserMetadata should have been null ", userMetadataMap); userMetadataByteArray = new byte[36]; byteBuffer = ByteBuffer.wrap(userMetadataByteArray); byteBuffer.putShort((short) 1); byteBuffer.putInt(22); byteBuffer.putInt(1); byteBuffer.putInt(keyLength); byteBuffer.put(keyInBytes); byteBuffer.putInt(valueLength); byteBuffer.put(valueInBytes); userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertNull("UserMetadata should have been null ", userMetadataMap); userMetadataByteArray = new byte[36]; byteBuffer = ByteBuffer.wrap(userMetadataByteArray); byteBuffer.putShort((short) 1); byteBuffer.putInt(22); byteBuffer.putInt(1); byteBuffer.putInt(keyLength); byteBuffer.put(keyInBytes); byteBuffer.putInt(valueLength); byteBuffer.put(valueInBytes); crc32 = new Crc32(); crc32.update(userMetadataByteArray, 0, userMetadataByteArray.length - 8); byteBuffer.putLong(crc32.getValue() - 1); userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertNull("UserMetadata should have been null ", userMetadataMap); userMetadataByteArray = new byte[36]; byteBuffer = ByteBuffer.wrap(userMetadataByteArray); byteBuffer.putShort((short) 1); byteBuffer.putInt(22); byteBuffer.putInt(1); byteBuffer.putInt(keyLength); byteBuffer.put(keyInBytes); byteBuffer.putInt(valueLength); byteBuffer.put(valueInBytes); crc32 = new Crc32(); crc32.update(userMetadataByteArray, 0, userMetadataByteArray.length - 8); byteBuffer.putLong(crc32.getValue()); userMetadataMap = RestUtils.buildUserMetadata(userMetadataByteArray); assertEquals("Sizes don't match ", userMetadataMap.size(), 1); assertTrue("User metadata " + RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + key + " not found in user metadata ", userMetadataMap.containsKey(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + key)); assertEquals("User metadata " + RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + key + " value don't match ", value, userMetadataMap.get(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + key)); }
public static byte[] buildUserMetadata(Map<String, Object> args) throws RestServiceException { ByteBuffer userMetadata; if (args.containsKey(MultipartPost.USER_METADATA_PART)) { userMetadata = (ByteBuffer) args.get(MultipartPost.USER_METADATA_PART); } else { Map<String, String> userMetadataMap = new HashMap<String, String>(); int sizeToAllocate = 0; for (Map.Entry<String, Object> entry : args.entrySet()) { String key = entry.getKey(); if (key.toLowerCase().startsWith(Headers.USER_META_DATA_HEADER_PREFIX)) { sizeToAllocate += 4; String keyToStore = key.substring(Headers.USER_META_DATA_HEADER_PREFIX.length()); sizeToAllocate += keyToStore.getBytes(StandardCharsets.US_ASCII).length; String value = getHeader(args, key, true); userMetadataMap.put(keyToStore, value); sizeToAllocate += 4; sizeToAllocate += value.getBytes(StandardCharsets.US_ASCII).length; } } if (sizeToAllocate == 0) { userMetadata = ByteBuffer.allocate(0); } else { sizeToAllocate += 2; sizeToAllocate += 4; sizeToAllocate += 4; sizeToAllocate += CRC_SIZE; userMetadata = ByteBuffer.allocate(sizeToAllocate); userMetadata.putShort(USER_METADATA_VERSION_V1); userMetadata.putInt(sizeToAllocate - 6 - CRC_SIZE); userMetadata.putInt(userMetadataMap.size()); for (Map.Entry<String, String> entry : userMetadataMap.entrySet()) { String key = entry.getKey(); Utils.serializeString(userMetadata, key, StandardCharsets.US_ASCII); Utils.serializeString(userMetadata, entry.getValue(), StandardCharsets.US_ASCII); } Crc32 crc = new Crc32(); crc.update(userMetadata.array(), 0, sizeToAllocate - CRC_SIZE); userMetadata.putLong(crc.getValue()); } } return userMetadata.array(); }
RestUtils { public static byte[] buildUserMetadata(Map<String, Object> args) throws RestServiceException { ByteBuffer userMetadata; if (args.containsKey(MultipartPost.USER_METADATA_PART)) { userMetadata = (ByteBuffer) args.get(MultipartPost.USER_METADATA_PART); } else { Map<String, String> userMetadataMap = new HashMap<String, String>(); int sizeToAllocate = 0; for (Map.Entry<String, Object> entry : args.entrySet()) { String key = entry.getKey(); if (key.toLowerCase().startsWith(Headers.USER_META_DATA_HEADER_PREFIX)) { sizeToAllocate += 4; String keyToStore = key.substring(Headers.USER_META_DATA_HEADER_PREFIX.length()); sizeToAllocate += keyToStore.getBytes(StandardCharsets.US_ASCII).length; String value = getHeader(args, key, true); userMetadataMap.put(keyToStore, value); sizeToAllocate += 4; sizeToAllocate += value.getBytes(StandardCharsets.US_ASCII).length; } } if (sizeToAllocate == 0) { userMetadata = ByteBuffer.allocate(0); } else { sizeToAllocate += 2; sizeToAllocate += 4; sizeToAllocate += 4; sizeToAllocate += CRC_SIZE; userMetadata = ByteBuffer.allocate(sizeToAllocate); userMetadata.putShort(USER_METADATA_VERSION_V1); userMetadata.putInt(sizeToAllocate - 6 - CRC_SIZE); userMetadata.putInt(userMetadataMap.size()); for (Map.Entry<String, String> entry : userMetadataMap.entrySet()) { String key = entry.getKey(); Utils.serializeString(userMetadata, key, StandardCharsets.US_ASCII); Utils.serializeString(userMetadata, entry.getValue(), StandardCharsets.US_ASCII); } Crc32 crc = new Crc32(); crc.update(userMetadata.array(), 0, sizeToAllocate - CRC_SIZE); userMetadata.putLong(crc.getValue()); } } return userMetadata.array(); } }
RestUtils { public static byte[] buildUserMetadata(Map<String, Object> args) throws RestServiceException { ByteBuffer userMetadata; if (args.containsKey(MultipartPost.USER_METADATA_PART)) { userMetadata = (ByteBuffer) args.get(MultipartPost.USER_METADATA_PART); } else { Map<String, String> userMetadataMap = new HashMap<String, String>(); int sizeToAllocate = 0; for (Map.Entry<String, Object> entry : args.entrySet()) { String key = entry.getKey(); if (key.toLowerCase().startsWith(Headers.USER_META_DATA_HEADER_PREFIX)) { sizeToAllocate += 4; String keyToStore = key.substring(Headers.USER_META_DATA_HEADER_PREFIX.length()); sizeToAllocate += keyToStore.getBytes(StandardCharsets.US_ASCII).length; String value = getHeader(args, key, true); userMetadataMap.put(keyToStore, value); sizeToAllocate += 4; sizeToAllocate += value.getBytes(StandardCharsets.US_ASCII).length; } } if (sizeToAllocate == 0) { userMetadata = ByteBuffer.allocate(0); } else { sizeToAllocate += 2; sizeToAllocate += 4; sizeToAllocate += 4; sizeToAllocate += CRC_SIZE; userMetadata = ByteBuffer.allocate(sizeToAllocate); userMetadata.putShort(USER_METADATA_VERSION_V1); userMetadata.putInt(sizeToAllocate - 6 - CRC_SIZE); userMetadata.putInt(userMetadataMap.size()); for (Map.Entry<String, String> entry : userMetadataMap.entrySet()) { String key = entry.getKey(); Utils.serializeString(userMetadata, key, StandardCharsets.US_ASCII); Utils.serializeString(userMetadata, entry.getValue(), StandardCharsets.US_ASCII); } Crc32 crc = new Crc32(); crc.update(userMetadata.array(), 0, sizeToAllocate - CRC_SIZE); userMetadata.putLong(crc.getValue()); } } return userMetadata.array(); }  }
RestUtils { public static byte[] buildUserMetadata(Map<String, Object> args) throws RestServiceException { ByteBuffer userMetadata; if (args.containsKey(MultipartPost.USER_METADATA_PART)) { userMetadata = (ByteBuffer) args.get(MultipartPost.USER_METADATA_PART); } else { Map<String, String> userMetadataMap = new HashMap<String, String>(); int sizeToAllocate = 0; for (Map.Entry<String, Object> entry : args.entrySet()) { String key = entry.getKey(); if (key.toLowerCase().startsWith(Headers.USER_META_DATA_HEADER_PREFIX)) { sizeToAllocate += 4; String keyToStore = key.substring(Headers.USER_META_DATA_HEADER_PREFIX.length()); sizeToAllocate += keyToStore.getBytes(StandardCharsets.US_ASCII).length; String value = getHeader(args, key, true); userMetadataMap.put(keyToStore, value); sizeToAllocate += 4; sizeToAllocate += value.getBytes(StandardCharsets.US_ASCII).length; } } if (sizeToAllocate == 0) { userMetadata = ByteBuffer.allocate(0); } else { sizeToAllocate += 2; sizeToAllocate += 4; sizeToAllocate += 4; sizeToAllocate += CRC_SIZE; userMetadata = ByteBuffer.allocate(sizeToAllocate); userMetadata.putShort(USER_METADATA_VERSION_V1); userMetadata.putInt(sizeToAllocate - 6 - CRC_SIZE); userMetadata.putInt(userMetadataMap.size()); for (Map.Entry<String, String> entry : userMetadataMap.entrySet()) { String key = entry.getKey(); Utils.serializeString(userMetadata, key, StandardCharsets.US_ASCII); Utils.serializeString(userMetadata, entry.getValue(), StandardCharsets.US_ASCII); } Crc32 crc = new Crc32(); crc.update(userMetadata.array(), 0, sizeToAllocate - CRC_SIZE); userMetadata.putLong(crc.getValue()); } } return userMetadata.array(); }  static BlobProperties buildBlobProperties(Map<String, Object> args); static byte[] buildUserMetadata(Map<String, Object> args); static Map<String, String> buildUserMetadata(byte[] userMetadata); static GetBlobOptions buildGetBlobOptions(Map<String, Object> args, SubResource subResource,
      GetOption getOption, int blobSegmentIdx); static Pair<String, Long> buildContentRangeAndLength(ByteRange range, long blobSize,
      boolean resolveRangeOnEmptyBlob); static RequestPath getRequestPath(RestRequest restRequest); static Long getTimeFromDateString(String dateString); static long toSecondsPrecisionInMs(long ms); static GetOption getGetOption(RestRequest restRequest, GetOption defaultGetOption); static boolean isPrivate(Map<String, Object> args); static boolean isChunkUpload(Map<String, Object> args); static void ensureRequiredHeadersOrThrow(RestRequest restRequest, Set<String> requiredHeaders); static String getHeader(Map<String, ?> args, String header, boolean required); static Long getLongHeader(Map<String, ?> args, String header, boolean required); static T getNumericalHeader(Map<String, ?> args, String header, boolean required,
      Function<String, T> converter); static boolean getBooleanHeader(Map<String, Object> args, String header, boolean required); static Account getAccountFromArgs(Map<String, Object> args); static Container getContainerFromArgs(Map<String, Object> args); static void accountAndContainerNamePreconditionCheck(RestRequest restRequest); static boolean setUserMetadataHeaders(byte[] userMetadata, RestResponseChannel restResponseChannel); }
RestUtils { public static byte[] buildUserMetadata(Map<String, Object> args) throws RestServiceException { ByteBuffer userMetadata; if (args.containsKey(MultipartPost.USER_METADATA_PART)) { userMetadata = (ByteBuffer) args.get(MultipartPost.USER_METADATA_PART); } else { Map<String, String> userMetadataMap = new HashMap<String, String>(); int sizeToAllocate = 0; for (Map.Entry<String, Object> entry : args.entrySet()) { String key = entry.getKey(); if (key.toLowerCase().startsWith(Headers.USER_META_DATA_HEADER_PREFIX)) { sizeToAllocate += 4; String keyToStore = key.substring(Headers.USER_META_DATA_HEADER_PREFIX.length()); sizeToAllocate += keyToStore.getBytes(StandardCharsets.US_ASCII).length; String value = getHeader(args, key, true); userMetadataMap.put(keyToStore, value); sizeToAllocate += 4; sizeToAllocate += value.getBytes(StandardCharsets.US_ASCII).length; } } if (sizeToAllocate == 0) { userMetadata = ByteBuffer.allocate(0); } else { sizeToAllocate += 2; sizeToAllocate += 4; sizeToAllocate += 4; sizeToAllocate += CRC_SIZE; userMetadata = ByteBuffer.allocate(sizeToAllocate); userMetadata.putShort(USER_METADATA_VERSION_V1); userMetadata.putInt(sizeToAllocate - 6 - CRC_SIZE); userMetadata.putInt(userMetadataMap.size()); for (Map.Entry<String, String> entry : userMetadataMap.entrySet()) { String key = entry.getKey(); Utils.serializeString(userMetadata, key, StandardCharsets.US_ASCII); Utils.serializeString(userMetadata, entry.getValue(), StandardCharsets.US_ASCII); } Crc32 crc = new Crc32(); crc.update(userMetadata.array(), 0, sizeToAllocate - CRC_SIZE); userMetadata.putLong(crc.getValue()); } } return userMetadata.array(); }  static BlobProperties buildBlobProperties(Map<String, Object> args); static byte[] buildUserMetadata(Map<String, Object> args); static Map<String, String> buildUserMetadata(byte[] userMetadata); static GetBlobOptions buildGetBlobOptions(Map<String, Object> args, SubResource subResource,
      GetOption getOption, int blobSegmentIdx); static Pair<String, Long> buildContentRangeAndLength(ByteRange range, long blobSize,
      boolean resolveRangeOnEmptyBlob); static RequestPath getRequestPath(RestRequest restRequest); static Long getTimeFromDateString(String dateString); static long toSecondsPrecisionInMs(long ms); static GetOption getGetOption(RestRequest restRequest, GetOption defaultGetOption); static boolean isPrivate(Map<String, Object> args); static boolean isChunkUpload(Map<String, Object> args); static void ensureRequiredHeadersOrThrow(RestRequest restRequest, Set<String> requiredHeaders); static String getHeader(Map<String, ?> args, String header, boolean required); static Long getLongHeader(Map<String, ?> args, String header, boolean required); static T getNumericalHeader(Map<String, ?> args, String header, boolean required,
      Function<String, T> converter); static boolean getBooleanHeader(Map<String, Object> args, String header, boolean required); static Account getAccountFromArgs(Map<String, Object> args); static Container getContainerFromArgs(Map<String, Object> args); static void accountAndContainerNamePreconditionCheck(RestRequest restRequest); static boolean setUserMetadataHeaders(byte[] userMetadata, RestResponseChannel restResponseChannel); static final String HTTP_DATE_FORMAT; static final String BYTE_RANGE_UNITS; static final String SIGNED_ID_PREFIX; static final String JSON_CONTENT_TYPE; }