keycloak-service
公開メンバ関数 | 非公開メンバ関数 | 非公開変数類 | 静的非公開変数類 | 全メンバ一覧
org.keycloak.services.error.KeycloakErrorHandler クラス
org.keycloak.services.error.KeycloakErrorHandler の継承関係図
Inheritance graph
org.keycloak.services.error.KeycloakErrorHandler 連携図
Collaboration graph

公開メンバ関数

Response toResponse (Throwable throwable)
 

非公開メンバ関数

int getStatusCode (Throwable throwable)
 
RealmModel resolveRealm ()
 
Map< String, Object > initAttributes (RealmModel realm, Theme theme, Locale locale, int statusCode) throws IOException
 

非公開変数類

KeycloakSession session
 
HttpHeaders headers
 
HttpResponse response
 

静的非公開変数類

static final Logger logger = Logger.getLogger(KeycloakErrorHandler.class)
 
static final Pattern realmNamePattern = Pattern.compile(".*/realms/([^/]+).*")
 

詳解

関数詳解

◆ getStatusCode()

int org.keycloak.services.error.KeycloakErrorHandler.getStatusCode ( Throwable  throwable)
inlineprivate
88  {
89  int status = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
90  if (throwable instanceof WebApplicationException) {
91  WebApplicationException ex = (WebApplicationException) throwable;
92  status = ex.getResponse().getStatus();
93  }
94  if (throwable instanceof Failure) {
95  Failure f = (Failure) throwable;
96  status = f.getErrorCode();
97  }
98  return status;
99  }

◆ initAttributes()

Map<String, Object> org.keycloak.services.error.KeycloakErrorHandler.initAttributes ( RealmModel  realm,
Theme  theme,
Locale  locale,
int  statusCode 
) throws IOException
inlineprivate
122  {
123  Map<String, Object> attributes = new HashMap<>();
124  Properties messagesBundle = theme.getMessages(locale);
125 
126  attributes.put("statusCode", statusCode);
127 
128  attributes.put("realm", realm);
129  attributes.put("url", new UrlBean(realm, theme, session.getContext().getUri().getBaseUri(), null));
130  attributes.put("locale", new LocaleBean(realm, locale, session.getContext().getUri().getBaseUriBuilder(), messagesBundle));
131 
132 
133  String errorKey = statusCode == 404 ? Messages.PAGE_NOT_FOUND : Messages.INTERNAL_SERVER_ERROR;
134  String errorMessage = messagesBundle.getProperty(errorKey);
135 
136  attributes.put("message", new MessageBean(errorMessage, MessageType.ERROR));
137 
138  try {
139  attributes.put("msg", new MessageFormatterMethod(locale, theme.getMessages(locale)));
140  } catch (IOException e) {
141  e.printStackTrace();
142  }
143 
144  try {
145  attributes.put("properties", theme.getProperties());
146  } catch (IOException e) {
147  e.printStackTrace();
148  }
149 
150  return attributes;
151  }
KeycloakSession session
Definition: KeycloakErrorHandler.java:45

◆ resolveRealm()

RealmModel org.keycloak.services.error.KeycloakErrorHandler.resolveRealm ( )
inlineprivate
101  {
102  String path = session.getContext().getUri().getPath();
103  Matcher m = realmNamePattern.matcher(path);
104  String realmName;
105  if(m.matches()) {
106  realmName = m.group(1);
107  } else {
108  realmName = Config.getAdminRealm();
109  }
110 
111  RealmManager realmManager = new RealmManager(session);
112  RealmModel realm = realmManager.getRealmByName(realmName);
113  if (realm == null) {
114  realm = realmManager.getRealmByName(Config.getAdminRealm());
115  }
116 
117  session.getContext().setRealm(realm);
118 
119  return realm;
120  }
static final Pattern realmNamePattern
Definition: KeycloakErrorHandler.java:42
KeycloakSession session
Definition: KeycloakErrorHandler.java:45

◆ toResponse()

Response org.keycloak.services.error.KeycloakErrorHandler.toResponse ( Throwable  throwable)
inline
54  {
55  KeycloakTransaction tx = ResteasyProviderFactory.getContextData(KeycloakTransaction.class);
56  tx.setRollbackOnly();
57 
58  int statusCode = getStatusCode(throwable);
59 
60  if (statusCode >= 500 && statusCode <= 599) {
61  logger.error("Uncaught server error", throwable);
62  }
63 
64  if (!MediaTypeMatcher.isHtmlRequest(headers)) {
65  return Response.status(statusCode).build();
66  }
67 
68  try {
69  RealmModel realm = resolveRealm();
70 
71  Theme theme = session.theme().getTheme(Theme.Type.LOGIN);
72 
73  Locale locale = session.getContext().resolveLocale(null);
74 
75  FreeMarkerUtil freeMarker = new FreeMarkerUtil();
76  Map<String, Object> attributes = initAttributes(realm, theme, locale, statusCode);
77 
78  String templateName = "error.ftl";
79 
80  String content = freeMarker.processTemplate(attributes, templateName, theme);
81  return Response.status(statusCode).type(MediaType.TEXT_HTML_UTF_8_TYPE).entity(content).build();
82  } catch (Throwable t) {
83  logger.error("Failed to create error page", t);
84  return Response.serverError().build();
85  }
86  }
int getStatusCode(Throwable throwable)
Definition: KeycloakErrorHandler.java:88
RealmModel resolveRealm()
Definition: KeycloakErrorHandler.java:101
HttpHeaders headers
Definition: KeycloakErrorHandler.java:48
KeycloakSession session
Definition: KeycloakErrorHandler.java:45
Map< String, Object > initAttributes(RealmModel realm, Theme theme, Locale locale, int statusCode)
Definition: KeycloakErrorHandler.java:122
static final Logger logger
Definition: KeycloakErrorHandler.java:40

メンバ詳解

◆ headers

HttpHeaders org.keycloak.services.error.KeycloakErrorHandler.headers
private

◆ logger

final Logger org.keycloak.services.error.KeycloakErrorHandler.logger = Logger.getLogger(KeycloakErrorHandler.class)
staticprivate

◆ realmNamePattern

final Pattern org.keycloak.services.error.KeycloakErrorHandler.realmNamePattern = Pattern.compile(".*/realms/([^/]+).*")
staticprivate

◆ response

HttpResponse org.keycloak.services.error.KeycloakErrorHandler.response
private

◆ session

KeycloakSession org.keycloak.services.error.KeycloakErrorHandler.session
private

このクラス詳解は次のファイルから抽出されました: