<commit_before>package namewebserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

import qora.account.Account;
import qora.block.Block;
import qora.crypto.Crypto;
import qora.naming.Name;
import qora.transaction.Transaction;
import settings.Settings;
import utils.AccountBalanceComparator;
import utils.BlogUtils;
import utils.GZIP;
import utils.JSonWriter;
import utils.NameUtils;
import utils.NameUtils.NameResult;
import utils.Pair;
import utils.Qorakeys;
import utils.Triplet;
import api.ATResource;
import api.AddressesResource;
import api.ApiErrorFactory;
import api.BlocksResource;
import api.BlogPostResource;
import api.NameSalesResource;
import api.NamesResource;
import api.TransactionsResource;
import controller.Controller;
import database.DBSet;
import database.NameMap;

@Path("/")
public class NamesWebResource {
	@Context
	HttpServletRequest request;

	@GET
	public Response Default() {
		return handleDefault();
	}

	public Response handleDefault() {
		try {

			String searchValue = request.getParameter("search");
			String webDirectory = request.getParameter("webdirectory");
			String blogDirectory = request.getParameter("blogdirectory");
			String content = readFile("web/index.html", StandardCharsets.UTF_8);

			if (StringUtil.isBlank(searchValue)
					&& StringUtil.isBlank(webDirectory)
					&& StringUtil.isBlank(blogDirectory) ) {

				content = replaceWarning(content);
				return Response.ok(content, "text/html; charset=utf-8").build();
			} else if (searchValue != null || webDirectory != null
					|| blogDirectory != null ) {
				List<Pair<String, String>> searchResults;
				content = readFile("web/index.mini.html",
						StandardCharsets.UTF_8);
				String searchResultTemplate = readFile("web/searchresult",
						StandardCharsets.UTF_8);
				if (webDirectory != null) {
					searchResults = NameUtils.getWebsitesByValue(null);
				} else if (blogDirectory != null) {
					return handleBlogSearch(content, searchResultTemplate, null);
				} else {
					searchResults = NameUtils.getWebsitesByValue(searchValue);
				} 

				String results = "";
				for (Pair<String, String> result : searchResults) {
					String name = result.getA();
					String websitecontent = result.getB();
					Document htmlDoc = Jsoup.parse(websitecontent);
					String title = selectTitleOpt(htmlDoc);
					title = title == null ? "" : title;
					String description = selectDescriptionOpt(htmlDoc);
					description = description == null ? "" : description;

					results += searchResultTemplate.replace("!Name!", name)
							.replace("!Title!", title)
							.replace("!Description!", description)
							.replace("!Titlelink!", "/" + name)
							.replace("!Namelink!", "/" + name)
							.replace("!keyslink!", "/namepairs:" + name);

				}

				content = content.replace("<resultlist></resultlist>", results);

			}

			return Response.ok(content, "text/html; charset=utf-8").build();
		} catch (IOException e) {
			e.printStackTrace();
			return error404(request);
		}
	}
	
	
	@Path("blogsearch.html")
	@GET
	public Response doBlogSearch() {
		
		String searchValue = request.getParameter("search");
		String content;
		try {
			content = readFile("web/index.html", StandardCharsets.UTF_8);
			if(StringUtil.isBlank(searchValue)) {
				
				content = replaceWarning(content);
				return Response.ok(content, "text/html; charset=utf-8").build();
			}
			
			content = readFile("web/index.mini.html",
					StandardCharsets.UTF_8);
			String searchResultTemplate = readFile("web/searchresult",
					StandardCharsets.UTF_8);
			return handleBlogSearch(content, searchResultTemplate, searchValue);
			
			
		} catch (IOException e) {
			e.printStackTrace();
			return error404(request);
		}
		
	}
	
	

