Skip to content

Instantly share code, notes, and snippets.

Created December 17, 2012 17:09

Revisions

  1. @invalid-email-address Anonymous created this gist Dec 17, 2012.
    27 changes: 27 additions & 0 deletions ApiOriginFilter.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    package com.silverchalice.cors;

    import java.io.IOException;

    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletResponse;

    public class ApiOriginFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse res = (HttpServletResponse) response;
    res.addHeader("Access-Control-Allow-Origin", "*");
    res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
    res.addHeader("Access-Control-Allow-Headers", "Content-Type");
    chain.doFilter(request, response);
    }

    public void init(FilterConfig filterConfig) throws ServletException { }

    public void destroy() { }

    }
    50 changes: 50 additions & 0 deletions web.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>Silver Chalice Web API</display-name>

    <!--
    If no active profile is set via
    -Dspring.profiles.active
    or some other mechanism then this will be used. -->
    <context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>dev</param-value>
    </context-param>

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:META-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Adds CORS Headers to support cross domain requests -->
    <filter>
    <filter-name>ApiOriginFilter</filter-name>
    <filter-class>com.silverchalice.cors.ApiOriginFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>ApiOriginFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
    <servlet-name>spring-mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:META-INF/spring/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring-mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    </web-app>