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   */
50  public class Redirect extends AbstractActionResult {
51  
52  	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  	public Redirect(final String path) {
63  		this.path = path;
64  	}
65  	
66  	/**
67  	 * パスを取得します。
68  	 * @return パス
69  	 */
70  	public String getPath() {
71  		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  		if ("/".equals(request.getContextPath())) {
81  			contextPath = "";
82  		} else {
83  			contextPath = request.getContextPath();
84  		}
85  		if (this.path.startsWith("/")) {
86  			absolutePath = contextPath + this.path;
87  		} else {
88  			final String actionClassName = CubbyUtils
89  					.getActionClassName(context.getComponentDef()
90  							.getComponentClass());
91  			if (StringUtil.isEmpty(actionClassName)) {
92  				absolutePath = contextPath + "/" + this.path;
93  			} else {
94  				absolutePath = contextPath + "/" + actionClassName + "/"
95  						+ this.path;
96  			}
97  		}
98  		if (logger.isDebugEnabled()) {
99  			logger.log("DCUB0003", new String[] { absolutePath });
100 		}
101 		response.sendRedirect(absolutePath);
102 	}
103 
104 }