examples/iot-nirvana/app-engine/src/main/java/com/google/cloud/demo/iot/nirvana/frontend/shared/CorsFilter.java
/** Add CORS headers to the HTTP response in development environment */
Add CORS headers to the HTTP response in development environment
Add CORS headers to the HTTP response in development environment
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
      throws IOException, ServletException {
    HttpServletResponse httpServletResponse = (HttpServletResponse) resp;
    if (!Utils.isGaeProduction()) {
      httpServletResponse.addHeader("Access-Control-Allow-Origin", GAE_DEV_ADDRESS);
    }
    httpServletResponse.addHeader("Access-Control-Allow-Credentials", "true");
    HttpServletRequest httpServletRequest = (HttpServletRequest) req;
    if (httpServletRequest.getMethod().equals("OPTIONS")) {
      httpServletResponse.setStatus(HttpServletResponse.SC_ACCEPTED);
      return;
    }
    chain.doFilter(req, resp);
  }
public void doFilter ( ServletRequest req , ServletResponse resp , FilterChain chain ) throws IOException , ServletException { HttpServletResponse httpServletResponse = ( HttpServletResponse ) resp ; if ( ! Utils . isGaeProduction ( ) ) { httpServletResponse . addHeader ( "Access-Control-Allow-Origin" , GAE_DEV_ADDRESS ) ; } httpServletResponse . addHeader ( "Access-Control-Allow-Credentials" , "true" ) ; HttpServletRequest httpServletRequest = ( HttpServletRequest ) req ; if ( httpServletRequest . getMethod ( ) . equals ( "OPTIONS" ) ) { httpServletResponse . setStatus ( HttpServletResponse . SC_ACCEPTED ) ; return ; } chain . doFilter ( req , resp ) ; }
Add CORS headers to the HTTP response in development environment
Add CORS headers to the HTTP response in development environment
// Authorize CORS calls to localhost in development environment // Reply to HTTP OPTIONS request with ACCEPTED status // Pass the request along the filter chain