<commit_before>/*
 * Copyright 2012
 * Ubiquitous Knowledge Processing (UKP) Lab and FG Language Technology
 * Technische Universität Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.tudarmstadt.ukp.clarin.webanno.ui.core;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.Component;
import org.apache.wicket.RuntimeConfigurationType;
import org.apache.wicket.core.request.mapper.HomePageMapper;
import org.apache.wicket.devutils.stateless.StatelessChecker;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.IRequestMapper;
import org.apache.wicket.request.cycle.AbstractRequestCycleListener;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.request.resource.SharedResourceReference;
import org.apache.wicket.resource.DynamicJQueryResourceReference;
import org.apache.wicket.resource.loader.IStringResourceLoader;
import org.apache.wicket.resource.loader.NestedStringResourceLoader;
import org.apache.wicket.settings.ExceptionSettings;
import org.slf4j.MDC;
import org.springframework.context.ApplicationContext;
import org.wicketstuff.annotation.scan.AnnotatedMountList;
import org.wicketstuff.annotation.scan.AnnotatedMountScanner;

import com.giffing.wicket.spring.boot.starter.app.WicketBootSecuredWebApplication;

import de.tudarmstadt.ukp.clarin.webanno.api.DocumentService;
import de.tudarmstadt.ukp.clarin.webanno.support.ApplicationContextProvider;
import de.tudarmstadt.ukp.clarin.webanno.support.FileSystemResource;
import de.tudarmstadt.ukp.clarin.webanno.support.SettingsUtil;
import de.tudarmstadt.ukp.clarin.webanno.support.logging.Logging;
import de.tudarmstadt.ukp.clarin.webanno.ui.core.login.LoginPage;
import de.tudarmstadt.ukp.clarin.webanno.ui.core.page.MenuBar;

/**
 * The Wicket application class. Sets up pages, authentication, theme, and other application-wide
 * configuration.
 */
