Hi,

Many times it is desired to have some directory configured in our filesystem from where the Images can be loaded via Http URL. For example if we want to make sire that any images present inside the “/Users/jsensharma/Pictures” directory should be accessible via WildFLy HTTP URL. In that case we will need to use the feature offered by “undertow” subsystem of wildfly.

WildFLy’s undertow subsystem provides us a feature to configure the “handler” which can be used to achieve the same goal. We are going to configure the same as part of this article. Where we will be putting some images inside the “/Users/jsensharma/Pictures” directory and then we will be accessing them from “http://localhost:8080/images” URL by mentioning the Image file name in the URL.

Step-1). Start the WildFly 8 or WildFly 10as following:

  $ cd /PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/bin
  $ ./standalone.sh -c standalone.xml

Step-2). Now start the WildFly jboss-cli.sh script to configure the same as following:

  $ cd /PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/bin
  $ ./jboss-cli.sh -c 
  
  /subsystem=undertow/configuration=handler/file=ImagesDirHandler/:add(cache-buffer-size=1024,cache-buffers=1024,directory-listing=true,follow-symlink=true,path=/Users/jsensharma/Pictures)

  /subsystem=undertow/server=default-server/host=default-host/location=\/images/:add(handler=ImagesDirHandler)
  
  :reload

Step-3). Now we can see the “standalone.xml” file where the configurations are written inside the “undertow” subsystem:

        <subsystem xmlns="urn:jboss:domain:undertow:3.0">
            <buffer-cache name="default"/>
            <server name="default-server">
                <http-listener name="default" redirect-socket="https" socket-binding="http"/>
                <host name="default-host" alias="localhost">
                    <location name="/" handler="welcome-content"/>
                    <location name="/images" handler="ImagesDirHandler"/>  <!-- NOTICE HERE -->
                    <filter-ref name="server-header"/>
                    <filter-ref name="x-powered-by-header"/>
                </host>
            </server>
            <servlet-container name="default">
                <jsp-config/>
                <websockets/>
            </servlet-container>
            <handlers>
                <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
                
                <!-- NOTICE BELOW -->
                <file name="ImagesDirHandler" follow-symlink="true" directory-listing="true" cache-buffers="1024" cache-buffer-size="1024" path="/Users/jsensharma/Pictures"/>
            </handlers>
            <filters>
                <response-header name="server-header" header-value="WildFly/10" header-name="Server"/>
                <response-header name="x-powered-by-header" header-value="Undertow/1" header-name="X-Powered-By"/>
            </filters>
        </subsystem>

Step-4). Now after restart of wildfly, If you will now place any file (like “MiddlewareMagic.jpg”) inside the “/Users/jsensharma/Pictures” directory then it can be accessed via URL:

http://localhost:8080/images/MiddlewareMagic.jpg

Regards
Jay SenSharma

Regards
Jay SenSharma