2
* Copyright 2005-2012 Restlet S.A.S.
4
* The contents of this file are subject to the terms of one of the following
5
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
6
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
7
* not use this file except in compliance with one of these Licenses.
9
* You can obtain a copy of the Apache 2.0 license at
10
* http://www.opensource.org/licenses/apache-2.0
12
* You can obtain a copy of the LGPL 3.0 license at
13
* http://www.opensource.org/licenses/lgpl-3.0
15
* You can obtain a copy of the LGPL 2.1 license at
16
* http://www.opensource.org/licenses/lgpl-2.1
18
* You can obtain a copy of the CDDL 1.0 license at
19
* http://www.opensource.org/licenses/cddl1
21
* You can obtain a copy of the EPL 1.0 license at
22
* http://www.opensource.org/licenses/eclipse-1.0
24
* See the Licenses for the specific language governing permissions and
25
* limitations under the Licenses.
27
* Alternatively, you can obtain a royalty free commercial license with less
28
* limitations, transferable or non-transferable, directly at
29
* http://www.restlet.com/products/restlet-framework
31
* Restlet is a registered trademark of Restlet S.A.S.
34
package org.restlet.engine.component;
36
import java.util.logging.Level;
38
import org.restlet.Client;
39
import org.restlet.Component;
40
import org.restlet.Request;
41
import org.restlet.Response;
42
import org.restlet.Restlet;
43
import org.restlet.routing.Router;
46
* Router that collects calls from all applications and dispatches them to the
47
* appropriate client connectors.
49
* Concurrency note: instances of this class or its subclasses can be invoked by
50
* several threads at the same time and therefore must be thread-safe. You
51
* should be especially careful when storing state in member variables.
53
* @author Jerome Louvel
55
public class ClientRouter extends Router {
56
/** The parent component. */
57
private volatile Component component;
63
* The parent component.
65
public ClientRouter(Component component) {
66
super((component == null) ? null : component.getContext()
67
.createChildContext());
68
this.component = component;
71
@SuppressWarnings("deprecation")
73
protected void logRoute(org.restlet.routing.Route route) {
74
if (getLogger().isLoggable(Level.FINE)) {
75
if (route instanceof ClientRoute) {
76
Client client = ((ClientRoute) route).getClient();
79
"This client was selected: \"" + client.getProtocols() + "\"");
81
super.logRoute(route);
87
public Restlet getNext(Request request, Response response) {
88
Restlet result = super.getNext(request, response);
92
"The protocol used by this request is not declared in the list of client connectors. ("
93
+ request.getResourceRef()
94
.getSchemeProtocol() + ")");
100
* Returns the parent component.
102
* @return The parent component.
104
private Component getComponent() {
105
return this.component;
108
/** Starts the Restlet. */
110
public synchronized void start() throws Exception {
111
for (final Client client : getComponent().getClients()) {
112
getRoutes().add(new ClientRoute(this, client));