Search in sources :

Example 1 with Session

use of ninja.session.Session in project ninja by ninjaframework.

the class TemplateEngineFreemarkerTest method before.

@Before
public void before() throws Exception {
    //Setup that allows to to execute invoke(...) in a very minimal version.
    when(ninjaProperties.getWithDefault(FREEMARKER_CONFIGURATION_FILE_SUFFIX, ".ftl.html")).thenReturn(".ftl.html");
    templateEngineFreemarker = new TemplateEngineFreemarker(messages, lang, logger, templateEngineHelper, templateEngineManager, templateEngineFreemarkerReverseRouteMethod, templateEngineFreemarkerAssetsAtMethod, templateEngineFreemarkerWebJarsAtMethod, ninjaProperties);
    when(lang.getLanguage(any(Context.class), any(Optional.class))).thenReturn(Optional.<String>empty());
    Session session = Mockito.mock(Session.class);
    when(session.isEmpty()).thenReturn(true);
    when(context.getSession()).thenReturn(session);
    when(context.getRoute()).thenReturn(route);
    when(lang.getLocaleFromStringOrDefault(any(Optional.class))).thenReturn(Locale.ENGLISH);
    FlashScope flashScope = Mockito.mock(FlashScope.class);
    Map<String, String> flashScopeData = new HashMap<>();
    when(flashScope.getCurrentFlashCookieData()).thenReturn(flashScopeData);
    when(context.getFlashScope()).thenReturn(flashScope);
    when(templateEngineHelper.getTemplateForResult(any(Route.class), any(Result.class), Mockito.anyString())).thenReturn("views/template.ftl.html");
    writer = new StringWriter();
    ResponseStreams responseStreams = mock(ResponseStreams.class);
    when(context.finalizeHeaders(any(Result.class))).thenReturn(responseStreams);
    when(responseStreams.getWriter()).thenReturn(writer);
}
Also used : Context(ninja.Context) Optional(java.util.Optional) StringWriter(java.io.StringWriter) ResponseStreams(ninja.utils.ResponseStreams) HashMap(java.util.HashMap) FlashScope(ninja.session.FlashScope) Route(ninja.Route) Session(ninja.session.Session) Result(ninja.Result) Before(org.junit.Before)

Example 2 with Session

use of ninja.session.Session in project ninja by ninjaframework.

the class LoginLogoutController method loginPost.

public Result loginPost(@Param("username") String username, @Param("password") String password, @Param("rememberMe") Boolean rememberMe, Context context) {
    boolean isUserNameAndPasswordValid = userDao.isUserAndPasswordValid(username, password);
    if (isUserNameAndPasswordValid) {
        Session session = context.getSession();
        session.put("username", username);
        if (rememberMe != null && rememberMe) {
            session.setExpiryTime(24 * 60 * 60 * 1000L);
        }
        context.getFlashScope().success("login.loginSuccessful");
        return Results.redirect("/");
    } else {
        // something is wrong with the input or password not found.
        context.getFlashScope().put("username", username);
        context.getFlashScope().put("rememberMe", rememberMe);
        context.getFlashScope().error("login.errorLogin");
        return Results.redirect("/login");
    }
}
Also used : Session(ninja.session.Session)

Aggregations

Session (ninja.session.Session)2 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 Context (ninja.Context)1 Result (ninja.Result)1 Route (ninja.Route)1 FlashScope (ninja.session.FlashScope)1 ResponseStreams (ninja.utils.ResponseStreams)1 Before (org.junit.Before)1