Replace MiltonServlet with MiltonFilter

Finally we just need to integrate the new milton filter into your web.xml file.

But what if i'm using Spring?

The MiltonController class is still there and mostly unchanged, so you might not need to do anything except update the package names in your app context file. However to get the benefit of the new HttpManagerBuilder support you can use the new constructor in MiltonController:

public MiltonController(HttpManagerBuilder config) {

The old MiltonServlet would be integrated something like this:

    <servlet>
        <servlet-name>milton</servlet-name>
        <servlet-class>com.mycompany.ScratchServlet</servlet-class>
        <init-param>
            <param-name>resource.factory.class</param-name>
            <param-value>com.mycompany.TResourceFactory</param-value>
        </init-param>
        <init-param>
            <param-name>not.found.url</param-name>
            <param-value>/404.jsp</param-value>
        </init-param>    
    </servlet>

    <servlet-mapping>
        <servlet-name>milton</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
Configuration, and what to use instead of ResourceFactoryFactory?

I'm so glad ResourceFactoryFactory has been removed from Milton, as an approach to configuration it was just embarrassing. Milton2 uses the builder pattern for configuration, with a two-phase approach. First the builder is configured, and then the HttpManager is created from it.

To do your own configuration (ie instead of using ResourceFactoryFactory) just subclass DefaultMiltonConfigurator and override the build method.

For example, the new reference implement starts a LDAP server and mail server like this:

public class MyMiltonConfigurator extends DefaultMiltonConfigurator {

    private TResourceFactory resourceFactory;
    private TLdapUserFactory userFactory;
    private LdapServer ldapServer;
    private TMailResourceFactory mailResourceFactory;
    private MailServer mailServer;

    @Override
    protected void build() {
        super.build();
        
        resourceFactory = (TResourceFactory) builder.getMainResourceFactory(); // get our resource factory from the builder
        userFactory = new TLdapUserFactory(resourceFactory);
        LdapTransactionManager transactionManager = new NullLdapTransactionManager();
        ldapServer = new LdapServer(transactionManager, userFactory, builder.getWebDavProtocol());
        ldapServer.setPort(8369);
        ldapServer.start();
        
        mailResourceFactory = new TMailResourceFactory(resourceFactory);
        MailServerBuilder msb = new MailServerBuilder();
        msb.setSmtpPort(2525);
        msb.setMsaSmtpPort(8587);
        msb.setMailResourceFactory(mailResourceFactory);
        mailServer = msb.build();
        mailServer.start();
        
        
    }

    @Override
    public void shutdown() {
        super.shutdown();
        ldapServer.interrupt();
        mailServer.stop();
    }
}

Then reference it from your web.xml like this:

        <init-param>
            <param-name>milton.configurator</param-name>
            <param-value>com.mycompany.MyMiltonConfigurator</param-value>
        </init-param>

To use the new milton filter you would do this:

      <filter>
        <filter-name>MiltonFilter</filter-name>
        <filter-class>io.milton.servlet.MiltonFilter</filter-class>
        
        <!-- This param shows how to exclude certain paths from the MiltonFilter -->
        <!-- These paths will "fall through" the filter and be handled as normal servlet resources -->
        <init-param>
            <param-name>milton.exclude.paths</param-name>
            <param-value>/myExcludedPaths,/moreExcludedPaths</param-value>
        </init-param>
        <init-param>
            <param-name>resource.factory.class</param-name>
            <param-value>com.mycompany.TResourceFactory</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>MiltonFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>