	private Response handleBlogSearch(String content,
			String searchResultTemplate, String blogSearchOpt) {
		String results = "";
		List<Triplet<String, String, String>> allEnabledBlogs = BlogUtils
				.getEnabledBlogs(blogSearchOpt);
		for (Triplet<String, String, String> triplet : allEnabledBlogs) {<commit_msg>empty search is now recornized correctly<commit_after>package namewebserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

import qora.account.Account;
import qora.block.Block;
import qora.crypto.Crypto;
import qora.naming.Name;
import qora.transaction.Transaction;
import settings.Settings;
import utils.AccountBalanceComparator;
import utils.BlogUtils;
import utils.GZIP;
import utils.JSonWriter;
import utils.NameUtils;
import utils.NameUtils.NameResult;
import utils.Pair;
import utils.Qorakeys;
import utils.Triplet;
import api.ATResource;
import api.AddressesResource;
import api.ApiErrorFactory;
import api.BlocksResource;
import api.BlogPostResource;
import api.NameSalesResource;
import api.NamesResource;
import api.TransactionsResource;
import controller.Controller;
import database.DBSet;
import database.NameMap;

@Path("/")
public class NamesWebResource {
	@Context
	HttpServletRequest request;

	@GET
	public Response Default() {
		return handleDefault();
	}

	public Response handleDefault() {
		try {

			String searchValue = request.getParameter("search");
			String webDirectory = request.getParameter("webdirectory");
			String blogDirectory = request.getParameter("blogdirectory");
			String content = readFile("web/index.html", StandardCharsets.UTF_8);

			if (searchValue == null
					&& webDirectory == null
					&& blogDirectory == null ) {

				content = replaceWarning(content);
				return Response.ok(content, "text/html; charset=utf-8").build();
			} else if (searchValue != null || webDirectory != null
					|| blogDirectory != null ) {
				List<Pair<String, String>> searchResults;
				content = readFile("web/index.mini.html",
						StandardCharsets.UTF_8);
				String searchResultTemplate = readFile("web/searchresult",
						StandardCharsets.UTF_8);
				if (webDirectory != null) {
					searchResults = NameUtils.getWebsitesByValue(null);
				} else if (blogDirectory != null) {
					return handleBlogSearch(content, searchResultTemplate, null);
				} else {
					searchResults = NameUtils.getWebsitesByValue(searchValue);
				} 

				String results = "";
				for (Pair<String, String> result : searchResults) {
					String name = result.getA();
					String websitecontent = result.getB();
					Document htmlDoc = Jsoup.parse(websitecontent);
					String title = selectTitleOpt(htmlDoc);
					title = title == null ? "" : title;
					String description = selectDescriptionOpt(htmlDoc);
					description = description == null ? "" : description;

					results += searchResultTemplate.replace("!Name!", name)
							.replace("!Title!", title)
							.replace("!Description!", description)
							.replace("!Titlelink!", "/" + name)
							.replace("!Namelink!", "/" + name)
							.replace("!keyslink!", "/namepairs:" + name);

				}

				content = content.replace("<resultlist></resultlist>", results);

			}

			return Response.ok(content, "text/html; charset=utf-8").build();
		} catch (IOException e) {
			e.printStackTrace();
			return error404(request);
		}
	}
	
	
	@Path("blogsearch.html")
	@GET
	public Response doBlogSearch() {
		
		String searchValue = request.getParameter("search");
		String content;
		try {
			content = readFile("web/index.mini.html",
					StandardCharsets.UTF_8);
			if(StringUtil.isBlank(searchValue)) {
				
				content = replaceWarning(content);
				return Response.ok(content, "text/html; charset=utf-8").build();
			}
			
			String searchResultTemplate = readFile("web/searchresult",
					StandardCharsets.UTF_8);
			return handleBlogSearch(content, searchResultTemplate, searchValue);
			
			
		} catch (IOException e) {
			e.printStackTrace();
			return error404(request);
		}
		
	}
	
	

	private Response handleBlogSearch(String content,
			String searchResultTemplate, String blogSearchOpt) {
		String results = "";
		List<Triplet<String, String, String>> allEnabledBlogs = BlogUtils
				.getEnabledBlogs(blogSearchOpt);
		for (Triplet<String, String, String> triplet : allEnabledBlogs) {
{'fraction_non_alphanumeric': 0.12153579676674364, 'fraction_numerical': 0.0031755196304849883, 'mean_word_length': 4.5488521089161775, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '123235', 'n_tokens_mistral': 3583, 'n_tokens_neox': 3094, 'n_words': 651}