3

I have Apache Wiket + Spring web application that works without any issues . At present Spring used DI framework and to filter Mobile access . We are planning to use Spring Rest in our application , Can you advise me how do I do this in our existing web xml .
Initially Rest Api will be used by existing Web sessions to access data(ui pages using ajax calls ), Therefore I want Rest controllers able to access existing Wicket http session(if used logged in ) . e.g. : rest call from existing http session should be able to access existing http session . Any idea ? Thanks and appriciate your time.

My idea was to use ‘org.springframework.web.servlet.DispatcherServlet’ with a Tag , however my doubt is this way I want be sharing same session ?

My existing web.xml (working)

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:myapp-spring-config.xml
    </param-value>
</context-param>
<filter>
    <filter-name>myapp</filter-name>
    <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
    <init-param>
        <param-name>applicationFactoryClassName</param-name>
        <param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
    </init-param>
</filter>

<filter>
   <filter-name>myappMobileRequestFilter</filter-name>
   <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
   <filter-name>myappMobileRequestFilter</filter-name>
   <url-pattern>/*</url-pattern>
   <dispatcher>REQUEST</dispatcher>
   <dispatcher>ERROR</dispatcher>
</filter-mapping>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<listener>
<listener-class>
    org.springframework.web.context.request.RequestContextListener
</listener-class>

csf
  • 529
  • 1
  • 4
  • 16

2 Answers2

5

Solved for me ! Thanks for martin-g on directing me to WicketSessionFilter . I m posting the answer here for future reference of any one or for suggestion(if needed)..

Changes I/We made in my 'web.xml' addition to above posted web.xml.

  <filter>
        <filter-name>SessionFilter</filter-name>
        <filter-class>org.apache.wicket.protocol.http.servlet.WicketSessionFilter</filter-class>
        <init-param>
            <param-name>filterName</param-name>
            <param-value>myapp</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>SessionFilter</filter-name>
        <url-pattern>/rest/*</url-pattern>
    </filter-mapping>

    <servlet>
            <servlet-name>mvc-dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
            <servlet-name>mvc-dispatcher</servlet-name>
            <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

As you can see I have added a WicketSessionFilter to to filter /rest/* requests which consume by spring consume by spring DispatcherServlet.

and if you want to access spring session in Spring rest controller all you have to do is below e.g :

@RestController
@RequestMapping("/service")
public class TestServiceController {


    @RequestMapping(value = "/getValue/{name}", method = RequestMethod.GET)
    @SuppressWarnings("unused")
    public String getValue(@PathVariable String name,HttpServletRequest request, HttpServletResponse response) {

      HttpSession session = request.getSession(false);
      WebSession wicketSession = session.getAttribute("wicket:wicket.myapp:session");

          //rest of the code here ........
                 return result;
     }

And Spring-rest & Wicket lived happily ever after ....

csf
  • 529
  • 1
  • 4
  • 16
  • Also credit goes to this post too http://stackoverflow.com/questions/15764286/wicket-session-and-jersey-rest-webservice – csf Aug 27 '15 at 11:14
1

Since both Wicket's Filter and Spring's Servlet are in the same web.xml then they will share the same javax.servlet.HttpSession. If you need to be able to reach Wicket's org.apache.wicket.Session in Spring's REST handlers then you will need to setup Wicket's WicketSessionFilter [1].

  1. https://github.com/apache/wicket/blob/8d23f188e8509e4fae945bbfb9710c489c57423c/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/WicketSessionFilter.java
martin-g
  • 17,243
  • 2
  • 23
  • 35
  • Thanks martin-g , do i need at add 'org.springframework.web.servlet.DispatcherServlet' to web.xml ? because i tried doing that , however still i cant see my controller mapping using spring @RestController getting triggered , am i missing something here .. – csf Aug 24 '15 at 09:35