01: /*
02: * Copyright 2005-2007 Noelios Consulting.
03: *
04: * The contents of this file are subject to the terms of the Common Development
05: * and Distribution License (the "License"). You may not use this file except in
06: * compliance with the License.
07: *
08: * You can obtain a copy of the license at
09: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
10: * language governing permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL HEADER in each file and
13: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
14: * applicable, add the following below this CDDL HEADER, with the fields
15: * enclosed by brackets "[]" replaced with your own identifying information:
16: * Portions Copyright [yyyy] [name of copyright owner]
17: */
18:
19: package com.noelios.restlet.component;
20:
21: import org.restlet.Component;
22: import org.restlet.Restlet;
23: import org.restlet.Route;
24: import org.restlet.Router;
25: import org.restlet.VirtualHost;
26: import org.restlet.data.Request;
27: import org.restlet.data.Response;
28: import org.restlet.data.Status;
29:
30: /**
31: * Router that collects calls from all server connectors and dispatches them to
32: * the appropriate host routers for dispatching to the user applications.
33: *
34: * @author Jerome Louvel (contact@noelios.com)
35: */
36: public class ServerRouter extends Router {
37: /** The parent component. */
38: private Component component;
39:
40: /**
41: * Constructor.
42: *
43: * @param component
44: * The parent component.
45: */
46: public ServerRouter(Component component) {
47: super (component.getContext());
48: this .component = component;
49: setRoutingMode(FIRST);
50: }
51:
52: /** Starts the Restlet. */
53: public void start() throws Exception {
54: // Attach all virtual hosts
55: for (VirtualHost host : getComponent().getHosts()) {
56: getRoutes().add(new HostRoute(this , host));
57: }
58:
59: // Also attach the default host if it exists
60: if (getComponent().getDefaultHost() != null) {
61: getRoutes()
62: .add(
63: new HostRoute(this , getComponent()
64: .getDefaultHost()));
65: }
66:
67: // If no host matches, display and error page with a precise message
68: Restlet noHostMatched = new Restlet(getComponent().getContext()) {
69: public void handle(Request request, Response response) {
70: response.setStatus(Status.CLIENT_ERROR_NOT_FOUND,
71: "No virtual host could handle the request");
72: }
73: };
74: setDefaultRoute(new Route(this , "", noHostMatched));
75:
76: // Start the router
77: super .start();
78: }
79:
80: @Override
81: public void stop() throws Exception {
82: getRoutes().clear();
83: super .stop();
84: }
85:
86: /**
87: * Returns the parent component.
88: *
89: * @return The parent component.
90: */
91: private Component getComponent() {
92: return this.component;
93: }
94: }
|