From bf90a2ddb6724e84244de05bf30d498a65820271 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 14 Sep 2010 06:43:53 +0000 Subject: [PATCH] DispatcherPortlet throws custom NoHandlerFoundException instead of misleading UnavailableException (SPR-7542) --- .../web/portlet/DispatcherPortlet.java | 9 ++-- .../web/portlet/NoHandlerFoundException.java | 53 +++++++++++++++++++ .../mvc/PortletWrappingController.java | 4 +- .../AnnotationMethodHandlerAdapter.java | 8 +-- 4 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/NoHandlerFoundException.java diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/DispatcherPortlet.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/DispatcherPortlet.java index 7525f38ccd4..c57c33703e8 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/DispatcherPortlet.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/DispatcherPortlet.java @@ -39,7 +39,6 @@ import javax.portlet.RenderResponse; import javax.portlet.ResourceRequest; import javax.portlet.ResourceResponse; import javax.portlet.StateAwareResponse; -import javax.portlet.UnavailableException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -51,6 +50,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.core.OrderComparator; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PropertiesLoaderUtils; +import org.springframework.core.style.StylerUtils; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartException; @@ -988,14 +988,13 @@ public class DispatcherPortlet extends FrameworkPortlet { */ protected void noHandlerFound(PortletRequest request, PortletResponse response) throws Exception { if (pageNotFoundLogger.isWarnEnabled()) { - pageNotFoundLogger.warn("No mapping found for current request " + + pageNotFoundLogger.warn("No handler found for current request " + "in DispatcherPortlet with name '" + getPortletName() + "', mode '" + request.getPortletMode() + "', phase '" + request.getAttribute(PortletRequest.LIFECYCLE_PHASE) + - "', session '" + request.getRequestedSessionId() + - "', user '" + getUsernameForRequest(request) + "'"); + "', parameters " + StylerUtils.style(request.getParameterMap())); } - throw new UnavailableException("No handler found for request"); + throw new NoHandlerFoundException("No handler found for portlet request", request); } /** diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/NoHandlerFoundException.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/NoHandlerFoundException.java new file mode 100644 index 00000000000..bd5ab0c3ca7 --- /dev/null +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/NoHandlerFoundException.java @@ -0,0 +1,53 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.web.portlet; + +import javax.portlet.PortletException; +import javax.portlet.PortletRequest; + +import org.springframework.core.style.StylerUtils; + +/** + * Exception to be thrown if DispatcherPortlet is unable to determine + * a corresponding handler for an incoming portlet request. + * + * @author Juergen Hoeller + * @since 3.0.5 + */ +public class NoHandlerFoundException extends PortletException { + + /** + * Constructor for NoHandlerFoundException. + * @param msg the detail message + */ + public NoHandlerFoundException(String msg) { + super(msg); + } + + /** + * Constructor for NoHandlerFoundException. + * @param msg the detail message + * @param request the current portlet request, + * for further context to be included in the exception message + */ + public NoHandlerFoundException(String msg, PortletRequest request) { + super(msg + ": mode '" + request.getPortletMode() + + "', phase '" + request.getAttribute(PortletRequest.LIFECYCLE_PHASE) + + "', parameters " + StylerUtils.style(request.getParameterMap())); + } + +} diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/mvc/PortletWrappingController.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/mvc/PortletWrappingController.java index 880c81b054b..ad8d452ee36 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/mvc/PortletWrappingController.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/mvc/PortletWrappingController.java @@ -37,7 +37,6 @@ import javax.portlet.RenderResponse; import javax.portlet.ResourceRequest; import javax.portlet.ResourceResponse; import javax.portlet.ResourceServingPortlet; -import javax.portlet.UnavailableException; import javax.xml.XMLConstants; import javax.xml.namespace.QName; @@ -45,6 +44,7 @@ import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.web.portlet.ModelAndView; +import org.springframework.web.portlet.NoHandlerFoundException; import org.springframework.web.portlet.context.PortletConfigAware; import org.springframework.web.portlet.context.PortletContextAware; import org.springframework.web.portlet.util.PortletUtils; @@ -190,7 +190,7 @@ public class PortletWrappingController extends AbstractController ResourceRequest request, ResourceResponse response) throws Exception { if (!(this.portletInstance instanceof ResourceServingPortlet)) { - throw new UnavailableException("Cannot handle resource request - target portlet [" + + throw new NoHandlerFoundException("Cannot handle resource request - target portlet [" + this.portletInstance.getClass() + " does not implement ResourceServingPortlet"); } ResourceServingPortlet resourcePortlet = (ResourceServingPortlet) this.portletInstance; diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/mvc/annotation/AnnotationMethodHandlerAdapter.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/mvc/annotation/AnnotationMethodHandlerAdapter.java index 989d33a10fe..46e641bbe4f 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/mvc/annotation/AnnotationMethodHandlerAdapter.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/mvc/annotation/AnnotationMethodHandlerAdapter.java @@ -48,7 +48,6 @@ import javax.portlet.RenderResponse; import javax.portlet.ResourceRequest; import javax.portlet.ResourceResponse; import javax.portlet.StateAwareResponse; -import javax.portlet.UnavailableException; import javax.portlet.WindowState; import javax.servlet.http.Cookie; @@ -62,7 +61,6 @@ import org.springframework.core.LocalVariableTableParameterNameDiscoverer; import org.springframework.core.Ordered; import org.springframework.core.ParameterNameDiscoverer; import org.springframework.core.annotation.AnnotationUtils; -import org.springframework.core.style.StylerUtils; import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.Model; import org.springframework.util.Assert; @@ -88,6 +86,7 @@ import org.springframework.web.context.request.RequestScope; import org.springframework.web.multipart.MultipartRequest; import org.springframework.web.portlet.HandlerAdapter; import org.springframework.web.portlet.ModelAndView; +import org.springframework.web.portlet.NoHandlerFoundException; import org.springframework.web.portlet.bind.MissingPortletRequestParameterException; import org.springframework.web.portlet.bind.PortletRequestDataBinder; import org.springframework.web.portlet.bind.annotation.ActionMapping; @@ -505,10 +504,7 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator } } else { - throw new UnavailableException( - "No matching handler method found for portlet request: mode '" + request.getPortletMode() + - "', phase '" + request.getAttribute(PortletRequest.LIFECYCLE_PHASE) + - "', parameters " + StylerUtils.style(request.getParameterMap())); + throw new NoHandlerFoundException("No matching handler method found for portlet request", request); } }