// [A] First parse all the parts out of the POST request and message body
// -> bucket name is still encoded in a Host header
S3AuthParams params = new S3AuthParams();
List<S3MetaDataEntry> metaSet = new ArrayList<S3MetaDataEntry>();
S3PutObjectInlineRequest engineRequest = new S3PutObjectInlineRequest();
engineRequest.setBucketName(bucket);
// -> the last body part contains the content that is used to write the S3 object, all
// other body parts are header values
while (null != (oneLine = br.readLine())) {
if (oneLine.startsWith(lastBoundary)) {
// -> this is the data of the object to put
if (0 < temp.length()) {
value = temp.toString();
temp.setLength(0);
engineRequest.setContentLength(value.length());
engineRequest.setDataAsString(value);
}
break;
} else if (oneLine.startsWith(boundary)) {
// -> this is the header data
if (0 < temp.length()) {
value = temp.toString().trim();
temp.setLength(0);
//System.out.println( "param: " + name + " = " + value );
if (name.equalsIgnoreCase("key")) {
engineRequest.setKey(value);
} else if (name.equalsIgnoreCase("x-amz-acl")) {
engineRequest.setCannedAccess(value);
} else if (isMetaTag) {
S3MetaDataEntry oneMeta = new S3MetaDataEntry();
oneMeta.setName(metaName);
oneMeta.setValue(value);
metaSet.add(oneMeta);
countMeta++;
metaName = null;
}
// -> build up the headers so we can do authentication on this POST
HeaderParam oneHeader = new HeaderParam();
oneHeader.setName(name);
oneHeader.setValue(value);
params.addHeader(oneHeader);
}
state = 1;
} else if (1 == state && 0 == oneLine.length()) {
// -> data of a body part starts here
state = 2;
} else if (1 == state) {
// -> the name of the 'name-value' pair is encoded in the Content-Disposition header
if (oneLine.startsWith("Content-Disposition: form-data;")) {
isMetaTag = false;
int nameOffset = oneLine.indexOf("name=");
if (-1 != nameOffset) {
name = oneLine.substring(nameOffset + 5);
if (name.startsWith("\""))
name = name.substring(1);
if (name.endsWith("\""))
name = name.substring(0, name.length() - 1);
name = name.trim();
if (name.startsWith("x-amz-meta-")) {
metaName = name.substring(11);
isMetaTag = true;
}
}
}
} else if (2 == state) {
// -> the body parts data may take up multiple lines
//System.out.println( oneLine.length() + " body data: " + oneLine );
temp.append(oneLine);
}
// else System.out.println( oneLine.length() + " preamble: " + oneLine );
}
// [B] Authenticate the POST request after we have all the headers
try {
S3RestServlet.authenticateRequest(request, params);
} catch (Exception e) {
throw new IOException(e.toString());
}
// [C] Perform the request
if (0 < countMeta)
engineRequest.setMetaEntries(metaSet.toArray(new S3MetaDataEntry[0]));
S3PutObjectInlineResponse engineResponse = ServiceProvider.getInstance().getS3Engine().handleRequest(engineRequest);
response.setHeader("ETag", "\"" + engineResponse.getETag() + "\"");
String version = engineResponse.getVersion();
if (null != version)
response.addHeader("x-amz-version-id", version);