<commit_before>package br.com.dbsoft.util;

import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;

import br.com.dbsoft.core.DBSSDK.ENCODE;
import br.com.dbsoft.error.DBSIOException;
import br.com.dbsoft.util.DBSObject;

public class DBSHttp {

	private static Logger wLogger = Logger.getLogger(DBSHttp.class);

	/**
	 * Retorna o response da conexão web atual
	 * @return
	 */
	public static HttpServletResponse getHttpServletResponse(){ 		
		FacesContext xContext = FacesContext.getCurrentInstance();
		return (HttpServletResponse) xContext.getExternalContext().getResponse();
	}

	public static HttpServletRequest getHttpServletRequest(){ 		
		FacesContext xContext = FacesContext.getCurrentInstance();
		return (HttpServletRequest) xContext.getExternalContext().getRequest();
	}
	
	/**
	 * Evia um arquivo local para o browser
	 */
	public static Boolean sendFile(ByteArrayOutputStream pByteArrayOutputStream, String pRemoteFileName, String pContentType){
		FacesContext 	xFC = FacesContext.getCurrentInstance();
		ExternalContext xEC = xFC.getExternalContext();
		
		//Verifica se arquivo existe. Se não existir...
		try {
			//Se o arquivo já existir apenas entrega-o para o browser
			if (!DBSObject.isEmpty(pByteArrayOutputStream)) {
				xEC.responseReset();
				xEC.setResponseContentType(pContentType);
				xEC.setResponseContentLength(pByteArrayOutputStream.size());
				xEC.setResponseHeader("Content-Disposition", "attachment;filename=\"" + pRemoteFileName + "\"");
				OutputStream xOutputStream = xEC.getResponseOutputStream();
				if (xOutputStream != null){
					pByteArrayOutputStream.writeTo(xOutputStream);
				}
				xFC.responseComplete();
				return true;
			}
			return false;
		} catch (IOException e) {
			wLogger.error(e);
			return false;
		}			
	}
	
	/**
	 * Retorna o caminho real do web-info/classes da aplicação
	 * @param pExternalContext
	 * @return
	 */
	public static String getRealPathWebInfClasses(ExternalContext pExternalContext){
		return getRealPath(pExternalContext, "WEB-INF" + File.separator + "classes" + File.separator);
	}
	
	/**
	 * Retorna o caminho real a partir de caminho virtual
	 * @param pRelativePath
	 * @return
	 */
	public static String getRealPath(ExternalContext pExternalContext, String pRelativePath){
		return pExternalContext.getRealPath(pRelativePath);
	}

	/**
	 * Retorna caminho da URL do servidor local a partir do ExternalContext
	 * @return
	 */
	public static String getHTTPServerPath(ExternalContext pExternalContext){
		if (pExternalContext == null){return "";}
		StringBuilder xLink = new StringBuilder();
		xLink.append(pExternalContext.getRequestScheme()).append("://");
		xLink.append(pExternalContext.getRequestServerName());
		if (!DBSObject.isEmpty(pExternalContext.getRequestServerPort())){
			xLink.append(":").append(pExternalContext.getRequestServerPort());
		}
//		xLink += xEC.getRequestContextPath();
		return xLink.toString();
	}
	
	
	/**
	 * Retorna URL do servidor local a partir doe um request
	 * @param pContext
	 * @return
	 */
	public static String getHTTPServerPath(HttpServletRequest pContext){
		if (pContext == null){return "";}
		StringBuilder xLink = new StringBuilder();
		xLink.append(pContext.getScheme()).append("://");
		xLink.append(pContext.getServerName());
		if (!DBSObject.isEmpty(pContext.getServerPort())){
			xLink.append(":").append(pContext.getServerPort());
		}
		return xLink.toString();
	}
	/**<commit_msg>Alteração do nome do método getHTTPServerPath para getServerURLString<commit_after>package br.com.dbsoft.util;

import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;

import br.com.dbsoft.core.DBSSDK.ENCODE;
import br.com.dbsoft.error.DBSIOException;
import br.com.dbsoft.util.DBSObject;

public class DBSHttp {

	private static Logger wLogger = Logger.getLogger(DBSHttp.class);

	/**
	 * Retorna o response da conexão web atual
	 * @return
	 */
	public static HttpServletResponse getHttpServletResponse(){ 		
		FacesContext xContext = FacesContext.getCurrentInstance();
		return (HttpServletResponse) xContext.getExternalContext().getResponse();
	}

