This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • SpringBean outside Wicket
Skip to end of metadata
Go to start of metadata

Using @SpringBean beyond Wicket

Sometimes a web application also contains some non-Wicket stuff. For example you might use Restlets. You can still use the @SpringBean annotation in that code. The only prerequisite is that the Wicket application instance is set in a thread local variable. Use the filter given below to accomplish this. Make sure that in web.xml the filter is configured after the Wicket filter. This approach does not work in combination with the Wicket-servlet.

Example web.xml fragment:

  <filter>
    <filter-name>wicket.filter</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>wicket.application.filter</filter-name>
    <filter-class>com.tipspot.site.util.WicketApplicationFilter</filter-class>
    <init-param>
      <param-name>wicketFilterName</param-name>
      <param-value>wicket.filter</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>wicket.filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter-mapping>
    <filter-name>wicket.application.filter</filter-name>
    <url-pattern>/rest/*</url-pattern>
  </filter-mapping>

Here is the filter:

package com.example.wicket.filter;

import org.apache.wicket.Application;
import org.apache.wicket.protocol.http.WebApplication;

import javax.servlet.*;
import java.io.IOException;

/**
 * Filter that sets the wicket application, just like {@link org.apache.wicket.protocol.http.WicketFilter}
 * would do.
 *
 * <p>This implementation assumes that it is placed in a filter chain, <i>after</i> WicketFilter,
 * for URLs that are not handled by the WicketFilter.
 *
 * <p>The filter name of the wicket filter is retrieved from the init parameter wicketFilterName.
 *
 * @author Erik van Oosten
 */
public class WicketApplicationFilter implements Filter {

    private WebApplication webApplication;

    public void init(FilterConfig filterConfig) throws ServletException {
        // Get instance of the Wicket application, it is set by the Wicket filter.
        String contextKey = "wicket:" + filterConfig.getInitParameter("wicketFilterName");
        webApplication = (WebApplication) filterConfig.getServletContext().getAttribute(contextKey);

        if (webApplication == null) {
            throw new ServletException("Could not find the Wicket application, please make " +
                    "sure filter WicketApplicationFilter is embedded in Wicket's filter");
        }
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            // Set the webapplication for this thread
            Application.set(webApplication);
            // for wicket 1.5.x this is:
            // org.apache.wicket.ThreadContext.setApplication(webApplication);
            
            chain.doFilter(request, response);

        } finally {
            // always unset the application thread local
            Application.unset();

            // wicket 1.5.x:
            // org.apache.wicket.ThreadContext.setApplication(null);
        }
    }

    public void destroy() {
        webApplication = null;
    }
}
  • No labels