SERVLETPage added by willem jiangServlet ComponentThe servlet: component provides HTTP based endpoints for consuming HTTP requests that arrive at a http endpoint and this endpoint is bind with a published Servlet. NOTE: This component is new to Camel 2.0. URI format
jetty://RELATIVE_PATH[?options]
Options
Message HeadersCamel will apply the same Message Headers form the HTTP component. Camel will also populate all request.parameter and request.headers. For instance of a client request with http://myserver/myserver?orderid=123 then the exchange will contain a header named orderid with the value 123. UsageYou can only consume from endpoints generated by the Servlet component. Therefore it should only be used as input into your camel Routes. To issue HTTP requests against other HTTP endpoints you can use the HTTP Component SampleIn this sample we define a route where we expose a http service at http://localhost:8080/camel/services/hello:
<web-app>
<servlet>
<servlet-name>CamelServlet</servlet-name>
<display-name>Camel Http Transport Servlet</display-name>
<servlet-class>
org.apache.camel.component.servlet.CamelHttpTransportServlet
</servlet-class>
<init-param>
<param-name>matchOnUriPrefix</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CamelServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
Using the Activator to publish the Servlet in the OSGi platform
import java.util.Dictionary;
import java.util.Hashtable;
import javax.servlet.Servlet;
import org.apache.camel.component.servlet.CamelHttpTransportServlet;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
public final class ServletActivator implements BundleActivator {
/**
* HttpService reference.
*/
private ServiceReference httpServiceRef;
/**
* Called when the OSGi framework starts our bundle
*/
@SuppressWarnings("unchecked")
public void start(BundleContext bc) throws Exception {
httpServiceRef = bc.getServiceReference(HttpService.class.getName());
if (httpServiceRef != null) {
final HttpService httpService = (HttpService)bc.getService(httpServiceRef);
if (httpService != null) {
// create a default context to share between registrations
final HttpContext httpContext = httpService.createDefaultHttpContext();
// register the hello world servlet
final Dictionary initParams = new Hashtable();
initParams.put("matchOnUriPrefix", "false");
initParams.put("servlet-name", "camelServlet");
httpService.registerServlet("/camel/services", // alias
(Servlet)new CamelHttpTransportServlet(), // register servlet
initParams, // init params
httpContext // http context
);
}
}
}
/**
* Called when the OSGi framework stops our bundle
*/
public void stop(BundleContext bc) throws Exception {
if (httpServiceRef != null) {
bc.ungetService(httpServiceRef);
httpServiceRef = null;
}
}
}
Then you can define you router like this
from("servlet:///hello").process(new Processor() {
public void process(Exchange exchange) throws Exception {
String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
assertEquals("Get a wrong content type", CONTENT_TYPE, contentType);
String charsetEncoding = exchange.getIn().getHeader(Exchange.HTTP_CHARACTER_ENCODING, String.class);
assertEquals("Get a wrong charset name", "UTF-8", charsetEncoding);
exchange.getOut().setHeader(Exchange.CONTENT_TYPE, contentType + "; charset=UTF-8");
exchange.getOut().setHeader("PATH", path);
exchange.getOut().setBody("<b>Hello World</b>");
}
});
Since we are binding the Http transport with a published Servlet, and we don't know the Servlet's application context path, camel-servlet endpoint is using the relative path to specify the endpoint's URL. Client can access the camel-servlet endpoint through the Servlet publish address("http://localhost:8080/camel/services") + RELATIVE_PATH("/hello"). See Also
Change Notification Preferences
View Online
|
Add Comment
|
- [CONF] Apache Camel > SERVLET confluence
- [CONF] Apache Camel > SERVLET confluence
- [CONF] Apache Camel > SERVLET confluence
- [CONF] Apache Camel > SERVLET confluence
- [CONF] Apache Camel > SERVLET confluence