<commit_before>package com.marklogic.hub.web.controller.api;

import java.io.File;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.marklogic.hub.config.EnvironmentConfiguration;
import com.marklogic.hub.exception.DataHubException;
import com.marklogic.hub.service.DataHubService;
import com.marklogic.hub.service.DomainManagerService;
import com.marklogic.hub.web.controller.BaseController;
import com.marklogic.hub.web.form.LoginForm;

@RestController
@RequestMapping("/api/data-hub")
public class DataHubServerApiController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataHubServerApiController.class);

    @Autowired
    private EnvironmentConfiguration environmentConfiguration;
    
    @Autowired
    private DataHubService dataHubService;
    
    @Autowired
    private DomainManagerService domainManagerService;
    
    @RequestMapping(value="login", method = RequestMethod.POST, consumes={MediaType.APPLICATION_JSON_UTF8_VALUE}, produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public LoginForm postLogin(@RequestBody LoginForm loginForm, BindingResult bindingResult, HttpSession session, HttpServletRequest request) throws Exception {
        try {
        	if(isValidDirectory(loginForm.getUserPluginDir())) {
        		
	            updateEnvironmentConfiguration(loginForm);
	            
	            loginForm.setInstalled(dataHubService.isInstalled());
	            loginForm.setServerVersionAccepted(dataHubService.isServerAcceptable());
	            loginForm.setHasErrors(false);
	            loginForm.setLoggedIn(true);
	            loginForm.setDomains(domainManagerService.getDomains());
	            
	            environmentConfiguration.saveConfigurationToFile();
	            
	            session.setAttribute("loginForm", loginForm);
        	} else {
        		loginForm.setLoggedIn(false);
                displayError(loginForm, null, null, loginForm.getUserPluginDir() + " is not a valid directory.");
        	}
        }
        catch (DataHubException e) {
            LOGGER.error("Login failed", e);
            loginForm.setLoggedIn(false);
            displayError(loginForm, null, null, e.getMessage());
        }
        
        return loginForm;
    }
    
    private boolean isValidDirectory(String userPluginDir) {
		File file = new File(userPluginDir);
		if(file.exists() && file.isDirectory()) {
			return true;
		}
		
		File parentFile = file.getParentFile();
		if(parentFile.exists() && parentFile.isDirectory()) {
			file.mkdir();
			return true;
		}
		return false;
	}

	@RequestMapping(value="login", method = RequestMethod.GET)
    public LoginForm getLoginStatus(HttpSession session) {
    	LoginForm loginForm = (LoginForm) session.getAttribute("loginForm");
    	if(loginForm == null) {
    		loginForm = new LoginForm();
    		this.environmentConfiguration.loadConfigurationFromFile();
    		this.retrieveEnvironmentConfiguration(loginForm);
    		session.setAttribute("loginForm", loginForm);
    	}
        return loginForm;
    }
    
    @RequestMapping(value="logout", method = RequestMethod.POST)
    public LoginForm postLogout(HttpSession session) {<commit_msg>updated domain list when getting current status<commit_after>package com.marklogic.hub.web.controller.api;

import java.io.File;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.marklogic.hub.config.EnvironmentConfiguration;
import com.marklogic.hub.exception.DataHubException;
import com.marklogic.hub.service.DataHubService;
import com.marklogic.hub.service.DomainManagerService;
import com.marklogic.hub.web.controller.BaseController;
import com.marklogic.hub.web.form.LoginForm;

@RestController
@RequestMapping("/api/data-hub")
public class DataHubServerApiController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataHubServerApiController.class);

    @Autowired
    private EnvironmentConfiguration environmentConfiguration;
    
    @Autowired
    private DataHubService dataHubService;
    
    @Autowired
    private DomainManagerService domainManagerService;
    
    @RequestMapping(value="login", method = RequestMethod.POST, consumes={MediaType.APPLICATION_JSON_UTF8_VALUE}, produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public LoginForm postLogin(@RequestBody LoginForm loginForm, BindingResult bindingResult, HttpSession session, HttpServletRequest request) throws Exception {
        try {
        	if(isValidDirectory(loginForm.getUserPluginDir())) {
        		
	            updateEnvironmentConfiguration(loginForm);
	            
	            loginForm.setInstalled(dataHubService.isInstalled());
	            loginForm.setServerVersionAccepted(dataHubService.isServerAcceptable());
	            loginForm.setHasErrors(false);
	            loginForm.setLoggedIn(true);
	            loginForm.setDomains(domainManagerService.getDomains());
	            
	            environmentConfiguration.saveConfigurationToFile();
	            
	            session.setAttribute("loginForm", loginForm);
        	} else {
        		loginForm.setLoggedIn(false);
                displayError(loginForm, null, null, loginForm.getUserPluginDir() + " is not a valid directory.");
        	}
        }
        catch (DataHubException e) {
            LOGGER.error("Login failed", e);
            loginForm.setLoggedIn(false);
            displayError(loginForm, null, null, e.getMessage());
        }
        
        return loginForm;
    }
    
    private boolean isValidDirectory(String userPluginDir) {
		File file = new File(userPluginDir);
		if(file.exists() && file.isDirectory()) {
			return true;
		}
		
		File parentFile = file.getParentFile();
		if(parentFile.exists() && parentFile.isDirectory()) {
			file.mkdir();
			return true;
		}
		return false;
	}

	@RequestMapping(value="login", method = RequestMethod.GET)
    public LoginForm getLoginStatus(HttpSession session) {
    	LoginForm loginForm = (LoginForm) session.getAttribute("loginForm");
    	if(loginForm == null) {
    		loginForm = new LoginForm();
    		this.environmentConfiguration.loadConfigurationFromFile();
    		this.retrieveEnvironmentConfiguration(loginForm);
    		session.setAttribute("loginForm", loginForm);
    	}
    	else {
    	    loginForm.setDomains(domainManagerService.getDomains());
    	}
        return loginForm;
    }
    
    @RequestMapping(value="logout", method = RequestMethod.POST)
    public LoginForm postLogout(HttpSession session) {
{'fraction_non_alphanumeric': 0.09408495434696308, 'fraction_numerical': 0.0010586211459573905, 'mean_word_length': 3.7534591194968554, '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': '1121454', 'n_tokens_mistral': 2063, 'n_tokens_neox': 2008, 'n_words': 346}