Set web server headers (Access-Control-Allow-Origin) in web-app running under jetty-runner

1.4k views Asked by At

I'm running the plantuml.war on a server using jetty-runner. I need to enable the Access-Control-Allow-Origin header for CORS, but the way I found to do this points to a web.xml or an override-web.xml, which I think are out of my control because I'm using a third-party web-app in jetty-runner. I can't see a way to set this up in jetty.xml.

Is there a way to enable the Access-Control-Allow-Origin header inside of jetty-runner?

1

There are 1 answers

0
Fuhrmanator On BEST ANSWER

Disclaimer: I got help with the details on this (not my answer 100%). I tested it only under Windows 7 and Windows 8 with Java 7.

In addition to jetty-runner.jar, one needs also jetty-servlets.jar.

Add a file override-web.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">

<filter>
  <filter-name>cross-origin</filter-name>
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
    <init-param>
      <param-name>allowedOrigins</param-name>
      <param-value>*</param-value>
    </init-param>
    <init-param>
      <param-name>allowedMethods</param-name>
      <param-value>GET,POST,OPTIONS,DELETE,PUT,HEAD</param-value>
    </init-param>
    <init-param>
      <param-name>allowedHeaders</param-name>
      <param-value>origin, content-type, accept, authorization</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>cross-origin</filter-name>
    <url-pattern>*</url-pattern>
  </filter-mapping>

</web-app>

Then make a jetty-web.xml context

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="war">/</Set>
  <Set name="overrideDescriptor"><SystemProperty name="jetty.home" default="."/>/override-web.xml</Set>
</Configure>

Run PlantUML's server as follows

java -jar jetty-runner.jar --config jetty.xml --jar jetty-servlets.jar plantuml.war jetty-web.xml