Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Evolveum/midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaHonchar committed Oct 5, 2015
2 parents 0001148 + a9388fc commit 46ffc66
Show file tree
Hide file tree
Showing 21 changed files with 1,340 additions and 135 deletions.
Expand Up @@ -16,11 +16,10 @@

package com.evolveum.midpoint.web.component.menu.top;

import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.util.WebMiscUtil;
import org.apache.commons.io.IOUtils;
import com.evolveum.midpoint.web.security.LocaleDescriptor;
import com.evolveum.midpoint.web.security.MidPointApplication;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
Expand All @@ -34,88 +33,18 @@
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.*;
import java.util.Locale;

/**
* @author lazyman
*/
public class LocalePanel extends Panel {

private static final Trace LOGGER = TraceManager.getTrace(LocalePanel.class);
private static final String LOCALIZATION_DESCRIPTOR = "/localization/locale.properties";
private static final List<LocaleDescriptor> AVAILABLE_LOCALES;

private static final String PROP_NAME = ".name";
private static final String PROP_FLAG = ".flag";

private static final String ID_SELECT = "select";
private static final String ID_OPTIONS = "options";

static {
List<LocaleDescriptor> locales = new ArrayList<>();
try {
ClassLoader classLoader = LocalePanel.class.getClassLoader();
Enumeration<URL> urls = classLoader.getResources(LOCALIZATION_DESCRIPTOR);
while (urls.hasMoreElements()) {
final URL url = urls.nextElement();
LOGGER.debug("Found localization descriptor {}.", new Object[]{url.toString()});

Properties properties = new Properties();
Reader reader = null;
try {
reader = new InputStreamReader(url.openStream(), "utf-8");
properties.load(reader);

Map<String, Map<String, String>> localeMap = new HashMap<>();
Set<String> keys = (Set) properties.keySet();
for (String key : keys) {
String[] array = key.split("\\.");
if (array.length != 2) {
continue;
}

String locale = array[0];
Map<String, String> map = localeMap.get(locale);
if (map == null) {
map = new HashMap<>();
localeMap.put(locale, map);
}

map.put(key, properties.getProperty(key));
}

for (String key : localeMap.keySet()) {
Map<String, String> localeDefinition = localeMap.get(key);
if (!localeDefinition.containsKey(key + PROP_NAME)
|| !localeDefinition.containsKey(key + PROP_FLAG)) {
continue;
}

LocaleDescriptor descriptor = new LocaleDescriptor(
localeDefinition.get(key + PROP_NAME),
localeDefinition.get(key + PROP_FLAG),
WebMiscUtil.getLocaleFromString(key)
);
locales.add(descriptor);
}
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "Couldn't load localization", ex);
} finally {
IOUtils.closeQuietly(reader);
}
}

Collections.sort(locales);
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "Couldn't load locales", ex);
}

AVAILABLE_LOCALES = Collections.unmodifiableList(locales);
}

