View Javadoc

1   
2   /*
3    * Copyright (C) 2005 by Arno Schumacher
4    *
5    * This file is part of net.sourceforge.servletspy
6    *
7    * net.sourceforge.servletspy is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public 
9    * License as published by the Free Software Foundation; either 
10   * version 2, or (at your option) any later version.
11   *
12   * net.sourceforge.servletspy is distributed in the hope that it will
13   * be useful, but WITHOUT ANY WARRANTY; without even the implied 
14   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
15   * See the GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA 
20   */
21  
22  package net.sourceforge.servletspy.handler.struts;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  import net.sourceforge.servletspy.IContext;
31  import net.sourceforge.servletspy.IContextHandler;
32  import net.sourceforge.servletspy.config.Param;
33  
34  import org.apache.struts.action.ActionErrors;
35  import org.apache.struts.action.DynaActionForm;
36  
37  /***
38   * @author arno schumacher
39   */
40  public class StrutsHandler implements IContextHandler {
41      /*
42       * (non-Javadoc)
43       * 
44       * @see net.sourceforge.servletspy.IContextHandler#handle(net.sourceforge.servletspy.IContext)
45       */
46      public void handle(IContext context) throws Exception {
47          // NP bug in struts
48  
49          if (context.getSubject() instanceof ActionErrors) {
50              final ActionErrors errors = (ActionErrors) context.getSubject();
51              final Iterator it = errors.properties();
52              final Map map = new HashMap();
53  
54              while (it.hasNext()) {
55                  final String key = it.next().toString();
56                  final Object value = errors.get(key);
57                  final Iterator errorIt = (Iterator) value;
58                  final List errorList = new ArrayList();
59  
60                  while (errorIt.hasNext()) {
61                      errorList.add(errorIt.next());
62                  }
63  
64                  map.put(key, errorList);
65              }
66  
67              context.setSubject(map);
68          } else if (context.getSubject() instanceof DynaActionForm) {
69              context
70                      .setSubject(((DynaActionForm) context.getSubject())
71                              .getMap());
72          }
73          context.proceed();
74      }
75  
76      /*
77       * (non-Javadoc)
78       * 
79       * @see net.sourceforge.servletspy.ILifecycle#init(net.sourceforge.servletspy.config.Param[])
80       */
81      public void init(Param[] params) {
82      }
83  
84      /*
85       * (non-Javadoc)
86       * 
87       * @see net.sourceforge.servletspy.ILifecycle#destroy()
88       */
89      public void destroy() {
90      }
91  }