<commit_before>/*******************************************************************************
 * Copyright 2012 Apigee Corporation
 * 
 * Licensed 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.usergrid.rest.utils;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;

public class CORSUtils {

	private static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
	private static final String ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
	private static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
	private static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers";
	private static final String ACCESS_CONTROL_REQUEST_METHOD = "access-control-request-method";
	private static final String ACCESS_CONTROL_REQUEST_HEADERS = "access-control-request-headers";
	private static final String ORIGIN_HEADER = "origin";
	private static final String REFERER_HEADER = "referer";

	public static void allowAllOrigins(HttpServletRequest request,
			HttpServletResponse response) {

		if (request.getHeader(ACCESS_CONTROL_REQUEST_METHOD) != null) {
			@SuppressWarnings("unchecked")
			Enumeration<String> e = request
					.getHeaders(ACCESS_CONTROL_REQUEST_METHOD);
			while (e.hasMoreElements()) {
				String value = e.nextElement();
				response.addHeader(ACCESS_CONTROL_ALLOW_METHODS, value);
			}
		}

		if (request.getHeader(ACCESS_CONTROL_REQUEST_HEADERS) != null) {
			@SuppressWarnings("unchecked")<commit_msg>comments<commit_after>/*******************************************************************************
 * Copyright 2012 Apigee Corporation
 * 
 * Licensed 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.usergrid.rest.utils;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;

/* Note: At one point there was a special case in this code that would
   convert an origin header sent with a string containing the contents
   "null" to an actual null and dealing with that case as if the header
   had not been sent at all. This, however, caused problems with the
   Firefox and Chrome browsers that intend that the "null" be the actual
   origin when the file:// protocol is used and specifically look for it
   in the response. By removing this special case and instead allowing
   the normal processing (ie. allowing "null" to be a valid origin), it
   removed the issue for those browsers. Safari works regardless as it
   actually leaves off the header (a true null). IE still doesn't work
   on file:// regardless for reasons unknown.
*/
public class CORSUtils {

	private static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
	private static final String ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
	private static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
	private static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers";
	private static final String ACCESS_CONTROL_REQUEST_METHOD = "access-control-request-method";
	private static final String ACCESS_CONTROL_REQUEST_HEADERS = "access-control-request-headers";
	private static final String ORIGIN_HEADER = "origin";
	private static final String REFERER_HEADER = "referer";

	public static void allowAllOrigins(HttpServletRequest request,
			HttpServletResponse response) {

		if (request.getHeader(ACCESS_CONTROL_REQUEST_METHOD) != null) {
			@SuppressWarnings("unchecked")
			Enumeration<String> e = request
					.getHeaders(ACCESS_CONTROL_REQUEST_METHOD);
			while (e.hasMoreElements()) {
				String value = e.nextElement();
				response.addHeader(ACCESS_CONTROL_ALLOW_METHODS, value);
			}
		}

		if (request.getHeader(ACCESS_CONTROL_REQUEST_HEADERS) != null) {
			@SuppressWarnings("unchecked")
{'fraction_non_alphanumeric': 0.13063909774436092, 'fraction_numerical': 0.0030075187969924814, 'mean_word_length': 5.3045023696682465, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5104587', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1354, 'n_words': 470}