/**
   * This method finds the handler for a given request URI, or throws an exception if none were
   * found.
   *
   * <p>It will also ensure that the URI Parameters i.e. /context/test/{name} are added to the
   * request
   *
   * @param request the request for which to find a handler
   * @return The handler that agreed to handle the specified request.
   * @throws NoSuchMethodException if no acceptable handlers could be found
   */
  protected Object getHandler(final MockHttpServletRequest request) throws NoSuchMethodException {
    HandlerExecutionChain chain = null; // NOPMD by jon.adams on 5/14/12

    final Map<String, HandlerMapping> map = applicationContext.getBeansOfType(HandlerMapping.class);
    final Iterator<HandlerMapping> itt = map.values().iterator();

    while (itt.hasNext()) {
      final HandlerMapping mapping = itt.next();

      try {
        chain = mapping.getHandler(request);
      } catch (final HttpRequestMethodNotSupportedException exc) {
        // ignore and try next
        LOGGER.info(
            mapping.getClass().getName()
                + " handler determined it will not handle the request. Message: "
                + exc.getMessage(),
            exc);
      } catch (final Exception exc) {
        throw new RuntimeException(exc); // NOPMD
      }

      if (chain == null) {
        // ignore and try next
        LOGGER.debug(
            mapping.getClass().getName() + " handler determined it will not handle the request.");
      } else {
        // found one. quit looking for more.
        break;
      }
    }

    if (chain == null) {
      throw new NoSuchMethodException(
          "Unable to find handler for request URI: " + request.getRequestURI());
    }

    return chain.getHandler();
  }