001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket;
018
019import org.apache.wicket.request.IRequestHandler;
020import org.apache.wicket.request.IRequestMapper;
021import org.apache.wicket.request.Request;
022import org.apache.wicket.request.Url;
023import org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler;
024import org.apache.wicket.request.resource.ResourceReference;
025import org.apache.wicket.request.resource.UrlResourceReference;
026import org.apache.wicket.resource.bundles.ResourceBundleReference;
027
028/**
029 * A request mapper that is used to create Url out of UrlResourceReference.
030 * UrlResourceReference works with {@link org.apache.wicket.request.resource.UrlResourceReference.CalculatedUrl} and
031 * thus this mapper should not use {@link org.apache.wicket.SystemMapper.ParentFolderPlaceholderProvider}
032 */
033public class UrlResourceReferenceMapper implements IRequestMapper
034{
035        @Override
036        public Url mapHandler(IRequestHandler requestHandler)
037        {
038                Url url = null;
039                if (requestHandler instanceof ResourceReferenceRequestHandler)
040                {
041                        ResourceReferenceRequestHandler resourceReferenceRequestHandler = (ResourceReferenceRequestHandler) requestHandler;
042                        ResourceReference resourceReference = resourceReferenceRequestHandler.getResourceReference();
043
044                        while (resourceReference instanceof ResourceBundleReference)
045                        {
046                                // unwrap the bundle to render the url for the actual reference
047                                resourceReference = ((ResourceBundleReference)resourceReference).getBundleReference();
048                        }
049
050                        if (resourceReference instanceof UrlResourceReference)
051                        {
052                                UrlResourceReference urlResourceReference = (UrlResourceReference) resourceReference;
053                                url = urlResourceReference.getUrl();
054                        }
055                }
056                return url;
057        }
058
059        @Override
060        public IRequestHandler mapRequest(Request request)
061        {
062                return null;
063        }
064
065        @Override
066        public int getCompatibilityScore(Request request)
067        {
068                return Integer.MIN_VALUE;
069        }
070
071}