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.head;
018
019import java.util.Arrays;
020import java.util.Collections;
021import java.util.Objects;
022
023import org.apache.wicket.request.Response;
024import org.apache.wicket.request.UrlUtils;
025import org.apache.wicket.request.cycle.RequestCycle;
026import org.apache.wicket.util.string.Strings;
027
028/**
029 * {@link HeaderItem} for script tags that are rendered using a fixed URL, for example resources
030 * from an external site or context relative urls.
031 * 
032 * @author papegaaij
033 */
034public class JavaScriptUrlReferenceHeaderItem extends AbstractJavaScriptReferenceHeaderItem
035{
036        private final String url;
037
038        /**
039         * Creates a new {@code JavaScriptUrlReferenceHeaderItem}.
040         * 
041         * @param url
042         *            context-relative url of the the javascript resource
043         * @param id
044         *            id that will be used to filter duplicate reference (it's still filtered by URL
045         *            too)
046         */
047        public JavaScriptUrlReferenceHeaderItem(String url, String id)
048        {
049                this.url = url;
050                setId(id);
051        }
052
053        /**
054         * @return context-relative url of the the javascript resource
055         */
056        public String getUrl()
057        {
058                return url;
059        }
060
061        @Override
062        public void render(Response response)
063        {
064                internalRenderJavaScriptReference(response,
065                        UrlUtils.rewriteToContextRelative(getUrl(), RequestCycle.get()));
066        }
067
068        @Override
069        public Iterable<?> getRenderTokens()
070        {
071                String url = UrlUtils.rewriteToContextRelative(getUrl(), RequestCycle.get());
072                if (Strings.isEmpty(getId()))
073                        return Collections.singletonList("javascript-" + url);
074                else
075                        return Arrays.asList("javascript-" + getId(), "javascript-" + url);
076        }
077
078        @Override
079        public String toString()
080        {
081                return "JavaScriptUrlReferenceHeaderItem(" + getUrl() + ")";
082        }
083
084        @Override
085        public boolean equals(Object o)
086        {
087                if (this == o) return true;
088                if (o == null || getClass() != o.getClass()) return false;
089                if (!super.equals(o)) return false;
090                JavaScriptUrlReferenceHeaderItem that = (JavaScriptUrlReferenceHeaderItem) o;
091                return Objects.equals(url, that.url);
092        }
093
094        @Override
095        public int hashCode()
096        {
097                // Not using `Objects.hash` for performance reasons
098                int result = super.hashCode();
099                result = 31 * result + (url != null ? url.hashCode() : 0);
100                return result;
101        }
102}