<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.hw.macs.bisel.phis.iqs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Facilitates auto complete services from the SOLR API
 * 
 * @author kcm
 */
public class AutoSuggest extends HttpServlet {

    private static final String url = "http://beta.phenoimageshare.org/data/rest/getAutosuggest?"; // stem of every SOLR query
    private static final Logger logger = Logger.getLogger(System.class.getName());

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // set response type to JS and allow programs from other servers to send and receive
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");

        boolean error = false; // has an error been detected?
        String solrResult = ""; // JSON doc sent back to UI

        // create URL for SOLR query
        String queryURL = url;
        boolean first = true;
        Map<String, String[]> params = request.getParameterMap(); // get map of parameters and their values
        Enumeration<String> allParams = request.getParameterNames(); // get a list of parameter names
        if (allParams.hasMoreElements()) {
            String param = allParams.nextElement();
            if (param.equals("term")) { // ID of channel
                if (!first) { // at the moment it will always be the first (and only) param
                    queryURL += "&";
                }

                queryURL += "term=" + params.get("term")[0]; // extend stem with parameter
                first = false; // next time you need a separator
            
                
            // IT offers a number of paramaters that I do not:
            // mutantGene
            // expressedGeneOrAllele
            // phenotype
            // I am not sure when these would be used so I have not implemented them    
                
                
            // choose number of results to ask for... lots of results is very costly    
            } else if (param.equals("num")) { // number of results to return
                if (!first) {
                    queryURL += "&";
                }
                queryURL += "resultNo=" + params.get("num")[0];                
                
             
            } else { // parameter was not recognised, send error
                error = true; // error has been detected
                logger.log(Level.WARNING, "Client sent invalid parameter: " + param);
                solrResult = "{\"invalid_paramater\": \"" + param + "\"}";
            }
        }

        // should write query to log?
        // run query against SOLR API
        if (!error) { // if no error detected
            BufferedReader in = null;
            try {
                // connect to SOLR API and run query
                URL url = new URL(queryURL);
                in = new BufferedReader(new InputStreamReader(url.openStream()));

                // read JSON result
                String inputLine;
                if ((inputLine = in.readLine()) != null) { // should only be 1 line of result
                    // send result directly to client, should be no need to process (at the moment)
                    solrResult = inputLine;
                    
                    // remove the html tags IT leaves in output of SOLR
                    solrResult = solrResult.replaceAll("<b>", "");
                    solrResult = solrResult.replaceAll("</b>", "");
                }
            } catch (IOException e) {
                logger.log(Level.WARNING, e.getMessage());
                solrResult = "{\"server_error\": \"" + e.getMessage() + "\"}";
            }
        }

        // send result to client (UI)
        PrintWriter out = response.getWriter();
        try {<commit_msg>renable the <b> tags returned from SOLR on /getAutosuggest<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.hw.macs.bisel.phis.iqs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Facilitates auto complete services from the SOLR API
 * 
 * @author kcm
 */
public class AutoSuggest extends HttpServlet {

    private static final String url = "http://beta.phenoimageshare.org/data/rest/getAutosuggest?"; // stem of every SOLR query
    private static final Logger logger = Logger.getLogger(System.class.getName());

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // set response type to JS and allow programs from other servers to send and receive
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");

        boolean error = false; // has an error been detected?
        String solrResult = ""; // JSON doc sent back to UI

        // create URL for SOLR query
        String queryURL = url;
        boolean first = true;
        Map<String, String[]> params = request.getParameterMap(); // get map of parameters and their values
        Enumeration<String> allParams = request.getParameterNames(); // get a list of parameter names
        if (allParams.hasMoreElements()) {
            String param = allParams.nextElement();
            if (param.equals("term")) { // ID of channel
                if (!first) { // at the moment it will always be the first (and only) param
                    queryURL += "&";
                }

                queryURL += "term=" + params.get("term")[0]; // extend stem with parameter
                first = false; // next time you need a separator
            
                
            // IT offers a number of paramaters that I do not:
            // mutantGene
            // expressedGeneOrAllele
            // phenotype
            // I am not sure when these would be used so I have not implemented them    
                
                
            // choose number of results to ask for... lots of results is very costly    
            } else if (param.equals("num")) { // number of results to return
                if (!first) {
                    queryURL += "&";
                }
                queryURL += "resultNo=" + params.get("num")[0];                
                
             
            } else { // parameter was not recognised, send error
                error = true; // error has been detected
                logger.log(Level.WARNING, "Client sent invalid parameter: " + param);
                solrResult = "{\"invalid_paramater\": \"" + param + "\"}";
            }
        }

        // should write query to log?
        // run query against SOLR API
        if (!error) { // if no error detected
            BufferedReader in = null;
            try {
                // connect to SOLR API and run query
                URL url = new URL(queryURL);
                in = new BufferedReader(new InputStreamReader(url.openStream()));

                // read JSON result
                String inputLine;
                if ((inputLine = in.readLine()) != null) { // should only be 1 line of result
                    // send result directly to client, should be no need to process (at the moment)
                    solrResult = inputLine;
                    
                    // remove the html tags IT leaves in output of SOLR
                    //solrResult = solrResult.replaceAll("<b>", "");
                    //solrResult = solrResult.replaceAll("</b>", "");
                }
            } catch (IOException e) {
                logger.log(Level.WARNING, e.getMessage());
                solrResult = "{\"server_error\": \"" + e.getMessage() + "\"}";
            }
        }

        // send result to client (UI)
        PrintWriter out = response.getWriter();
        try {
{'fraction_non_alphanumeric': 0.09899031955865514, 'fraction_numerical': 0.0008327261371916311, 'mean_word_length': 1.849347568208778, 'pattern_counts': {'":': 4, '<': 20, '<?xml version=': 0, '>': 20, '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': '3314618', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2324, 'n_words': 820}