"No context associated with CURRENT request cycle!" when using WroServletContextListener and its helper

417 views
Skip to first unread message

Julien Wajsberg

unread,
May 13, 2012, 8:53:53 AM5/13/12
to wr...@googlegroups.com
Hi,

I'm trying to use the new WroServletContextListener in my taglib (I used to parse wro.xml with a custom implementation).

Basically, I'm trying to have the following informations from the WroModel :
- to know all group names and the ResourceTypes
- to know which files are in a specific group

I want to do this in a ServletContextListener as well to pre-calculate these and other informations and when I do this :

        ServletContextAttributeHelper helper = new ServletContextAttributeHelper(servletContext);
        WroModel model = helper.getManagerFactory().create().getModelFactory().create();

I get the following exception:

- Error while scanning @Inject annotation
ro.isdc.wro.WroRuntimeException: Exception while trying to process @Inject annotation
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:77)
    at ro.isdc.wro.model.group.processor.Injector.inject(Injector.java:48)
    at ro.isdc.wro.model.group.processor.InjectorBuilder$8.create(InjectorBuilder.java:135)
    at ro.isdc.wro.model.group.processor.InjectorBuilder$8.create(InjectorBuilder.java:132)
    at ro.isdc.wro.model.group.processor.Injector.acceptAnnotatedField(Injector.java:117)
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:64)
    at ro.isdc.wro.model.group.processor.Injector.inject(Injector.java:48)
    at ro.isdc.wro.manager.factory.BaseWroManagerFactory$1.initialize(BaseWroManagerFactory.java:109)
    at ro.isdc.wro.manager.factory.BaseWroManagerFactory$1.initialize(BaseWroManagerFactory.java:68)
    at ro.isdc.wro.util.LazyInitializer.get(LazyInitializer.java:33)
    at ro.isdc.wro.manager.factory.BaseWroManagerFactory.create(BaseWroManagerFactory.java:123)
    at ro.isdc.wro.manager.factory.BaseWroManagerFactory.create(BaseWroManagerFactory.java:49)
    at ro.isdc.wro.manager.factory.DefaultWroManagerFactory.create(DefaultWroManagerFactory.java:59)
    at ro.isdc.wro.manager.factory.DefaultWroManagerFactory.create(DefaultWroManagerFactory.java:18)
    at org.everlong.wro4j.taglib.WroConfig.loadConfig(WroConfig.java:60)
    at
org.everlong.wro4j.taglib.WroConfig.getInstance(WroConfig.java:50)
    at
org.everlong.wro4j.taglib.IncludeTag.doTag(IncludeTag.java:35)
    at org.apache.jsp.index_min_jsp._jspx_meth_wro_005fstyle_005f0(index_min_jsp.java:159)
    at org.apache.jsp.index_min_jsp._jspService(index_min_jsp.java:58)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:679)
Caused by: ro.isdc.wro.WroRuntimeException: Exception while trying to process @Inject annotation
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:77)
    at ro.isdc.wro.model.group.processor.Injector.inject(Injector.java:48)
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:73)
    ... 36 more
Caused by: ro.isdc.wro.WroRuntimeException: Exception while trying to process @Inject annotation
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:77)
    at ro.isdc.wro.model.group.processor.Injector.inject(Injector.java:48)
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:73)
    ... 38 more
Caused by: ro.isdc.wro.WroRuntimeException: Exception while trying to process @Inject annotation
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:77)
    at ro.isdc.wro.model.group.processor.Injector.inject(Injector.java:48)
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:73)
    ... 40 more
Caused by: ro.isdc.wro.WroRuntimeException: Exception while trying to process @Inject annotation
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:77)
    at ro.isdc.wro.model.group.processor.Injector.inject(Injector.java:48)
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:73)
    ... 42 more
Caused by: ro.isdc.wro.WroRuntimeException: No context associated with CURRENT request cycle!
    at ro.isdc.wro.config.Context.validateContext(Context.java:125)
    at ro.isdc.wro.config.Context.get(Context.java:106)
    at ro.isdc.wro.model.group.processor.InjectorBuilder$10.create(InjectorBuilder.java:147)
    at ro.isdc.wro.model.group.processor.InjectorBuilder$10.create(InjectorBuilder.java:145)
    at ro.isdc.wro.model.group.processor.Injector.acceptAnnotatedField(Injector.java:117)
    at ro.isdc.wro.model.group.processor.Injector.processInjectAnnotation(Injector.java:64)
    ... 44 more

I tried to use these lines in a ContextServletListener's contextInitialized or in a doTag (so during a request).

What is the correct way to get the WroModel ?

Thanks !
--
Julien

Alex Objelean

unread,
May 13, 2012, 9:12:04 AM5/13/12
to wr...@googlegroups.com
Hi Julien,

I've managed to reproduce this problem too. Apparently the WroManager creation is tightly coupled with Context existence. I'm not yet sure how to overcome this problem, but the simplest solution right now is the following:

- create a filter which set the context before proceeding with the chain & unset it at the end. 
- the filter should be mapped to /*, in order to be sure that all requests can access the context.
The best suited name for this filter is: WroContextFilter. 

Sorry for not noticing this limitation after release.

Thanks,
Alex

Alex Objelean

unread,
May 13, 2012, 9:20:38 AM5/13/12
to wr...@googlegroups.com
Here is the sample implementation of the WroContextFilter: 


/**
 * This filter is responsible for setting the {@link Context} to the current request cycle. This is required if you want
 * to use {@link ServletContextAttributeHelper} in order to access wro related attributes from within a tag or a
 * servlet. Usually this filter will be mapped to all requests:
 * 
 * <pre>
 *  <filter-mapping>
 *     <filter-name>wroContextFilter</filter-name>
 *     <url-pattern>/*</url-pattern>
 *  </filter-mapping>
 * 
 * </pre>
 */
public class WroContextFilter
  implements Filter {
  private FilterConfig filterConfig;
  
  public void init(final FilterConfig filterConfig)
      throws ServletException {
    this.filterConfig = filterConfig;
  }
  
  /**
   * {@inheritDoc}
   */
  public final void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain)
      throws IOException, ServletException {
    final HttpServletRequest request = (HttpServletRequest) req;
    final HttpServletResponse response = (HttpServletResponse) res;
    try {
      // add request, response & servletContext to thread local
      Context.set(Context.webContext(request, response, filterConfig));
      chain.doFilter(Context.get().getRequest(), response);
    } finally {
      Context.unset();
    }
  }

  /**
   * {@inheritDoc}
   */
  public void destroy() {
    Context.destroy();
  }
}

I'll add this filter in the next release and will update the documentation.

Cheers,
Alex

On Sunday, May 13, 2012 3:53:53 PM UTC+3, JulienW wrote:

Julien Wajsberg

unread,
May 13, 2012, 9:47:35 AM5/13/12
to wr...@googlegroups.com
Thanks, I'll try with this and keep you informed :)

BTW, I guess you could just do :

           chain.doFilter(req, res);

Sorry as I should have tested this before the last release.

--
You received this message because you are subscribed to the Google Groups "wro4j" group.
To view this discussion on the web visit https://groups.google.com/d/msg/wro4j/-/F61E7Go1KaIJ.

To post to this group, send email to wr...@googlegroups.com.
To unsubscribe from this group, send email to wro4j+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/wro4j?hl=en.

Alex Objelean

unread,
May 13, 2012, 10:04:19 AM5/13/12
to wr...@googlegroups.com
I agree, the code can be improved. I'm changing it right now. I'll commit the updated version in a separate branch and will let you know.

Thanks,
Alex

Alex Objelean

unread,
May 13, 2012, 10:12:33 AM5/13/12
to wr...@googlegroups.com
A branch called issue431 has been created. You can view the changes here: https://github.com/alexo/wro4j/commit/7c51b1ddc64053d1ed1efad2954f9d4d4549f7b6

Cheers,
Alex
To unsubscribe from this group, send email to wro4j+unsubscribe@googlegroups.com.

Sameer Pawar

unread,
Dec 3, 2013, 9:45:34 AM12/3/13
to wr...@googlegroups.com
Hi,
I am getting similar error.
"No context associated with CURRENT request cycle!"

In web.xml I have 

<filter>
    <filter-name>disableUrlSessionFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
<filter-mapping>
  <filter-name>disableUrlSessionFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
        <filter-name>wroContextFilter</filter-name>
        <filter-class>ro.isdc.wro.http.WroContextFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>wroContextFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error/generic-error.jsp</location>
</error-page>

If I keep 
<wro:style groupNames='error' exploded="false" />
in the template jsp then it works fine. But if I keep it inside generic-error.jsp then it gives "No context associated with CURRENT request cycle!  during server startup.

When I ran server in debug mode then I can see that DelegatingFilterProxy calls 
ro.isdc.wro.config.Context.set(final Context context, final WroConfiguration config) and initialises the CONTEXT_MAP

But it fails when generic-error.jsp is loaded on startup and wro:style tag is evaluated. This time it finds CONTEXT_MAP empty.


To unsubscribe from this group, send email to wro4j+un...@googlegroups.com.

Sameer Pawar

unread,
Dec 3, 2013, 9:57:38 AM12/3/13
to wro4j
I think it is the problem of error page not using filter on start-up. I may be wrong.

regards,
Sameer Pawar


--
You received this message because you are subscribed to a topic in the Google Groups "wro4j" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wro4j/dQf4vnN5Zhg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wro4j+un...@googlegroups.com.

To post to this group, send email to wr...@googlegroups.com.

Alex Objelean

unread,
Dec 3, 2013, 10:07:51 AM12/3/13
to wr...@googlegroups.com
Probably the jsp is evaluated using forward or include request and the context does not survive at that phase. 
Try to update the filter configuration and add the dispatcher elements, like in the example below:

  <filter-mapping>
    <filter-name>wroContextFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>

Let me know if that works.

Cheers,
Alex

To unsubscribe from this group and stop receiving emails from it, send an email to wro4j+un...@googlegroups.com.

To post to this group, send email to wr...@googlegroups.com.

Sameer Pawar

unread,
Dec 3, 2013, 10:20:34 AM12/3/13
to wro4j
Adding error attribute as below fixed it.

<dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>

But I will add other attributes also.


regards,
Sameer Pawar


--
You received this message because you are subscribed to a topic in the Google Groups "wro4j" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wro4j/dQf4vnN5Zhg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wro4j+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages