<commit_before>package org.wheatinitiative.vivo.adminapp.controller;

import java.io.IOException;
import java.net.URISyntaxException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.utils.URIBuilder;
import org.wheatinitiative.vivo.adminapp.datasource.DataSourceManager;
import org.wheatinitiative.vivo.adminapp.datasource.RDFServiceModelConstructor;
import org.wheatinitiative.vivo.datasource.DataSourceDescription;
import org.wheatinitiative.vivo.datasource.dao.DataSourceDao;
import org.wheatinitiative.vivo.datasource.service.DataSourceDescriptionSerializer;
import org.wheatinitiative.vivo.datasource.util.http.HttpUtils;

import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.AuthorizationRequest;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.RedirectResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;

public class ServiceInvoker extends FreemarkerHttpServlet {

    private static final Log log = LogFactory.getLog(ServiceInvoker.class);
    private static final String REDIRECT_PAGE = "/listDataSources";
    private HttpUtils httpUtils = new HttpUtils();
    protected static final AuthorizationRequest REQUIRED_ACTIONS = 
            SimplePermission.USE_MISCELLANEOUS_ADMIN_PAGES.ACTION;
    
    @Override
    protected AuthorizationRequest requiredActions(VitroRequest vreq) {
        return REQUIRED_ACTIONS;
    }
    
    // It would be awfully nice to send an error if someone tries to GET this 
    // servlet, but it would mean overriding everything in FreemarkerHttpServlet,
    // which implements doGet() and then calls
    // doGet() from doPost(), instead of putting the processing in a separate 
    // method and calling it from each.
//    @Override 
//    public void doGet(HttpServletRequest request, HttpServletResponse response) 
//            throws IOException {
//        // GETting this servlet is not allowed
//        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
//    }
    
    @Override
    public ResponseValues processRequest(VitroRequest request) 
            throws IOException, URISyntaxException {
        VitroRequest vreq = new VitroRequest(request);
        DataSourceDao dsm = new DataSourceDao(
                new RDFServiceModelConstructor(vreq.getRDFService()));
        String dataSourceURI = vreq.getParameter("uri");
        DataSourceDescription dataSource = dsm.getDataSource(dataSourceURI);
        String deploymentURL = dataSource.getConfiguration().getDeploymentURI();
        if(deploymentURL != null) {
            if(vreq.getParameter("start") != null) {
                log.info("Starting data source");
                this.startService(dataSource);
            } else if (vreq.getParameter("stop") != null) {
                log.info("Stopping data source");
                this.stopService(dataSource);
            }
        }
        URIBuilder locationB = new URIBuilder(REDIRECT_PAGE);
        String type = vreq.getParameter("type");
        if(type != null) {
            locationB.addParameter("type", type);        
        }<commit_msg>Pass Vitro.defaultNamespace to services<commit_after>package org.wheatinitiative.vivo.adminapp.controller;

import java.io.IOException;
import java.net.URISyntaxException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.utils.URIBuilder;
import org.wheatinitiative.vivo.adminapp.datasource.RDFServiceModelConstructor;
import org.wheatinitiative.vivo.datasource.DataSourceDescription;
import org.wheatinitiative.vivo.datasource.dao.DataSourceDao;
import org.wheatinitiative.vivo.datasource.service.DataSourceDescriptionSerializer;
import org.wheatinitiative.vivo.datasource.util.http.HttpUtils;

import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.AuthorizationRequest;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.RedirectResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;

public class ServiceInvoker extends FreemarkerHttpServlet {

    private static final long serialVersionUID = 1L;
    private static final Log log = LogFactory.getLog(ServiceInvoker.class);
    private static final String REDIRECT_PAGE = "/listDataSources";
    private HttpUtils httpUtils = new HttpUtils();
    protected static final AuthorizationRequest REQUIRED_ACTIONS = 
            SimplePermission.USE_MISCELLANEOUS_ADMIN_PAGES.ACTION;
    
    @Override
    protected AuthorizationRequest requiredActions(VitroRequest vreq) {
        return REQUIRED_ACTIONS;
    }
    
    // It would be awfully nice to send an error if someone tries to GET this 
    // servlet, but it would mean overriding everything in FreemarkerHttpServlet,
    // which implements doGet() and then calls
    // doGet() from doPost(), instead of putting the processing in a separate 
    // method and calling it from each.
//    @Override 
//    public void doGet(HttpServletRequest request, HttpServletResponse response) 
//            throws IOException {
//        // GETting this servlet is not allowed
//        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
//    }
    
    @Override
    public ResponseValues processRequest(VitroRequest request) 
            throws IOException, URISyntaxException {
        VitroRequest vreq = new VitroRequest(request);
        DataSourceDao dsm = new DataSourceDao(
                new RDFServiceModelConstructor(vreq.getRDFService()));
        String dataSourceURI = vreq.getParameter("uri");
        DataSourceDescription dataSource = dsm.getDataSource(dataSourceURI);
        String defaultNamespace = ConfigurationProperties.getBean(
                request.getSession().getServletContext()).getProperty(
                        "Vitro.defaultNamespace");
        if(defaultNamespace != null) {
            dataSource.getConfiguration().getParameterMap().put(
                    "Vitro.defaultNamespace", defaultNamespace);
        }
        String deploymentURL = dataSource.getConfiguration().getDeploymentURI();
        if(deploymentURL != null) {
            if(vreq.getParameter("start") != null) {
                log.info("Starting data source");
                this.startService(dataSource);
            } else if (vreq.getParameter("stop") != null) {
                log.info("Stopping data source");
                this.stopService(dataSource);
            }
        }
        URIBuilder locationB = new URIBuilder(REDIRECT_PAGE);
        String type = vreq.getParameter("type");
        if(type != null) {
            locationB.addParameter("type", type);        
        }
{'fraction_non_alphanumeric': 0.08671536796536797, 'fraction_numerical': 0.00013528138528138528, 'mean_word_length': 3.546740467404674, 'pattern_counts': {'":': 0, '<': 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': '4235506', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1994, 'n_words': 399}