Wednesday, February 10, 2010

Jetty 7 maven plugin authentication realms

The configuration directive UserRealm seems to no longer work with the jetty 7 maven plugin (jetty-maven-plugin). I received the following error:

java.lang.IllegalStateException: No LoginService for org.eclipse.jetty.security.authentication.BasicAuthenticator@4095c5ec in ConstraintSecurityHandler@28f52a14@

Fixing this involved:

  • Creating a jetty configuration file, such as the following:


    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
      <Get name="securityHandler">
        <Set name="loginService">
          <New class="org.eclipse.jetty.security.HashLoginService">
            <Set name="name">YourSecurityRealmHere</Set>
            <Set name="config">src/test/resources/jetty-realm.properties</Set>
            <Call name="start"/>
          </New>
        </Set>
        <Set name="checkWelcomeFiles">true</Set>
      </Get>
    </Configure>

    To be saved as src/test/resources/jetty-test.xml

  • Create a src/test/resources/jetty-realm.properties with your Jetty 7 password file.

  • Updating the jetty configuration in my pom.xml:

          <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>7.0.1.v20091125</version>
            <configuration>
              <webAppXml>src/test/resources/jetty-test.xml</webAppXml>
            </configuration>
            <dependencies>
              <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-servlets</artifactId>
                <version>7.0.1.v20091125</version>
              </dependency>
            </dependencies>
          </plugin>
    

    The important entry is webAppXML. The extra dependency to jetty-servlets was added to support GZip in Jetty 7.

Jetty seems to ignore webdefault.xml

If just tried to edit webdefaults.xml to disable directory views, and the change was not reflected in my webapp. The reason: webdefaults.xml is not read by default, but must be explicitly added to the context of the webapp:


 <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>

And suddenly all changes in webdefaults.xml are applied.

Wednesday, January 27, 2010

Enabling JSP support in Jetty 7

As Jetty 6 failed to use the port inherited from xinetd, I have to use Jetty 7. Unfortunately Jetty is missing the JSP support by default. Here is how to enable it:

  1. Download the Jetty 6 distribution.
  2. Copy the jsp-2.1 folder to the Jetty 7 lib/ folder.
  3. You can check if it works by running java -jar start.jar --list-options
  4. Edit start.ini to include jsp-2.1 in the OPTIONS line.

Jetty 6 vs. Jetty 7

There are several differences between Jetty 6 and Jetty 7:

  • Most classes have been renamed from org.mortbay to org.eclipse and sometimes moved to a different path. Find the new class names in the Jetty 7 JavaDoc. This applies to most settings in jetty.xml and web.xml.
  • Jetty 7 comes with fewer plugins. Important extras such as JSP support are missing from the standard distribution.
  • Jetty 7 includes several bugfixes. Example: Taking a port from xinetd did not work with Jetty 6 on my machine.

More information is at the Porting to Jetty 7 page.

Jetty 7 gzip Filter

Jetty includes a gzip-filter for dynamic compression of contents, thus saving bandwidth and enabling your site to load faster (especially important if you have a lot of javascript).

To configure it, add the follwing filter definition to your web.xml:


  <filter>
    <filter-name>GzipFilter</filter-name>
    <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
    <init-param>
      <param-name>mimeTypes</param-name>
      <param-value>text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,image/svg+xml</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>GzipFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping> 

Please note: This configuration is for Jetty 7. For Jetty 6 change the filter to org.mortbay.servlet.GzipFilter.

Jettys missing passwd.sh

Jetty comes with a very nice Password encrypting command. Unfortunately, there is no passwd.sh to call it, and you have to manually find out where it is defined. Also, the documentation is for Jetty 6 only, and the names of the classes and libraries have changed in Jetty 7.

Here is the missing passwd.sh for Jetty 7:


#!/bin/bash

# look for JETTY_HOME
if [ -z "$JETTY_HOME" ] 
then
  cd $(dirname "$0")
  JETTY_HOME_1=$(pwd)
  cd - > /dev/null
  JETTY_HOME_1=$(dirname "$JETTY_HOME_1")
  JETTY_HOME=${JETTY_HOME_1} 
fi

java -cp $JETTY_HOME/lib/jetty-http-7.0.1.v20091125.jar:\
$JETTY_HOME/lib/jetty-util-7.0.1.v20091125.jar \
org.eclipse.jetty.http.security.Password $*

Please note that the JETTY_HOME detection is also changed from the original jetty shell scripts to be more sane.

Tuesday, January 26, 2010

Jetty vs. Tomcat

When setting up a new webserver, I decided to evaluate Jetty as an alternative to Tomcat. I have previousely used Tomcat, and was content with it. Here is what I've found:
Both:
  • Implement recent JSP and Servlet standards
  • Can easily run .WAR files
  • Are pretty fast when serving content (speed comparisons on the web show only minor differences)
Pro Jetty:
  • Jetty uses less memory
  • Jetty seams more "lightweight"
Pro Tomcat:
  • Tomcat is very well documented - Jetty Documentation is sparse and targeted towards Java developers. The Jetty config file is (intentionally) like coding
  • I know Tomcat by now
As the machine I am using now is low on memory, I decided to give Jetty a try. However, installing it was not easy. But that's a topic for the next post.

Home

Blog

Followers