001// Copyright 2010 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.linktransform;
016
017import org.apache.tapestry5.Link;
018import org.apache.tapestry5.services.ComponentEventLinkEncoder;
019import org.apache.tapestry5.services.ComponentEventRequestParameters;
020import org.apache.tapestry5.services.PageRenderRequestParameters;
021import org.apache.tapestry5.services.Request;
022import org.apache.tapestry5.services.linktransform.LinkTransformer;
023
024/**
025 * Intercepts some methods of {@link ComponentEventLinkEncoder}, passing the returned {@link Link}s
026 * through methods of{@link LinkTransformer}.
027 * 
028 * @since 5.2.0
029 */
030public class LinkTransformerInterceptor implements ComponentEventLinkEncoder
031{
032    private final LinkTransformer linkTransformer;
033
034    private final ComponentEventLinkEncoder delegate;
035
036    public LinkTransformerInterceptor(LinkTransformer linkTransformer, ComponentEventLinkEncoder delegate)
037    {
038        this.linkTransformer = linkTransformer;
039        this.delegate = delegate;
040    }
041
042    public Link createComponentEventLink(ComponentEventRequestParameters parameters, boolean forForm)
043    {
044        return linkTransformer.transformComponentEventLink(delegate.createComponentEventLink(parameters, forForm),
045                parameters);
046    }
047
048    public Link createPageRenderLink(PageRenderRequestParameters parameters)
049    {
050        return linkTransformer.transformPageRenderLink(delegate.createPageRenderLink(parameters), parameters);
051    }
052
053    public ComponentEventRequestParameters decodeComponentEventRequest(Request request)
054    {
055        ComponentEventRequestParameters parameters = linkTransformer.decodeComponentEventRequest(request);
056
057        if (parameters == null)
058            parameters = delegate.decodeComponentEventRequest(request);
059
060        return parameters;
061    }
062
063    public PageRenderRequestParameters decodePageRenderRequest(Request request)
064    {
065        PageRenderRequestParameters parameters = linkTransformer.decodePageRenderRequest(request);
066
067        if (parameters == null)
068            parameters = delegate.decodePageRenderRequest(request);
069
070        return parameters;
071    }
072
073}