Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can a service factory instantiated bean have access to a httpServletRequest object?
    primarykey
    data
    text
    <p>Ok, I had a bounty about this same subject with only a partial answer so I'm going to open this one and simplify the subject since I guess the original one was way too bloated.</p> <p>I'm facing a situation where I basically have a filter that gets a Spring service factory object from the servlet context, This factory allocates an instance of a bean when the filter calls it's getService method, but I need access the session or the actual httpServletRequest object of the request since I need the userID from the request. I think, from what I have read, that I shouldn't pass it down to the bean not just because it is not thread safe but because it will break the abstraction of the remote services bridge (CP2JavaWS) that owns the filter. </p> <p>How can I get this bean to access the session or the httpServletRequest??</p> <p>I try to use FacesContext but it didn't worked since I think the bean wasn't instantiated by a jsp call but from a filter.</p> <p>Now Some code.</p> <p>this is my web.xml</p> <pre><code>&lt;display-name&gt;CP2JavaWSTest&lt;/display-name&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;index.html&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;error-page&gt; &lt;error-code&gt;404&lt;/error-code&gt; &lt;location&gt;/err.jsp&lt;/location&gt; &lt;/error-page&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/applicationContext*.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.request.RequestContextListener &lt;/listener-class&gt; </code></pre> <p></p> <pre><code>&lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;listener&gt; &lt;listener-class&gt;com.cp2javaws.listeners.SpringContextWrapperListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;listener&gt; &lt;listener-class&gt;com.bucle.listeners.BCLUserDatabaseContextListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;filter&gt; &lt;filter-name&gt;BCLAuthenticationFilter&lt;/filter-name&gt; &lt;filter-class&gt;com.bucle.filters.BCLAuthenticationFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter&gt; &lt;filter-name&gt;BCLAuthorizationFilter&lt;/filter-name&gt; &lt;filter-class&gt;com.bucle.filters.BCLAuthorizationFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter&gt; &lt;filter-name&gt;CPJSonFilter&lt;/filter-name&gt; &lt;filter-class&gt;com.cp2javaws.filters.CPJSonFilter&lt;/filter-class&gt; &lt;/filter&gt; </code></pre> <p>the application context:</p> <pre><code> &lt;bean id="service1" class="com.bucle.database.BCLDb4oManager" scope="request"/&gt; &lt;bean id="service2" class="com.bucle.services.appe.BCLUserCFVC"/&gt; &lt;bean id="service3" class="com.bucle.services.appe.BCLUserCustomizedFutureValueCalculator"/&gt; </code></pre> <p>the CPWJavaWS filter</p> <pre><code>public class CPJSonFilter implements Filter { //.. properties public void init(FilterConfig filterConfig) throws ServletException { //.. init code } public void destroy() { //.. } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpSession session = ((HttpServletRequest)request).getSession(true); //..Class and method decoding and mapping from JSON Class type = null; String value = request.getParameter(CP2JavaWSRequestParameterType+paramOrderString); if(paramName.indexOf(CP2JavaWSRequestGenericParamSuffix)&gt;0 || request.getParameter(CP2JavaWSRequestParameterType+paramOrderString).equals("id")) {//SAMIR type = Object.class; } else { if(paramName.indexOf(CP2JavaWSRequestNullParamSuffix)&gt;0) { String CPClassName = paramName.substring(paramName.indexOf(CP2JavaWSRequestNullParamSuffix)+CP2JavaWSRequestNullParamSuffix.length()); try { type = getJavaClassForCPClass(CPClassName); } catch (CP2JavaWSException e) { throw new ServletException("Cannot find corresponding Java class for null argument at index "+paramOrderString+" (passed CP class name is"+CPClassName+")"); } } else if(List.class.isAssignableFrom(convertedObject.getClass())) { type = List.class; } else if(Map.class.isAssignableFrom(convertedObject.getClass())) { type = Map.class; } else { type = convertedObject.getClass(); } } typesOrderedMap.put(new Integer(paramOrderString), type); } } // invoke the service method using the provided service factory class Object result = null; try { Class serviceInterfaceClass = Class.forName(serviceInterfaceName); ServiceFactory serviceFactory = (ServiceFactory) filterConfig.getServletContext().getAttribute(ServiceFactory.CP2JAVAWS_SERVICES_FACTORY); Object service = serviceFactory.getService(serviceInterfaceClass); Method method = service.getClass().getDeclaredMethod(serviceMethodName, (Class[]) typesOrderedMap.values().toArray(new Class[typesOrderedMap.size()])); result = method.invoke(service, argumentsOrderedMap.values().toArray()); } catch(Exception e) { throw new ServletException("Error invoking the service method :"+serviceMethodName+" on service "+serviceInterfaceName,e); } //...Convert result to JSON response.setContentType("application/json"); PrintWriter writer = response.getWriter(); StringBuffer stb = new StringBuffer(); //... append result and some other stuff to the string buffer stb response.setContentLength(stb.length()); ((HttpServletResponse)response).setStatus(HttpServletResponse.SC_OK); writer.write(stb.toString()); writer.flush(); } public static Class getJavaClassForCPClass(String CPClassName) throws CP2JavaWSException { //... returns the matching Class.class according to a default map like: CPArray -&gt; List.class } } </code></pre> <p>this is the Spring context listener:</p> <pre><code>public class SpringContextWrapperListener implements ServletContextListener { private ServletContext context = null; public void contextDestroyed(ServletContextEvent event) { this.context = null; //log.info("Spring Context Destruido"); } public void contextInitialized(ServletContextEvent event) { this.context = event.getServletContext(); ApplicationContext springContext = (ApplicationContext) WebApplicationContextUtils.getRequiredWebApplicationContext(this.context); SpringContextWrapper aSpringContextWrapper = new SpringContextWrapper(springContext); this.context.setAttribute(ServiceFactory.CP2JAVAWS_SERVICES_FACTORY, aSpringContextWrapper); } } </code></pre> <p>and the factory:</p> <pre><code>public class SpringContextWrapper implements ServiceFactory { private ApplicationContext springContext; public SpringContextWrapper(ApplicationContext springContext) { this.springContext = springContext; } public Object getService(Class serviceInterfaceClass) throws CP2JavaWSException { Map services = this.springContext.getBeansOfType(serviceInterfaceClass); Iterator it = services.values().iterator(); if(it.hasNext()) { return it.next(); } throw new CP2JavaWSException("can't find service for interface "+serviceInterfaceClass.getName()); } public Object getService(String serviceName) throws CP2JavaWSException { Object service = this.springContext.getBean(serviceName); if(service==null) { throw new CP2JavaWSException("can't find service for name "+serviceName); } return service; }} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload