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 ability to set a filename generator on a ZipTransformer #9452

Closed
michaelwiles opened this issue Sep 12, 2024 · 2 comments
Closed

Add ability to set a filename generator on a ZipTransformer #9452

michaelwiles opened this issue Sep 12, 2024 · 2 comments

Comments

@michaelwiles
Copy link
Contributor

Right now when using the zip transformer it is not possible to set the name of the new generated zip file.

Observing the code of doZipTransform:

  protected Object doZipTransform(Message<?> message) {
        Object payload = message.getPayload();
        String baseFileName = this.fileNameGenerator.generateFileName(message);
        String zipEntryName;
        if (message.getHeaders().containsKey("zip_entryFilename")) {
            zipEntryName = (String)message.getHeaders().get("zip_entryFilename");
        } else {
            zipEntryName = baseFileName;
        }

        String zipFileName;
        if (message.getHeaders().containsKey("file_name")) {
            zipFileName = (String)message.getHeaders().get("file_name");
        } else {
            zipFileName = baseFileName + ".zip";
        }

Notice that if a file_name header is not found, then the name from the fileNameGenerator is used and .zip is appended. This behaviour would be great. However, there is a file_name header present (there should be) and that is what the defaultFileNameGenerator uses to generate the file name (and you can't change the filename generator from the default one). So the alternative zipFileName = baseFileName + ".zip"; would never be used if there's a file_name header.

So please can the zip transformers be changed so that the file name generator can be set?

I know I could manually set the file_name to add the zip extension before hand but I'd argue it makes more sense to allow me to set the file name generator to automatically change the file name so that the zip extension is included once the zip transformer has run. If I do this and then remove the zip transformer I will also need to set the file_name back to the one without the zip extension.

@artembilan artembilan transferred this issue from spring-projects/spring-integration-extensions Sep 12, 2024
@artembilan
Copy link
Member

I would say the logic for that if (message.getHeaders().containsKey(FileHeaders.FILENAME)) { is wrong.
The DefaultFileNameGenerator really does the stuff against that header if present.
So, I agree about exposing such an option o an AbstractZipTransformer, however the logic is more complicated than it seems.
Plus pay attention to this in the end:

		return getMessageBuilderFactory()
				.withPayload(zippedData)
				.copyHeaders(message.getHeaders())
				.setHeader(FileHeaders.FILENAME, zipFileName)
				.build();

So, after this transformer that header is indeed overridden to a .zip one.
I still believe that this is a valid behavior.
But for fileNameGenerator option we have provide a fix.

Thanks

@artembilan
Copy link
Member

BTW, the spring-integration-zip module in the Extensions project is out of support.
Please, consider to use as latest Spring Integration version as possible.
Not sure that the fix we will provide here would be back-ported to 6.3.x and 6.2.x: might be slight breaking change in behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants