Example usage for org.apache.wicket.util.cookies CookieUtils save

List of usage examples for org.apache.wicket.util.cookies CookieUtils save

Introduction

In this page you can find the example usage for org.apache.wicket.util.cookies CookieUtils save.

Prototype

public final void save(String key, final String value) 

Source Link

Document

Create a Cookie with key and value and save it in the browser with the next response

Usage

From source file:dk.frankbille.scoreboard.security.LoginPage.java

License:Open Source License

private void addLogin() {
    final LoginUser user = new LoginUser();
    final Form<Void> loginForm = new Form<Void>("loginForm") {
        private static final long serialVersionUID = 1L;

        @Override/*w ww. jav a 2s  .  c om*/
        protected void onSubmit() {
            if (user.isLoginPersistent()) {
                CookieDefaults settings = new CookieDefaults();
                // 14 days
                settings.setMaxAge(60 * 60 * 24 * 14);
                CookieUtils cookieUtils = new CookieUtils(settings);
                cookieUtils.save("loginUsername", user.getUsername());
                cookieUtils.save("loginPassword", user.getPassword());
            }
            authenticated();
        }
    };
    add(loginForm);

    loginForm.add(new FeedbackPanel("loginMessages", new ContainerFeedbackMessageFilter(loginForm)));

    final TextField<String> usernameField = new TextField<String>("usernameField",
            new PropertyModel<String>(user, "username"));
    loginForm.add(usernameField);

    final PasswordTextField passwordField = new PasswordTextField("passwordField",
            new PropertyModel<String>(user, "password"));
    loginForm.add(passwordField);

    CheckBox persistentField = new CheckBox("persistentField",
            new PropertyModel<Boolean>(user, "loginPersistent"));
    loginForm.add(persistentField);

    loginForm.add(new AbstractFormValidator() {
        private static final long serialVersionUID = 1L;

        @Override
        public FormComponent<?>[] getDependentFormComponents() {
            return new FormComponent<?>[] { usernameField, passwordField };
        }

        @Override
        public void validate(Form<?> form) {
            boolean authenticated = ScoreBoardSession.get().authenticate(usernameField.getInput(),
                    passwordField.getInput());
            if (false == authenticated) {
                error(usernameField, "incorrectUsernamePassword");
            }
        }
    });
}

From source file:dk.teachus.frontend.pages.UnAuthenticatedBasePage.java

License:Apache License

private void signin() {
    TeachUsSession teachUsSession = TeachUsSession.get();

    teachUsSession.signIn(user.getUsername(), user.getPassword());

    if (teachUsSession.isAuthenticated()) {
        if (user.isRemember()) {
            CookieDefaults settings = new CookieDefaults();
            // 14 days
            settings.setMaxAge(60 * 60 * 24 * 14);
            CookieUtils cookieUtils = new CookieUtils(settings);
            cookieUtils.save("username", user.getUsername());
            cookieUtils.save("password", user.getPassword());
        }/* w w w .ja v a2 s  .c o  m*/

        if (continueToOriginalDestination() == false) {
            throw new RestartResponseAtInterceptPageException(Application.get().getHomePage());
        }
    }
}

From source file:org.apache.isis.viewer.wicket.ui.components.widgets.themepicker.ThemeChooser.java

License:Apache License

private void saveActiveThemeToCookie(String themeName) {
    CookieUtils cookieUtils = new CookieUtils();
    cookieUtils.save(ISIS_THEME_COOKIE_NAME, themeName);
}

From source file:org.apache.isis.viewer.wicket.ui.pages.accmngt.password_reset.PasswordResetEmailPanel.java

License:Apache License

/**
 * Constructor/*from   ww  w.  j a va  2  s.c o m*/
 *
 * @param id
 *            the component id
 */
public PasswordResetEmailPanel(final String id) {
    super(id);

    StatelessForm<Void> form = new StatelessForm<>("signUpForm");
    addOrReplace(form);

    final RequiredTextField<String> emailField = new RequiredTextField<>("email", Model.of(""));
    emailField.setLabel(new ResourceModel("emailLabel"));
    emailField.add(EmailAddressValidator.getInstance());
    emailField.add(EmailAvailableValidator.EXISTS);

    FormGroup formGroup = new FormGroup("formGroup", emailField);
    form.add(formGroup);

    formGroup.add(emailField);

    Button signUpButton = new Button("passwordResetSubmit") {
        @Override
        public void onSubmit() {
            super.onSubmit();

            String email = emailField.getModelObject();

            String confirmationUrl = emailVerificationUrlService.createVerificationUrl(PageType.PASSWORD_RESET,
                    email);

            /**
             * We have to init() the services here because the Isis runtime is not available to us
             * (guice will have instantiated a new instance of the service).
             *
             * We do it this way just so that the programming model for the EmailService is similar to regular Isis-managed services.
             */
            emailNotificationService.init();
            emailService.init();

            final PasswordResetEvent passwordResetEvent = new PasswordResetEvent(email, confirmationUrl,
                    applicationName);
            boolean emailSent = emailNotificationService.send(passwordResetEvent);
            if (emailSent) {
                Map<String, String> map = new HashMap<>();
                map.put("email", email);
                IModel<Map<String, String>> model = Model.ofMap(map);
                String emailSentMessage = getString("emailSentMessage", model);

                CookieUtils cookieUtils = new CookieUtils();
                cookieUtils.save(AccountManagementPageAbstract.FEEDBACK_COOKIE_NAME, emailSentMessage);
                pageNavigationService.navigateTo(PageType.SIGN_IN);
            }
        }
    };

    form.add(signUpButton);
}