	public static HttpServletRequest getHttpServletRequest(){ 		
		FacesContext xContext = FacesContext.getCurrentInstance();
		return (HttpServletRequest) xContext.getExternalContext().getRequest();
	}
	
	/**
	 * Evia um arquivo local para o browser
	 */
	public static Boolean sendFile(ByteArrayOutputStream pByteArrayOutputStream, String pRemoteFileName, String pContentType){
		FacesContext 	xFC = FacesContext.getCurrentInstance();
		ExternalContext xEC = xFC.getExternalContext();
		
		//Verifica se arquivo existe. Se não existir...
		try {
			//Se o arquivo já existir apenas entrega-o para o browser
			if (!DBSObject.isEmpty(pByteArrayOutputStream)) {
				xEC.responseReset();
				xEC.setResponseContentType(pContentType);
				xEC.setResponseContentLength(pByteArrayOutputStream.size());
				xEC.setResponseHeader("Content-Disposition", "attachment;filename=\"" + pRemoteFileName + "\"");
				OutputStream xOutputStream = xEC.getResponseOutputStream();
				if (xOutputStream != null){
					pByteArrayOutputStream.writeTo(xOutputStream);
				}
				xFC.responseComplete();
				return true;
			}
			return false;
		} catch (IOException e) {
			wLogger.error(e);
			return false;
		}			
	}
	
	/**
	 * Retorna o caminho real do web-info/classes da aplicação
	 * @param pExternalContext
	 * @return
	 */
	public static String getRealPathWebInfClasses(ExternalContext pExternalContext){
		return getRealPath(pExternalContext, "WEB-INF" + File.separator + "classes" + File.separator);
	}
	
	/**
	 * Retorna o caminho real a partir de caminho virtual
	 * @param pRelativePath
	 * @return
	 */
	public static String getRealPath(ExternalContext pExternalContext, String pRelativePath){
		return pExternalContext.getRealPath(pRelativePath);
	}
	
	/**
	 * Retorna caminho da URL do servidor local a partir do ExternalContext corrente
	 * @return
	 */
	public static String getServerURLString(){
		if (FacesContext.getCurrentInstance() == null){
			return "";
		}
		return getServerURLString(FacesContext.getCurrentInstance().getExternalContext());
	}

	/**
	 * Retorna caminho da URL do servidor local a partir do ExternalContext informado
	 * @return
	 */
	public static String getServerURLString(ExternalContext pExternalContext){
		if (pExternalContext == null){return "";}
		StringBuilder xLink = new StringBuilder();
		xLink.append(pExternalContext.getRequestScheme()).append("://");
		xLink.append(pExternalContext.getRequestServerName());
		if (!DBSObject.isEmpty(pExternalContext.getRequestServerPort())){
			xLink.append(":").append(pExternalContext.getRequestServerPort());
		}
//		xLink += xEC.getRequestContextPath();
		return xLink.toString();
	}
	
	
	/**
	 * Retorna URL do servidor local a partir doe um request
	 * @param pContext
	 * @return
	 */
	public static String getHTTPServerPath(HttpServletRequest pContext){
		if (pContext == null){return "";}
		StringBuilder xLink = new StringBuilder();
		xLink.append(pContext.getScheme()).append("://");
		xLink.append(pContext.getServerName());
		if (!DBSObject.isEmpty(pContext.getServerPort())){
			xLink.append(":").append(pContext.getServerPort());
		}
		return xLink.toString();
	}
	/**
{'fraction_non_alphanumeric': 0.11246273106738224, 'fraction_numerical': 0.0002385211687537269, 'mean_word_length': 4.943302622253721, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2881337', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2451, 'n_words': 543}