Search code examples
jodd

How to set maxFileSize for FileUploadFactory when using Joy?


Can someone please let me know how to configure the maxFileSize for fileUploads in Jodd, when using Joy?

I've tried in joy.props, even under [joy.madvoc] section with the earlier working:

madvocConfig.fileUploadFactory.maxFileSize=-1

But have no luck :(

Another question is how to force it, to report error, as without it, got:

java.lang.NullPointerException
com.mycircle.action.CircleAction.add(CircleAction.java:78)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
jodd.madvoc.ActionRequest.invokeActionMethod(ActionRequest.java:256)
jodd.madvoc.ActionRequest.lambda$createExecutionArray$1(ActionRequest.java:226)
jodd.madvoc.ActionRequest.invoke(ActionRequest.java:240)
jodd.madvoc.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:79)
jodd.madvoc.interceptor.ActionInterceptor.apply(ActionInterceptor.java:38)
jodd.madvoc.ActionRequest.invoke(ActionRequest.java:240)
jodd.joy.i18n.I18nInterceptor.intercept(I18nInterceptor.java:46)
jodd.madvoc.interceptor.ActionInterceptor.apply(ActionInterceptor.java:38)
jodd.madvoc.ActionRequest.invoke(ActionRequest.java:240)
jodd.madvoc.ActionRequest.lambda$createExecutionArray$0(ActionRequest.java:209)
jodd.madvoc.ActionRequest.invoke(ActionRequest.java:240)
jodd.madvoc.component.MadvocController.invoke(MadvocController.java:163)
jodd.madvoc.MadvocServletFilter.doFilter(MadvocServletFilter.java:109)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
jodd.decora.DecoraServletFilter.doFilter(DecoraServletFilter.java:144)

After debugging, I figured out that is due to file size limitation 100240 bytes defined in FileUploadFactory and AdaptiveFileUploadFactory as well.

I have a @In FileUpload image; in CircleAction and it become null due to this.


Solution

  • Here is how to do so:

    1. from Joy get to the Madvoc
    2. in Madvoc get the FileUploader component - it's a new one that simply holds a FileUploadFactory instance.
    3. Use it:)

    Something like this:

    public class MyWebApplication extends JoyContextListener {
    
        @Override
        protected JoddJoy createJoy() {
            final JoddJoy joy = super.createJoy();
            joy.withWebApp(webApp -> {
                webApp.withRegisteredComponent(FileUploader.class, fileUploader -> {
                    AdaptiveFileUploadFactory adaptiveFileUploadFactory =
                        (AdaptiveFileUploadFactory) fileUploader.get();
                    adaptiveFileUploadFactory.setMaxFileSize(10000000);
                });
            });
            return joy;
        }
    
    }
    

    The seccond way is to use registerComponent and simply pass your implementation of FileUploader.

    Atm, it is not possible to set this value from the madvoc config, but I will fix that soon. There is no more madvocConfig - instead you can set each component. So you will be able to do so like this: fileUploader.maxFileSize (after the fix:).