<commit_before>/*******************************************************************************
 * Copyright 2015-2019 Smart Community Lab, FBK
 * 
 *    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 it.smartcommunitylab.aac.common;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;

import it.smartcommunitylab.aac.jaxbmodel.ResourceDeclaration;
import it.smartcommunitylab.aac.jaxbmodel.ResourceMapping;
import it.smartcommunitylab.aac.model.ServiceDescriptor;

/**
 * Common methods and functions
 * @author raman
 *
 */
public class Utils {

	/**
	 * Generate set of strings out of specified delimited string. Remove also leading/trailing spaces around the elements.
	 * @param input
	 * @param delimiter
	 * @return
	 */
	public static Set<String> delimitedStringToSet(String input, String delimiter) {
		HashSet<String> res = new HashSet<String>();
		String[] arr = null;
		if (delimiter != null) {
			arr = input.split(delimiter);
			for (String s : arr) {
				res.add(s.trim());
			}
		}
		return res;
	}
	
	/**
	 * Correct values of the specified comma-separated string: remove redundant spaces
	 * @param in
	 * @return
	 */
	public static String normalizeValues(String in) {
		return StringUtils.trimAllWhitespace(in);
	}
	
	/**
	 * Convert {@link it.smartcommunitylab.aac.jaxbmodel.Service} object
	 * to {@link ServiceDescriptor} persisted entity
	 * @param s
	 * @return converted {@link ServiceDescriptor} entity
	 */
	public static ServiceDescriptor toServiceEntity(it.smartcommunitylab.aac.jaxbmodel.Service s) {
		ObjectMapper mapper = new ObjectMapper();
		ServiceDescriptor res = new ServiceDescriptor();
		res.setDescription(s.getDescription());
		res.setServiceName(s.getName());
		res.setServiceId(s.getId());
		try {
			res.setResourceDefinitions(mapper.writeValueAsString(s.getResource()));
			res.setResourceMappings(mapper.writeValueAsString(s.getResourceMapping()));
		} catch (JsonProcessingException e) {
		}
		res.setApiKey(s.getApiKey());
		return res;
	} 
	/**
	 * Convert {@link ServiceDescriptor} entity to {@link it.smartcommunitylab.aac.jaxbmodel.Service} object
	 * @param s
	 * @return converted {@link it.smartcommunitylab.aac.jaxbmodel.Service} object
	 */
	public static it.smartcommunitylab.aac.jaxbmodel.Service toServiceObject(ServiceDescriptor s) {
		it.smartcommunitylab.aac.jaxbmodel.Service res = new it.smartcommunitylab.aac.jaxbmodel.Service();
		res.setDescription(s.getDescription());
		res.setId(s.getServiceId());
		res.setName(s.getServiceName());
		res.setApiKey(s.getApiKey());
		res.getResource().clear();
		res.getResource().addAll(toObjectList(s.getResourceDefinitions(), ResourceDeclaration.class));
		res.getResourceMapping().clear();
		List<ResourceMapping> resourceMapping = toObjectList(s.getResourceMappings(), ResourceMapping.class);
		res.getResourceMapping().addAll(resourceMapping);
		return res;
	} 
	
	/**
	 * URL for authentication filters of identity providers
	 * @param provider
	 * @return
	 */
	public static String filterRedirectURL(String provider) {
		return "/auth/"+provider+"-oauth/callback";
	}
	
	public static String extractUserFromTenant(String tenant) {
		String un = tenant;
		
		int index = un.indexOf('@');
		int lastIndex = un.lastIndexOf('@');
		
		if (index != lastIndex) {
			un = un.substring(0, lastIndex);
		}
		
		return un;
	}
	public static String getUserNameAtTenant(String username, String tenantName) {
		return username + "@" + tenantName;
	}
	
	public static String[] extractInfoFromTenant(String tenant) {
		int index = tenant.indexOf('@');
		int lastIndex = tenant.lastIndexOf('@');
		
		if (index != lastIndex) {
			String result[] = new String[2];
			result[0] = tenant.substring(0, lastIndex);
			result[1] = tenant.substring(lastIndex + 1, tenant.length());
			return result;
		}
		return new String[] {tenant, "carbon.super"};
	}	
	
