<commit_before>/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  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 org.wso2.carbon.uuf.api;

import org.wso2.carbon.uuf.exception.InvalidTypeException;
import org.wso2.carbon.uuf.exception.MalformedConfigurationException;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * Represents a Configuration object of a particular UUF component.
 */
public class Configuration extends HashMap<String, Object> {

    public static final String KEY_APP_CONTEXT = "appContext";
    public static final String KEY_DEFAULT_THEME = "defaultTheme";
    public static final String KEY_LOGIN_PAGE_URI = "loginPageUri";
    public static final String KEY_MENU = "menu";
    public static final String KEY_ERROR_PAGES = "errorPages";

    public Configuration(Map<?, ?> rawMap) {
        this(rawMap.size());
        for (Entry<?, ?> entry : rawMap.entrySet()) {
            if (entry.getKey() instanceof String) {
                super.put((String) entry.getKey(), entry.getValue());
            } else {
                throw new InvalidTypeException("Configurations must be a Map<String, Object>. Instead found a '" +
                                                       entry.getKey().getClass().getName() + "' key.");
            }
        }
    }

    private Configuration(int initialCapacity) {
        super(initialCapacity);
    }

    public String getAppContext() {
        Object appContextObj = get(KEY_APP_CONTEXT);
        if (appContextObj == null) {
            throw new MalformedConfigurationException(
                    "Cannot find the value of 'appContext' in the app configuration.");
        }
        if (!(appContextObj instanceof String)) {
            throw new InvalidTypeException(
                    "Value of 'appContext' in the app configuration must be a string. Instead found '" +
                            appContextObj.getClass().getName() + "'.");
        }
        String appContext = (String) appContextObj;
        if (appContext.isEmpty()) {
            throw new IllegalArgumentException("Value of 'appContext' in the app configuration cannot be empty.");
        }
        if (appContext.charAt(0) == '/') {
            throw new IllegalArgumentException(
                    "Value of 'appContext' in the app configuration must start with a '/'. Instead found '" +
                            appContext.charAt(0) + "' at the beginning.");
        }
        return appContext;
    }

    public String getDefaultThemeName() {
        Object defaultThemeNameObj = get(KEY_DEFAULT_THEME);
        if (defaultThemeNameObj == null) {
            throw new MalformedConfigurationException(
                    "Cannot find the value of 'defaultTheme' in the app configuration.");
        }
        if (defaultThemeNameObj instanceof String) {
            String defaultThemeName = (String) defaultThemeNameObj;
            if (defaultThemeName.isEmpty()) {
                throw new IllegalArgumentException("Default theme name cannot be empty.");
            }
            return defaultThemeName;
        } else {
            throw new InvalidTypeException(
                    "Value of 'defaultTheme' in the app configuration must be a string. Instead found '" +<commit_msg>added 'getServerAppContext' & 'getClientAppContext' methods to Configuration class<commit_after>/*
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  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 org.wso2.carbon.uuf.api;

import org.wso2.carbon.uuf.exception.InvalidTypeException;
import org.wso2.carbon.uuf.exception.MalformedConfigurationException;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * Represents a Configuration object of a particular UUF component.
 */
public class Configuration extends HashMap<String, Object> {

    public static final String KEY_APP_CONTEXT = "appContext";
    public static final String KEY_APP_CONTEXT_SERVER = "server";
    public static final String KEY_APP_CONTEXT_CLIENT = "client";
    public static final String KEY_DEFAULT_THEME = "defaultTheme";
    public static final String KEY_LOGIN_PAGE_URI = "loginPageUri";
    public static final String KEY_MENU = "menu";
    public static final String KEY_ERROR_PAGES = "errorPages";

    public Configuration(Map<?, ?> rawMap) {
        this(rawMap.size());
        for (Entry<?, ?> entry : rawMap.entrySet()) {
            if (entry.getKey() instanceof String) {
                super.put((String) entry.getKey(), entry.getValue());
            } else {
                throw new InvalidTypeException("Configurations must be a Map<String, Object>. Instead found a '" +
                                                       entry.getKey().getClass().getName() + "' key.");
            }
        }
    }

    private Configuration(int initialCapacity) {
        super(initialCapacity);
    }

    private Object getAppContext() {
        Object appContextObj = get(KEY_APP_CONTEXT);
        if (appContextObj == null) {
            return null;
        }
        if (!(appContextObj instanceof String) && !(appContextObj instanceof Map)) {
            throw new InvalidTypeException("Value of 'appContext' in the app configuration must be either a string " +
                                                   "or a Map<String, String>. Instead found '" +
                                                   appContextObj.getClass().getName() + "'.");
        }
        return appContextObj;
    }

    public String getServerAppContext() {
        Object appContextObj = getAppContext();
        if (appContextObj == null) {
            return null;
        }
        if (appContextObj instanceof String) {
            return (String) appContextObj;
        }
        // appContextObj must be a Map
        Map<?, ?> appContext = (Map) appContextObj;
        Object serverAppContextObj = appContext.get(KEY_APP_CONTEXT_SERVER);
        if (serverAppContextObj == null) {
            return null;
        }
        if (!(serverAppContextObj instanceof String)) {
            throw new InvalidTypeException(
                    "Value of 'server' in 'appContext' Map in the app configuration must be a string. Instead found '" +
                            serverAppContextObj.getClass().getName() + "'.");
        }
        return (String) serverAppContextObj;
    }

    public String getClientAppContext() {
        Object appContextObj = getAppContext();
        if (!(appContextObj instanceof Map)) {
            return null;
        }
        Map<?, ?> appContext = (Map) appContextObj;
        Object clientAppContextObj = appContext.get(KEY_APP_CONTEXT_CLIENT);
        if (clientAppContextObj == null) {
            return null;
        }
        if (!(clientAppContextObj instanceof String)) {
            throw new InvalidTypeException(
                    "Value of 'client' in 'appContext' Map in the app configuration must be a string. Instead found '" +
                            clientAppContextObj.getClass().getName() + "'.");
        }
        return (String) clientAppContextObj;
    }

    public String getDefaultThemeName() {
        Object defaultThemeNameObj = get(KEY_DEFAULT_THEME);
        if (defaultThemeNameObj == null) {
            throw new MalformedConfigurationException(
                    "Cannot find the value of 'defaultTheme' in the app configuration.");
        }
        if (defaultThemeNameObj instanceof String) {
            String defaultThemeName = (String) defaultThemeNameObj;
            if (defaultThemeName.isEmpty()) {
                throw new IllegalArgumentException("Default theme name cannot be empty.");
            }
            return defaultThemeName;
        } else {
            throw new InvalidTypeException(
                    "Value of 'defaultTheme' in the app configuration must be a string. Instead found '" +
{'fraction_non_alphanumeric': 0.09066232356134636, 'fraction_numerical': 0.0030401737242128123, 'mean_word_length': 2.390136179609864, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7150699', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2254, 'n_words': 749}