Search in sources :

Example 1 with WebClientSession

use of com.runwaysdk.web.WebClientSession in project geoprism-registry by terraframe.

the class RegistrySessionController method loginWithLocales.

public ClientRequestIF loginWithLocales(ServletRequestIF req, String username, String password, Locale[] locales) {
    try {
        WebClientSession clientSession = WebClientSession.createUserSession(username, password, locales);
        ClientRequestIF clientRequest = clientSession.getRequest();
        req.getSession().setMaxInactiveInterval(CommonProperties.getSessionTime());
        req.getSession().setAttribute(ClientConstants.CLIENTSESSION, clientSession);
        req.setAttribute(ClientConstants.CLIENTREQUEST, clientRequest);
        ClientConfigurationService.handleSessionEvent(new SessionEvent(EventType.LOGIN_SUCCESS, clientRequest, username));
        return clientRequest;
    } catch (RuntimeException e) {
        ClientConfigurationService.handleSessionEvent(new SessionEvent(EventType.LOGIN_FAILURE, null, username));
        throw e;
    }
}
Also used : WebClientSession(com.runwaysdk.web.WebClientSession) SessionEvent(net.geoprism.SessionEvent) ClientRequestIF(com.runwaysdk.constants.ClientRequestIF)

Example 2 with WebClientSession

use of com.runwaysdk.web.WebClientSession 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();
    }
}
Also used : Locale(java.util.Locale) Cookie(javax.servlet.http.Cookie) RedirectResponse(com.runwaysdk.mvc.RedirectResponse) JsonObject(com.google.gson.JsonObject) ClientRequestIF(com.runwaysdk.constants.ClientRequestIF) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) WebClientSession(com.runwaysdk.web.WebClientSession) JsonArray(com.google.gson.JsonArray) JSONObject(org.json.JSONObject) SessionController(net.geoprism.SessionController) Endpoint(com.runwaysdk.mvc.Endpoint)

Example 3 with WebClientSession

use of com.runwaysdk.web.WebClientSession in project geoprism-registry by terraframe.

the class NotificationEndpoint method onOpen.

@OnOpen
public void onOpen(Session session, EndpointConfig config) throws IOException {
    HttpSession s = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
    WebClientSession clientSession = (WebClientSession) s.getAttribute(ClientConstants.CLIENTSESSION);
    this.session = session;
    if (!clientSession.getRequest().isPublicUser()) {
        this.userId = clientSession.getRequest().getSessionUser().getOid();
    }
    if (this.userId != null) {
        endpoints.add(this);
        logger.debug("Connecting websocket for user [" + this.userId + "]");
    }
}
Also used : WebClientSession(com.runwaysdk.web.WebClientSession) HttpSession(javax.servlet.http.HttpSession) OnOpen(javax.websocket.OnOpen)

Aggregations

WebClientSession (com.runwaysdk.web.WebClientSession)3 ClientRequestIF (com.runwaysdk.constants.ClientRequestIF)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ProgrammingErrorException (com.runwaysdk.dataaccess.ProgrammingErrorException)1 Endpoint (com.runwaysdk.mvc.Endpoint)1 RedirectResponse (com.runwaysdk.mvc.RedirectResponse)1 Locale (java.util.Locale)1 Cookie (javax.servlet.http.Cookie)1 HttpSession (javax.servlet.http.HttpSession)1 OnOpen (javax.websocket.OnOpen)1 SessionController (net.geoprism.SessionController)1 SessionEvent (net.geoprism.SessionEvent)1 JSONObject (org.json.JSONObject)1