Skip to content

Commit

Permalink
Allow AbstractUrlHandlerMapping to add/remote handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
awmeinema authored and snicoll committed Apr 30, 2024
1 parent a7ceedf commit 109d985
Showing 1 changed file with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,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 @@ -456,6 +457,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

0 comments on commit 109d985

Please sign in to comment.