public abstract class WicketApplicationBase
    extends WicketBootSecuredWebApplication
{
    protected boolean isInitialized = false;

    @Override
    protected void init()
    {
        super.init();
        
//        initSpring();
        
        initStatelessChecker();
        
        if (!isInitialized) {
            initOnce();
            
            isInitialized = true;
        }
    }

    protected void initOnce()
    {
        // Allow nested string resource resolving using "#(key)"
        initNestedStringResourceLoader();
        
//        // This should avoid some application-reloading while working on I18N
//        getResourceSettings().setThrowExceptionOnMissingResource(false);
//        getResourceSettings().setCachingStrategy(new NoOpResourceCachingStrategy());
        
        // Enable dynamic switching between JQuery 1 and JQuery 2 based on the browser
        // identification. 
        initDynamicJQueryResourceReference();

        initDefaultPageMounts();
        
        initLogoReference();

        // Display stack trace instead of internal error
        initShowExceptionPage();

        initMDCLifecycle();
    }

    protected void initMDCLifecycle()
    {
        getRequestCycleListeners().add(new AbstractRequestCycleListener()
        {
            @Override
            public void onBeginRequest(RequestCycle cycle)
            {
                ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();
                DocumentService repo = ctx.getBean(DocumentService.class);
                MDC.put(Logging.KEY_REPOSITORY_PATH, repo.getDir().getAbsolutePath());
            }

            @Override
            public void onEndRequest(RequestCycle cycle)
            {
                MDC.remove(Logging.KEY_REPOSITORY_PATH);
            }
        });
    }

    protected void initShowExceptionPage()
    {
        Properties settings = SettingsUtil.getSettings();
        if ("true".equalsIgnoreCase(settings.getProperty("debug.showExceptionPage"))) {
            getExceptionSettings().setUnexpectedExceptionDisplay(
                    ExceptionSettings.SHOW_EXCEPTION_PAGE);
        }
    }

    protected void initLogoReference()
    {<commit_msg>#486 - Refactoring project structure (3.3.0)<commit_after>/*
 * Copyright 2012
 * Ubiquitous Knowledge Processing (UKP) Lab and FG Language Technology
 * Technische Universität Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.tudarmstadt.ukp.clarin.webanno.ui.core;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.Component;
import org.apache.wicket.RuntimeConfigurationType;
import org.apache.wicket.core.request.mapper.HomePageMapper;
import org.apache.wicket.devutils.stateless.StatelessChecker;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.IRequestMapper;
import org.apache.wicket.request.cycle.AbstractRequestCycleListener;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.request.resource.SharedResourceReference;
import org.apache.wicket.resource.DynamicJQueryResourceReference;
import org.apache.wicket.resource.loader.IStringResourceLoader;
import org.apache.wicket.resource.loader.NestedStringResourceLoader;
import org.apache.wicket.settings.ExceptionSettings;
import org.slf4j.MDC;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.wicketstuff.annotation.scan.AnnotatedMountList;
import org.wicketstuff.annotation.scan.AnnotatedMountScanner;

import com.giffing.wicket.spring.boot.starter.app.WicketBootSecuredWebApplication;

import de.tudarmstadt.ukp.clarin.webanno.api.DocumentService;
import de.tudarmstadt.ukp.clarin.webanno.support.ApplicationContextProvider;
import de.tudarmstadt.ukp.clarin.webanno.support.FileSystemResource;
import de.tudarmstadt.ukp.clarin.webanno.support.SettingsUtil;
import de.tudarmstadt.ukp.clarin.webanno.support.logging.Logging;
import de.tudarmstadt.ukp.clarin.webanno.ui.core.login.LoginPage;
import de.tudarmstadt.ukp.clarin.webanno.ui.core.page.MenuBar;

/**
 * The Wicket application class. Sets up pages, authentication, theme, and other application-wide
 * configuration.
 */
public abstract class WicketApplicationBase
    extends WicketBootSecuredWebApplication
{
    protected boolean isInitialized = false;

    @Override
    protected void init()
    {
        super.init();
        
//        initSpring();
        
        initStatelessChecker();
        
        if (!isInitialized) {
            initOnce();
            
            isInitialized = true;
        }
    }

    protected void initOnce()
    {
        // Allow nested string resource resolving using "#(key)"
        initNestedStringResourceLoader();
        
//        // This should avoid some application-reloading while working on I18N
//        getResourceSettings().setThrowExceptionOnMissingResource(false);
//        getResourceSettings().setCachingStrategy(new NoOpResourceCachingStrategy());
        
        // Enable dynamic switching between JQuery 1 and JQuery 2 based on the browser
        // identification. 
        initDynamicJQueryResourceReference();

        initDefaultPageMounts();
        
        initLogoReference();

        // Display stack trace instead of internal error
        initShowExceptionPage();

        initMDCLifecycle();
    }

    protected void initMDCLifecycle()
    {
        getRequestCycleListeners().add(new AbstractRequestCycleListener()
        {
            @Override
            public void onBeginRequest(RequestCycle cycle)
            {
                ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();
                try {
                    DocumentService repo = ctx.getBean(DocumentService.class);
                    MDC.put(Logging.KEY_REPOSITORY_PATH, repo.getDir().getAbsolutePath());
                }
                catch (NoSuchBeanDefinitionException e) {
                    // Well, if the document service is not there, then we don't need to
                    // configure the logging placeholder
                }
            }

            @Override
            public void onEndRequest(RequestCycle cycle)
            {
                MDC.remove(Logging.KEY_REPOSITORY_PATH);
            }
        });
    }

    protected void initShowExceptionPage()
    {
        Properties settings = SettingsUtil.getSettings();
        if ("true".equalsIgnoreCase(settings.getProperty("debug.showExceptionPage"))) {
            getExceptionSettings().setUnexpectedExceptionDisplay(
                    ExceptionSettings.SHOW_EXCEPTION_PAGE);
        }
    }

    protected void initLogoReference()
    {
{'fraction_non_alphanumeric': 0.08580204101852769, 'fraction_numerical': 0.003368671356385614, 'mean_word_length': 3.2198996655518393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2021292', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2669, 'n_words': 587}