| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with this |
| * work for additional information regarding copyright ownership. The ASF |
| * licenses this file to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.oodt.cas.product.jaxrs.filters; |
| |
| 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.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| import javax.ws.rs.ext.Provider; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * CORS Filter class for Proposing Apache OODT-2.0 FileManager REST-APIs This is the CORS |
| * (Cross-origin Resource Sharing) Filter class for FMProd REST APIs |
| * |
| * @author ngimhana (Nadeeshan Gimhana) |
| */ |
| @Provider |
| public class CORSFilter implements Filter { |
| |
| private static Logger logger = LoggerFactory.getLogger(CORSFilter.class); |
| |
| @Override |
| public void init(FilterConfig filterConfig) throws ServletException {} |
| |
| /** |
| * @param servletRequest |
| * @param servletResponse |
| * @param filterChain |
| * @throws IOException |
| * @throws ServletException |
| */ |
| @Override |
| public void doFilter( |
| ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) |
| throws IOException, ServletException { |
| HttpServletRequest request = (HttpServletRequest) servletRequest; |
| logger.debug("CORSFilter HTTP Request: {}", request.getMethod()); |
| |
| HttpServletResponse resp = (HttpServletResponse) servletResponse; |
| |
| // Authorize (allow) all domains to consume the content |
| resp.addHeader("Access-Control-Allow-Origin", "*"); |
| resp.addHeader("Access-Control-Allow-Methods", "GET, OPTIONS, HEAD, PUT, POST, DELETE"); |
| |
| // For HTTP OPTIONS verb/method reply with ACCEPTED status code -- per CORS handshake |
| if (request.getMethod().equals("OPTIONS")) { |
| resp.setStatus(HttpServletResponse.SC_ACCEPTED); |
| return; |
| } |
| |
| // pass the request along the filter chain |
| filterChain.doFilter(request, servletResponse); |
| } |
| |
| @Override |
| public void destroy() {} |
| } |