Search in sources :

Example 1 with ClientRequestIF

use of com.runwaysdk.constants.ClientRequestIF 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 ClientRequestIF

use of com.runwaysdk.constants.ClientRequestIF 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 ClientRequestIF

use of com.runwaysdk.constants.ClientRequestIF in project geoprism-registry by terraframe.

the class TestDataSet method runAsUser.

public static void runAsUser(TestUserInfo user, RequestExecutor executor) {
    ClientSession session = null;
    try {
        session = ClientSession.createUserSession(user.getUsername(), user.getPassword(), new Locale[] { CommonProperties.getDefaultLocale() });
        ClientRequestIF request = session.getRequest();
        TestRegistryAdapterClient adapter = new TestRegistryAdapterClient();
        adapter.setClientRequest(request);
        adapter.refreshMetadataCache();
        try {
            executor.execute(request, adapter);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } finally {
        if (session != null) {
            session.logout();
        }
    }
}
Also used : Locale(java.util.Locale) ClientSession(com.runwaysdk.ClientSession) ClientRequestIF(com.runwaysdk.constants.ClientRequestIF)

Example 4 with ClientRequestIF

use of com.runwaysdk.constants.ClientRequestIF in project geoprism-registry by terraframe.

the class RegistrySessionController method login.

@Endpoint(method = ServletMethod.POST, error = ErrorSerialization.JSON)
public ResponseIF login(ServletRequestIF req, @RequestParamter(name = "username", required = true) String username, @RequestParamter(name = "password", required = true) String password) throws JSONException {
    if (username != null) {
        username = username.trim();
    }
    Locale[] locales = this.getLocales(req);
    // Locale sessionLocale = req.getLocale();
    // 
    // JSONArray installedLocalesArr = new JSONArray();
    // Collection<Locale> installedLocales = LocalizationFacade.getInstalledLocales();
    // for (Locale loc : installedLocales)
    // {
    // JSONObject locObj = new JSONObject();
    // locObj.put("language", loc.getDisplayLanguage(sessionLocale));
    // locObj.put("country", loc.getDisplayCountry(sessionLocale));
    // locObj.put("name", loc.getDisplayName(sessionLocale));
    // locObj.put("variant", loc.getDisplayVariant(sessionLocale));
    // 
    // installedLocalesArr.put(locObj);
    // }
    ClientRequestIF clientRequest = loginWithLocales(req, username, password, locales);
    JSONArray jaLocales = new JSONArray(ServiceFactory.getRegistryService().getLocales(clientRequest.getSessionId()).toString());
    JsonArray roles = JsonParser.parseString(RoleViewDTO.getCurrentRoles(clientRequest)).getAsJsonArray();
    JsonArray roleDisplayLabels = JsonParser.parseString(RoleViewDTO.getCurrentRoleDisplayLabels(clientRequest)).getAsJsonArray();
    JsonObject cookieValue = new JsonObject();
    cookieValue.addProperty("loggedIn", clientRequest.isLoggedIn());
    cookieValue.add("roles", roles);
    cookieValue.add("roleDisplayLabels", roleDisplayLabels);
    cookieValue.addProperty("userName", username);
    cookieValue.addProperty("version", ClientConfigurationService.getServerVersion());
    CookieResponse response = new CookieResponse("user", -1, cookieValue.toString());
    response.set("installedLocales", jaLocales);
    return response;
}
Also used : Locale(java.util.Locale) JsonArray(com.google.gson.JsonArray) JSONArray(org.json.JSONArray) JsonObject(com.google.gson.JsonObject) CookieResponse(com.runwaysdk.mvc.CookieResponse) ClientRequestIF(com.runwaysdk.constants.ClientRequestIF) Endpoint(com.runwaysdk.mvc.Endpoint)

Aggregations

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