From 128459178f46b554160d71310f9aadf1f874709d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 7 Oct 2016 14:18:31 -0700 Subject: [PATCH] api: putObject() should properly save contentType. (#465) Current code was not working when contentType is set to a custom value. Fixes #464 --- src/main/java/io/minio/MinioClient.java | 21 ++++++++++++------ src/main/java/io/minio/ObjectStat.java | 24 ++++++++++++--------- src/test/java/io/minio/MinioClientTest.java | 23 ++++++++++++++++++++ test/FunctionalTest.java | 7 +++++- 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/minio/MinioClient.java b/src/main/java/io/minio/MinioClient.java index c1e5d6b23..935f96404 100644 --- a/src/main/java/io/minio/MinioClient.java +++ b/src/main/java/io/minio/MinioClient.java @@ -2065,8 +2065,9 @@ public void putObject(String bucketName, String objectName, InputStream stream, * @param uploadId Upload ID of multipart put object. * @param partNumber Part number of multipart put object. */ - private String putObject(String bucketName, String objectName, int length, Object data, - String uploadId, int partNumber) + private String putObject(String bucketName, String objectName, int length, + Object data, String uploadId, String contentType, + int partNumber) throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException, InternalException { @@ -2076,8 +2077,14 @@ private String putObject(String bucketName, String objectName, int length, Objec queryParamMap.put("partNumber", Integer.toString(partNumber)); queryParamMap.put(UPLOAD_ID, uploadId); } - - HttpResponse response = executePut(bucketName, objectName, null, queryParamMap, data, length); + Map headerMap = new HashMap<>(); + if (contentType != null) { + headerMap.put("Content-Type", contentType); + } else { + headerMap.put("Content-Type", "application/octet-stream"); + } + HttpResponse response = executePut(bucketName, objectName, headerMap, + queryParamMap, data, length); return response.header().etag(); } @@ -2098,8 +2105,8 @@ private void putObject(String bucketName, String objectName, String contentType, InternalException, InvalidArgumentException, InsufficientDataException { if (size <= MIN_MULTIPART_SIZE) { - // single put object - putObject(bucketName, objectName, (int) size, data, null, 0); + // Single put object. + putObject(bucketName, objectName, (int) size, data, null, contentType, 0); return; } @@ -2146,7 +2153,7 @@ private void putObject(String bucketName, String objectName, String contentType, } } - String etag = putObject(bucketName, objectName, expectedReadSize, data, uploadId, partNumber); + String etag = putObject(bucketName, objectName, expectedReadSize, data, uploadId, null, partNumber); totalParts[partNumber - 1] = new Part(partNumber, etag); } diff --git a/src/main/java/io/minio/ObjectStat.java b/src/main/java/io/minio/ObjectStat.java index 99d5f8daa..6c08d92e8 100644 --- a/src/main/java/io/minio/ObjectStat.java +++ b/src/main/java/io/minio/ObjectStat.java @@ -64,7 +64,7 @@ public boolean equals(Object o) { if (length != that.length) { return false; - } + } if (!bucketName.equals(that.bucketName)) { return false; } @@ -97,6 +97,13 @@ public int hashCode() { } + /** + * Returns bucket name. + */ + public String bucketName() { + return bucketName; + } + /** * Returns object name. */ @@ -120,15 +127,6 @@ public long length() { return length; } - - /** - * Returns bucket name. - */ - public String bucketName() { - return bucketName; - } - - /** * Returns ETag. */ @@ -136,6 +134,12 @@ public String etag() { return etag; } + /** + * Returns content type of object. + */ + public String contentType() { + return contentType; + } /** * Returns ObjectStat as string. diff --git a/src/test/java/io/minio/MinioClientTest.java b/src/test/java/io/minio/MinioClientTest.java index 56b9e162f..c68a1aee5 100644 --- a/src/test/java/io/minio/MinioClientTest.java +++ b/src/test/java/io/minio/MinioClientTest.java @@ -54,6 +54,7 @@ public class MinioClientTest { private static final String BUCKET = "bucket"; private static final String CONTENT_LENGTH = "Content-Length"; private static final String APPLICATION_OCTET_STREAM = "application/octet-stream"; + private static final String APPLICATION_JAVASCRIPT = "application/javascript"; private static final String CONTENT_TYPE = "Content-Type"; private static final String MON_04_MAY_2015_07_58_51_GMT = "Mon, 04 May 2015 07:58:51 GMT"; private static final String LAST_MODIFIED = "Last-Modified"; @@ -614,6 +615,28 @@ public void testNullContentTypeWorks() client.putObject(BUCKET, "key", data, 11, null); } + @Test + public void testCustomContentTypeWorks() + throws NoSuchAlgorithmException, InvalidKeyException, IOException, XmlPullParserException, MinioException { + MockWebServer server = new MockWebServer(); + MockResponse response = new MockResponse(); + + response.addHeader("Date", SUN_29_JUN_2015_22_01_10_GMT); + response.addHeader(LAST_MODIFIED, MON_04_MAY_2015_07_58_51_UTC); + response.addHeader("ETag", MD5_HASH_STRING); + response.setResponseCode(200); + + server.enqueue(response); + server.start(); + + MinioClient client = new MinioClient(server.url("")); + + String inputString = HELLO_WORLD; + ByteArrayInputStream data = new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8)); + + client.putObject(BUCKET, "key", data, 11, APPLICATION_JAVASCRIPT); + } + @Test public void testSigningKey() throws NoSuchAlgorithmException, InvalidKeyException, IOException, XmlPullParserException, MinioException { diff --git a/test/FunctionalTest.java b/test/FunctionalTest.java index 613391f11..636a047bc 100644 --- a/test/FunctionalTest.java +++ b/test/FunctionalTest.java @@ -43,6 +43,7 @@ public class FunctionalTest { public static final int MB = 1024 * 1024; public static final SecureRandom random = new SecureRandom(); public static final String bucketName = getRandomName(); + public static final String customContenType = "application/javascript"; public static String endpoint; public static String accessKey; public static String secretKey; @@ -193,9 +194,13 @@ public static void putObject_test4() throws Exception { println("Test: putObject(String bucketName, String objectName, String contentType, long size, InputStream body)"); String fileName = createFile(3 * MB); InputStream is = Files.newInputStream(Paths.get(fileName)); - client.putObject(bucketName, fileName, is, 1024 * 1024, null); + client.putObject(bucketName, fileName, is, 1024 * 1024, customContenType); is.close(); Files.delete(Paths.get(fileName)); + ObjectStat objectStat = client.statObject(bucketName, fileName); + if (!customContenType.equals(objectStat.contentType())) { + println("FAILED"); + } client.removeObject(bucketName, fileName); }