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.markup.ComponentTag;
020import org.apache.wicket.markup.MarkupStream;
021import org.apache.wicket.markup.html.WebMarkupContainer;
022import org.apache.wicket.markup.parser.XmlTag.TagType;
023
024/**
025 * A container that renders the content that was bucketed into a certain bucket by
026 * {@link FilteringHeaderResponse}.
027 * 
028 * Note that this container renders only its body by default.
029 * 
030 * @author Jeremy Thomerson
031 */
032public class HeaderResponseContainer extends WebMarkupContainer
033{
034        private static final long serialVersionUID = 1L;
035
036        private final String filterName;
037
038        /**
039         * Construct.
040         * 
041         * @param id
042         *            the wicket id for this container
043         * @param filterName
044         *            the name of the filter that is bucketing stuff to be rendered in this container
045         */
046        public HeaderResponseContainer(String id, String filterName)
047        {
048                super(id);
049                this.filterName = filterName;
050                setRenderBodyOnly(true);
051        }
052
053        @Override
054        protected void onComponentTag(ComponentTag tag)
055        {
056                super.onComponentTag(tag);
057                // force this into an open-close tag rather than a self-closing tag
058                tag.setType(TagType.OPEN);
059        }
060
061        @Override
062        public void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag)
063        {
064                FilteringHeaderResponse response = FilteringHeaderResponse.get();
065                if (!response.isClosed())
066                {
067                        throw new RuntimeException(
068                                "there was an error processing the header response - you tried to render a bucket of response from FilteringHeaderResponse, but it had not yet run and been closed.  this should occur when the header container that is standard in wicket renders, so perhaps you have done something to keep that from rendering?");
069                }
070                CharSequence foot = response.getContent(filterName);
071                replaceComponentTagBody(markupStream, openTag, foot);
072        }
073}