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.http.Link;
018import org.apache.tapestry5.http.services.Request;
019import org.apache.tapestry5.ioc.annotations.Primary;
020import org.apache.tapestry5.services.ComponentEventRequestParameters;
021import org.apache.tapestry5.services.PageRenderRequestParameters;
022import org.apache.tapestry5.services.linktransform.ComponentEventLinkTransformer;
023import org.apache.tapestry5.services.linktransform.LinkTransformer;
024import org.apache.tapestry5.services.linktransform.PageRenderLinkTransformer;
025
026public class LinkTransformerImpl implements LinkTransformer
027{
028    private final ComponentEventLinkTransformer componentEventLinkTransformer;
029
030    private final PageRenderLinkTransformer pageRenderLinkTransformer;
031
032    public LinkTransformerImpl(@Primary
033    ComponentEventLinkTransformer componentEventLinkTransformer, @Primary
034    PageRenderLinkTransformer pageRenderLinkTransformer)
035    {
036        this.componentEventLinkTransformer = componentEventLinkTransformer;
037        this.pageRenderLinkTransformer = pageRenderLinkTransformer;
038    }
039
040    public Link transformComponentEventLink(Link defaultLink, ComponentEventRequestParameters parameters)
041    {
042        return or(componentEventLinkTransformer.transformComponentEventLink(defaultLink, parameters), defaultLink);
043    }
044
045    public Link transformPageRenderLink(Link defaultLink, PageRenderRequestParameters parameters)
046    {
047        return or(pageRenderLinkTransformer.transformPageRenderLink(defaultLink, parameters), defaultLink);
048    }
049
050    public ComponentEventRequestParameters decodeComponentEventRequest(Request request)
051    {
052        return componentEventLinkTransformer.decodeComponentEventRequest(request);
053    }
054
055    public PageRenderRequestParameters decodePageRenderRequest(Request request)
056    {
057        return pageRenderLinkTransformer.decodePageRenderRequest(request);
058    }
059
060    private Link or(Link left, Link right)
061    {
062        return left != null ? left : right;
063    }
064
065}