View Javadoc

1   package net.trajano.twiff.internal.servlet;
2   
3   import static net.trajano.twiff.Twiff.TOKEN_RESUBMIT_FIELD_NAME;
4   import java.io.Serializable;
5   import java.lang.reflect.InvocationTargetException;
6   import javax.servlet.ServletException;
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpSession;
9   import net.trajano.twiff.MissingSessionException;
10  import net.trajano.twiff.adapter.RequestAdapter;
11  import net.trajano.twiff.internal.ServletContextAdapterImpl;
12  import net.trajano.twiff.internal.token.dao.SessionTokenDao;
13  import net.trajano.twiff.internal.util.InitParamUtil;
14  import org.apache.commons.beanutils.BeanUtils;
15  import org.hibernate.Session;
16  import org.hibernate.Transaction;
17  
18  /***
19   * This adapts a request so {@link net.trajano.twiff.web.ViewServlet} and
20   * {@link net.trajano.twiff.web.AttachmentServlet} can use it in a more type
21   * safe manner.
22   * 
23   * @author Archimedes Trajano
24   */
25  public class ViewServletRequestAdapter extends RequestAdapter {
26      /***
27       * Constructs the adapter.
28       * 
29       * @param request
30       *                   servlet request.
31       * @param tokenFieldName
32       *                   the name of the field that contains the token ID.
33       */
34      public ViewServletRequestAdapter(final HttpServletRequest request, final String tokenFieldName) {
35          super(request);
36          this.request = request;
37          tokenId = request.getParameter(tokenFieldName);
38          tokenExist = (tokenId != null);
39      }
40  
41      /***
42       * Token Id.
43       */
44      private final String tokenId;
45  
46      /***
47       * The servlet request.
48       */
49      private final HttpServletRequest request;
50  
51      /***
52       * Existence of the token Id.
53       */
54      private final boolean tokenExist;
55  
56      /***
57       * @return Returns the tokenExist.
58       */
59      public final boolean isTokenExist() {
60          return tokenExist;
61      }
62  
63      /***
64       * @return Returns the tokenId.
65       */
66      public final String getTokenId() {
67          return tokenId;
68      }
69  
70      /***
71       * Registeres the class into the {@link org.picocontainer.PicoContainer}
72       * stored in the request.
73       * 
74       * @param pageBeanClass
75       *                   the page bean class.
76       * @throws ServletException
77       */
78      public void registerComponentImplementation(final Class pageBeanClass) throws ServletException {
79          if (isTokenExist()) {
80              final HttpSession httpSession = request.getSession(false);
81              if (httpSession == null) {
82                  throw new MissingSessionException();
83              }
84              ServletContextAdapterImpl servletContextAdapter = new ServletContextAdapterImpl(httpSession.getServletContext());
85              Session session = null;
86              try {
87                  session = servletContextAdapter.getSessionFactory().openSession();
88                  Transaction tx = session.beginTransaction();
89                  SessionTokenDao dao = new SessionTokenDao(session);
90                  final Serializable pageBean = dao.getPageBean(request.getSession().getId(), getTokenId());
91                  if (dao.getRequestCount(request.getSession().getId(), getTokenId()) > 1) {
92                      request.setAttribute(InitParamUtil.getInitParam(request.getSession(false).getServletContext(), TOKEN_RESUBMIT_FIELD_NAME), true);
93                  }
94                  if (pageBean.getClass() == pageBeanClass) {
95                      getContainer().registerComponentInstance(pageBean);
96                  } else {
97                      getContainer().registerComponentImplementation(pageBeanClass);
98                      final Object object = getContainer().getComponentInstanceOfType(pageBeanClass);
99                      BeanUtils.copyProperties(object, pageBean);
100                     getContainer().unregisterComponent(pageBeanClass);
101                     getContainer().registerComponentInstance(pageBeanClass, object);
102                 }
103                 tx.commit();
104             } catch (IllegalAccessException e) {
105                 throw new ServletException(e);
106             } catch (InvocationTargetException e) {
107                 throw new ServletException(e);
108             } finally {
109                 if (session != null) {
110                     session.close();
111                 }
112             }
113         } else {
114             getContainer().registerComponentImplementation(pageBeanClass);
115         }
116     }
117 }