private void onException(Throwable e, Response r, boolean mapped) {
    if (request.isTracingEnabled()) {
      Response.Status s = Response.Status.fromStatusCode(r.getStatus());
      if (s != null) {
        request.trace(
            String.format(
                "mapped exception to response: %s -> %d (%s)",
                ReflectionHelper.objectToString(e), r.getStatus(), s.getReasonPhrase()));
      } else {
        request.trace(
            String.format(
                "mapped exception to response: %s -> %d",
                ReflectionHelper.objectToString(e), r.getStatus()));
      }
    }

    if (!mapped && r.getStatus() >= 500) {
      logException(e, r, Level.SEVERE);
    } else if (LOGGER.isLoggable(Level.FINE)) {
      logException(e, r, Level.FINE);
    }

    setResponse(r);

    this.mappedThrowable = e;

    if (getEntity() != null && getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE) == null) {
      Object m = request.getProperties().get(HttpMethodRule.CONTENT_TYPE_PROPERTY);
      if (m != null) {
        request.getProperties().remove(HttpMethodRule.CONTENT_TYPE_PROPERTY);
        getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, m);
      }
    }
  }
  /**
   * 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;
  }
  /**
   * Write the response.
   *
   * <p>The status and headers will be written by calling the method {@link
   * ContainerResponseWriter#writeStatusAndHeaders} on the provided {@link ContainerResponseWriter}
   * instance. The {@link OutputStream} returned from that method call is used to write the entity
   * (if any) to that {@link OutputStream}. An appropriate {@link MessageBodyWriter} will be found
   * to write the entity.
   *
   * @throws WebApplicationException if {@link MessageBodyWriter} cannot be found for the entity
   *     with a 500 (Internal Server error) response.
   * @throws java.io.IOException if there is an error writing the entity
   */
  public void write() throws IOException {
    if (isCommitted) return;

    if (request.isTracingEnabled()) {
      configureTrace(responseWriter);
    }

    if (entity == null) {
      isCommitted = true;
      responseWriter.writeStatusAndHeaders(-1, this);
      responseWriter.finish();
      return;
    }

    if (!getHttpHeaders().containsKey(HttpHeaders.VARY)) {
      final String varyHeader = (String) request.getProperties().get(ContainerRequest.VARY_HEADER);
      if (varyHeader != null) {
        getHttpHeaders().add(HttpHeaders.VARY, varyHeader);
      }
    }

    MediaType contentType = getMediaType();
    if (contentType == null) {
      contentType =
          getMessageBodyWorkers()
              .getMessageBodyWriterMediaType(
                  entity.getClass(), entityType, annotations, request.getAcceptableMediaTypes());
      if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype())
        contentType = MediaType.APPLICATION_OCTET_STREAM_TYPE;

      getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentType);
    }

    final MessageBodyWriter p =
        getMessageBodyWorkers()
            .getMessageBodyWriter(entity.getClass(), entityType, annotations, contentType);
    if (p == null) {
      String message =
          "A message body writer for Java class "
              + entity.getClass().getName()
              + ", and Java type "
              + entityType
              + ", and MIME media type "
              + contentType
              + " was not found";
      LOGGER.severe(message);
      Map<MediaType, List<MessageBodyWriter>> m = getMessageBodyWorkers().getWriters(contentType);
      LOGGER.severe(
          "The registered message body writers compatible with the MIME media type are:\n"
              + getMessageBodyWorkers().writersToString(m));

      if (request.getMethod().equals("HEAD")) {
        isCommitted = true;
        responseWriter.writeStatusAndHeaders(-1, this);
        responseWriter.finish();
        return;
      } else {
        throw new WebApplicationException(new MessageException(message), 500);
      }
    }

    final long size = p.getSize(entity, entity.getClass(), entityType, annotations, contentType);
    if (request.getMethod().equals("HEAD")) {
      if (size != -1) getHttpHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(size));
      isCommitted = true;
      responseWriter.writeStatusAndHeaders(0, this);
    } else {
      if (request.isTracingEnabled()) {
        request.trace(
            String.format(
                "matched message body writer: %s, \"%s\" -> %s",
                ReflectionHelper.objectToString(entity),
                contentType,
                ReflectionHelper.objectToString(p)));
      }

      if (out == null) out = new CommittingOutputStream(size);
      p.writeTo(
          entity, entity.getClass(), entityType, annotations, contentType, getHttpHeaders(), out);
      if (!isCommitted) {
        isCommitted = true;
        responseWriter.writeStatusAndHeaders(-1, this);
      }
    }
    responseWriter.finish();
  }