0

I am wondering what is the correct way of getting HttpServletRequest object in DWR and in Spring Application - I am trying to do as follows:

private HttpServletRequest getRequest() {
    ServletRequestAttributes servletAttributes =
            (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();

    // this is Spring application's DispatcherServlet call
    if (servletAttributes != null) {
        return servletAttributes.getRequest();
    } else {
        if (WebContextFactory.get() == null) {
            // non-HttpRequest call
            return null;
        } else {
            // dwr call
            return WebContextFactory.get().getHttpServletRequest();
        }
    }
}

I am asking this because when this method is run out of any of http contexts, the methods WebContextFactory logs following warning:

  WARN org.directwebremoting.WebContextFactory:39 - Missing WebContextBuilder. Is DWR setup properly?

I am probably missing method which would tell if this method call is within HttpServletRequest, so I could return directly null value:

 private HttpServletRequest getRequest() {
     // something like this would be ideal:
     if (!ServletContextFactory.isInServletContext()) {
         // method was not called from ServletRequest, so there is none to be found
         return null;
     }
     ...
Vojtěch
  • 11,312
  • 31
  • 103
  • 173

1 Answers1

0

You can use DWR class as spring bean so you do not need a HttpServletRequest.

For example, this is your dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
 "http://getahead.org/dwr/dwr30.dtd">
  <dwr>
   <allow>
    <create creator="spring" javascript="Report">
     <param name="beanName" value="reportController" />
    </create>
   </allow> 
 </dwr>

This is your spring bean

    <bean name="reportController" class="com.repsys.ajax.ReportController">
      <property name="reportService" ref="reportService"></property>
    </bean>
zawhtut
  • 8,335
  • 5
  • 52
  • 76