Search code examples
javaundertow

configure embedded undertow server programmatically


the code below allows me to create an embedded undertow servlet server, i have a problem setting the 'max-parameters' of the connector settings, the way i understand it undertow is normally configured via xml file.

   public static String initCustomServer_(Servlet servlet,int preferedPort,String servletName,String[] resourceList,String... domainName){
    String contextURL = null;
    int curPort = preferedPort==-1?9001:preferedPort;
    boolean initServ = false;
    System.out.println("====servlet running in local mode====");
    while(!initServ) {
        try{
            io.undertow.servlet.api.DeploymentInfo servletBuilder = io.undertow.servlet.Servlets.deployment()
                    .setClassLoader(servlet.getClass().getClassLoader())
                    .setContextPath(domainName.length==0?"/":"/"+domainName[0])
                    .setDeploymentName("test.war")
                    .addServlets(
                            io.undertow.servlet.Servlets.servlet(servletName, servlet.getClass()).addMapping("/"+servletName)
                    )
                    .setResourceManager(new io.undertow.server.handlers.resource.FileResourceManager(new File("src/dss_core/HTML5/webapp"), 1));

            io.undertow.servlet.api.DeploymentManager manager = io.undertow.servlet.Servlets.defaultContainer().addDeployment(servletBuilder);

            manager.deploy();

            io.undertow.server.HttpHandler servletHandler = manager.start();
            io.undertow.server.handlers.PathHandler path = io.undertow.Handlers.path(io.undertow.Handlers.redirect(domainName.length==0?"/":"/"+domainName[0]))
                    .addPrefixPath(domainName.length==0?"/":"/"+domainName[0], servletHandler);

            io.undertow.Undertow server = io.undertow.Undertow.builder()
                    .addHttpListener(curPort, "localhost")
                    .setHandler(path)
                    .build();

            server.start();
            initServ = true;
            contextURL = "http://localhost:"+curPort+(domainName.length==0?"":"/"+domainName[0])+"/"+servletName;
        } catch (Exception ex) {
            //creation of server at certain port fails therefore try again on another port
            System.err.println(" server unable to initialize :" + ex.getMessage());
            ex.printStackTrace();
            curPort++;
        }
    }
    return contextURL;
}

rather than using an xml like the one below how do i change configurations such as 'max-parameter' via embedded java code?

           <server name="default-server">
            <http-listener name="default" socket-binding="http" max-parameters="5000"/>

found here are list of stuff that i can configure via xml how can i set them via java code?

UPDATE 1: yay found some options in io.undertow.UndertowOptions, how ever this doesn't work as it is declared final, what now?

io.undertow.UndertowOptions.MAX_PARAMETERS = 10000;

Solution

  • after hours of research and trial and error finally i got it, my first idea was to simply get the code and compile it myself, negative side of that is that i'd have to download all source code then compile it, such proved to be trouble some and i decided to quit after seeing endless dependencies and hours of downloading their source code. configuring the server looked like this

                    io.undertow.Undertow server = io.undertow.Undertow.builder()
                        .addHttpListener(curPort, "localhost")
                        .setHandler(path)
                        .setServerOption(io.undertow.UndertowOptions.MAX_PARAMETERS, 10000)
                        .setServerOption(io.undertow.UndertowOptions.OPTION2, Value2)
                        .build();
    

    setServerOption method and io.undertow.UndertowOptions class finally made sense, it's too bad undertow isn't very popular and not much sample code lying around, i hope i help anybody wishing to take the embedded road of undertow