Thursday, February 5, 2009

Jetty and double-slashes in the URL

Hit a really brief snag today with Jetty and Flex...

Basically, in order to make your Flex app portable for different environments, you want to use tokens in the server url
e.g. this snippet from a Flex services-config.xml

<channels>
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint">
</channel-definition>
...
</channels>

See the {context.root} part?
If you have this project generate foo.war and deploy it to a server, you would access it through a URL like http://myhost/foo/
So,
server.name=foo
server.port=80
context.root=foo

The Flex client will connect to: http://myhost/foo/messagebroker/amf

Now suppose you want to deploy as the root webapp instead... URL becomes http://myhost/
So,
server.name=foo
server.port=80
context.root=<empty>

The Flex client tries connecting to http://myhost//messagebroker/amf (note the double slash because the context root is actually blank).

This actually works fine in Tomcat and IIS, but Jetty takes a different interpretation of the double slash and throws up a 404.
HTTP ERROR: 404
NOT_FOUND
RequestURI=//messagebroker/amf
Powered by Jetty://

Luckily there's an easy fix if you're using Maven and the jetty-maven-plugin like I am... just edit the 'pom.xml' and add <compactPath>true</compactPath>

<plugins>
...
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>80</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<webAppConfig>
<contextPath>/</contextPath>
<compactPath>true</compactPath>
</webAppConfig>
</configuration>
</plugin>
...
</plugins>

No comments: