2016-02-03 10:20:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
|
|
|
* and other contributors as indicated by the @author tags.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-01-28 11:52:18 +00:00
|
|
|
package org.keycloak.theme;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2016-07-04 08:42:15 +00:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.Reader;
|
2014-01-28 11:52:18 +00:00
|
|
|
import java.net.URL;
|
2016-07-04 08:42:15 +00:00
|
|
|
import java.nio.charset.Charset;
|
2015-02-21 16:36:47 +00:00
|
|
|
import java.util.Locale;
|
2014-01-28 11:52:18 +00:00
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
|
|
|
*/
|
|
|
|
public class ClassLoaderTheme implements Theme {
|
|
|
|
|
2014-05-12 22:34:17 +00:00
|
|
|
private String name;
|
2014-01-28 11:52:18 +00:00
|
|
|
|
2014-05-12 22:34:17 +00:00
|
|
|
private String parentName;
|
2014-01-28 11:52:18 +00:00
|
|
|
|
2014-05-12 22:34:17 +00:00
|
|
|
private String importName;
|
2014-04-24 08:25:50 +00:00
|
|
|
|
2014-05-12 22:34:17 +00:00
|
|
|
private Type type;
|
2014-01-28 11:52:18 +00:00
|
|
|
|
2014-05-12 22:34:17 +00:00
|
|
|
private ClassLoader classLoader;
|
2014-03-05 15:19:37 +00:00
|
|
|
|
2014-05-12 22:34:17 +00:00
|
|
|
private String templateRoot;
|
2014-01-28 11:52:18 +00:00
|
|
|
|
2014-05-12 22:34:17 +00:00
|
|
|
private String resourceRoot;
|
2014-01-28 11:52:18 +00:00
|
|
|
|
2015-02-21 16:36:47 +00:00
|
|
|
private String messageRoot;
|
2014-01-28 11:52:18 +00:00
|
|
|
|
2014-05-12 22:34:17 +00:00
|
|
|
private Properties properties;
|
2014-01-28 11:52:18 +00:00
|
|
|
|
2014-03-05 15:19:37 +00:00
|
|
|
public ClassLoaderTheme(String name, Type type, ClassLoader classLoader) throws IOException {
|
2014-05-12 22:34:17 +00:00
|
|
|
init(name, type, classLoader);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void init(String name, Type type, ClassLoader classLoader) throws IOException {
|
2014-01-28 11:52:18 +00:00
|
|
|
this.name = name;
|
|
|
|
this.type = type;
|
2014-03-05 15:19:37 +00:00
|
|
|
this.classLoader = classLoader;
|
2014-01-28 11:52:18 +00:00
|
|
|
|
2015-03-23 07:41:51 +00:00
|
|
|
String themeRoot = "theme/" + name + "/" + type.toString().toLowerCase() + "/";
|
2014-01-28 11:52:18 +00:00
|
|
|
|
|
|
|
this.templateRoot = themeRoot;
|
|
|
|
this.resourceRoot = themeRoot + "resources/";
|
2015-02-21 16:36:47 +00:00
|
|
|
this.messageRoot = themeRoot + "messages/";
|
2014-01-28 11:52:18 +00:00
|
|
|
this.properties = new Properties();
|
|
|
|
|
2014-03-05 15:19:37 +00:00
|
|
|
URL p = classLoader.getResource(themeRoot + "theme.properties");
|
2014-01-28 11:52:18 +00:00
|
|
|
if (p != null) {
|
2016-07-04 08:42:15 +00:00
|
|
|
Charset encoding = PropertiesUtil.detectEncoding(p.openStream());
|
|
|
|
try (Reader reader = new InputStreamReader(p.openStream(), encoding)) {
|
|
|
|
properties.load(reader);
|
|
|
|
}
|
2014-01-28 11:52:18 +00:00
|
|
|
this.parentName = properties.getProperty("parent");
|
2014-04-24 08:25:50 +00:00
|
|
|
this.importName = properties.getProperty("import");
|
2014-01-28 11:52:18 +00:00
|
|
|
} else {
|
|
|
|
this.parentName = null;
|
2014-04-24 08:25:50 +00:00
|
|
|
this.importName = null;
|
2014-01-28 11:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getParentName() {
|
|
|
|
return parentName;
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:25:50 +00:00
|
|
|
@Override
|
|
|
|
public String getImportName() {
|
|
|
|
return importName;
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:52:18 +00:00
|
|
|
@Override
|
|
|
|
public Type getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public URL getTemplate(String name) {
|
2014-03-05 15:19:37 +00:00
|
|
|
return classLoader.getResource(templateRoot + name);
|
2014-01-28 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InputStream getResourceAsStream(String path) {
|
2014-03-05 15:19:37 +00:00
|
|
|
return classLoader.getResourceAsStream(resourceRoot + path);
|
2014-01-28 11:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-02-21 16:36:47 +00:00
|
|
|
public Properties getMessages(Locale locale) throws IOException {
|
2015-09-08 17:28:17 +00:00
|
|
|
return getMessages("messages", locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Properties getMessages(String baseBundlename, Locale locale) throws IOException {
|
2015-02-21 16:36:47 +00:00
|
|
|
if(locale == null){
|
|
|
|
return null;
|
|
|
|
}
|
2014-01-28 11:52:18 +00:00
|
|
|
Properties m = new Properties();
|
2015-02-21 16:36:47 +00:00
|
|
|
|
2015-09-08 17:28:17 +00:00
|
|
|
URL url = classLoader.getResource(this.messageRoot + baseBundlename + "_" + locale.toString() + ".properties");
|
2014-01-28 11:52:18 +00:00
|
|
|
if (url != null) {
|
2016-07-04 08:42:15 +00:00
|
|
|
Charset encoding = PropertiesUtil.detectEncoding(url.openStream());
|
|
|
|
try (Reader reader = new InputStreamReader(url.openStream(), encoding)) {
|
|
|
|
m.load(reader);
|
|
|
|
}
|
2014-01-28 11:52:18 +00:00
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Properties getProperties() {
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|