AllieCat
AllieCat

Reputation: 3960

CORS support for a Jersey app with WADL

I have a Java Application in Eclipse. Using Jersey, it successfully creates a webpage locally (localhost).

The problem is I have a javascript on another server trying to access that webpage and it throws a CORS error.

Is there any built-in CORS support for Jersey/WADL applications?

I've tried creating a Jersey filter that supports CORS - following these instructions. No luck. :( I'm looking into this, but am unsure if it is the right choice for what I want to do.

Essentially, my header is currently this:

enter image description here

But I want it to look something like this:

enter image description here

Thanks all!

EDIT:

As per jgm's suggestion, I have created a filter (called CORSFilter) and added the requesite dependencies. Am I registering the filter correctly in the web.xml file?

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>createJPA</display-name>
  <servlet>
    <servlet-name>Jersey Root REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
 </servlet>
  <servlet-mapping>
    <servlet-name>Jersey Root REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>CORSFilter</filter-name>
    <filter-class>model.producer.CORSFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>CORSFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Upvotes: 1

Views: 921

Answers (1)

user1596371
user1596371

Reputation:

Here is a simple CORS filter for jersey, adapted from the Weald Technology utilities:

import com.google.inject.Inject;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
import com.wealdtech.jersey.CORSConfiguration;

/**
 * Filter to handle cross-origin resource sharing.
 */
public class CORSFilter implements ContainerResponseFilter
{
  private static final String ACAOHEADER = "Access-Control-Allow-Origin";
  private static final String ACRHHEADER = "Access-Control-Request-Headers";
  private static final String ACAHHEADER = "Access-Control-Allow-Headers";
  private static final String ACAMHEADER = "Access-Control-Allow-Methods";
  private static final String ACACHEADER = "Access-Control-Allow-Credentials";

  private final transient CORSConfiguration configuration;

  @Inject
  public CORSFilter(final CORSConfiguration configuration)
  {
    this.configuration = configuration;
  }

  @Override
  public ContainerResponse filter(final ContainerRequest request, final ContainerResponse response)
  {
    response.getHttpHeaders().add(ACAOHEADER, this.configuration.getOrigin());

    final String requestHeaders = request.getHeaderValue(ACRHHEADER);
    response.getHttpHeaders().add(ACAHHEADER, requestHeaders);
    response.getHttpHeaders().add(ACAMHEADER, this.configuration.getAllowedMethods());
    response.getHttpHeaders().add(ACACHEADER, this.configuration.allowCredentials());

    return response;
  }
}

For the configuration you can either use the existing configuration setup or incorporate it in to your own system. Note that this uses Google Guice to inject the configuration, if you aren't using Guice you'll need to do this manually.

Upvotes: 1

Related Questions