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.response.filter;
018
019import org.apache.wicket.util.string.AppendingStringBuffer;
020import org.slf4j.Logger;
021import org.slf4j.LoggerFactory;
022
023
024/**
025 * The EmptySrcAttributeFilter checks if an empty src attribute is found in the response.
026 * 
027 * Empty src attribute is problematic as it makes some browsers, i.e. firefox, request an URL twice.
028 * Two issues arise:
029 * 
030 * 1. Unnecessary server load.
031 * 
032 * 2. If the browser only renders the first response, some links in the page might be broken as
033 * wicket rerendered them in the second request and dropped the ones rendered in the first request.
034 */
035public class EmptySrcAttributeCheckFilter implements IResponseFilter
036{
037        private static final Logger log = LoggerFactory.getLogger(EmptySrcAttributeCheckFilter.class);
038
039        /**
040         * Indicates that an empty src attribute is found in the response.
041         */
042        public static final EmptySrcAttributeCheckFilter INSTANCE = new EmptySrcAttributeCheckFilter();
043
044        @Override
045        public AppendingStringBuffer filter(final AppendingStringBuffer responseBuffer)
046        {
047                int pos = responseBuffer.indexOf("src=\"\"");
048                if (pos < 0)
049                {
050                        pos = responseBuffer.indexOf("src=''");
051                        if (pos < 0)
052                        {
053                                pos = responseBuffer.indexOf("src=\"#\"");
054                                if (pos < 0)
055                                {
056                                        pos = responseBuffer.indexOf("src='#'");
057                                }
058                        }
059                }
060                if (pos >= 0)
061                {
062                        log.warn("Empty src attribute found in response:");
063                        int from = Math.max(0, pos - 32);
064                        int to = Math.min(pos + 32, responseBuffer.length());
065                        log.warn("[...]" + responseBuffer.substring(from, to) + "[...]");
066                }
067                return responseBuffer;
068        }
069}