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