title: ExceptionMapper causes "No 'Access-Control-Allow-Origin' header is present on the requested resource" when handling an exception
body: <p>I have an AngularJS client trying to consume a REST Web Service on Wildfly.
  It works when the server returns an object, but when an exception is thrown, I'm getting the following message:    </p>
  
  <blockquote>
    <p>XMLHttpRequest cannot load
    <a href="http://localhost:8080/ProdutosBackend-0.0.1-SNAPSHOT/rest/user/create" rel="nofollow">http://localhost:8080/ProdutosBackend-0.0.1-SNAPSHOT/rest/user/create</a>.
    No 'Access-Control-Allow-Origin' header is present on the requested
    resource. Origin '<a href="http://127.0.0.1:51647" rel="nofollow">http://127.0.0.1:51647</a>' is therefore not allowed
    access. The response had HTTP status code 500.</p>
  </blockquote>
  
  <p>I tried lots of combinations of headers and filters, but nothing can make this works.</p>
  
  <p>Client code:</p>
  
  <pre><code>var user = { 
        email : $scope.signUpData.email,
        password : $scope.signUpData.password,
        name : $scope.signUpData.name
      };
  
  $http.post(
    'http://localhost:8080/ProdutosBackend-0.0.1-SNAPSHOT/rest/user/create',
    user).
    then(function(data) {
      console.log("It works!");
    },
    function(response) {
      console.log(response);
    });
  </code></pre>
  
  <p>Web Service</p>
  
  <pre><code>import javax.servlet.http.HttpServletRequest;
  import javax.ws.rs.Consumes;
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.Context;
  import javax.ws.rs.core.MediaType;
  
  import br.com.produtos.business.UserBO;
  import br.com.produtos.business.exceptions.BusinessException;
  import br.com.produtos.entity.User;
  import br.com.produtos.transaction.Transaction;
  
  @Path("/user")
  public class UserREST {
  
      @POST
      @Path("/create")
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces({ MediaType.APPLICATION_JSON })
      public User createAcount(@Context HttpServletRequest httpServletRequest, User user) throws BusinessException {
          if (user.getEmail().equals("fff")) {
              throw new BusinessException("Bussiness error.");
          }
  
          {...}
  
          return user;
      }
  
  }
  </code></pre>
  
  <p>ExceptionMapper</p>
  
  <pre><code>import javax.ws.rs.core.Response;
  import javax.ws.rs.ext.ExceptionMapper;
  
  public class ThrowableMapper implements ExceptionMapper&lt;Throwable&gt; {
  
      @Override
      public Response toResponse(Throwable throwable) {
          return Response.status(500).entity(throwable.getMessage()).build();
      }
  
  }
  </code></pre>
  
  <p>Application</p>
  
  <pre><code>@ApplicationPath("/rest")
  public class ThisApplication extends Application {
  
      public static CorsFilter cors;
  
      private Set&lt;Object&gt; singletons = new HashSet&lt;Object&gt;();
      private Set&lt;Class&lt;?&gt;&gt; empty = new HashSet&lt;Class&lt;?&gt;&gt;();
  
      public ThisApplication() {
          CorsFilter filter = new CorsFilter();
          filter.getAllowedOrigins().add("*");
  
          cors = filter;
  
          singletons.add(filter);
  
          singletons.add(new ThrowableMapper());
  
          singletons.add(new UserREST());
      }
  
      public Set&lt;Class&lt;?&gt;&gt; getClasses() {
          return empty;
      }
  
      public Set&lt;Object&gt; getSingletons() {
          return singletons;
      }
  
  }
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"&gt;
    &lt;display-name&gt;ProdutosBackend&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
    &lt;context-param&gt;
      &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
      &lt;param-value&gt;br.com.produtos.rest.ThisApplication&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;context-param&gt;
      &lt;param-name&gt;resteasy.servlet.mapping.prefix&lt;/param-name&gt;
      &lt;param-value&gt;/rest&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;listener&gt;
      &lt;listener-class&gt;org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap&lt;/listener-class&gt;
    &lt;/listener&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;Resteasy&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Resteasy&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>