KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > filter > HibernateFilter


1 /*
2  * $Id: HibernateFilter.java,v 1.6 2005/01/17 21:36:05 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.filter;
27
28 import java.io.IOException JavaDoc;
29
30 import javax.servlet.Filter JavaDoc;
31 import javax.servlet.FilterChain JavaDoc;
32 import javax.servlet.FilterConfig JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.ServletRequest JavaDoc;
35 import javax.servlet.ServletResponse JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 import com.j2biz.blogunity.util.HibernateUtil;
42
43 /**
44  * @author michelson
45  * @version $$
46  * @since 0.1
47  *
48  *
49  */

50 public class HibernateFilter implements Filter JavaDoc {
51     /**
52      * Logger for this class
53      */

54     private static final Log log = LogFactory.getLog(HibernateFilter.class);
55
56     /*
57      * (non-Javadoc)
58      *
59      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
60      */

61     public void init(FilterConfig JavaDoc arg0) throws ServletException JavaDoc {
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
68      * javax.servlet.ServletResponse, javax.servlet.FilterChain)
69      */

70     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain)
71             throws IOException JavaDoc, ServletException JavaDoc {
72
73         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
74         if (log.isDebugEnabled()) {
75
76             log.debug("###################################################");
77             log.debug("Path-Translated: " + req.getPathTranslated());
78             log.debug("Context-Path: " + req.getContextPath());
79             log.debug("Path-Info: " + req.getPathInfo());
80             log.debug("Query-String: " + req.getQueryString());
81             log.debug("Request-URI: " + req.getRequestURI());
82             log.debug("Servlet-Path: " + req.getServletPath());
83             log.debug("Request-URL: " + req.getRequestURL());
84             log.debug("###################################################");
85         }
86
87         // >> PATCH for JETTY-Server
88
if (req.getRequestURI().endsWith(".jpg") || req.getRequestURI().endsWith(".jpeg")
89                 || req.getRequestURI().endsWith(".gif") || req.getRequestURI().endsWith(".png")
90                 || req.getRequestURI().endsWith(".css") || req.getRequestURI().endsWith(".htm")
91                 || req.getRequestURI().endsWith(".html")) {
92             chain.doFilter(request, response);
93         } else {
94
95             chain.doFilter(request, response);
96
97             try {
98                 if (log.isDebugEnabled()) {
99                     log.debug("Commiting transaction...");
100                 }
101                 HibernateUtil.commitTransaction();
102             } finally {
103                 HibernateUtil.closeSession();
104             }
105         }
106
107     }
108
109     // public void doFilter(ServletRequest request,
110
// ServletResponse response, FilterChain chain)
111
// throws IOException, ServletException {
112
// if (log.isDebugEnabled()) {
113
// log.debug("doFilter(ServletRequest, ServletResponse, FilterChain) -
114
// start");
115
// }
116
//
117
// // Try to get a Hibernate Session from the HttpSession
118
// ServletContext ctx = BlogunityManager.getServletContext();
119
//
120
// //HttpSession userSession = ((HttpServletRequest)
121
// // request).getSession();
122
//
123
// // Session hibernateSession = (Session)
124
// // userSession.getAttribute(IConstants.Session.HIBERNATE_SESSION);
125
// Session hibernateSession = (Session)
126
// ctx.getAttribute(IConstants.ServletCtx.HIBERNATE_SESSION);
127
//
128
// // and reconnect it to the current thread
129
// if (hibernateSession != null)
130
// HibernateUtil.reconnect(hibernateSession);
131
// try {
132
// HibernateUtil.beginTransaction();
133
//
134
// chain.doFilter(request, response);
135
// // Commit any pending database transaction.
136
// HibernateUtil.commitTransaction();
137
// } finally {
138
// // Disconnect the Session
139
// hibernateSession = HibernateUtil.disconnectSession();
140
// // and store it in the user's HttpSession
141
// // userSession.setAttribute(
142
// // IConstants.Session.HIBERNATE_SESSION,
143
// // hibernateSession);
144
// ctx.setAttribute(IConstants.ServletCtx.HIBERNATE_SESSION,
145
// hibernateSession);
146
// }
147
//
148
// if (log.isDebugEnabled()) {
149
// log.debug("doFilter(ServletRequest, ServletResponse, FilterChain) -
150
// end");
151
// }
152
// }
153

154     /*
155      * (non-Javadoc)
156      *
157      * @see javax.servlet.Filter#destroy()
158      */

159     public void destroy() {
160
161     }
162
163 }
Popular Tags