View Javadoc

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(&quot;list&quot;);
36   * </pre>
37   * 
38   * </p>
39   * <p>
40   * 使用例2 : リダイレクト先を絶対パスで指定
41   * 
42   * <pre>
43   * return new Redirect(&quot;/todo/list&quot;);
44   * </pre>
45   * 
46   * </p>
47   * 
48   * @author baba
49   * @since 1.0.0
50   */
51  public class Redirect extends AbstractActionResult {
52  
53  	private static final Logger logger = Logger.getLogger(Redirect.class);
54  
55  	private final String path;
56  
57  	/**
58  	 * インスタンスを生成します。
59  	 * 
60  	 * @param path
61  	 *            リダイレクト先のパス
62  	 */
63  	public Redirect(final String path) {
64  		this.path = path;
65  	}
66  
67  	/**
68  	 * パスを取得します。
69  	 * 
70  	 * @return パス
71  	 */
72  	public String getPath() {
73  		return this.path;
74  	}
75  
76  	/**
77  	 * {@inheritDoc}
78  	 */
79  	public void execute(final ActionContext context,
80  			final HttpServletRequest request, final HttpServletResponse response)
81  			throws Exception {
82  
83  		final String absolutePath;
84  		final String contextPath;
85  		if ("/".equals(request.getContextPath())) {
86  			contextPath = "";
87  		} else {
88  			contextPath = request.getContextPath();
89  		}
90  		if (this.path.startsWith("/")) {
91  			absolutePath = contextPath + this.path;
92  		} else {
93  			final String actionDirectory = CubbyUtils
94  					.getActionDirectory(context.getComponentDef()
95  							.getComponentClass());
96  			if (StringUtil.isEmpty(actionDirectory)) {
97  				absolutePath = contextPath + "/" + this.path;
98  			} else {
99  				absolutePath = contextPath + "/" + actionDirectory + "/"
100 						+ this.path;
101 			}
102 		}
103 		if (logger.isDebugEnabled()) {
104 			logger.log("DCUB0003", new String[] { absolutePath });
105 		}
106 		response.sendRedirect(absolutePath);
107 	}
108 
109 }