KEYCLOAK-8162 Added resourcesPath

This commit is contained in:
Vidhyadharan Deivamani 2019-11-20 21:30:14 +05:30 committed by Stian Thorgersen
parent ff4c94506f
commit 318b290f55
2 changed files with 37 additions and 1 deletions

View file

@ -18,6 +18,7 @@
package org.keycloak.email.freemarker;
import java.io.IOException;
import java.net.URI;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collections;
@ -37,13 +38,14 @@ import org.keycloak.email.freemarker.beans.ProfileBean;
import org.keycloak.events.Event;
import org.keycloak.events.EventType;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakUriInfo;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.sessions.AuthenticationSessionModel;
import org.keycloak.theme.FreeMarkerException;
import org.keycloak.theme.FreeMarkerUtil;
import org.keycloak.theme.Theme;
import org.keycloak.theme.ThemeProvider;
import org.keycloak.forms.login.freemarker.model.UrlBean;
import org.keycloak.theme.beans.LinkExpirationFormatterMethod;
import org.keycloak.theme.beans.MessageFormatterMethod;
@ -184,9 +186,12 @@ public class FreeMarkerEmailTemplateProvider implements EmailTemplateProvider {
protected void addLinkInfoIntoAttributes(String link, long expirationInMinutes, Map<String, Object> attributes) throws EmailException {
attributes.put("link", link);
attributes.put("linkExpiration", expirationInMinutes);
KeycloakUriInfo uriInfo = session.getContext().getUri();
URI baseUri = uriInfo.getBaseUri();
try {
Locale locale = session.getContext().resolveLocale(user);
attributes.put("linkExpirationFormatter", new LinkExpirationFormatterMethod(getTheme().getMessages(locale), locale));
attributes.put("url", new UrlBean(realm, getTheme(), baseUri, null));
} catch (IOException e) {
throw new EmailException("Failed to template email", e);
}

View file

@ -93,6 +93,10 @@ public class UrlBean {
return Urls.firstBrokerLoginProcessor(baseURI, realm).toString();
}
public String getResourcesUrl() {
return getHostPortScheme(baseURI)+ getResourcesPath();
}
public String getOauthAction() {
if (this.actionuri != null) {
return this.actionuri.getPath();
@ -105,4 +109,31 @@ public class UrlBean {
URI uri = Urls.themeRoot(baseURI);
return uri.getPath() + "/" + theme.getType().toString().toLowerCase() +"/" + theme.getName();
}
private String getHostPortScheme(URI baseUri) {
StringBuilder hostPostScheme = new StringBuilder("");
String scheme = baseUri.getScheme();
int port = baseUri.getPort();
String host = baseUri.getHost();
if (scheme == null) {
scheme = "http";
}
hostPostScheme
.append(scheme)
.append("://");
if (host == null) {
host = "localhost";
}
hostPostScheme.append(host);
if (port != -1) {
hostPostScheme.append(":")
.append(port);
}
return hostPostScheme.toString();
}
}