<commit_before>/*
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 *     http://duracloud.org/license/
 */
package org.duracloud.duradmin.control;

import org.duracloud.common.util.InitUtil;
import org.duracloud.duradmin.config.DuradminConfig;
import org.duracloud.duradmin.contentstore.ContentStoreProvider;
import org.duracloud.duradmin.domain.AdminInit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
import static org.duracloud.appconfig.xml.DuradminInitDocumentBinding.createDuradminConfigFrom;
import static org.duracloud.common.util.ExceptionUtil.getStackTraceAsString;

/**
 * This class initializes the application based on the xml body of the
 * servlet request.
 *
 * @author: <NAME>
 * Date: Apr 30, 2010
 */
public class InitController extends BaseCommandController {

    private final Logger log = LoggerFactory.getLogger(InitController.class);

    public InitController() {
    	setCommandClass(AdminInit.class);
    	setCommandName("adminInit");
    }
    
    protected ModelAndView handle(HttpServletRequest request,
                                  HttpServletResponse response,
                                  Object o,
                                  BindException be) throws Exception {
        String method = request.getMethod();
        if (method.equalsIgnoreCase("POST")) {
            return initialize(request, response);
        } else if(method.equalsIgnoreCase("GET")) {
            return isInitialized(response);
        } else {
            return respond(response, "unsupported: " + method, SC_METHOD_NOT_ALLOWED);
        }
    }

    private ModelAndView initialize(HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        log.debug("Initializing " + APP_NAME);

        ServletInputStream xml = request.getInputStream();
        if (xml != null) {
            try {
                updateInit(createDuradminConfigFrom(xml));
                return respond(response, "Initialization Successful\n", SC_OK);

            } catch (Exception e) {
                return respond(response,
                        getStackTraceAsString(e),
                        SC_INTERNAL_SERVER_ERROR);
            }
        } else {
            return respond(response, "no duradminConfig in request\n", SC_BAD_REQUEST);
        }
    }

    private void updateInit(org.duracloud.appconfig.domain.DuradminConfig config)
        throws Exception {
        AdminInit init = new AdminInit();
        init.setDuraServiceHost(config.getDuraserviceHost());
        init.setDuraServicePort(config.getDuraservicePort());
        init.setDuraServiceContext(config.getDuraserviceContext());
        init.setDuraStoreHost(config.getDurastoreHost());
        init.setDuraStorePort(config.getDurastorePort());
        init.setDuraStoreContext(config.getDurastoreContext());
        init.setAmaUrl(config.getAmaUrl());
        init.setDuraReportContext(config.getDurareportContext());

        DuradminConfig.setConfig(init);

        ContentStoreProvider contentStoreProvider = getContentStoreProvider();
        contentStoreProvider.reinitializeContentStoreManager();
    }

    private ModelAndView isInitialized(HttpServletResponse response) {
        if(DuradminConfig.isInitialized()) {
            String text = InitUtil.getInitializedText(APP_NAME);
            return respond(response, text, SC_OK);
        } else {<commit_msg>InitController mods were  missing from the last commit.<commit_after>/*
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 *     http://duracloud.org/license/
 */
package org.duracloud.duradmin.control;

import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
import static org.duracloud.appconfig.xml.DuradminInitDocumentBinding.createDuradminConfigFrom;
import static org.duracloud.common.util.ExceptionUtil.getStackTraceAsString;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.duracloud.common.util.InitUtil;
import org.duracloud.duradmin.config.DuradminConfig;
import org.duracloud.duradmin.domain.AdminInit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

/**
 * This class initializes the application based on the xml body of the
 * servlet request.
 *
 * @author: <NAME>
 * Date: Apr 30, 2010
 */
public class InitController extends BaseCommandController {

    private final Logger log = LoggerFactory.getLogger(InitController.class);

    public InitController() {
    	setCommandClass(AdminInit.class);
    	setCommandName("adminInit");
    }
    
    protected ModelAndView handle(HttpServletRequest request,
                                  HttpServletResponse response,
                                  Object o,
                                  BindException be) throws Exception {
        String method = request.getMethod();
        if (method.equalsIgnoreCase("POST")) {
            return initialize(request, response);
        } else if(method.equalsIgnoreCase("GET")) {
            return isInitialized(response);
        } else {
            return respond(response, "unsupported: " + method, SC_METHOD_NOT_ALLOWED);
        }
    }

    private ModelAndView initialize(HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        log.debug("Initializing " + APP_NAME);

        ServletInputStream xml = request.getInputStream();
        if (xml != null) {
            try {
                updateInit(createDuradminConfigFrom(xml));
                return respond(response, "Initialization Successful\n", SC_OK);

            } catch (Exception e) {
                return respond(response,
                        getStackTraceAsString(e),
                        SC_INTERNAL_SERVER_ERROR);
            }
        } else {
            return respond(response, "no duradminConfig in request\n", SC_BAD_REQUEST);
        }
    }

    private void updateInit(org.duracloud.appconfig.domain.DuradminConfig config)
        throws Exception {
        AdminInit init = new AdminInit();
        init.setDuraServiceHost(config.getDuraserviceHost());
        init.setDuraServicePort(config.getDuraservicePort());
        init.setDuraServiceContext(config.getDuraserviceContext());
        init.setDuraStoreHost(config.getDurastoreHost());
        init.setDuraStorePort(config.getDurastorePort());
        init.setDuraStoreContext(config.getDurastoreContext());
        init.setAmaUrl(config.getAmaUrl());
        init.setDuraReportContext(config.getDurareportContext());

        DuradminConfig.setConfig(init);

        this.controllerSupport.getContentStoreManager().reinitialize(DuradminConfig.getDuraStoreHost(), 
                                              DuradminConfig.getDuraStorePort(), 
                                              DuradminConfig.getDuraStoreContext());
    }

    private ModelAndView isInitialized(HttpServletResponse response) {
        if(DuradminConfig.isInitialized()) {
            String text = InitUtil.getInitializedText(APP_NAME);
            return respond(response, text, SC_OK);
        } else {
{'fraction_non_alphanumeric': 0.08400665399239544, 'fraction_numerical': 0.0019011406844106464, 'mean_word_length': 3.010004764173416, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4671991', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2159, 'n_words': 434}