<commit_before>package org.mskcc.cgds.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.regex.Pattern;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mskcc.cgds.dao.DaoCancerStudy;
import org.mskcc.cgds.dao.DaoCase;
import org.mskcc.cgds.dao.DaoCaseList;
import org.mskcc.cgds.dao.DaoException;
import org.mskcc.cgds.dao.DaoGeneticProfile;
import org.mskcc.cgds.model.CancerStudy;
import org.mskcc.cgds.model.CaseList;
import org.mskcc.cgds.model.GeneticProfile;
import org.mskcc.cgds.util.AccessControl;
import org.mskcc.cgds.util.DatabaseProperties;
import org.mskcc.cgds.web_api.GetCaseLists;
import org.mskcc.cgds.web_api.GetClinicalData;
import org.mskcc.cgds.web_api.GetGeneticProfiles;
import org.mskcc.cgds.web_api.GetMutationData;
import org.mskcc.cgds.web_api.GetMutationFrequencies;
import org.mskcc.cgds.web_api.GetNetwork;
import org.mskcc.cgds.web_api.GetMutSig;
import org.mskcc.cgds.web_api.GetProfileData;
import org.mskcc.cgds.web_api.GetProteinArrayData;
import org.mskcc.cgds.web_api.GetTypesOfCancer;
import org.mskcc.cgds.web_api.ProtocolException;
import org.mskcc.cgds.web_api.WebApiUtil;

/**
 * Core Web Service.
 *
 * @author <NAME>.
 * @author <NAME> <EMAIL>
 */
public class WebService extends HttpServlet {

    public static final String CANCER_STUDY_ID = "cancer_study_id";
    public static final String CANCER_TYPE_ID = "cancer_type_id";
    public static final String GENETIC_PROFILE_ID = "genetic_profile_id";
    public static final String GENE_LIST = "gene_list";
    public static final String CMD = "cmd";
    public static final String Q_VALUE_THRESHOLD = "q_value_threshold";
    public static final String GENE_SYMBOL = "gene_symbol";
    public static final String ENTREZ_GENE_ID = "entrez_gene_id";
    public static final String CASE_LIST = "case_list";
    public static final String CASE_SET_ID = "case_set_id";
    public static final String SUPPRESS_MONDRIAN_HEADER = "suppress_mondrian_header";
    public static final String EMAIL_ADDRESS = "email_address";
    public static final String SECRET_KEY = "secret_key";
    public static final String PROTEIN_ARRAY_TYPE = "protein_array_type";
    public static final String PROTEIN_ARRAY_ID = "protein_array_id";

    /**
     * Shutdown the Servlet.
     */
    public void destroy() {
        super.destroy();
    }

    /**
     * Initializes Servlet with parameters in web.xml file.
     *
     * @throws javax.servlet.ServletException Servlet Initialization Error.
     */
    public void init() throws ServletException {
        super.init();
        System.out.println("Starting up the Cancer Genomics Data Server...");
        System.out.println("Reading in init parameters from web.xml");
        DatabaseProperties dbProperties = DatabaseProperties.getInstance();
        ServletConfig config = this.getServletConfig();
        String dbHost = config.getInitParameter("db_host");
        String dbUser = config.getInitParameter("db_user");
        String dbPassword = config.getInitParameter("db_password");
        String dbName = config.getInitParameter("db_name");
        System.out.println("Starting CGDS Server");
        dbProperties.setDbName(dbName);
        dbProperties.setDbHost(dbHost);
        dbProperties.setDbUser(dbUser);
        dbProperties.setDbPassword(<PASSWORD>);
        verifyDbConnection();
    }

    /**
     * Handles GET Requests.
     *
     * @param httpServletRequest  HttpServlet Request.
     * @param httpServletResponse HttpServlet Response.
     * @throws ServletException Servlet Error.
     * @throws IOException      IO Error.
     */
    protected void doGet(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse) throws ServletException, IOException {
        processClient(httpServletRequest, httpServletResponse);
    }

    /**
     * Handles POST Requests.
     *
     * @param httpServletRequest  HttpServlet Request.
     * @param httpServletResponse HttpServlet Response.
     * @throws ServletException Servlet Error.
     * @throws IOException      IO Error.
     */
    protected void doPost(HttpServletRequest httpServletRequest,
                          HttpServletResponse httpServletResponse) throws ServletException, IOException {
        processClient(httpServletRequest, httpServletResponse);
    }

