0

Wildfly 8, Jsf2.2, Primefaces 5.3

I want to make some file name transformation, before it reaches Primefaces <p:fileUpload> listener. So that UploadedFile in listener has converted file name. My first idea were to use WebFilter but apparently it's not so good. The reason is given in this StackOverflow answer https://stackoverflow.com/a/7444514/2023524

Because we are using apache commons, I've made a hack for it. Overwrite DiskFileItemFactory, and return adjusted file name.

public class MyFactory extends DiskFileItemFactory{
    public FileItem createItem(String fieldName, String contentType,
            boolean isFormField, String fileName) {
        fileName = convertFileName(fileName);
        return  super.createItem(fieldName, contentType, isFormField, fileName);
    }

Also Primefaces FileUploadFilter is overwritten to instantiate the new factory.

Is it possible something like that for native Jsf2.2 upload ?

EDIT: The reason for this idea: from file name should be stripped off some bad unicode characters. So in listener method always remains the right characters. The place to save a file is determined dynamically based on a file and a problem. So I'm not sure how to make a composite component for this.

Community
  • 1
  • 1
Tony
  • 2,266
  • 4
  • 33
  • 54
  • 2
    Why not change it in the uploadListener before really storing it? Seems like problem in a weird hack for a different underlying problem – Kukeltje Apr 01 '16 at 09:05
  • Of course it is possible, but you can forget to do it. And there are already many implented upload listeners . With apache hack it is only configuration thing. – Tony Apr 01 '16 at 09:08
  • what do you mean you 'can forget to do it'? If it is a business requirement that a file should have a specific name, implement it in a business method `saveFile(.....)`, not in a webfilter or similar hack. No one can forget it then since it is IN the business method. And you are saying you'd rather have a hack than good code? Bad choice imo... Bite the apple, fix the uploadListeners (they do work now do they?) – Kukeltje Apr 01 '16 at 09:18
  • Sounds like perfect candidate for a composite component with a backing component which does all the "under the covers" work and offers an already renamed and saved file back as bean property value. – BalusC Apr 01 '16 at 09:45
  • @Kukeltje True there are good reasons, to do it as you describe. But the problem in a question is challenging :-) – Tony Apr 01 '16 at 12:06
  • @BalusC thank you. It were cool, but I'm afraid it would need too much refactoring. – Tony Apr 01 '16 at 12:08
  • @BalusC I've edited the question. – Tony Apr 01 '16 at 12:32
  • @Kukeltje I've edited the question. – Tony Apr 01 '16 at 12:32
  • 1
    Unicode characters are only bad for your business logic right? Better perform the fix/cleanup right there. – BalusC Apr 01 '16 at 13:06

0 Answers0