Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fix source code references

...

The jetty component provides HTTP-based endpoints for consuming and producing HTTP requests. That is, the Jetty component behaves as a simple Web server.
Jetty can also be used as a http client which mean you can also use it with Camel as a producer.

Info
titleStream

The assert call appears in this example, because the code is part of an unit test.Jetty is stream based, which means the input it receives is submitted to Camel as a stream. That means you will only be able to read the content of the stream once.
If you find a situation where the message body appears to be empty or you need to access the Exchange.HTTP_RESPONSE_CODE data multiple times (e.g.: doing multicasting, or redelivery error handling), you should use Stream caching or convert the message body to a String which is safe to be re-read multiple times.

...

In this sample we define a route that exposes a HTTP service at http://localhost:8080/myapp/myservice:

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java}

...

Our business logic is implemented in the MyBookService class, which accesses the HTTP request contents and then returns a response.
Note: The assert call appears in this example, because the code is part of an unit test.

Wiki Markup
{snippet:id=e2|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java}
The following sample shows a content-based route that routes all requests containing the URI parameter, one, to the endpoint, mock:one, and all others to mock:other.
Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java}
So if a client sends the HTTP request, http://serverUri?one=hello, the Jetty component will copy the HTTP request parameter, one to the exchange's in.header. We can then use the simple language to route exchanges that contain this header to a specific endpoint and all others to another. If we used a language more powerful than Simple (such as EL or OGNL) we could also test for the parameter value and do routing based on the header value as well.

...

The following example shows how to customize the DefaultHttpBinding in order to change how exceptions are returned:

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/HttpBindingRefTest.java}
We can then create an instance of our binding and register it in the Spring registry as follows:

...

You may want to return a custom reply message when something goes wrong, instead of the default reply message Camel Jetty replies with.
You could use a custom HttpBinding to be in control of the message mapping, but often it may be easier to use Camel's Exception Clause to construct the custom reply message. For example as show here, where we return Dude something went wrong with HTTP error code 500:

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/JettyOnExceptionHandledTest.java}

...

From Camel 2.3.0, camel-jetty support to multipart form post out of box. The submitted form-data are mapped into the message header. Camel-jetty creates an attachment for each uploaded file. The file name is mapped to the name of the attachment. The content type is set as the content type of the attachment file name. You can find the example here.

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java|title=Note: getName() functions as shown below in versions 2.5 and higher. In earlier versions you receive the temporary file name for the attachment instead}

...