Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Redirect |
|
| 2.3333333333333335;2.333 |
1 | /* | |
2 | * Copyright 2004-2008 the Seasar Foundation and the Others. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
13 | * either express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
15 | */ | |
16 | package org.seasar.cubby.action; | |
17 | ||
18 | import javax.servlet.http.HttpServletRequest; | |
19 | import javax.servlet.http.HttpServletResponse; | |
20 | ||
21 | import org.seasar.cubby.controller.ActionContext; | |
22 | import org.seasar.cubby.util.CubbyUtils; | |
23 | import org.seasar.framework.log.Logger; | |
24 | import org.seasar.framework.util.StringUtil; | |
25 | ||
26 | /** | |
27 | * 指定されたパスにリダイレクトする {@link ActionResult} です。 | |
28 | * <p> | |
29 | * アクションメソッドの戻り値としてこのインスタンスを指定することで、指定されたパスにリダイレクトします。 | |
30 | * </p> | |
31 | * <p> | |
32 | * 使用例1 : リダイレクト先を相対パスで指定 | |
33 | * | |
34 | * <pre> | |
35 | * return new Redirect("list"); | |
36 | * </pre> | |
37 | * | |
38 | * </p> | |
39 | * <p> | |
40 | * 使用例2 : リダイレクト先を絶対パスで指定 | |
41 | * | |
42 | * <pre> | |
43 | * return new Redirect("/todo/list"); | |
44 | * </pre> | |
45 | * | |
46 | * </p> | |
47 | * | |
48 | * @author baba | |
49 | */ | |
50 | public class Redirect extends AbstractActionResult { | |
51 | ||
52 | 1 | private static final Logger logger = Logger.getLogger(Redirect.class); |
53 | ||
54 | private final String path; | |
55 | ||
56 | /** | |
57 | * インスタンスを生成します。 | |
58 | * | |
59 | * @param path | |
60 | * リダイレクト先のパス | |
61 | */ | |
62 | 5 | public Redirect(final String path) { |
63 | 5 | this.path = path; |
64 | 5 | } |
65 | ||
66 | /** | |
67 | * パスを取得します。 | |
68 | * @return パス | |
69 | */ | |
70 | public String getPath() { | |
71 | 1 | return this.path; |
72 | } | |
73 | ||
74 | public void execute(final ActionContext context, | |
75 | final HttpServletRequest request, final HttpServletResponse response) | |
76 | throws Exception { | |
77 | ||
78 | final String absolutePath; | |
79 | final String contextPath; | |
80 | 4 | if ("/".equals(request.getContextPath())) { |
81 | 1 | contextPath = ""; |
82 | } else { | |
83 | 3 | contextPath = request.getContextPath(); |
84 | } | |
85 | 4 | if (this.path.startsWith("/")) { |
86 | 1 | absolutePath = contextPath + this.path; |
87 | } else { | |
88 | 3 | final String actionClassName = CubbyUtils |
89 | .getActionClassName(context.getComponentDef() | |
90 | .getComponentClass()); | |
91 | 3 | if (StringUtil.isEmpty(actionClassName)) { |
92 | 0 | absolutePath = contextPath + "/" + this.path; |
93 | } else { | |
94 | 3 | absolutePath = contextPath + "/" + actionClassName + "/" |
95 | + this.path; | |
96 | } | |
97 | } | |
98 | 4 | if (logger.isDebugEnabled()) { |
99 | 4 | logger.log("DCUB0003", new String[] { absolutePath }); |
100 | } | |
101 | 4 | response.sendRedirect(absolutePath); |
102 | 4 | } |
103 | ||
104 | } |