Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more headers in platform-http multipart uploads #17070

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ private void populateMultiFormData(
}

protected void populateAttachments(List<FileUpload> uploads, Message message) {
message.setHeader(Exchange.ATTACHMENTS_SIZE, uploads.size());
for (FileUpload upload : uploads) {
final String name = upload.name();
final String fileName = upload.fileName();
Expand All @@ -357,13 +358,15 @@ protected void populateAttachments(List<FileUpload> uploads, Message message) {

// populate body in case there is only one attachment
if (uploads.size() == 1) {
message.setHeader(Exchange.FILE_PATH, localFile.getAbsolutePath());
message.setHeader(Exchange.FILE_LENGTH, upload.size());
message.setHeader(Exchange.FILE_NAME, upload.fileName());
String ct = MimeTypeHelper.probeMimeType(upload.fileName());
if (ct == null) {
ct = upload.contentType();
}
if (ct != null) {
message.setHeader(Exchange.CONTENT_TYPE, ct);
message.setHeader(Exchange.FILE_CONTENT_TYPE, ct);
}
message.setBody(localFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ public void configure() {
}

exchange.getIn().setHeader("ConcatFileContent", result);
});
})
.setHeader("UploadedAttachments", simple("${headers.CamelAttachmentsSize}"));
}
});

Expand All @@ -444,6 +445,7 @@ public void configure() {
.then()
.statusCode(204)
.body(emptyOrNullString())
.header("UploadedAttachments", is(String.valueOf(attachmentIds.size())))
.header("ConcatFileContent",
is("Test multipart upload content myFirstTestFileTest multipart upload content mySecondTestFile"));
} finally {
Expand Down Expand Up @@ -476,7 +478,9 @@ public void configure() {
AttachmentMessage message = exchange.getMessage(AttachmentMessage.class);
DataHandler attachment = message.getAttachment(attachmentId);
exchange.getMessage().setHeader("myDataHandler", attachment);
});
})
.setHeader("UploadedFileContentType", simple("${header.CamelFileContentType}"))
.setHeader("UploadedFileSize", simple("${header.CamelFileLength}"));
}
});

Expand All @@ -489,6 +493,8 @@ public void configure() {
.then()
.statusCode(200)
.header("myDataHandler", containsString("jakarta.activation.DataHandler"))
.header("UploadedFileContentType", is("text/plain"))
.header("UploadedFileSize", is(String.valueOf(fileContent.getBytes().length)))
.body(is(fileContent));
} finally {
context.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ Since Apache Camel 4.10, multipart file uploads are easier and harmonized across
* The Apache Camel message contains:
** The uploaded file in the message body
** The file name in the "CamelFileName" message header
** The file's content type in the "Content-Type" message header
** The file's content type in the "CamelFileContentType" message header
** The file's size in the "CamelFileLength" message header
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick

Suggested change
** The file's content type in the "CamelFileContentType" message header
** The file's size in the "CamelFileLength" message header
** The file content type in the "CamelFileContentType" message header
** The file size in the "CamelFileLength" message header


In case of multiple uploads, the header "CamelAttachmentsSize" contains the number of files uploaded.

include::spring-boot:partial$starter.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ExchangeConstantProvider {

private static final Map<String, String> MAP;
static {
Map<String, String> map = new HashMap<>(160);
Map<String, String> map = new HashMap<>(161);
map.put("ACCEPT_CONTENT_TYPE", "CamelAcceptContentType");
map.put("AGGREGATED_COLLECTION_GUARD", "CamelAggregatedCollectionGuard");
map.put("AGGREGATED_COMPLETED_BY", "CamelAggregatedCompletedBy");
Expand All @@ -25,6 +25,7 @@ public class ExchangeConstantProvider {
map.put("AGGREGATION_COMPLETE_CURRENT_GROUP", "CamelAggregationCompleteCurrentGroup");
map.put("AGGREGATION_STRATEGY", "CamelAggregationStrategy");
map.put("ASYNC_WAIT", "CamelAsyncWait");
map.put("ATTACHMENTS_SIZE", "CamelAttachmentsSize");
map.put("AUTHENTICATION", "CamelAuthentication");
map.put("AUTHENTICATION_FAILURE_POLICY_ID", "CamelAuthenticationFailurePolicyId");
map.put("BATCH_COMPLETE", "CamelBatchComplete");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public interface Exchange extends VariableAware {
String CONTENT_TYPE = "Content-Type";
String COOKIE_HANDLER = "CamelCookieHandler";
String CORRELATION_ID = "CamelCorrelationId";
String ATTACHMENTS_SIZE = "CamelAttachmentsSize";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is somewhere you need to document this header, try look for some of the other headers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also put it up to A headers so its sorted

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah add it to ExchangePropertyKey also and doc via @Metadata

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, I cannot find much documentation for other headers, should I annotate it with Metadata and add a description?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/apache/camel/pull/17070/files#diff-c4832886119fc6dd6a4a54e68c5e2740ea9ae86b8699a469648284189d51e839 like this? I do not fully understand how ExchangePropertyKey works, for example, FILE* related headers are not there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its the most commonly used headers - and since HTTP with attachments is fairly common then IMHO it would be okay to add here


// The schema of the message payload
String CONTENT_SCHEMA = "CamelContentSchema";
Expand Down