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);
      }
    }
  }
  @Override
  public Response getResponse() {
    if (response == null) {
      setResponse(null);
    }

    return response;
  }
  @Override
  public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {

    ResponseBuilder responseBuilder = Response.fromResponse(response.getResponse());

    responseBuilder.header(ACCESS_CONTROL_ALLOW_ORIGIN, this.origin);

    response.setResponse(responseBuilder.build());

    return response;
  }
  @Override
  public ContainerResponse filter(ContainerRequest req, ContainerResponse contResp) {

    LOGGER.info("Enter CORS filter");
    LOGGER.info("Request= { path:" + req.getPath() + ", method:" + req.getMethod() + " }");

    ResponseBuilder resp = Response.fromResponse(contResp.getResponse());
    resp.header("Access-Control-Allow-Origin", "*");
    resp.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");

    String reqHead = req.getHeaderValue("Access-Control-Request-Headers");

    if (null != reqHead && !reqHead.equals(null)) {
      resp.header("Access-Control-Allow-Headers", reqHead);
    }

    contResp.setResponse(resp.build());

    LOGGER.info("Exit CORS filter");

    return contResp;
  }
 /** Reset the response to 204 (No content) with no headers. */
 public void reset() {
   setResponse(Responses.noContent().build());
 }
  public static final void broadcast(
      final AtmosphereResource r, final AtmosphereResourceEvent e, final Broadcaster broadcaster) {
    AtmosphereRequest request = r.getRequest();
    ContainerResponse cr = null;

    // Make sure only one thread can play with the ContainerResponse. Threading issue can arise if
    // there is a scheduler
    // or if ContainerResponse is associated with more than Broadcaster.
    cr = (ContainerResponse) request.getAttribute(FrameworkConfig.CONTAINER_RESPONSE);

    if (cr == null || !r.isSuspended() && !r.getAtmosphereResourceEvent().isResumedOnTimeout()) {
      if (cr == null) {
        logger.warn(
            "Unexpected state. ContainerResponse has been resumed. Caching message {} for {}",
            e.getMessage(),
            r.uuid());
      } else {
        logger.warn("The AtmosphereResource {} hasn't been suspended yet.", r.uuid(), e);
      }

      if (DefaultBroadcaster.class.isAssignableFrom(broadcaster.getClass())) {
        DefaultBroadcaster.class.cast(broadcaster).cacheLostMessage(r, true);
      }
      AtmosphereResourceImpl.class.cast(r)._destroy();
      return;
    }

    synchronized (cr) {
      try {
        // This is required when you change the response's type
        String m = null;

        if (request.getAttribute(FrameworkConfig.EXPECTED_CONTENT_TYPE) != null) {
          m = (String) request.getAttribute(FrameworkConfig.EXPECTED_CONTENT_TYPE);
        }

        if (m == null || m.equalsIgnoreCase("text/event-stream")) {
          if (cr.getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE) != null) {
            m = cr.getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE).toString();
          }

          if (m == null || m.equalsIgnoreCase("application/octet-stream")) {
            m = r.getAtmosphereConfig().getInitParameter(ApplicationConfig.SSE_CONTENT_TYPE);
            if (m == null) {
              m = "text/plain";
            }
          }
        }

        if (e.getMessage() instanceof Response) {
          cr.setResponse((Response) e.getMessage());
          cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m);
          cr.write();
          try {
            cr.getOutputStream().flush();
          } catch (IOException ex) {
            logger.trace("", ex);
          }
        } else if (e.getMessage() instanceof List) {
          for (Object msg : (List<Object>) e.getMessage()) {
            cr.setResponse(Response.ok(msg).build());
            cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m);
            cr.write();
          }

          // https://github.com/Atmosphere/atmosphere/issues/169
          try {
            cr.getOutputStream().flush();
          } catch (IOException ex) {
            logger.trace("", ex);
          }
        } else {
          if (e.getMessage() == null) {
            logger.warn("Broadcasted message is null");
            return;
          }

          cr.setResponse(Response.ok(e.getMessage()).build());
          cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m);
          cr.write();
          try {
            cr.getOutputStream().flush();
          } catch (IOException ex) {
            logger.trace("", ex);
          }
        }
      } catch (Throwable t) {
        boolean notifyAndCache = true;
        logger.trace(
            "Unexpected exception for AtmosphereResource {} and Broadcaster {}",
            r.uuid(),
            broadcaster.getID());
        if (isJetty(r)) {
          for (StackTraceElement element : t.getStackTrace()) {
            if (element.getClassName().equals("java.io.BufferedWriter")
                && element.getMethodName().equals("flush")) {
              logger.trace("Workaround issue https://github.com/Atmosphere/atmosphere/issues/710");
              notifyAndCache = false;
            }
          }
        }

        if (DefaultBroadcaster.class.isAssignableFrom(broadcaster.getClass())) {
          DefaultBroadcaster.class.cast(broadcaster).onException(t, r, notifyAndCache);
        } else {
          onException(t, r);
        }
      } finally {
        if (cr != null) {
          cr.setEntity(null);
        }

        Boolean resumeOnBroadcast =
            (Boolean) request.getAttribute(ApplicationConfig.RESUME_ON_BROADCAST);
        if (resumeOnBroadcast != null && resumeOnBroadcast) {

          String uuid = (String) request.getAttribute(AtmosphereFilter.RESUME_UUID);
          if (uuid != null) {
            if (request.getAttribute(AtmosphereFilter.RESUME_CANDIDATES) != null) {
              ((ConcurrentHashMap<String, AtmosphereResource>)
                      request.getAttribute(AtmosphereFilter.RESUME_CANDIDATES))
                  .remove(uuid);
            }
          }
          r.getRequest().setAttribute(FrameworkConfig.CONTAINER_RESPONSE, null);
          r.resume();
        }
      }
    }
  }