protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    try {
      this.target.handleRequest(request, response);
    } catch (HttpRequestMethodNotSupportedException ex) {
      String[] supportedMethods =
          ((HttpRequestMethodNotSupportedException) ex).getSupportedMethods();
      if (supportedMethods != null) {
        response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
      }
      response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
    }
  }
Esempio n. 2
0
 @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
 @ResponseBody
 @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
 public ErrorDTO processMethodNotSupportedException(
     HttpRequestMethodNotSupportedException exception) {
   return new ErrorDTO(ErrorConstants.ERR_METHOD_NOT_SUPPORTED, exception.getMessage());
 }
 /** Exception thrown when a request handler does not support a specific request method. */
 @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
 public ResponseEntity<Map<String, Object>> handleHttpRequestMethodNotSupportedException(
     HttpRequestMethodNotSupportedException error) {
   Map<String, Object> errorMap = new HashMap<String, Object>();
   errorMap.put("hasErrors", "true");
   errorMap.put(
       "developerMessage",
       error.getMethod()
           + " supports only "
           + error.getSupportedHttpMethods()
           + ", change the request type to "
           + error.getSupportedHttpMethods());
   errorMap.put("userMessage", "");
   errorMap.put("moreInfo", error.getMessage());
   errorMap.put("errorCode", HttpStatus.METHOD_NOT_ALLOWED);
   error.printStackTrace();
   return new ResponseEntity<Map<String, Object>>(errorMap, HttpStatus.METHOD_NOT_ALLOWED);
 }
  /**
   * 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();
  }