01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.request.target.basic;
18:
19: import org.apache.wicket.IRequestTarget;
20: import org.apache.wicket.RequestCycle;
21: import org.apache.wicket.Response;
22: import org.apache.wicket.markup.html.pages.RedirectPage;
23:
24: /**
25: * A RequestTarget that will sent a redirect url to the browser. Use this if you
26: * want to direct the browser to some external URL, like Google etc, immediantly.
27: * or if you want to redirect to a Wicket page.
28: *
29: * If you want to redirect with a delay the {@link RedirectPage} will do a meta
30: * tag redirect with a delay.
31: *
32: * @author jcompagner
33: */
34: public class RedirectRequestTarget implements IRequestTarget {
35:
36: private final String redirectUrl;
37:
38: /**
39: * Your URL should be one of the following:
40: * <ul>
41: * <li>Fully qualified "http://foo.com/bar"</li>
42: * <li>Relative to the Wicket filter/servlet, e.g. "?wicket:interface=foo", "mounted_page"</li>
43: * <li>Absolute within your web application's context root, e.g. "/foo.html"</li>
44: * </ul>
45: *
46: * @param redirectUrl URL to redirect to.
47: */
48: public RedirectRequestTarget(String redirectUrl) {
49: this .redirectUrl = redirectUrl;
50:
51: }
52:
53: /**
54: * @see org.apache.wicket.IRequestTarget#detach(org.apache.wicket.RequestCycle)
55: */
56: public void detach(RequestCycle requestCycle) {
57: }
58:
59: /**
60: * @see org.apache.wicket.IRequestTarget#respond(org.apache.wicket.RequestCycle)
61: */
62: public void respond(RequestCycle requestCycle) {
63: Response response = requestCycle.getResponse();
64: response.reset();
65: if (redirectUrl.startsWith("/")) {
66: response.redirect(RequestCycle.get().getRequest()
67: .getRelativePathPrefixToContextRoot()
68: + redirectUrl.substring(1));
69: } else if (redirectUrl.startsWith("http://")
70: || redirectUrl.startsWith("https://")) {
71: response.redirect(redirectUrl);
72: } else {
73: response.redirect(RequestCycle.get().getRequest()
74: .getRelativePathPrefixToWicketHandler()
75: + redirectUrl);
76: }
77: }
78:
79: }
|