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 java.lang.reflect.Method;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  /**
24   * エラーのレスポンスを返すことを示す {@link ActionResult} です。
25   * <p>
26   * アクションメソッドの戻り値としてこのインスタンスを指定すると、エラーのレスポンスを返します。
27   * </p>
28   * 
29   * @author baba
30   * @since 1.1.0
31   */
32  public class SendError implements ActionResult {
33  
34  	/** ステータスコード。 */
35  	private final int statusCode;
36  
37  	/** メッセージ。 */
38  	private final String message;
39  
40  	/**
41  	 * インスタンスを生成します。
42  	 * 
43  	 * @param statusCode
44  	 *            ステータスコード
45  	 * @see HttpServletResponse#sendError(int)
46  	 */
47  	public SendError(final int statusCode) {
48  		this(statusCode, null);
49  	}
50  
51  	/**
52  	 * インスタンスを生成します。
53  	 * 
54  	 * @param statusCode
55  	 *            ステータスコード
56  	 * @param message
57  	 *            メッセージ
58  	 * @see HttpServletResponse#sendError(int, String)
59  	 */
60  	public SendError(final int statusCode, String message) {
61  		this.statusCode = statusCode;
62  		this.message = message;
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	public void execute(final Action action,
69  			final Class<? extends Action> actionClass, final Method method,
70  			final HttpServletRequest request, final HttpServletResponse response)
71  			throws Exception {
72  		if (message == null) {
73  			response.sendError(statusCode);
74  		} else {
75  			response.sendError(statusCode, message);
76  		}
77  	}
78  
79  }