	public static String parseHeaderToken(HttpServletRequest request) {
		Enumeration<String> headers = request.getHeaders("Authorization");
		while (headers.hasMoreElements()) { // typically there is only one (most servers enforce that)
			String value = headers.nextElement();
			if ((value.toLowerCase().startsWith(OAuth2AccessToken.BEARER_TYPE.toLowerCase()))) {
				String authHeaderValue = value.substring(OAuth2AccessToken.BEARER_TYPE.length()).trim();
				int commaIndex = authHeaderValue.indexOf(',');
				if (commaIndex > 0) {
					authHeaderValue = authHeaderValue.substring(0, commaIndex);
				}<commit_msg>Update Utils.java<commit_after>/*******************************************************************************
 * Copyright 2015-2019 Smart Community Lab, FBK
 * 
 *    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 it.smartcommunitylab.aac.common;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;

import it.smartcommunitylab.aac.jaxbmodel.ResourceDeclaration;
import it.smartcommunitylab.aac.jaxbmodel.ResourceMapping;
import it.smartcommunitylab.aac.model.ServiceDescriptor;

/**
 * Common methods and functions
 * @author raman
 *
 */
public class Utils {

	/**
	 * Generate set of strings out of specified delimited string. Remove also leading/trailing spaces around the elements.
	 * @param input
	 * @param delimiter
	 * @return
	 */
	public static Set<String> delimitedStringToSet(String input, String delimiter) {
		HashSet<String> res = new HashSet<String>();
		String[] arr = null;
		if (delimiter != null) {
			arr = input.split(delimiter);
			for (String s : arr) {
				res.add(s.trim());
			}
		}
		return res;
	}
	
	/**
	 * Correct values of the specified comma-separated string: remove redundant spaces
	 * @param in
	 * @return
	 */
	public static String normalizeValues(String in) {
		return StringUtils.trimAllWhitespace(in);
	}
	
	/**
	 * Convert {@link it.smartcommunitylab.aac.jaxbmodel.Service} object
	 * to {@link ServiceDescriptor} persisted entity
	 * @param s
	 * @return converted {@link ServiceDescriptor} entity
	 */
	public static ServiceDescriptor toServiceEntity(it.smartcommunitylab.aac.jaxbmodel.Service s) {
		ObjectMapper mapper = new ObjectMapper();
		ServiceDescriptor res = new ServiceDescriptor();
		res.setDescription(s.getDescription());
		res.setServiceName(s.getName());
		res.setServiceId(s.getId());
		try {
			res.setResourceDefinitions(mapper.writeValueAsString(s.getResource()));
			res.setResourceMappings(mapper.writeValueAsString(s.getResourceMapping()));
		} catch (JsonProcessingException e) {
		}
		res.setApiKey(s.getApiKey());
		return res;
	} 
	/**
	 * Convert {@link ServiceDescriptor} entity to {@link it.smartcommunitylab.aac.jaxbmodel.Service} object
	 * @param s
	 * @return converted {@link it.smartcommunitylab.aac.jaxbmodel.Service} object
	 */
	public static it.smartcommunitylab.aac.jaxbmodel.Service toServiceObject(ServiceDescriptor s) {
		it.smartcommunitylab.aac.jaxbmodel.Service res = new it.smartcommunitylab.aac.jaxbmodel.Service();
		res.setDescription(s.getDescription());
		res.setId(s.getServiceId());
		res.setName(s.getServiceName());
		res.setApiKey(s.getApiKey());
		res.getResource().clear();
		res.getResource().addAll(toObjectList(s.getResourceDefinitions(), ResourceDeclaration.class));
		res.getResourceMapping().clear();
		List<ResourceMapping> resourceMapping = toObjectList(s.getResourceMappings(), ResourceMapping.class);
		res.getResourceMapping().addAll(resourceMapping);
		return res;
	} 
	
	/**
	 * URL for authentication filters of identity providers
	 * @param provider
	 * @return
	 */
	public static String filterRedirectURL(String provider) {
		return "/auth/"+provider+"-oauth/callback";
	}
	
	public static String extractUserFromTenant(String tenant) {
		String un = tenant;
		
		int index = un.indexOf('@');
		int lastIndex = un.lastIndexOf('@');
		
		if (index != lastIndex) {
			un = un.substring(0, lastIndex);
		} else if (un.endsWith("@carbon.super")) {
			un = un.substring(0, un.indexOf('@'));
		}
		
		return un;
	}
	public static String getUserNameAtTenant(String username, String tenantName) {
		return username + "@" + tenantName;
	}
	
	public static String[] extractInfoFromTenant(String tenant) {
		int index = tenant.indexOf('@');
		int lastIndex = tenant.lastIndexOf('@');
		
		if (index != lastIndex) {
			String result[] = new String[2];
			result[0] = tenant.substring(0, lastIndex);
			result[1] = tenant.substring(lastIndex + 1, tenant.length());
			return result;
		} else if (tenant.endsWith("@carbon.super")) {
			return tenant.split("@");
		}
		return new String[] {tenant, "carbon.super"};
	}	
	
	public static String parseHeaderToken(HttpServletRequest request) {
		Enumeration<String> headers = request.getHeaders("Authorization");
		while (headers.hasMoreElements()) { // typically there is only one (most servers enforce that)
			String value = headers.nextElement();
			if ((value.toLowerCase().startsWith(OAuth2AccessToken.BEARER_TYPE.toLowerCase()))) {
				String authHeaderValue = value.substring(OAuth2AccessToken.BEARER_TYPE.length()).trim();
				int commaIndex = authHeaderValue.indexOf(',');
				if (commaIndex > 0) {
					authHeaderValue = authHeaderValue.substring(0, commaIndex);
				}
{'fraction_non_alphanumeric': 0.14108889465939584, 'fraction_numerical': 0.0042413226001904265, 'mean_word_length': 4.7368421052631575, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, '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': '4624055', 'n_tokens_mistral': 3648, 'n_tokens_neox': 3253, 'n_words': 800}