예제 #1
0
 public boolean executeExceptionMapperForClass(
     HttpRequest request, HttpResponse response, Throwable exception, Class clazz) {
   ExceptionMapper mapper = providerFactory.getExceptionMapper(clazz);
   if (mapper == null) return false;
   writeFailure(request, response, mapper.toResponse(exception));
   return true;
 }
예제 #2
0
  /**
   * Execute an ExceptionMapper if one exists for the given exception. Recurse to base class if not
   * found
   *
   * @param response
   * @param exception
   * @return true if an ExceptionMapper was found and executed
   */
  public boolean executeExceptionMapper(
      HttpRequest request, HttpResponse response, Throwable exception) {
    ExceptionMapper mapper = null;

    Class causeClass = exception.getClass();
    while (mapper == null) {
      if (causeClass == null) break;
      mapper = providerFactory.getExceptionMapper(causeClass);
      if (mapper == null) causeClass = causeClass.getSuperclass();
    }
    if (mapper != null) {
      Response jaxrsResponse = mapper.toResponse(exception);
      if (jaxrsResponse == null) {
        jaxrsResponse = Response.status(204).build();
      }
      writeFailure(request, response, jaxrsResponse);
      return true;
    }
    return false;
  }
  /**
   * Map an exception to a response.
   *
   * @param e the exception.
   * @return true if the exception was mapped, otherwise false.
   */
  public boolean mapException(Throwable e) {
    ExceptionMapper em = wa.getExceptionMapperContext().find(e.getClass());
    if (em == null) {
      wa.getResponseListener().onError(Thread.currentThread().getId(), e);

      return false;
    }

    wa.getResponseListener().onMappedException(Thread.currentThread().getId(), e, em);

    if (request.isTracingEnabled()) {
      request.trace(
          String.format(
              "matched exception mapper: %s -> %s",
              ReflectionHelper.objectToString(e), ReflectionHelper.objectToString(em)));
    }

    try {
      Response r = em.toResponse(e);
      if (r == null) r = Response.noContent().build();
      onException(e, r, true);
    } catch (MappableContainerException ex) {
      // If the exception mapper throws a MappableContainerException then
      // rethrow it to the HTTP container
      throw ex;
    } catch (RuntimeException ex) {
      LOGGER.severe(
          "Exception mapper "
              + em
              + " for Throwable "
              + e
              + " threw a RuntimeException when "
              + "attempting to obtain the response");
      Response r = Response.serverError().build();
      onException(ex, r, false);
    }
    return true;
  }