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.request.IRequestCycle;
020import org.apache.wicket.request.IRequestHandler;
021import org.apache.wicket.request.IRequestHandlerDelegate;
022import org.apache.wicket.request.Request;
023import org.apache.wicket.request.cycle.RequestCycle;
024import org.apache.wicket.util.lang.Args;
025
026/**
027 * A {@link IRequestHandler} that sets the current {@link Request} before delegating the responding
028 * to a wrapped request handler. This is useful when the request received from the browser is not the same
029 * request used to respond, like when the request mapper clones the request with a new URL.
030 *
031 * @author Jesse Long
032 */
033public class RequestSettingRequestHandler implements IRequestHandlerDelegate
034{
035        private final Request request;
036        private final IRequestHandler delegate;
037
038        /**
039         * Creates a new instance
040         * @param request
041         *      The request to use when responding
042         * @param delegate
043         *      The request handler to delegate responding to
044         */
045        public RequestSettingRequestHandler(Request request, IRequestHandler delegate)
046        {
047                this.request = Args.notNull(request, "request");
048                this.delegate = Args.notNull(delegate,"delegate");
049        }
050
051        /**
052         * Returns the request that will be set before responding.
053         * @return the request that will be set before responding.
054         */
055        public Request getRequest()
056        {
057                return request;
058        }
059
060        /**
061         * Returns the request handler to which responding will be delegated.
062         * @return the request handler to which responding will be delegated.
063         */
064        @Override
065        public IRequestHandler getDelegateHandler()
066        {
067                return delegate;
068        }
069
070        @Override
071        public void respond(IRequestCycle requestCycle)
072        {
073                RequestCycle cycle = (RequestCycle) requestCycle;
074                Request originalRequest = cycle.getRequest();
075                try
076                {
077                        cycle.setRequest(request);
078                        delegate.respond(requestCycle);
079                }
080                finally
081                {
082                        cycle.setRequest(originalRequest);
083                }
084        }
085
086        @Override
087        public void detach(IRequestCycle requestCycle)
088        {
089                delegate.detach(requestCycle);
090        }
091}