InterceptorのHttpServletRequestの取得
InterceptorはHotDeployではなくSingletonなのでHttpServletRequestがDIがされないみたいです。いくらdiconとかいらっても無駄みたいです・・・
seasar2の公式ページにdiconファイルを設定してという内容があるので一生懸命弄ったけど無駄だったみたいです・・・
http://www.seasar.org/wiki/index.php?FAQ%2FS2AOP#q69751d4
いくらやっても無駄だったのでスマートではないけれども以下のようにして対応した。
private S2Container container; public void setS2Container(S2Container container){ this.container = container; } private HttpServletRequest getRequestFromContainer() { HttpServletRequest request = (HttpServletRequest)container.getComponent(HttpServletRequest.class); return request; } public final Object handleThrowable(final Exception e, final MethodInvocation invocation) throws Throwable { HttpServletRequest request = getRequestFromContainer(); if (request == null) { e.printStackTrace(); } else { Utils.procException(request, e); return "/error.jsp"; } return invocation.proceed(); }
追記
seasar2のMLに問い合わせてみたところ、以下のようにするといいらしいです。
基本的に「http://www.seasar.org/wiki/index.php?FAQ%2FS2AOP#q69751d4」のやり方なのです。
<components> <include path="default-customizer.dicon"/> <component name="formCustomizer" class="org.seasar.framework.container.customizer.CustomizerChain"/> <component name="actionCustomizer" class="org.seasar.framework.container.customizer.CustomizerChain"> <!-- ここから --> <initMethod name="addCustomizer"> <arg> <component class="org.seasar.framework.container.customizer.AspectCustomizer"> <property name="useLookupAdapter">true</property> <property name="interceptorName">"myThrowableInterceptor"</property> </component> </arg> </initMethod> <!-- ここまで --> <initMethod name="addAspectCustomizer"> <arg>"actionMessagesThrowsInterceptor"</arg> </initMethod>
public class MyThrowableInterceptor extends ThrowsInterceptor { private static final long serialVersionUID = 5306143265122341227L; @Resource private HttpServletRequest request; public final String handleThrowable(final Exception e, final MethodInvocation invocation) throws Throwable { Utils.procException(request, e); return "/error.jsp"; } }
package miraque.utils; public class Utils { public static void procException(final HttpServletRequest request, final Exception e) { // 例外詳細内容を取得 StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); // 呼び出し関数を取得 String[] methods = request.getParameterValues("SAStruts.method"); String method = ""; if (methods != null) { method = "/" + methods[0]; } String body = ""; body += "リクエスト元\n"; body += request.getRequestURI() + method + "\n\n"; body += "リクエスト内容\n"; try { body += Utils.getAllParameter(request); } catch (Exception e1) { e.printStackTrace(); } body += "=====================================================================\n"; try { body += Utils.getAllHeader(request); } catch (Exception e1) { e.printStackTrace(); } body += "=====================================================================\n"; body += sw.toString(); // 例外を標準出力へ e.printStackTrace(); MailTransfer.send(body); } }
それでもって@Aspectをなくするといいらしい。@Aspectでインターセプターを設定すると2回呼び出されてしまい。@Aspectで呼び出されたインターセプターにはHttpServletRequestがNULLになるようです。