From source file:org.apache.isis.viewer.wicket.ui.pages.accmngt.signup.RegistrationFormPanel.java

License:Apache License

/**
 * Constructor/*from   w  w w  . j  ava2 s .c o m*/
 *
 * @param id
 *            the component id
 */
public RegistrationFormPanel(final String id) {
    super(id);

    addOrReplace(new NotificationPanel("feedback"));

    StatelessForm<Void> form = new StatelessForm<>("signUpForm");
    addOrReplace(form);

    final RequiredTextField<String> emailField = new RequiredTextField<>("email", Model.of(""));
    emailField.setLabel(new ResourceModel("emailLabel"));
    emailField.add(EmailAddressValidator.getInstance());
    emailField.add(EmailAvailableValidator.DOESNT_EXIST);

    FormGroup formGroup = new FormGroup("formGroup", emailField);
    form.add(formGroup);

    formGroup.add(emailField);

    Button signUpButton = new Button("signUp") {
        @Override
        public void onSubmit() {
            super.onSubmit();

            String email = emailField.getModelObject();
            String confirmationUrl = emailVerificationUrlService.createVerificationUrl(PageType.SIGN_UP_VERIFY,
                    email);

            /**
             * We have to init() the services here because the Isis runtime is not available to us
             * (guice will have instantiated a new instance of the service).
             *
             * We do it this way just so that the programming model for the EmailService is similar to regular Isis-managed services.
             */
            emailNotificationService.init();
            emailService.init();

            final EmailRegistrationEvent emailRegistrationEvent = new EmailRegistrationEvent(email,
                    confirmationUrl, applicationName);
            boolean emailSent = emailNotificationService.send(emailRegistrationEvent);
            if (emailSent) {
                Map<String, String> map = new HashMap<>();
                map.put("email", email);
                String emailSentMessage = getString("emailSentMessage", Model.ofMap(map));

                CookieUtils cookieUtils = new CookieUtils();
                cookieUtils.save(AccountManagementPageAbstract.FEEDBACK_COOKIE_NAME, emailSentMessage);
                pageNavigationService.navigateTo(PageType.SIGN_IN);
            }
        }
    };

    form.add(signUpButton);
}

From source file:org.apache.syncope.client.enduser.resources.InfoResource.java

License:Apache License

@Override
protected ResourceResponse newResourceResponse(final IResource.Attributes attributes) {
    ResourceResponse response = new ResourceResponse();

    try {//from   ww w. j  a  v a2  s.  co  m
        final CookieUtils sessionCookieUtils = SyncopeEnduserSession.get().getCookieUtils();
        // set XSRF_TOKEN cookie
        if (!SyncopeEnduserSession.get().isXsrfTokenGenerated()
                && (sessionCookieUtils.getCookie(SyncopeEnduserConstants.XSRF_COOKIE) == null
                        || StringUtils.isBlank(sessionCookieUtils.getCookie(SyncopeEnduserConstants.XSRF_COOKIE)
                                .getValue()))) {
            LOG.debug("Set XSRF-TOKEN cookie");
            SyncopeEnduserSession.get().setXsrfTokenGenerated(true);
            sessionCookieUtils.save(SyncopeEnduserConstants.XSRF_COOKIE,
                    SaltGenerator.generate(SyncopeEnduserSession.get().getId()));
        }
        response.setWriteCallback(new WriteCallback() {

            @Override
            public void writeData(final IResource.Attributes attributes) throws IOException {
                attributes.getResponse().write(MAPPER.writeValueAsString(PlatformInfoAdapter
                        .toPlatformInfoRequest(SyncopeEnduserSession.get().getPlatformInfo())));
            }
        });
        response.setStatusCode(Response.Status.OK.getStatusCode());
    } catch (Exception e) {
        LOG.error("Error retrieving syncope info", e);
        response.setError(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
                new StringBuilder().append("ErrorMessage{{ ").append(e.getMessage()).append(" }}").toString());
    }

    return response;
}

From source file:org.sakaiproject.profile2.tool.pages.BasePage.java

License:Educational Community License

/**
 * Set the cookie that stores the current tab index.
 * /*from  w ww  .  j  a  va  2  s .  c om*/
 * @param tabIndex the current tab index.
 */
protected void setTabCookie(int tabIndex) {

    CookieDefaults defaults = new CookieDefaults();
    defaults.setMaxAge(-1);

    CookieUtils utils = new CookieUtils(defaults);
    utils.save(ProfileConstants.TAB_COOKIE, String.valueOf(tabIndex));
}