Skip to content

Commit

Permalink
api: Regression contentType set by user was never set properly. (#511)
Browse files Browse the repository at this point in the history
The reason is because contentType is overriden by this
operation even if the contentType is set through header.

```
    public MediaType contentType() {
          if (contentType != null && !contentType.isEmpty()) {
            return MediaType.parse(contentType);
          } else {
            return MediaType.parse("application/octet-stream");
          }
        }
```
  • Loading branch information
harshavardhana authored Jan 14, 2017
1 parent e409e73 commit 711b2aa
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 26 deletions.
101 changes: 75 additions & 26 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,16 @@ private Request createRequest(Method method, String bucketName, String objectNam
requestBody = new RequestBody() {
@Override
public MediaType contentType() {
MediaType mediaType = null;

if (contentType != null) {
return MediaType.parse(contentType);
} else {
return MediaType.parse("application/octet-stream");
mediaType = MediaType.parse(contentType);
}
if (mediaType == null) {
mediaType = MediaType.parse("application/octet-stream");
}

return mediaType;
}

@Override
Expand Down Expand Up @@ -782,10 +787,15 @@ public void writeTo(BufferedSink sink) throws IOException {
*/
private HttpResponse execute(Method method, String region, String bucketName, String objectName,
Map<String,String> headerMap, Map<String,String> queryParamMap,
String contentType, Object body, int length)
Object body, int length)
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException {
String contentType = null;
if (headerMap != null) {
contentType = headerMap.get("Content-Type");
}

Request request = createRequest(method, bucketName, objectName, region,
headerMap, queryParamMap,
contentType, body, length);
Expand Down Expand Up @@ -919,7 +929,7 @@ private void updateRegionCache(String bucketName)
queryParamMap.put("location", null);

HttpResponse response = execute(Method.GET, US_EAST_1, bucketName, null,
null, queryParamMap, null, null, 0);
null, queryParamMap, null, 0);

// existing XmlEntity does not work, so fallback to regular parsing.
XmlPullParser xpp = xmlPullParserFactory.newPullParser();
Expand Down Expand Up @@ -983,7 +993,7 @@ private HttpResponse executeGet(String bucketName, String objectName, Map<String
updateRegionCache(bucketName);
return execute(Method.GET, BucketRegionCache.INSTANCE.region(bucketName),
bucketName, objectName, headerMap, queryParamMap,
null, null, 0);
null, 0);
}


Expand All @@ -1000,7 +1010,7 @@ private HttpResponse executeHead(String bucketName, String objectName)
updateRegionCache(bucketName);
HttpResponse response = execute(Method.HEAD, BucketRegionCache.INSTANCE.region(bucketName),
bucketName, objectName, null,
null, null, null, 0);
null, null, 0);
response.body().close();
return response;
}
Expand All @@ -1020,7 +1030,7 @@ private HttpResponse executeDelete(String bucketName, String objectName, Map<Str
updateRegionCache(bucketName);
HttpResponse response = execute(Method.DELETE, BucketRegionCache.INSTANCE.region(bucketName),
bucketName, objectName, null,
queryParamMap, null, null, 0);
queryParamMap, null, 0);
response.body().close();
return response;
}
Expand All @@ -1043,7 +1053,7 @@ private HttpResponse executePost(String bucketName, String objectName, Map<Strin
updateRegionCache(bucketName);
return execute(Method.POST, BucketRegionCache.INSTANCE.region(bucketName),
bucketName, objectName, headerMap, queryParamMap,
null, data, 0);
data, 0);
}


Expand All @@ -1065,7 +1075,7 @@ private HttpResponse executePut(String bucketName, String objectName, Map<String
InternalException {
HttpResponse response = execute(Method.PUT, region, bucketName, objectName,
headerMap, queryParamMap,
"", data, length);
data, length);
response.body().close();
return response;
}
Expand Down Expand Up @@ -1926,8 +1936,6 @@ public void makeBucket(String bucketName, String region)
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException {
Map<String,String> headerMap = new HashMap<>();

String configString;
if (region == null || US_EAST_1.equals(region)) {
// for 'us-east-1', location constraint is not required. for more info
Expand All @@ -1938,7 +1946,7 @@ public void makeBucket(String bucketName, String region)
configString = config.toString();
}

executePut(bucketName, null, headerMap, null, US_EAST_1, configString, 0);
executePut(bucketName, null, null, null, US_EAST_1, configString, 0);
}


Expand Down Expand Up @@ -1989,6 +1997,7 @@ public void removeBucket(String bucketName)
* @param bucketName Bucket name.
* @param objectName Object name to create in the bucket.
* @param fileName File name to upload.
* @param contentType File content type of the object, user supplied.
*
* @throws InvalidBucketNameException upon invalid bucket name is given
* @throws NoResponseException upon no response from server
Expand All @@ -1997,7 +2006,7 @@ public void removeBucket(String bucketName)
* @throws ErrorResponseException upon unsuccessful execution
* @throws InternalException upon internal library error
*/
public void putObject(String bucketName, String objectName, String fileName)
public void putObject(String bucketName, String objectName, String fileName, String contentType)
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException,
Expand All @@ -2011,7 +2020,10 @@ public void putObject(String bucketName, String objectName, String fileName)
throw new InvalidArgumentException("'" + fileName + "': not a regular file");
}

