Posts Tagged ‘http’

Embed a Jetty file server within Mule 3.1.1

June 7, 2011 1 comment

This post details how to embed a Jetty webserver within Mule, such that static files hosted within your application are accessible to the outside world. The resources describing how to do this are few and far between; I also found them erroneous. For some reason, any time I include a test:component element in my Mule configuration files, I get a timeout. By eliminating that piece, I got things to work.

These config files assume that both jetty.xml and mule-config.xml are located in the same folder, namely conf.


<mule xmlns=""
  This configuration uses an embedded Jetty instance to serve static content.

  <jetty:connector configFile="${app.home}/conf/jetty.xml" name="jetty_connector" ></jetty:connector>
  <!-- do not use localhost here or you will not be able to access the server except locally.-->
  <jetty:endpoint address="" 

  <model name="Jetty">
    <service name="jettyUMO">
        <jetty:inbound-endpoint ref="jettyEndpoint" /> 


Modified from Newbie Guide to Jetty, namely changing class names (the classes in question are bundled with Mule 3.1.1, in the Jar file found in $MULE_HOME/lib/opt/jetty-6.1.26.jar).

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "">

<Configure id="FileServer" class="org.mortbay.jetty.Server">
  <Set name="handler">
    <New class="org.mortbay.jetty.handler.HandlerList">
      <Set name="handlers">
        <Array type="org.mortbay.jetty.Handler">
            <New class="org.mortbay.jetty.handler.ResourceHandler">
              <!--  Jetty 6.1.26, which comes with Mule 3.1, does not have this method --> 
              <!--<Set name="directoriesListed">true</Set>-->
              <Set name="welcomeFiles">
                <Array type="String">
              <!-- This folder maps to the root URL configured for this Jetty endpoint.  If I wanted to start serving content from the a folder named "static", I would replace the . with "static".-->
              <Set name="resourceBase">.</Set>
            <New class="org.mortbay.jetty.handler.DefaultHandler" />

A gist with both of these code snippets can be found here.


With these two configuration files, you can launch an embedded instance of Jetty within your application, and use it to serve static content. Due to a limitation in the version of Jetty 6.1.26 which Mule 3.1.1 comes with, you cannot use the Jetty instance to list the contents of folders; instead the client must know the absolute path to the file. For my purposes this was not a problem.