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.core.request.handler;
018
019import org.apache.wicket.Application;
020import org.apache.wicket.request.IRequestCycle;
021import org.apache.wicket.request.IRequestHandler;
022import org.apache.wicket.request.http.WebResponse;
023
024/**
025 * The empty AJAX request target does output an empty AJAX response.
026 * <br/>
027 * May be used as a light, "do nothing" Ajax response.
028 *
029 * @author Matej Knopp
030 */
031public final class EmptyAjaxRequestHandler implements IRequestHandler
032{
033        /** immutable hashcode. */
034        private static final int HASH = 17 * 1542323;
035
036        /** singleton instance. */
037        private static final EmptyAjaxRequestHandler instance = new EmptyAjaxRequestHandler();
038
039        /**
040         * Construct.
041         */
042        private EmptyAjaxRequestHandler()
043        {
044        }
045
046        /**
047         * Gets the singleton instance.
048         *
049         * @return the singleton instance
050         */
051        public static EmptyAjaxRequestHandler getInstance()
052        {
053                return instance;
054        }
055
056        /** {@inheritDoc} */
057        @Override
058        public void respond(IRequestCycle requestCycle)
059        {
060                WebResponse response = (WebResponse)requestCycle.getResponse();
061                final String encoding = Application.get()
062                        .getRequestCycleSettings()
063                        .getResponseRequestEncoding();
064
065                // Set content type based on markup type for page
066                response.setContentType("text/xml; charset=" + encoding);
067
068                // Make sure it is not cached by a client
069                response.disableCaching();
070
071                response.write("<?xml version=\"1.0\" encoding=\"");
072                response.write(encoding);
073                response.write("\"?><ajax-response></ajax-response>");
074        }
075
076        @Override
077        public boolean equals(Object obj)
078        {
079                return obj instanceof EmptyAjaxRequestHandler;
080        }
081
082        @Override
083        public int hashCode()
084        {
085                return HASH;
086        }
087
088        @Override
089        public String toString()
090        {
091                return "EmptyAjaxRequestTarget";
092        }
093}