001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package com.noelios.restlet.application;
020:
021: import java.util.logging.Logger;
022:
023: import org.restlet.Application;
024: import org.restlet.Uniform;
025: import org.restlet.data.Protocol;
026: import org.restlet.data.Request;
027: import org.restlet.data.Response;
028:
029: /**
030: * Application dispatcher.
031: *
032: * @author Jerome Louvel (contact@noelios.com)
033: */
034: public class ApplicationDispatcher extends Uniform {
035: /** The parent context. */
036: private ApplicationContext applicationContext;
037:
038: /**
039: * Constructor.
040: *
041: * @param applicationContext
042: * The parent application context.
043: */
044: public ApplicationDispatcher(ApplicationContext applicationContext) {
045: this .applicationContext = applicationContext;
046: }
047:
048: /**
049: * Handles a call.
050: *
051: * @param request
052: * The request to handle.
053: * @param response
054: * The response to update.
055: */
056: public void handle(Request request, Response response) {
057: Protocol protocol = request.getProtocol();
058:
059: if (protocol == null) {
060: throw new UnsupportedOperationException(
061: "Unable to determine the protocol to use for this call.");
062: } else {
063: // Add the application in request and response attributes
064: request.getAttributes().put(Application.KEY,
065: this .applicationContext.getApplication());
066: response.getAttributes().put(Application.KEY,
067: this .applicationContext.getApplication());
068:
069: if (protocol.equals(Protocol.WAR)) {
070: this .applicationContext.getWarClient().handle(request,
071: response);
072: } else {
073: if (!this .applicationContext.getApplication()
074: .getConnectorService().getClientProtocols()
075: .contains(protocol)) {
076: this .applicationContext
077: .getLogger()
078: .fine(
079: "The protocol used by this request is not declared in the application's connector service. "
080: + "Please update the list of client connectors used by your application and restart it.");
081: }
082:
083: if (this .applicationContext != null) {
084: if (this .applicationContext.getParentContext() != null) {
085: this .applicationContext.getParentContext()
086: .getDispatcher().handle(request,
087: response);
088: } else {
089: Logger
090: .getLogger(
091: ApplicationDispatcher.class
092: .getCanonicalName())
093: .warning(
094: "Your Application doesn't have a parent context available. Ensure that the parent Component has a context set.");
095: }
096: } else {
097: Logger
098: .getLogger(
099: ApplicationDispatcher.class
100: .getCanonicalName())
101: .warning(
102: "Your Application doesn't have a context set. Ensure that you pass the parent Component's context to your Application constructor.");
103: }
104: }
105: }
106: }
107:
108: }
|