001// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.services.*;
018
019import java.io.IOException;
020
021/**
022 * Processes component action events sent as requests from the client. Component events include an event type, identify
023 * a page and a component, and may provide additional context strings.
024 *
025 * @see org.apache.tapestry5.services.ComponentEventLinkEncoder
026 */
027public class ComponentEventDispatcher implements Dispatcher
028{
029    private final ComponentRequestHandler componentRequestHandler;
030
031    private final ComponentEventLinkEncoder linkEncoder;
032
033    public ComponentEventDispatcher(ComponentRequestHandler componentRequestHandler,
034                                    ComponentEventLinkEncoder linkEncoder)
035    {
036        this.componentRequestHandler = componentRequestHandler;
037        this.linkEncoder = linkEncoder;
038    }
039
040    public boolean dispatch(Request request, Response response) throws IOException
041    {
042        ComponentEventRequestParameters parameters = linkEncoder.decodeComponentEventRequest(request);
043
044        if (parameters == null) return false;
045
046        componentRequestHandler.handleComponentEvent(parameters);
047
048        return true;
049    }
050}