use of com.runwaysdk.mvc.RedirectResponse in project geoprism-registry by terraframe.
the class RegistrySessionController method ologin.
@Endpoint(method = ServletMethod.GET, error = ErrorSerialization.JSON)
public ResponseIF ologin(ServletRequestIF req, @RequestParamter(name = "code", required = true) String code, @RequestParamter(name = "state", required = true) String state) throws MalformedURLException, JSONException {
final SessionController geoprism = new SessionController();
// URL url = new URL(req.getScheme(), req.getServerName(), req.getServerPort(), req.getContextPath());
//
// String redirect = url.toString();
JSONObject stateObject = new JSONObject(state);
String serverId = stateObject.getString(OauthServerIF.SERVER_ID);
Locale[] locales = geoprism.getLocales(req);
WebClientSession clientSession = WebClientSession.createAnonymousSession(locales);
try {
ClientRequestIF clientRequest = clientSession.getRequest();
String cgrSessionJsonString = RegistrySessionServiceDTO.ologin(clientRequest, serverId, code, LocaleSerializer.serialize(locales), null);
JsonObject cgrSessionJson = (JsonObject) JsonParser.parseString(cgrSessionJsonString);
final String sessionId = cgrSessionJson.get("sessionId").getAsString();
final String username = cgrSessionJson.get("username").getAsString();
geoprism.createSession(req, sessionId, locales);
clientRequest = (ClientRequestIF) req.getAttribute(ClientConstants.CLIENTREQUEST);
JsonArray roles = (JsonArray) JsonParser.parseString(RoleViewDTO.getCurrentRoles(clientRequest));
JsonArray roleDisplayLabels = (JsonArray) JsonParser.parseString(RoleViewDTO.getCurrentRoleDisplayLabels(clientRequest));
JsonObject cookieJson = new JsonObject();
cookieJson.addProperty("loggedIn", clientRequest.isLoggedIn());
cookieJson.add("roles", roles);
cookieJson.add("roleDisplayLabels", roleDisplayLabels);
cookieJson.addProperty("userName", username);
cookieJson.addProperty("version", ClientConfigurationService.getServerVersion());
// final Locale sessionLocale = Session.getCurrentLocale();
//
// JsonArray installedLocalesArr = new JsonArray();
// Set<SupportedLocaleIF> installedLocales = LocalizationFacade.getSupportedLocales();
// for (SupportedLocaleIF supportedLocale : installedLocales)
// {
// Locale locale = supportedLocale.getLocale();
//
// JsonObject locObj = new JsonObject();
// locObj.addProperty("language", locale.getDisplayLanguage(sessionLocale));
// locObj.addProperty("country", locale.getDisplayCountry(sessionLocale));
// locObj.addProperty("name", locale.getDisplayName(sessionLocale));
// locObj.addProperty("variant", locale.getDisplayVariant(sessionLocale));
//
// installedLocalesArr.add(locObj);
// }
JsonArray jaLocales = ServiceFactory.getRegistryService().getLocales(clientRequest.getSessionId());
cookieJson.add("installedLocales", jaLocales);
final String cookieValue = URLEncoder.encode(cookieJson.toString(), "UTF-8");
Cookie cookie = new Cookie("user", cookieValue);
cookie.setMaxAge(-1);
RedirectResponse response = new RedirectResponse("/");
response.addCookie(cookie);
return response;
} catch (Throwable t) {
Locale locale = CommonProperties.getDefaultLocale();
if (locales.length > 0) {
locale = locales[0];
}
String errorMessage = RunwayException.localizeThrowable(t, locale);
try {
errorMessage = URLEncoder.encode(errorMessage, StandardCharsets.UTF_8.name());
} catch (Throwable t2) {
throw new ProgrammingErrorException(t2);
}
RedirectResponse response = new RedirectResponse("/cgr/manage#/login/" + errorMessage);
return response;
} finally {
clientSession.logout();
}
}
Aggregations