String contentType = Files.probeContentType(filePath);
if (contentType == null) {
contentType = Files.probeContentType(filePath);
}

long size = Files.size(filePath);

RandomAccessFile file = new RandomAccessFile(filePath.toFile(), "r");
Expand All @@ -2022,6 +2034,40 @@ public void putObject(String bucketName, String objectName, String fileName)
}
}

/**
* Uploads given file as object in given bucket.
* <p>
* If the object is larger than 5MB, the client will automatically use a multipart session.
* </p>
* <p>
* If the session fails, the user may attempt to re-upload the object by attempting to create
* the exact same object again. The client will examine all parts of any current upload session
* and attempt to reuse the session automatically. If a mismatch is discovered, the upload will fail
* before uploading any more data. Otherwise, it will resume uploading where the session left off.
* </p>
* <p>
* If the multipart session fails, the user is responsible for resuming or removing the session.
* </p>
*
* @param bucketName Bucket name.
* @param objectName Object name to create in the bucket.
* @param fileName File name to upload.
*
* @throws InvalidBucketNameException upon invalid bucket name is given
* @throws NoResponseException upon no response from server
* @throws IOException upon connection error
* @throws XmlPullParserException upon parsing response xml
* @throws ErrorResponseException upon unsuccessful execution
* @throws InternalException upon internal library error
*/
public void putObject(String bucketName, String objectName, String fileName)
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException,
InvalidArgumentException, InsufficientDataException {
putObject(bucketName, objectName, fileName, null);
}


/**
* Uploads data from given stream as object to given bucket.
Expand Down Expand Up @@ -2103,20 +2149,21 @@ private String putObject(String bucketName, String objectName, int length,
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException {
Map<String,String> headerMap = new HashMap<>();
if (contentType != null && !contentType.isEmpty()) {
headerMap.put("Content-Type", contentType);
} else {
headerMap.put("Content-Type", "application/octet-stream");
}

Map<String,String> queryParamMap = null;
if (partNumber > 0 && uploadId != null && !"".equals(uploadId)) {
queryParamMap = new HashMap<>();
queryParamMap.put("partNumber", Integer.toString(partNumber));
queryParamMap.put(UPLOAD_ID, uploadId);
}
Map<String,String> 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);

HttpResponse response = executePut(bucketName, objectName, headerMap, queryParamMap, data, length);
return response.header().etag();
}

Expand Down Expand Up @@ -2252,13 +2299,15 @@ private void setBucketPolicy(String bucketName, BucketPolicy policy)
throws InvalidBucketNameException, InvalidObjectPrefixException, NoSuchAlgorithmException,
InsufficientDataException, IOException, InvalidKeyException, NoResponseException,
XmlPullParserException, ErrorResponseException, InternalException {
Map<String,String> headerMap = new HashMap<>();
headerMap.put("Content-Type", "application/json");

Map<String,String> queryParamMap = new HashMap<>();
queryParamMap.put("policy", "");

String policyJson = policy.getJson();

HttpResponse response = executePut(bucketName, "", null, queryParamMap, policyJson, policyJson.length());
response.body().close();
executePut(bucketName, null, headerMap, queryParamMap, policyJson, policyJson.length());
}


Expand Down Expand Up @@ -2565,7 +2614,7 @@ private String initMultipartUpload(String bucketName, String objectName, String
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException {
Map<String,String> headerMap = new HashMap<>();
if (contentType != null) {
if (contentType != null && !contentType.isEmpty()) {
headerMap.put("Content-Type", contentType);
} else {
headerMap.put("Content-Type", "application/octet-stream");
Expand Down
18 changes: 18 additions & 0 deletions functional/FunctionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ public static void putObject_test4() throws Exception {
client.removeObject(bucketName, fileName);
}

/**
* Test: multipart: putObject(String bucketName, String objectName, String fileName, String contentType).
*/
public static void putObject_test5() throws Exception {
System.out.println("Test: multipart: putObject(String bucketName, String objectName, String fileName,"
+ " String contentType)");
String fileName = createFile(13 * MB);
client.putObject(bucketName, fileName, fileName, customContenType);
Files.delete(Paths.get(fileName));
ObjectStat objectStat = client.statObject(bucketName, fileName);
if (!customContenType.equals(objectStat.contentType())) {
throw new Exception("[FAILED] Test: putObject(String bucketName, String objectName, String fileName,"
+ " String contentType)");
}
client.removeObject(bucketName, fileName);
}

/**
* Test: statObject(String bucketName, String objectName).
*/
Expand Down Expand Up @@ -858,6 +875,7 @@ public static void main(String[] args) {
putObject_test2();
putObject_test3();
putObject_test4();
putObject_test5();

statObject_test();

Expand Down

0 comments on commit 711b2aa

Please sign in to comment.