~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet/src/org/restlet/engine/component/ClientRouter.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
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.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
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
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.engine.component;
 
35
 
 
36
import java.util.logging.Level;
 
37
 
 
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;
 
44
 
 
45
/**
 
46
 * Router that collects calls from all applications and dispatches them to the
 
47
 * appropriate client connectors.
 
48
 * 
 
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.
 
52
 * 
 
53
 * @author Jerome Louvel
 
54
 */
 
55
public class ClientRouter extends Router {
 
56
    /** The parent component. */
 
57
    private volatile Component component;
 
58
 
 
59
    /**
 
60
     * Constructor.
 
61
     * 
 
62
     * @param component
 
63
     *            The parent component.
 
64
     */
 
65
    public ClientRouter(Component component) {
 
66
        super((component == null) ? null : component.getContext()
 
67
                .createChildContext());
 
68
        this.component = component;
 
69
    }
 
70
 
 
71
    @SuppressWarnings("deprecation")
 
72
    @Override
 
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();
 
77
 
 
78
                getLogger().fine(
 
79
                        "This client was selected: \"" + client.getProtocols() + "\"");
 
80
            } else {
 
81
                super.logRoute(route);
 
82
            }
 
83
        }
 
84
    }
 
85
 
 
86
    @Override
 
87
    public Restlet getNext(Request request, Response response) {
 
88
        Restlet result = super.getNext(request, response);
 
89
        if (result == null) {
 
90
            getLogger()
 
91
                    .warning(
 
92
                            "The protocol used by this request is not declared in the list of client connectors. ("
 
93
                                    + request.getResourceRef()
 
94
                                            .getSchemeProtocol() + ")");
 
95
        }
 
96
        return result;
 
97
    }
 
98
 
 
99
    /**
 
100
     * Returns the parent component.
 
101
     * 
 
102
     * @return The parent component.
 
103
     */
 
104
    private Component getComponent() {
 
105
        return this.component;
 
106
    }
 
107
 
 
108
    /** Starts the Restlet. */
 
109
    @Override
 
110
    public synchronized void start() throws Exception {
 
111
        for (final Client client : getComponent().getClients()) {
 
112
            getRoutes().add(new ClientRoute(this, client));
 
113
        }
 
114
 
 
115
        super.start();
 
116
    }
 
117
}