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.Objects;
021
022import org.apache.wicket.request.Response;
023import org.apache.wicket.request.UrlUtils;
024import org.apache.wicket.request.cycle.RequestCycle;
025
026/**
027 * {@link HeaderItem} for style tags that are rendered using a fixed URL, for example resources from
028 * an external site or context relative urls.
029 * 
030 * @author papegaaij
031 */
032public class CssUrlReferenceHeaderItem extends AbstractCssReferenceHeaderItem
033{
034        private static final long serialVersionUID = 1L;
035
036        private final String url;
037
038        /**
039         * Creates a new {@code CSSUrlReferenceHeaderItem}.
040         * 
041         * @param url
042         *            context-relative url of the CSS resource
043         * @param media
044         *            the media type for this CSS ("print", "screen", etc.)
045         * @param rel
046         *            the rel attribute content
047         */
048        public CssUrlReferenceHeaderItem(String url, String media, String rel)
049        {
050                super(media, rel);
051                
052                this.url = url;
053        }
054
055        /**
056         * Creates a new {@code CSSUrlReferenceHeaderItem}.
057         * 
058         * @param url
059         *            context-relative url of the CSS resource
060         * @param media
061         *            the media type for this CSS ("print", "screen", etc.)
062         */
063        public CssUrlReferenceHeaderItem(String url, String media)
064        {
065                super(media, null);
066                
067                this.url = url;
068        }
069
070        /**
071         * @return context-relative url of the CSS resource
072         */
073        public String getUrl()
074        {
075                return url;
076        }
077
078        @Override
079        public void render(Response response)
080        {
081                internalRenderCSSReference(response, UrlUtils.rewriteToContextRelative(getUrl(), RequestCycle.get()));
082        }
083
084        @Override
085        public Iterable<?> getRenderTokens()
086        {
087                return Arrays.asList(
088                        "css-" + UrlUtils.rewriteToContextRelative(getUrl(), RequestCycle.get()) + "-" + getMedia());
089        }
090
091        @Override
092        public String toString()
093        {
094                return "CSSUrlReferenceHeaderItem(" + getUrl() + ")";
095        }
096
097        @Override
098        public int hashCode()
099        {
100                // Not using `Objects.hash` for performance reasons
101                int result = super.hashCode();
102                result = 31 * result + ((url != null) ? url.hashCode() : 0);
103                result = 31 * result + ((getMedia() != null) ? getMedia().hashCode() : 0);
104                result = 31 * result + ((getRel() != null) ? getRel().hashCode() : 0);
105                return result;
106        }
107
108        @Override
109        public boolean equals(Object o)
110        {
111                if (this == o)
112                        return true;
113                if (o == null || getClass() != o.getClass())
114                        return false;
115                if (!super.equals(o))
116                        return false;
117                CssUrlReferenceHeaderItem that = (CssUrlReferenceHeaderItem)o;
118                return Objects.equals(url, that.url) && Objects.equals(getMedia(), that.getMedia()) &&
119                        Objects.equals(getRel(), that.getRel());
120        }
121}