* The HTTP Servlet request.
* @return The new HTTP server handling calls.
*/
protected HttpServerHelper createServer(HttpServletRequest request) {
HttpServerHelper result = null;
final Component component = getComponent();
if (component != null) {
// First, let's create a pseudo server
final Server server = new Server(component.getContext()
.createChildContext(), (List<Protocol>) null, request
.getLocalAddr(), request.getLocalPort(), component);
result = new HttpServerHelper(server);
// Attach the hosted application(s) to the right path
final String uriPattern = request.getContextPath()
+ request.getServletPath();
if (isDefaultComponent()) {
if (this.application != null) {
log("[Noelios Restlet Engine] - Attaching application: "
+ this.application + " to URI: " + uriPattern);
component.getDefaultHost().attach(uriPattern,
this.application);
}
} else {
// According to the mode, configure correctly the component.
final String autoWire = getInitParameter(AUTO_WIRE_KEY,
AUTO_WIRE_KEY_DEFAULT);
if (AUTO_WIRE_KEY_DEFAULT.equalsIgnoreCase(autoWire)) {
// Translate all defined routes as much as possible
// with the context path only or the full servlet path.
// 1- get the offset
boolean addContextPath = false;
boolean addFullServletPath = false;
if (component.getDefaultHost().getRoutes().isEmpty()) {
// Case where the default host has a default route (with
// an empty pattern).
addFullServletPath = component.getDefaultHost()
.getDefaultRoute() != null;
} else {
for (final Route route : component.getDefaultHost()
.getRoutes()) {
if (route.getTemplate().getPattern() == null) {
addFullServletPath = true;
continue;
}
if (!route.getTemplate().getPattern().startsWith(
uriPattern)) {
if (!route.getTemplate().getPattern()
.startsWith(request.getServletPath())) {
addFullServletPath = true;
} else {
addContextPath = true;
break;
}
}
}
}
if (!addContextPath) {
for (final VirtualHost virtualHost : component
.getHosts()) {
if (virtualHost.getRoutes().isEmpty()) {
// Case where the default host has a default
// route (with an empty pattern).
addFullServletPath = virtualHost
.getDefaultRoute() != null;
} else {
for (final Route route : virtualHost
.getRoutes()) {
if (route.getTemplate().getPattern() == null) {
addFullServletPath = true;
continue;
}
if (!route.getTemplate().getPattern()
.startsWith(uriPattern)) {
if (!route
.getTemplate()
.getPattern()
.startsWith(
request
.getServletPath())) {
addFullServletPath = true;
} else {
addContextPath = true;
break;
}
}
}
}
if (addContextPath) {
break;
}
}
}
// 2- Translate all routes.
if (addContextPath || addFullServletPath) {
String offsetPath = null;
if (addContextPath) {
offsetPath = request.getContextPath();
} else {
offsetPath = uriPattern;
}
// Shift the default route (if any) of the default host
Route defaultRoute = component.getDefaultHost()
.getDefaultRoute();
if (defaultRoute != null) {
defaultRoute.getTemplate().setPattern(
offsetPath
+ defaultRoute.getTemplate()
.getPattern());
log("[Noelios Restlet Engine] - Attaching restlet: "
+ defaultRoute.getNext()
+ " to URI: "
+ offsetPath
+ defaultRoute.getTemplate().getPattern());
}
// Shift the routes of the default host
for (final Route route : component.getDefaultHost()
.getRoutes()) {
log("[Noelios Restlet Engine] - Attaching restlet: "
+ route.getNext()
+ " to URI: "
+ offsetPath
+ route.getTemplate().getPattern());
route.getTemplate().setPattern(
offsetPath
+ route.getTemplate().getPattern());
}
for (final VirtualHost virtualHost : component
.getHosts()) {
// Shift the default route (if any) of the virtual
// host
defaultRoute = virtualHost.getDefaultRoute();
if (defaultRoute != null) {