@Test public void getUserMetadataWithUserMetadataArgTest() throws Exception { byte[] original = new byte[100]; RANDOM.nextBytes(original); JSONObject headers = new JSONObject(); headers.put(RestUtils.MultipartPost.USER_METADATA_PART, ByteBuffer.wrap(original)); RestRequest restRequest = createRestRequest(RestMethod.POST, "/", headers); byte[] rcvd = RestUtils.buildUserMetadata(restRequest.getArgs()); assertArrayEquals("Received user metadata does not match with original", original, rcvd); }
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; }