use of org.springframework.orm.hibernate5.support.OpenSessionInViewFilter in project opennms by OpenNMS.
the class AbstractSpringJerseyRestTestCase method setUp.
@Before
public void setUp() throws Throwable {
beforeServletStart();
setUser("admin", new String[] { "ROLE_ADMIN" });
DaoTestConfigBean bean = new DaoTestConfigBean();
bean.afterPropertiesSet();
MockDatabase db = new MockDatabase(true);
DataSourceFactory.setInstance(db);
XADataSourceFactory.setInstance(db);
try {
MockFilterConfig filterConfig = new MockFilterConfig(servletContext, "openSessionInViewFilter");
setFilter(new OpenSessionInViewFilter());
getFilter().init(filterConfig);
// Jersey
/*
setServletConfig(new MockServletConfig(servletContext, "dispatcher"));
getServletConfig().addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
getServletConfig().addInitParameter("com.sun.jersey.config.property.packages", "org.codehaus.jackson.jaxrs;org.opennms.web.rest;org.opennms.web.rest.config");
getServletConfig().addInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter");
getServletConfig().addInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter");
setDispatcher(new SpringServlet());
getDispatcher().init(getServletConfig());
*/
// Apache CXF
setServletConfig(new MockServletConfig(servletContext, "dispatcher"));
getServletConfig().addInitParameter("config-location", m_cxfContextPath);
CXFServlet servlet = new CXFServlet();
setDispatcher(servlet);
getDispatcher().init(getServletConfig());
} catch (ServletException se) {
throw se.getRootCause();
}
afterServletStart();
System.err.println("------------------------------------------------------------------------------");
}
use of org.springframework.orm.hibernate5.support.OpenSessionInViewFilter in project opennms by OpenNMS.
the class HttpRemotingContextIT method testLoadContext.
public void testLoadContext() throws Throwable {
DaoTestConfigBean bean = new DaoTestConfigBean();
bean.afterPropertiesSet();
MockDatabase db = new MockDatabase(true);
DataSourceFactory.setInstance(db);
servletContext = new MockServletContext("file:src/main/webapp");
servletContext.addInitParameter("contextConfigLocation", "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml " + "classpath:/META-INF/opennms/applicationContext-soa.xml " + "classpath:/META-INF/opennms/applicationContext-mockDao.xml " + "classpath*:/META-INF/opennms/component-service.xml " + "classpath*:/META-INF/opennms/component-dao.xml " + // Contexts within this project
"/WEB-INF/applicationContext-common.xml " + "/WEB-INF/applicationContext-serviceRegistryRemoting.xml " + "/WEB-INF/applicationContext-spring-security.xml " + "/WEB-INF/applicationContext-svclayer.xml ");
servletContext.addInitParameter("parentContextKey", "daoContext");
ServletContextEvent e = new ServletContextEvent(servletContext);
contextListener = new ContextLoaderListener();
contextListener.contextInitialized(e);
servletContext.setContextPath(contextPath);
servletConfig = new MockServletConfig(servletContext, "dispatcher");
servletConfig.addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
servletConfig.addInitParameter("com.sun.jersey.config.property.packages", "org.opennms.web.rest");
try {
MockFilterConfig filterConfig = new MockFilterConfig(servletContext, "openSessionInViewFilter");
filter = new OpenSessionInViewFilter();
filter.init(filterConfig);
} catch (ServletException se) {
throw se.getRootCause();
}
}
use of org.springframework.orm.hibernate5.support.OpenSessionInViewFilter in project mzzb-server by mingzuozhibi.
the class WebMvcConfig method filterRegistrationBean.
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setUrlPatterns(Collections.singletonList("/api/*"));
bean.setFilter(new OpenSessionInViewFilter());
LOGGER.info("已成功配置OpenSessionInViewFilter");
return bean;
}
use of org.springframework.orm.hibernate5.support.OpenSessionInViewFilter in project spring-framework by spring-projects.
the class OpenSessionInViewFilter method doFilterInternal.
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
SessionFactory sessionFactory = lookupSessionFactory(request);
boolean participate = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
String key = getAlreadyFilteredAttributeName();
if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
// Do not modify the Session: just set the participate flag.
participate = true;
} else {
boolean isFirstRequest = !isAsyncDispatch(request);
if (isFirstRequest || !applySessionBindingInterceptor(asyncManager, key)) {
logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
Session session = openSession(sessionFactory);
SessionHolder sessionHolder = new SessionHolder(session);
TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(sessionFactory, sessionHolder);
asyncManager.registerCallableInterceptor(key, interceptor);
asyncManager.registerDeferredResultInterceptor(key, interceptor);
}
}
try {
filterChain.doFilter(request, response);
} finally {
if (!participate) {
SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
if (!isAsyncStarted(request)) {
logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
SessionFactoryUtils.closeSession(sessionHolder.getSession());
}
}
}
}
Aggregations