public LocalePanel(String id) {
super(id);

Expand All @@ -132,7 +61,8 @@ protected void onUpdate(AjaxRequestTarget target) {
});
select.setOutputMarkupId(true);
add(select);
SelectOptions<LocaleDescriptor> options = new SelectOptions<LocaleDescriptor>(ID_OPTIONS, AVAILABLE_LOCALES,
SelectOptions<LocaleDescriptor> options = new SelectOptions<LocaleDescriptor>(ID_OPTIONS,
MidPointApplication.AVAILABLE_LOCALES,
new IOptionRenderer<LocaleDescriptor>() {

@Override
Expand Down Expand Up @@ -168,7 +98,7 @@ private LocaleDescriptor getSelectedLocaleDescriptor() {
// returns 'sk' as a locale from session, while other browsers return 'sk_SK'.
// This is the reason, why in firefox selected locale is ignored (the commented
// condition is not met) so we are adding second condition to overcome this issue.
for (LocaleDescriptor desc : AVAILABLE_LOCALES) {
for (LocaleDescriptor desc : MidPointApplication.AVAILABLE_LOCALES) {
// if (locale.equals(desc.getLocale())
if (locale.equals(desc.getLocale()) || locale.getLanguage().equals(desc.getLocale().getLanguage())) {
return desc;
Expand Down
Expand Up @@ -99,6 +99,7 @@
<!-- OBSOLETE START -->
<link type="text/css" rel="stylesheet" href="css/silk.css"/>
<link type="text/css" rel="stylesheet" href="css/flag.css"/>
<link rel="stylesheet" type="text/css" href="css/bootstrap-select/bootstrap-multiselect.css"/>
<link rel="stylesheet" type="text/css" href="css/bootstrap-select/bootstrap-select.css"/>
<link type="text/css" rel="stylesheet" href="wro/all.css"/>
<link type="text/css" rel="stylesheet" href="wro/midpoint.css"/>
Expand Down Expand Up @@ -193,6 +194,7 @@ <h1 wicket:id="pageTitle">
</wicket:link>
<!-- OBSOLETE START -->
<script type="text/javascript" src="js/bootstrap-select/bootstrap-select.js"></script>
<script type="text/javascript" src="js/bootstrap-select/bootstrap-multiselect.js"></script>
<script type="text/javascript" src="js/ace/ace.js" charset="utf-8"></script>
<script type="text/javascript" src="wro/midpoint.js"></script>
<!-- OBSOLETE END-->
Expand Down
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.evolveum.midpoint.web.component.menu.top;
package com.evolveum.midpoint.web.security;

import com.evolveum.midpoint.web.util.WebMiscUtil;
import org.apache.commons.lang.StringUtils;
Expand All @@ -32,28 +32,18 @@ public class LocaleDescriptor implements Serializable, Comparable<LocaleDescript
private static final String PROPERTY_NAME = "name";
private static final String PROPERTY_FLAG = "flag";
private static final String PROPERTY_LOCALE = "locale";
private static final String PROPERTY_DEFAULT = "def";

private String name;
private String flag;
private Locale locale;
private boolean def;

public LocaleDescriptor(Properties properties) {
Validate.notNull(properties);

this.name = (String) properties.get(PROPERTY_NAME);
this.flag = (String) properties.get(PROPERTY_FLAG);

String locale = (String) properties.get(PROPERTY_LOCALE);
if (StringUtils.isEmpty(locale)) {
throw new IllegalStateException("Property file - locale descriptor doesn't contain property '"
+ PROPERTY_LOCALE + "' with locale definition.");
}
this.locale = WebMiscUtil.getLocaleFromString(locale);
}

public LocaleDescriptor(String name, String flag, Locale locale) {
public LocaleDescriptor(String name, String flag, String def, Locale locale) {
this.flag = flag;
this.locale = locale;
this.name = name;
this.def = Boolean.parseBoolean(def);
}

public String getFlag() {
Expand All @@ -68,6 +58,10 @@ public String getName() {
return name;
}

public boolean isDefault() {
return def;
}

@Override
public int compareTo(LocaleDescriptor o) {
if (o == null) {
Expand Down
Expand Up @@ -38,7 +38,9 @@
import com.evolveum.midpoint.web.resource.img.ImgResources;
import com.evolveum.midpoint.web.util.MidPointPageParametersEncoder;
import com.evolveum.midpoint.web.util.Utf8BundleStringResourceLoader;
import com.evolveum.midpoint.web.util.WebMiscUtil;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.wicket.RuntimeConfigurationType;
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
Expand All @@ -65,10 +67,11 @@

import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.net.URL;
import java.util.*;

/**
* @author lazyman
Expand All @@ -83,8 +86,79 @@ public class MidPointApplication extends AuthenticatedWebApplication {

public static final String WEB_APP_CONFIGURATION = "midpoint.webApplication";

public static final List<LocaleDescriptor> AVAILABLE_LOCALES;

private static final String LOCALIZATION_DESCRIPTOR = "/localization/locale.properties";

private static final String PROP_NAME = ".name";
private static final String PROP_FLAG = ".flag";
private static final String PROP_DEFAULT = ".default";

private static final Trace LOGGER = TraceManager.getTrace(MidPointApplication.class);

static {
List<LocaleDescriptor> locales = new ArrayList<>();
try {
ClassLoader classLoader = MidPointApplication.class.getClassLoader();
Enumeration<URL> urls = classLoader.getResources(LOCALIZATION_DESCRIPTOR);
while (urls.hasMoreElements()) {
final URL url = urls.nextElement();
LOGGER.debug("Found localization descriptor {}.", new Object[]{url.toString()});

Properties properties = new Properties();
Reader reader = null;
try {
reader = new InputStreamReader(url.openStream(), "utf-8");
properties.load(reader);

Map<String, Map<String, String>> localeMap = new HashMap<>();
Set<String> keys = (Set) properties.keySet();
for (String key : keys) {
String[] array = key.split("\\.");
if (array.length != 2) {
continue;
}

String locale = array[0];
Map<String, String> map = localeMap.get(locale);
if (map == null) {
map = new HashMap<>();
localeMap.put(locale, map);
}

map.put(key, properties.getProperty(key));
}

for (String key : localeMap.keySet()) {
Map<String, String> localeDefinition = localeMap.get(key);
if (!localeDefinition.containsKey(key + PROP_NAME)
|| !localeDefinition.containsKey(key + PROP_FLAG)) {
continue;
}

LocaleDescriptor descriptor = new LocaleDescriptor(
localeDefinition.get(key + PROP_NAME),
localeDefinition.get(key + PROP_FLAG),
localeDefinition.get(key + PROP_DEFAULT),
WebMiscUtil.getLocaleFromString(key)
);
locales.add(descriptor);
}
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "Couldn't load localization", ex);
} finally {
IOUtils.closeQuietly(reader);
}
}

Collections.sort(locales);
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "Couldn't load locales", ex);
}

AVAILABLE_LOCALES = Collections.unmodifiableList(locales);
}

@Autowired
transient ModelService model;
@Autowired
Expand Down Expand Up @@ -250,6 +324,30 @@ public String getString(String key) {
return loader.loadStringResource((Class) null, key, null, null, null);
}

public static boolean containsLocale(Locale locale) {
if (locale == null) {
return false;
}

for (LocaleDescriptor descriptor : AVAILABLE_LOCALES) {
if (locale.equals(descriptor.getLocale())) {
return true;
}
}

return false;
}

public static Locale getDefaultLocale() {
for (LocaleDescriptor descriptor : AVAILABLE_LOCALES) {
if (descriptor.isDefault()) {
return descriptor.getLocale();
}
}

return new Locale("en", "US");
}

private static class ResourceFileFilter implements FilenameFilter {

@Override
Expand Down
Expand Up @@ -70,10 +70,13 @@ public MidPointAuthWebSession(Request request) {
super(request);
Injector.get().inject(this);

if (getLocale() == null) {
Locale locale = getLocale();
LOGGER.debug("Found locale {}", locale);
if (locale == null || !MidPointApplication.containsLocale(locale)) {
//default locale for web application
setLocale(new Locale("en", "US"));
setLocale(MidPointApplication.getDefaultLocale());
}
LOGGER.debug("Using {} as locale", getLocale());
}

@Override
Expand Down

0 comments on commit 46ffc66

Please sign in to comment.