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.beanmodel.services.*;
016import org.apache.tapestry5.http.services.Dispatcher;
017import org.apache.tapestry5.http.services.Request;
018import org.apache.tapestry5.http.services.Response;
019import org.apache.tapestry5.internal.InternalConstants;
020import org.apache.tapestry5.services.ComponentEventLinkEncoder;
021import org.apache.tapestry5.services.ComponentEventRequestParameters;
022import org.apache.tapestry5.services.ComponentRequestHandler;
023
024import java.io.IOException;
025
026/**
027 * Processes component action events sent as requests from the client. Component events include an event type, identify
028 * a page and a component, and may provide additional context strings.
029 *
030 * @see org.apache.tapestry5.services.ComponentEventLinkEncoder
031 */
032public class ComponentEventDispatcher implements Dispatcher
033{
034    private final ComponentRequestHandler componentRequestHandler;
035
036    private final ComponentEventLinkEncoder linkEncoder;
037
038    public ComponentEventDispatcher(ComponentRequestHandler componentRequestHandler,
039                                    ComponentEventLinkEncoder linkEncoder)
040    {
041        this.componentRequestHandler = componentRequestHandler;
042        this.linkEncoder = linkEncoder;
043    }
044
045    public boolean dispatch(Request request, Response response) throws IOException
046    {
047        ComponentEventRequestParameters parameters = linkEncoder.decodeComponentEventRequest(request);
048
049        if (parameters == null) return false;
050
051        // Inside this pipeline, may find that the component id does not exist (this check only occurs in production
052        // mode) ...
053
054        componentRequestHandler.handleComponentEvent(parameters);
055
056        // ... in which case, this attribute is set.
057        if (request.getAttribute(InternalConstants.REFERENCED_COMPONENT_NOT_FOUND) != null) {
058            return false;
059        }
060
061        return true;
062    }
063}