Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for AbstractUrlHandlerMapping to add/remove handlers at runtime #32064

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,14 @@ protected void registerHandler(String[] urlPaths, String beanName) throws BeansE

/**
* Register the specified handler for the given URL path.
* <p>This method may be invoked at runtime after initialization has completed.
* @param urlPath the URL the bean should be mapped to
* @param handler the handler instance or handler bean name String
* (a bean name will automatically be resolved into the corresponding handler bean)
* @throws BeansException if the handler couldn't be registered
* @throws IllegalStateException if there is a conflicting handler registered
*/
protected void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException {
public void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException {
Assert.notNull(urlPath, "URL path must not be null");
Assert.notNull(handler, "Handler object must not be null");
Object resolvedHandler = handler;
Expand Down Expand Up @@ -455,6 +456,39 @@ else if (urlPath.equals("/*")) {
}
}

/**
* Un-register the given mapping.
* <p>This method may be invoked at runtime after initialization has completed.
* @param urlPath the mapping to unregister
*/
public void unregisterHandler(String urlPath) throws IllegalArgumentException {
Assert.notNull(urlPath, "URL path must not be null");
Object mappedHandler = this.handlerMap.get(urlPath);
if (mappedHandler != null) {
if (urlPath.equals("/")) {
if (logger.isTraceEnabled()) {
logger.trace("Unregistered root mapping.");
}
setRootHandler(null);
}
else if (urlPath.equals("/*")) {
if (logger.isTraceEnabled()) {
logger.trace("Unregistered default mapping.");
}
setDefaultHandler(null);
}
else {
if (logger.isTraceEnabled()) {
logger.trace("Unregistered mapping \"" + urlPath + "\"");
}
this.handlerMap.remove(urlPath);
if(getPatternParser() != null) {
this.pathPatternHandlerMap.remove(getPatternParser().parse(urlPath));
}
}
}
}

private String getHandlerDescription(Object handler) {
return (handler instanceof String ? "'" + handler + "'" : handler.toString());
}
Expand Down