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.markup.html.image;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.behavior.Behavior;
021import org.apache.wicket.markup.ComponentTag;
022import org.apache.wicket.model.IModel;
023import org.apache.wicket.model.Model;
024import org.apache.wicket.request.UrlUtils;
025import org.apache.wicket.request.cycle.RequestCycle;
026
027/**
028 * A behavior that converts the provider url fragment to a context-relative url. For example if the
029 * current url is <code>http://localhost/context/product/1231</code> and the specified url is
030 * <code>images/border.jpg</code> the generated url will be <code>../../images/border.jpg</code>
031 * 
032 * @author Igor Vaynberg (ivaynberg)
033 */
034public class ContextPathGenerator extends Behavior
035{
036        private static final long serialVersionUID = 1L;
037
038        private final IModel<String> contextRelativePath;
039
040        /**
041         * Constructor
042         * 
043         * @param contextRelativePath
044         *            context-relative path, eg <code>images/border.jpg</code>
045         */
046        public ContextPathGenerator(IModel<String> contextRelativePath)
047        {
048                this.contextRelativePath = contextRelativePath;
049        }
050
051        /**
052         * Constructor
053         * 
054         * @param contextRelativePath
055         *            context-relative path, eg <code>images/border.jpg</code>
056         */
057        public ContextPathGenerator(String contextRelativePath)
058        {
059                this.contextRelativePath = new Model<String>(contextRelativePath);
060        }
061
062        /** {@inheritDoc} **/
063        @Override
064        public void onComponentTag(Component component, ComponentTag tag)
065        {
066                // get path
067                final String path = contextRelativePath.getObject();
068
069                final String rewritten = UrlUtils.rewriteToContextRelative(path, RequestCycle.get());
070
071                tag.put("src", rewritten);
072        }
073
074        /** {@inheritDoc} **/
075        @Override
076        public void detach(Component component)
077        {
078                contextRelativePath.detach();
079                super.detach(component);
080        }
081
082
083}