Skip to content

Commit

Permalink
Merge pull request #191 from habizzle/wicket-8-improve-wicket-web-ini…
Browse files Browse the repository at this point in the history
…tializer

Improve WicketWebInitializer (Wicket 8)
  • Loading branch information
MarcGiffing authored Oct 24, 2022
2 parents 2632725 + 1280aad commit 23bf020
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package com.giffing.wicket.spring.boot.starter.web;

import java.util.EnumSet;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.wicket.protocol.http.WicketFilter;
import org.apache.wicket.spring.SpringWebApplicationFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.giffing.wicket.spring.boot.context.extensions.boot.actuator.WicketAutoConfig;
import com.giffing.wicket.spring.boot.context.extensions.boot.actuator.WicketEndpointRepository;
import com.giffing.wicket.spring.boot.starter.app.WicketBootWebApplication;
import com.giffing.wicket.spring.boot.starter.web.config.WicketWebInitializerAutoConfig;
import com.giffing.wicket.spring.boot.starter.web.config.WicketWebInitializerConfig;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WicketFilter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.EnumSet;

/**
* Primary container configuration to configure Wicket requests.
Expand All @@ -32,52 +23,36 @@
@Configuration
@Import(value = { WicketWebInitializerAutoConfig.class })
@EnableConfigurationProperties({ WicketWebInitializerProperties.class })
public class WicketWebInitializer implements ServletContextInitializer {
public class WicketWebInitializer {

public static final String WICKET_FILTERNAME = "wicket-filter";

@Autowired
private ApplicationContext applicationContext;

@Autowired
private WicketWebInitializerConfig wicketWebInitializerConfig;

@Autowired
private WicketWebInitializerProperties props;

@Autowired
private WicketEndpointRepository wicketEndpointRepository;

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

String[] beanNamesForType = applicationContext.getBeanNamesForType(WicketBootWebApplication.class);
if(beanNamesForType.length != 1){
throw new IllegalStateException("Could not find exactly one bean for type WicketBootWebApplication " + beanNamesForType.length);
}

FilterRegistration filter = servletContext.addFilter(WICKET_FILTERNAME, wicketWebInitializerConfig.filterClass());
@Bean
public FilterRegistrationBean<WicketFilter> wicketFilter(
WicketWebInitializerConfig wicketWebInitializerConfig,
WicketWebInitializerProperties props,
WicketEndpointRepository wicketEndpointRepository,
WicketBootWebApplication wicketBootWebApplication
) {
WicketFilter wicketFilter = wicketWebInitializerConfig.createWicketFilter((WebApplication) wicketBootWebApplication);

// Spring configuration
filter.setInitParameter(WicketFilter.APP_FACT_PARAM, SpringWebApplicationFactory.class.getName());
filter.setInitParameter("applicationBean", beanNamesForType[0]);
FilterRegistrationBean<WicketFilter> filter = new FilterRegistrationBean<>(wicketFilter);
filter.setName(WICKET_FILTERNAME);
filter.addUrlPatterns(props.getFilterMappingParam());
filter.setDispatcherTypes(EnumSet.copyOf( props.getDispatcherTypes() ));
filter.setMatchAfter(props.isFilterMatchAfter());

filter.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, props.getFilterMappingParam());
filter.addMappingForUrlPatterns(EnumSet.copyOf( props.getDispatcherTypes() ), false, props.getFilterMappingParam());
filter.addInitParameter(WicketFilter.FILTER_MAPPING_PARAM, props.getFilterMappingParam());
props.getInitParameters().forEach(filter::addInitParameter);

Map<String, String> initParameters = props.getInitParameters();
for (Entry<String, String> initParam : initParameters.entrySet()) {
filter.setInitParameter(initParam.getKey(), initParam.getValue());
}


wicketEndpointRepository.add(new WicketAutoConfig.Builder(this.getClass())
.withDetail("wicketFilterName", WICKET_FILTERNAME)
.withDetail("wicketFilterClass", wicketWebInitializerConfig.filterClass())
.withDetail("wicketFilterClass", wicketFilter.getClass())
.withDetail("properties", props)
.build());


return filter;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public class WicketWebInitializerProperties {

private String filterMappingParam = "/*";

//Adds posibility to add init parameters dynamically
// Adds possibility to add init parameters dynamically
private Map<String, String> initParameters = new HashMap<>();

private List<DispatcherType> dispatcherTypes = Arrays.asList( DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.ASYNC );

private boolean filterMatchAfter;

public String getFilterMappingParam() {
return filterMappingParam;
}
Expand All @@ -44,4 +46,12 @@ public List<DispatcherType> getDispatcherTypes() {
public void setDispatcherTypes(List<DispatcherType> dispatcherTypes) {
this.dispatcherTypes = dispatcherTypes;
}

public boolean isFilterMatchAfter() {
return filterMatchAfter;
}

public void setFilterMatchAfter(boolean filterMatchAfter) {
this.filterMatchAfter = filterMatchAfter;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.giffing.wicket.spring.boot.starter.web.config;

import javax.servlet.Filter;

import com.giffing.wicket.spring.boot.starter.web.WicketWebInitializer;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WicketFilter;

/**
* Dynamic configuration support which will be used in {@link WicketWebInitializer}.
Expand All @@ -15,8 +15,8 @@
public interface WicketWebInitializerConfig {

/**
* @return a filter class which will be configured in the {@link WicketWebInitializer}
* @return a {@link WicketFilter} which will be configured in the {@link WicketWebInitializer}
*/
public Class<? extends Filter> filterClass();
WicketFilter createWicketFilter(WebApplication application);

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.giffing.wicket.spring.boot.starter.web.servlet.standard;

import javax.servlet.Filter;

import org.apache.wicket.protocol.http.WicketFilter;

import com.giffing.wicket.spring.boot.starter.web.config.WicketWebInitializerConfig;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WicketFilter;

/**
* The {@link StandardWicketWebInitializer} will be configured when no other
Expand All @@ -15,8 +13,8 @@
public class StandardWicketWebInitializer implements WicketWebInitializerConfig {

@Override
public Class<? extends Filter> filterClass() {
return WicketFilter.class;
public WicketFilter createWicketFilter(WebApplication application) {
return new WicketFilter(application);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.giffing.wicket.spring.boot.starter.web.servlet.websocket;

import javax.servlet.Filter;

import org.apache.wicket.protocol.ws.javax.JavaxWebSocketFilter;

import com.giffing.wicket.spring.boot.starter.web.config.WicketWebInitializerConfig;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WicketFilter;
import org.apache.wicket.protocol.ws.javax.JavaxWebSocketFilter;

/**
* This initializer will be used if the JSR 356 - Java API for WebSocket is
Expand All @@ -13,9 +12,10 @@
* @author Marc Giffing
*/
public class WebSocketWicketWebInitializer implements WicketWebInitializerConfig {

@Override
public Class<? extends Filter> filterClass() {
return JavaxWebSocketFilter.class;
public WicketFilter createWicketFilter(WebApplication application) {
return new JavaxWebSocketFilter(application);
}

}

0 comments on commit 23bf020

Please sign in to comment.