<commit_before>/*
 *  Copyright WSO2 Inc.
 *
 *  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.wso2.carbon.apimgt.gateway.handlers.security;

import org.apache.axis2.Constants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpStatus;
import org.apache.synapse.ManagedLifecycle;
import org.apache.synapse.MessageContext;
import org.apache.synapse.config.xml.rest.VersionStrategyFactory;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.rest.*;
import org.apache.synapse.rest.dispatch.RESTDispatcher;
import org.wso2.carbon.apimgt.gateway.handlers.Utils;
import org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class CORSRequestHandler extends AbstractHandler implements ManagedLifecycle {

	private static final Log log = LogFactory.getLog(CORSRequestHandler.class);
	private String inline;
	private String allowHeaders;
	private List<String> allowedOrigins;

	public void init(SynapseEnvironment synapseEnvironment) {
		log.debug("Initializing CORSRequest Handler instance");
		if (allowHeaders == null) {
			allowHeaders = Utils
					.getAllowedHeaders();
		}
		if (allowedOrigins == null) {
			allowedOrigins = Arrays.asList(ServiceReferenceHolder.getInstance().getAPIManagerConfiguration().
					getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_ORIGIN).split(","));
		}
	}

	public void destroy() {
		log.debug("Destroying CORSRequest Handler handler instance");
	}

	public boolean handleRequest(MessageContext messageContext) {
		String apiContext = (String) messageContext.getProperty(RESTConstants.REST_API_CONTEXT);
		String apiVersion = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
		String httpMethod = (String) ((Axis2MessageContext) messageContext).getAxis2MessageContext().
				getProperty(Constants.Configuration.HTTP_METHOD);
		API selectedApi = null;
		Resource selectedResource = null;
		boolean status;

		for (API api : messageContext.getConfiguration().getAPIs()) {
			if (apiContext.equals(api.getContext()) && apiVersion.equals(api.getVersion())) {
				selectedApi = api;
				break;
			}
		}
		String subPath;<commit_msg>Changes done after Performance measurement<commit_after>/*
 *  Copyright WSO2 Inc.
 *
 *  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.wso2.carbon.apimgt.gateway.handlers.security;

import org.apache.axis2.Constants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpStatus;
import org.apache.synapse.ManagedLifecycle;
import org.apache.synapse.MessageContext;
import org.apache.synapse.config.xml.rest.VersionStrategyFactory;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.rest.*;
import org.apache.synapse.rest.dispatch.RESTDispatcher;
import org.wso2.carbon.apimgt.gateway.handlers.Utils;
import org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class CORSRequestHandler extends AbstractHandler implements ManagedLifecycle {

	private static final Log log = LogFactory.getLog(CORSRequestHandler.class);
	private String inline;
	private String allowHeaders;
	private List<String> allowedOrigins;
	private boolean headerStatus;
	public void init(SynapseEnvironment synapseEnvironment) {
		if (log.isDebugEnabled()) {
			log.debug("Initializing CORSRequest Handler instance");
		}
		if (ServiceReferenceHolder.getInstance().getApiManagerConfigurationService() != null) {
			headerStatus = initializeHeaders();
		}
	}

	public boolean initializeHeaders() {
		if (allowHeaders == null) {
			allowHeaders = Utils
					.getAllowedHeaders();
		}
		if (allowedOrigins == null) {
			allowedOrigins = Arrays.asList(ServiceReferenceHolder.getInstance().getAPIManagerConfiguration().
					getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_ORIGIN).split(","));
		}
		return true;
	}
	public void destroy() {
		log.debug("Destroying CORSRequest Handler handler instance");
	}

	public boolean handleRequest(MessageContext messageContext) {
		if (!headerStatus) {
			headerStatus = initializeHeaders();
		}
		String apiContext = (String) messageContext.getProperty(RESTConstants.REST_API_CONTEXT);
		String apiVersion = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
		String httpMethod = (String) ((Axis2MessageContext) messageContext).getAxis2MessageContext().
				getProperty(Constants.Configuration.HTTP_METHOD);
		API selectedApi = null;
		Resource selectedResource = null;
		boolean status;

		for (API api : messageContext.getConfiguration().getAPIs()) {
			if (apiContext.equals(api.getContext()) && apiVersion.equals(api.getVersion())) {
				selectedApi = api;
				break;
			}
		}
		String subPath;
{'fraction_non_alphanumeric': 0.09746774964166269, 'fraction_numerical': 0.004777830864787387, 'mean_word_length': 5.187192118226601, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '3409684', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1810, 'n_words': 438}