    /**
     * Processes all Client Requests.
     *
     * @param httpServletRequest  HttpServlet Request.
     * @param httpServletResponse HttpServlet Response.
     * @throws IOException IO Error.
     */
    public void processClient(HttpServletRequest httpServletRequest,
                              HttpServletResponse httpServletResponse) throws IOException {
        PrintWriter writer = httpServletResponse.getWriter();
        Date startTime = new Date();
        String cmd = httpServletRequest.getParameter(CMD);

        try {
            httpServletResponse.setContentType("text/plain");
            writer.print(WebApiUtil.WEP_API_HEADER);

            // Branch, based on command.
            if (null == cmd) {
                outputMissingParameterError(writer, CMD);
                return;
            }
         // Branch, based on command.
         if (null == cmd) {
            outputMissingParameterError(writer, CMD);
            return;
         }
         
         // check command
         if( !goodCommand( writer, cmd ) ){
            return;
         }
         
         if (cmd.equals("getTypesOfCancer")) {
            // getTypesOfCancer requires no access control 
            getTypesOfCancer(writer);
            return;
         }
         if (cmd.equals("getNetwork")) {
            // getNetwork doesn't access any data; so no access control needed  
            getNetwork(httpServletRequest, writer);
            return;
         }
         if (cmd.equals("getProteinArrayInfo")) {
             getProteinArrayInfo(httpServletRequest, writer);
             return;
         }
         if (cmd.equals("getProteinArrayData")) {
             getProteinArrayData(httpServletRequest, writer);
             return;
         }


            // check command
            if (!goodCommand(writer, cmd)) {
                return;
            }

            if (cmd.equals("getTypesOfCancer")) {
                // getTypesOfCancer requires no access control
                getTypesOfCancer(writer);
                return;
            }
            if (cmd.equals("getNetwork")) {
                // getNetwork doesn't access any data; so no access control needed
                getNetwork(httpServletRequest, writer);
                return;
            }

            //  We support the new getCancerStudies plus the deprecated getCancerTypes command
            if (cmd.equals("getCancerStudies") || cmd.equals("getCancerTypes")) {

                // getCancerStudies requires special access control
                // identify every study accessible to the user
                getCancerStudies(httpServletRequest, writer);
                return;
            }


            // TODO: CASES: REMOVE?
            // no cancer_study_id or no case_set_id or genetic_profile_id<commit_msg>Removed duplicate statements that must have gotten in via the last merge.<commit_after>package org.mskcc.cgds.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.regex.Pattern;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mskcc.cgds.dao.DaoCancerStudy;
import org.mskcc.cgds.dao.DaoCase;
import org.mskcc.cgds.dao.DaoCaseList;
import org.mskcc.cgds.dao.DaoException;
import org.mskcc.cgds.dao.DaoGeneticProfile;
import org.mskcc.cgds.model.CancerStudy;
import org.mskcc.cgds.model.CaseList;
import org.mskcc.cgds.model.GeneticProfile;
import org.mskcc.cgds.util.AccessControl;
import org.mskcc.cgds.util.DatabaseProperties;
import org.mskcc.cgds.web_api.GetCaseLists;
import org.mskcc.cgds.web_api.GetClinicalData;
import org.mskcc.cgds.web_api.GetGeneticProfiles;
import org.mskcc.cgds.web_api.GetMutationData;
import org.mskcc.cgds.web_api.GetMutationFrequencies;
import org.mskcc.cgds.web_api.GetNetwork;
import org.mskcc.cgds.web_api.GetMutSig;
import org.mskcc.cgds.web_api.GetProfileData;
import org.mskcc.cgds.web_api.GetProteinArrayData;
import org.mskcc.cgds.web_api.GetTypesOfCancer;
import org.mskcc.cgds.web_api.ProtocolException;
import org.mskcc.cgds.web_api.WebApiUtil;

/**
 * Core Web Service.
 *
 * @author <NAME>.
 * @author <NAME> <EMAIL>
 */
public class WebService extends HttpServlet {

    public static final String CANCER_STUDY_ID = "cancer_study_id";
    public static final String CANCER_TYPE_ID = "cancer_type_id";
    public static final String GENETIC_PROFILE_ID = "genetic_profile_id";
    public static final String GENE_LIST = "gene_list";
    public static final String CMD = "cmd";
    public static final String Q_VALUE_THRESHOLD = "q_value_threshold";
    public static final String GENE_SYMBOL = "gene_symbol";
    public static final String ENTREZ_GENE_ID = "entrez_gene_id";
    public static final String CASE_LIST = "case_list";
    public static final String CASE_SET_ID = "case_set_id";
    public static final String SUPPRESS_MONDRIAN_HEADER = "suppress_mondrian_header";
    public static final String EMAIL_ADDRESS = "email_address";
    public static final String SECRET_KEY = "secret_key";
    public static final String PROTEIN_ARRAY_TYPE = "protein_array_type";
    public static final String PROTEIN_ARRAY_ID = "protein_array_id";

    /**
     * Shutdown the Servlet.
     */
    public void destroy() {
        super.destroy();
    }

    /**
     * Initializes Servlet with parameters in web.xml file.
     *
     * @throws javax.servlet.ServletException Servlet Initialization Error.
     */
    public void init() throws ServletException {
        super.init();
        System.out.println("Starting up the Cancer Genomics Data Server...");
        System.out.println("Reading in init parameters from web.xml");
        DatabaseProperties dbProperties = DatabaseProperties.getInstance();
        ServletConfig config = this.getServletConfig();
        String dbHost = config.getInitParameter("db_host");
        String dbUser = config.getInitParameter("db_user");
        String dbPassword = config.getInitParameter("db_password");
        String dbName = config.getInitParameter("db_name");
        System.out.println("Starting CGDS Server");
        dbProperties.setDbName(dbName);
        dbProperties.setDbHost(dbHost);
        dbProperties.setDbUser(dbUser);
        dbProperties.setDbPassword(<PASSWORD>);
        verifyDbConnection();
    }

    /**
     * Handles GET Requests.
     *
     * @param httpServletRequest  HttpServlet Request.
     * @param httpServletResponse HttpServlet Response.
     * @throws ServletException Servlet Error.
     * @throws IOException      IO Error.
     */
    protected void doGet(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse) throws ServletException, IOException {
        processClient(httpServletRequest, httpServletResponse);
    }

    /**
     * Handles POST Requests.
     *
     * @param httpServletRequest  HttpServlet Request.
     * @param httpServletResponse HttpServlet Response.
     * @throws ServletException Servlet Error.
     * @throws IOException      IO Error.
     */
    protected void doPost(HttpServletRequest httpServletRequest,
                          HttpServletResponse httpServletResponse) throws ServletException, IOException {
        processClient(httpServletRequest, httpServletResponse);
    }

    /**
     * Processes all Client Requests.
     *
     * @param httpServletRequest  HttpServlet Request.
     * @param httpServletResponse HttpServlet Response.
     * @throws IOException IO Error.
     */
    public void processClient(HttpServletRequest httpServletRequest,
                              HttpServletResponse httpServletResponse) throws IOException {
        PrintWriter writer = httpServletResponse.getWriter();
        Date startTime = new Date();
        String cmd = httpServletRequest.getParameter(CMD);

        try {
            httpServletResponse.setContentType("text/plain");
            writer.print(WebApiUtil.WEP_API_HEADER);

            // Branch, based on command.
            if (null == cmd) {
                outputMissingParameterError(writer, CMD);
                return;
            }

            // check command
            if (!goodCommand(writer, cmd)) {
                return;
            }

            if (cmd.equals("getTypesOfCancer")) {
                // getTypesOfCancer requires no access control
                getTypesOfCancer(writer);
                return;
            }
            if (cmd.equals("getNetwork")) {
                // getNetwork doesn't access any data; so no access control needed
                getNetwork(httpServletRequest, writer);
                return;
            }

			if (cmd.equals("getProteinArrayInfo")) {
				getProteinArrayInfo(httpServletRequest, writer);
				return;
			}
			if (cmd.equals("getProteinArrayData")) {
				getProteinArrayData(httpServletRequest, writer);
				return;
			}

            //  We support the new getCancerStudies plus the deprecated getCancerTypes command
            if (cmd.equals("getCancerStudies") || cmd.equals("getCancerTypes")) {

                // getCancerStudies requires special access control
                // identify every study accessible to the user
                getCancerStudies(httpServletRequest, writer);
                return;
            }


            // TODO: CASES: REMOVE?
            // no cancer_study_id or no case_set_id or genetic_profile_id
{'fraction_non_alphanumeric': 0.08626354421530934, 'fraction_numerical': 0.0, 'mean_word_length': 2.7946949602122015, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2971944', 'n_tokens_mistral': 4040, 'n_tokens_neox': 3922, 'n_words': 895}