/*
 * Copyright 2015 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @author tags. 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.keycloak.services.resources.admin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import org.keycloak.freemarker.Theme;

/**
 * Simple loader and cache for message bundles consumed by angular-translate.
 *
 * Note that these bundles are converted to JSON before being shipped to the UI.
 * Also, the content should be formatted such that it can be interpolated by
 * angular-translate.  This is somewhat different from an ordinary Java bundle.
 *
 * @author Stan Silvert ssilvert@redhat.com (C) 2015 Red Hat Inc.
 */
public class AdminMessagesLoader {
    private static final Map<String, Properties> allMessages = new HashMap<String, Properties>();

    static Properties getMessages(Theme theme, String strLocale) throws IOException {
        String allMessagesKey = theme.getName() + "_" + strLocale;
        Properties messages = allMessages.get(allMessagesKey);
        if (messages != null) return messages;

        Locale locale = Locale.forLanguageTag(strLocale);
        messages = theme.getMessages("admin-messages", locale);
        if (messages == null) return new Properties();

        allMessages.put(allMessagesKey, messages);
        return messages;
    }
}

@@ -1826,72 +1826,477 @@
 e);%0A
-        if (messages == null) return new Properties
+%0A        validateMessages(messages, strLocale);%0A%0A        allMessages.put(allMessagesKey, messages);%0A        return messages;%0A    %7D%0A%0A    private static void validateMessages(Properties messages, String strLocale) %7B%0A        if (messages == null) throw new NullPointerException(%22Unable to find admin-messages bundle for locale=%22 + strLocale);%0A%0A        if (allMessages.isEmpty()) return;%0A%0A        Properties standardBundle = allMessages.values().iterator().next
 ();%0A
-%0A
         
 allM
@@ -2295,75 +2295,842 @@
     
-allMessages.put(allMessagesKey, messages);%0A        return messages;
+if (standardBundle.keySet().containsAll(messages.keySet()) &&%0A            (messages.keySet().containsAll(standardBundle.keySet()))) %7B%0A            return; // it all checks out%0A        %7D%0A%0A        // otherwise, find the offending key%0A        for (Object key : standardBundle.keySet()) %7B%0A            if (!messages.containsKey(key)) %7B%0A                throw new RuntimeException(%22Key '%22 + key + %22' not found in admin-messages bundle for locale=%22 + strLocale);%0A            %7D%0A        %7D%0A%0A        for (Object key : messages.keySet()) %7B%0A            if (!standardBundle.containsKey(key)) %7B%0A                throw new RuntimeException(%22Key '%22 + key + %22' was found in admin-messages bundle for locale=%22 + strLocale +%0A                                           %22. However, this key is not found in previously loaded bundle.%22);%0A            %7D%0A        %7D
 %0A   

Validate resource bundles for admin console