<commit_before>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;

/**
 * Servlet connects client (presumably a GUI) to the SOLR API that wraps the SOLR repository
 * for ROIs
 *
 * @author kcm
 */
public class GetROIs extends HttpServlet {

    private static final String url = "http://dev.phenoimageshare.org/data/rest/getRois?"; // stem of every SOLR query
    private static final Logger logger = Logger.getLogger(System.class.getName());
    /**
     * Enables discovery of ROI information for multiple ROIs.
     * Handles requests from the PhIS UI by simply forwarding them to the SOLR API and 
     * then returning the result directly to the UI.  Provides very basic error handling
     * (only deals with unknown parameters).
     * 
     * Parameters expected:
     * <ol>
     * <li>imageId = image ID, e.g., komp2_roi_112003</li>
     * <li>start = pagination, the first result that should be returned</li>
     * <li>num = pagination, the total number of results that should be
     * returned</li>
     * </ol>
     *      
     *
     * Future versions will:
     * <ol>
     * <li>send queries to the SIS, and then integrate the results
     * with those from SOLR</li>  
     * <li>likely to include sorting the results</li>
     * <li>include a wider range of query parameters</li>
     * <li>provide access to the "OLS" functionality from SOLR</li>
     * </ol>
     * 
     * 
     * 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.equalsIgnoreCase("imageId")) { // deal with phenotypes
                if (!first) { // if this is not the first parameter added to queryURL include separator
                    queryURL += "&";
                }
                
                queryURL += "imageId=" + params.get("imageId")[0]; // extend stem with parameter
                first = false; // next time you need a seperator
                
            // pagination    
            } else if (param.equalsIgnoreCase("start")) { // number of initial result
            	if(!first) {
            		queryURL += "&";
            	}    
            	queryURL += "start="+params.get("start")[0];
            	first = false;
            } else if (param.equalsIgnoreCase("num")) { // number of results to return<commit_msg>changed if(hasMoreElements()) to while()<commit_after>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;

/**
 * Servlet connects client (presumably a GUI) to the SOLR API that wraps the SOLR repository
 * for ROIs
 *
 * @author kcm
 */
public class GetROIs extends HttpServlet {

    private static final String url = "http://dev.phenoimageshare.org/data/rest/getRois?"; // stem of every SOLR query
    private static final Logger logger = Logger.getLogger(System.class.getName());
    /**
     * Enables discovery of ROI information for multiple ROIs.
     * Handles requests from the PhIS UI by simply forwarding them to the SOLR API and 
     * then returning the result directly to the UI.  Provides very basic error handling
     * (only deals with unknown parameters).
     * 
     * Parameters expected:
     * <ol>
     * <li>imageId = image ID, e.g., komp2_roi_112003</li>
     * <li>start = pagination, the first result that should be returned</li>
     * <li>num = pagination, the total number of results that should be
     * returned</li>
     * </ol>
     *      
     *
     * Future versions will:
     * <ol>
     * <li>send queries to the SIS, and then integrate the results
     * with those from SOLR</li>  
     * <li>likely to include sorting the results</li>
     * <li>include a wider range of query parameters</li>
     * <li>provide access to the "OLS" functionality from SOLR</li>
     * </ol>
     * 
     * 
     * 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
        while (allParams.hasMoreElements()) {
            String param = allParams.nextElement();
            if (param.equalsIgnoreCase("imageId")) { // deal with phenotypes
                if (!first) { // if this is not the first parameter added to queryURL include separator
                    queryURL += "&";
                }
                
                queryURL += "imageId=" + params.get("imageId")[0]; // extend stem with parameter
                first = false; // next time you need a seperator
                
            // pagination    
            } else if (param.equalsIgnoreCase("start")) { // number of initial result
            	if(!first) {
            		queryURL += "&";
            	}    
            	queryURL += "start="+params.get("start")[0];
            	first = false;
            } else if (param.equalsIgnoreCase("num")) { // number of results to return
{'fraction_non_alphanumeric': 0.09981804003119314, 'fraction_numerical': 0.0025994281258123215, 'mean_word_length': 2.475609756097561, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, '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': '3399932', 'n_tokens_mistral': 2102, 'n_tokens_neox': 2035, 'n_words': 692}