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.filter;
018
019import org.apache.wicket.Application;
020import org.apache.wicket.markup.head.HeaderItem;
021import org.apache.wicket.markup.head.IHeaderResponse;
022import org.apache.wicket.markup.head.ISubresourceHeaderItem;
023import org.apache.wicket.markup.head.IWrappedHeaderItem;
024import org.apache.wicket.markup.head.ResourceAggregator;
025import org.apache.wicket.markup.html.DecoratingHeaderResponse;
026
027/**
028 * Add <em>Subresource<em> integrity and crossOrigin to all {@link ISubresourceHeaderItem}s.
029 * <p>
030 * The subclass implementation of {@link #configure(ISubresourceHeaderItem)} has to decide
031 * on how to configure the item.
032 * <p>
033 * Note: please don't forget to wrap with {@link ResourceAggregator} when setting it up with
034 * {@link Application#setHeaderResponseDecorator}, otherwise dependencies will not be rendered.
035 * 
036 * @see ISubresourceHeaderItem
037 */
038public abstract class SubresourceHeaderResponse extends DecoratingHeaderResponse
039{
040
041        public SubresourceHeaderResponse(IHeaderResponse real)
042        {
043                super(real);
044        }
045
046        @Override
047        public void render(HeaderItem item)
048        {
049                while (item instanceof IWrappedHeaderItem)
050                {
051                        item = ((IWrappedHeaderItem)item).getWrapped();
052                }
053
054                if (item instanceof ISubresourceHeaderItem)
055                {
056                        configure((ISubresourceHeaderItem)item);
057                }
058                
059                super.render(item);
060        }
061
062        /**
063         * Configure the item <em>Subresource</em>.
064         * 
065         * @param item to configure
066         */
067        protected abstract void configure(ISubresourceHeaderItem item);
068}