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

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

Introduction

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

Prototype

public CookieUtils() 

Source Link

Document

Construct.

Usage

From source file:com.norconex.commons.wicket.markup.html.i18n.SessionLocaleUtils.java

License:Apache License

/**
 * Sets the locale to a cookie. //from  w  w  w.  j  a  v a2s . co  m
 * @param locale session locale
 */
public static void setCookieLocale(Locale locale) {
    new CookieUtils().save(COOKIE_LOCALE_KEY, locale.toString());
}

From source file:com.norconex.commons.wicket.markup.html.i18n.SessionLocaleUtils.java

License:Apache License

/**
 * Gets the locale from a cookie.  If the cookie does not have the locale
 * set, returns the default locale./*from  w w  w. j a v a 2  s . co m*/
 * @param defaultLocale default local when no cookie locale is set
 * @return locale from cookie
 */
public static Locale getCookieLocale(Locale defaultLocale) {
    String localeStr = new CookieUtils().load(COOKIE_LOCALE_KEY);
    Locale locale = null;
    if (StringUtils.isNotBlank(localeStr)) {
        locale = LocaleUtils.toLocale(localeStr);
    }
    if (locale == null) {
        locale = defaultLocale;
    }
    return locale;
}

From source file:com.norconex.jefmon.JEFMonSession.java

License:Apache License

private void initLocale() {
    String cookieLocale = new CookieUtils().load(SessionLocaleUtils.COOKIE_LOCALE_KEY);
    Locale[] locales = getApp().getSupportedLocales();
    Locale locale = null;// w  w  w.j  av  a  2 s. com
    if (StringUtils.isNotBlank(cookieLocale)) {
        locale = LocaleUtils.toLocale(cookieLocale);
    }
    if (!ArrayUtils.contains(locales, locale)) {
        locale = getLocale();
        if (!ArrayUtils.contains(locales, locale) && locale.getCountry() != null) {
            locale = new Locale(locale.getLanguage(), locale.getCountry());
        }
        if (!ArrayUtils.contains(locales, locale)) {
            locale = new Locale(locale.getLanguage());
        }
        if (!ArrayUtils.contains(locales, locale)) {
            locale = locales[0];
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("User initial locale is:" + locale);
        }
    }
    setLocale(locale);
}

From source file:dk.frankbille.scoreboard.ScoreBoardSession.java

License:Open Source License

public ScoreBoardSession(Request request) {
    super(request);
    Injector.get().inject(this);

    CookieUtils cookieUtils = new CookieUtils();
    String loginUsername = cookieUtils.load("loginUsername");
    String loginPassword = cookieUtils.load("loginPassword");
    if (false == Strings.isEmpty(loginUsername) && false == Strings.isEmpty(loginPassword)) {
        authenticate(loginUsername, loginPassword);
    }/*  w  w  w .j  a v  a2  s  .c  o  m*/
}

From source file:dk.frankbille.scoreboard.ScoreBoardSession.java

License:Open Source License

public void logout() {
    CookieUtils cookieUtils = new CookieUtils();
    cookieUtils.remove("loginUsername");
    cookieUtils.remove("loginPassword");
    user = null;// ww w  .  j  a  v a2s  .  co m
    invalidate();
}

From source file:dk.teachus.frontend.TeachUsSession.java

License:Apache License

public TeachUsSession(Request request) {
    super(request);

    CookieUtils cookieUtils = new CookieUtils();
    String username = cookieUtils.load("username");
    String password = cookieUtils.load("password");
    if (false == Strings.isEmpty(username) && false == Strings.isEmpty(password)) {
        signIn(username, password);// w ww. ja v  a 2  s  .c o m
    } else {
        changeLocale(getLocale());
    }
}

From source file:dk.teachus.frontend.TeachUsSession.java

License:Apache License

public void signOut() {
    CookieUtils cookieUtils = new CookieUtils();
    cookieUtils.remove("username");
    cookieUtils.remove("password");
    person = null;//  w  w  w  .j a v a  2s .c  o m
    invalidateNow();
}

From source file:gr.abiss.calipso.wicket.CalipsoApplication.java

License:Open Source License

private boolean attemptRememberMeAutoLogin() {
    String value = new CookieUtils().load(REMEMBER_ME);
    if (StringUtils.isNotBlank(value)) {
        int index = value.indexOf(':');
        if (index > -1) {
            String loginName = value.substring(0, index);
            String encodedPassword = value.substring(index + 1);
            // logger.debug("valid cookie, attempting authentication");
            User user = (User) getCalipso().loadUserByUsername(loginName);
            if (encodedPassword.equals(user.getPassword())) {
                ((CalipsoSession) Session.get()).setUser(user);
                // user.setRoleSpaceStdFieldList(getJtrac().findSpaceFieldsForUser(user));
                // ((CalipsoSession) Session.get()).setUser(user);

                // logger.debug("remember me login success");
                return true;
            }/*www.ja v a 2s .co m*/
        }
    }
    // no valid cookies were found
    return false;
}

From source file:it.av.es.web.BasePageSimple.java

License:Apache License

/**
 * Construct.// w w w. j  a  v  a2  s  .c om
 */
public BasePageSimple() {
    HtmlUtil.fixInitialHtml(this);
    titlePage = new Label("pageTitle", ":: EasyTrack - Eurocargo ::");
    add(titlePage);

    WebMarkupContainer googleCode = new WebMarkupContainer("googleCode");
    add(googleCode);
    googleCode.setVisible(
            getApplication().getConfigurationType().compareTo(RuntimeConfigurationType.DEPLOYMENT) >= 0);

    feedbackPanel = new CustomFeedbackPanel("feedBackPanel");
    feedbackPanel.add(new AbstractDefaultAjaxBehavior() {

        //            @Override
        //            public void onBeforeRespond(Map<String, Component> map, AjaxRequestTarget target) {
        //            }
        //            
        //            @Override
        //            public void onAfterRespond(Map<String, Component> map, IJavaScriptResponse response) {
        //                response.addJavaScript("jQuery('#" + electionContainer.getMarkupId() + "').fadeTo(300, 0.5, function() {})");
        //                response.addJavaScript("jQuery('#" + electionContainer.getMarkupId() + "').fadeTo(300, 1.0, function() {})");
        //            }
        //            
        @Override
        protected void respond(AjaxRequestTarget target) {
            target.appendJavaScript(
                    "jQuery('#" + feedbackPanel.getMarkupId() + "').fadeTo(300, 0.5, function() {})");

            // register the onSuccess listener that will execute Handlebars logic
            target.addListener(new IListener() {

                @Override
                public void onBeforeRespond(Map<String, Component> map, AjaxRequestTarget target) {
                    System.out.println("");
                }

                @Override
                public void onAfterRespond(Map<String, Component> map, IJavaScriptResponse response) {
                    response.addJavaScript(
                            "jQuery('#" + feedbackPanel.getMarkupId() + "').fadeTo(300, 0.5, function() {})");
                    response.addJavaScript(
                            "jQuery('#" + feedbackPanel.getMarkupId() + "').fadeTo(300, 1.0, function() {})");
                }
            });
        }
    });
    ;

    feedbackPanel.setOutputMarkupId(true);
    feedbackPanel.setOutputMarkupPlaceholderTag(true);
    add(feedbackPanel);

    session = ((SecuritySession) getSession());

    loggedInUser = session.getLoggedInUser();
    if (new CookieUtils().load(CookieUtil.LANGUAGE) != null) {
        getSession().setLocale(new Locale(new CookieUtils().load(CookieUtil.LANGUAGE)));
    } else {
        if (loggedInUser != null) {
            ((WebResponse) RequestCycle.get().getResponse())
                    .addCookie((new Cookie(CookieUtil.LANGUAGE, loggedInUser.getLanguage().getLanguage())));
            getSession().setLocale(new Locale(loggedInUser.getLanguage().getLanguage()));
        }
    }

    //BookmarkablePageLink goAccount = new BookmarkablePageLink<String>("goAccount", UserAccountPage.class, goAccountParameters);

    //        Label name = new Label("loggedInUser", loggedInUser != null ? loggedInUser.getFirstname() + " " +loggedInUser.getLastname() : "");
    //        add(name);

    add(new BookmarkablePageLink<String>("goUserManagerPage", UserManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(UserManagerPage.class)));
        }
    });
    add(new BookmarkablePageLink<String>("goProjectManagerPage", ProjectManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(ProjectManagerPage.class)));
        }
    });
    //        add(new BookmarkablePageLink<String>("goProductManagerPage", ProductManagerPage.class) {
    //            @Override
    //            protected void onBeforeRender() {
    //                super.onBeforeRender();
    //                setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
    //                        .isInstantiationAuthorized(ProductManagerPage.class)));
    //            }
    //        });
    add(new BookmarkablePageLink<String>("goOrderManagerPage", OrderManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(OrderManagerPage.class)));
        }
    });
    add(new BookmarkablePageLink<String>("goPlaceNewOrderPage", PlaceNewOrderPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(PlaceNewOrderPage.class)));
        }
    });
    add(new BookmarkablePageLink<String>("goCustomerManagerPage", CustomerManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(CustomerManagerPage.class)));
        }
    });
    add(new BookmarkablePageLink<String>("goCustomerNewPage", CustomerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(CustomerPage.class)));
        }
    });
    add(new BookmarkablePageLink<String>("goSignOut", SignOut.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(SignOut.class)));
        }
    });
    add(new BookmarkablePageLink<String>("goUserAccountPage", UserAccountPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible(loggedInUser != null && (getApplication().getSecuritySettings()
                    .getAuthorizationStrategy().isInstantiationAuthorized(UserAccountPage.class)));
            if (loggedInUser != null) {
                add(AttributeModifier.replace("value",
                        loggedInUser.getFirstname() + " " + loggedInUser.getLastname()));
            }
        }
    });
    add(new BookmarkablePageLink<String>("goGroupManagerPage", GroupManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible(loggedInUser != null && (getApplication().getSecuritySettings()
                    .getAuthorizationStrategy().isInstantiationAuthorized(GroupManagerPage.class)));
        }
    });

    add(new BookmarkablePageLink<String>("goGroupMembersManagerPage", GroupMembersManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible(loggedInUser != null && (getApplication().getSecuritySettings()
                    .getAuthorizationStrategy().isInstantiationAuthorized(GroupMembersManagerPage.class)));
        }
    });

    add(new BookmarkablePageLink<String>("goProductFamilyManagerPage", ProductFamilyManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible(loggedInUser != null && (getApplication().getSecuritySettings()
                    .getAuthorizationStrategy().isInstantiationAuthorized(ProductFamilyManagerPage.class)));
        }
    });

    //        BookmarkablePageLink goInfo = new BookmarkablePageLink("goInfo", AboutPage.class);
    //        add(goInfo);
    //        
    //        BookmarkablePageLink goPrivacy = new BookmarkablePageLink("goPrivacy", PrivacyPage.class);
    //        add(goPrivacy);
}

From source file:it.av.youeat.web.page.BasePage.java

License:Apache License

/**
 * Construct./*from w  ww  .  ja v a2 s .c  o  m*/
 */
public BasePage() {
    SecuritySession securitySession = ((SecuritySession) getSession());
    if (securitySession.getAuth() != null && securitySession.getAuth().isAuthenticated()) {
        isAuthenticated = true;
    }

    loggedInUser = securitySession.getLoggedInUser();
    if (new CookieUtils().load(CookieUtil.LANGUAGE) != null) {
        getSession().setLocale(new Locale(new CookieUtils().load(CookieUtil.LANGUAGE)));
    } else {
        if (loggedInUser != null) {
            ((WebResponse) RequestCycle.get().getResponse())
                    .addCookie((new Cookie(CookieUtil.LANGUAGE, loggedInUser.getLanguage().getLanguage())));
            getSession().setLocale(new Locale(loggedInUser.getLanguage().getLanguage()));
        }
    }
    HtmlUtil.fixInitialHtml(this);
    titlePage = new Label("pageTitle", ":: YouEat ::");
    add(titlePage);
    feedbackPanel = new YouEatFeedbackPanel("feedBackPanel");
    feedbackPanel.setOutputMarkupPlaceholderTag(true);
    add(feedbackPanel);

    add(new BookmarkablePageLink<String>("goUserPage", UserManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(UserManagerPage.class)));
        }
    });

    add(new BookmarkablePageLink<String>("goUserProfilePage", UserProfilePage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(UserProfilePage.class)));
        }
    });

    add(new BookmarkablePageLink<String>("goSearchRistorantePage", UserHomePage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(UserHomePage.class)));
        }
    });

    add(new BookmarkablePageLink<String>("goRistoranteAddNewPage", RistoranteAddNewPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(RistoranteAddNewPage.class)));
        }
    });

    long numberPendingFrienRequest = (getLoggedInUser() != null)
            ? eaterRelationService.getAllPendingFriendRequetToUsers(getLoggedInUser()).size()
            : 0;
    final Label pendingFriendRequests = new Label("pendingFriendRequests",
            new Model<String>("(" + numberPendingFrienRequest + ")"));
    pendingFriendRequests.setOutputMarkupPlaceholderTag(true);
    pendingFriendRequests.setVisible(numberPendingFrienRequest > 0);
    add(new BookmarkablePageLink<String>("goFriendPage", FriendsPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(FriendsPage.class)));
        }
    }.add(pendingFriendRequests));

    BookmarkablePageLink<String> goMessagesPage = new BookmarkablePageLink<String>("goMessagesPage",
            MessageListPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(MessageListPage.class)));
        }
    };
    add(goMessagesPage);
    long numberofMessages = (getLoggedInUser() != null)
            ? messageService.countIncomingMessages(getLoggedInUser())
            : 0;
    final Label numberMessages = new Label("numberMessages", new Model<Long>(numberofMessages));
    numberMessages.setOutputMarkupPlaceholderTag(true);
    // numberMessages.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(30)) {
    // @Override
    // protected void onPostProcessTarget(AjaxRequestTarget target) {
    // super.onPostProcessTarget(target);
    // long numberMessagesRefreshed = messageService.countMessages(getLoggedInUser());
    // numberMessages.setDefaultModelObject((Long) numberMessagesRefreshed);
    // numberMessages.setVisible(numberMessagesRefreshed > 0);
    // if (target != null) {
    // // target.appendJavascript("new Effect.Highlight($('" + numberMessages.getMarkupId() + "'));");
    // }
    // }
    // });
    numberMessages.setVisible(numberofMessages > 0);
    goMessagesPage.add(numberMessages);

    long numberUnreadMsgs = (getLoggedInUser() != null) ? messageService.countUnreadMessages(getLoggedInUser())
            : 0;

    final WebMarkupContainer separator = new WebMarkupContainer("separator");
    separator.setVisible(numberUnreadMsgs > 0);
    goMessagesPage.add(separator);

    final Label unreadMsgs = new Label("unreadMessages", new Model<Long>(numberUnreadMsgs));
    unreadMsgs.setOutputMarkupPlaceholderTag(true);
    unreadMsgs.setVisible(numberUnreadMsgs > 0);
    // unreadMsgs.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(30)) {
    // @Override
    // protected void onPostProcessTarget(AjaxRequestTarget target) {
    // super.onPostProcessTarget(target);
    // long numberUnreadMessagesRefreshed = messageService.countUnreadMessages(getLoggedInUser());
    // unreadMsgs.setDefaultModelObject((Long) numberUnreadMessagesRefreshed);
    // if (target != null) {
    // // target.appendJavascript("new Effect.Highlight($('" + unreadMsgs.getMarkupId() + "'));");
    // }
    // }
    // });
    goMessagesPage.add(unreadMsgs);

    Link<String> goItalian = new Link<String>("goItalian") {
        @Override
        public void onClick() {
            getSession().setLocale(Locales.ITALIAN);
            new CookieUtils().save(CookieUtil.LANGUAGE, Locales.ITALIAN.getLanguage());
        }

        @Override
        protected void onComponentTag(ComponentTag tag) {
            super.onComponentTag(tag);
            if (getSession().getLocale().getLanguage().equals(Locales.ITALIAN.getLanguage())) {
                tag.getAttributes().put("class", "selected");
            }
        }
    };
    add(goItalian);

    Link<String> goEnglish = new Link<String>("goEnglish") {
        @Override
        public void onClick() {
            getSession().setLocale(Locales.ENGLISH);
            new CookieUtils().save(CookieUtil.LANGUAGE, Locales.ENGLISH.getLanguage());
        }

        @Override
        protected void onComponentTag(ComponentTag tag) {
            super.onComponentTag(tag);
            if (getSession().getLocale().getLanguage().equals(Locales.ENGLISH.getLanguage())) {
                tag.getAttributes().put("class", "selected");
            }
        }
    };
    add(goEnglish);

    //        Link<String> goDutch = new Link<String>("goDutch") {
    //            @Override
    //            public void onClick() {
    //                getWebRequestCycle().getWebResponse().addCookie(
    //                        new Cookie(CookieUtil.LANGUAGE, Locales.DUTCH.getLanguage()));
    //                getSession().setLocale(Locales.DUTCH);
    //            }
    //
    //            @Override
    //            protected void onComponentTag(ComponentTag tag) {
    //                super.onComponentTag(tag);
    //                if (getSession().getLocale().getLanguage().equals(Locales.DUTCH.getLanguage())) {
    //                    tag.getAttributes().put("class", "selected");
    //                }
    //            }
    //        };
    //        add(goDutch);

    BookmarkablePageLink goSignOut = new BookmarkablePageLink("goSignOut", SignOut.class);

    BookmarkablePageLink goSignIn = new BookmarkablePageLink("goSignIn", SignIn.class);

    goSignIn.setOutputMarkupId(true);
    goSignOut.setOutputMarkupId(true);

    if (isAuthenticated) {
        goSignOut.setVisible(true);
        goSignIn.setVisible(false);
        // goSignUp.setVisible(false);
    } else {
        goSignOut.setVisible(false);
        goSignIn.setVisible(true);
        // goSignUp.setVisible(true);
    }
    add(goSignOut);
    add(goSignIn);
    // add(goSignUp);

    PageParameters goAccountParameters = new PageParameters();
    if (loggedInUser != null) {
        goAccountParameters.add(YoueatHttpParams.YOUEAT_ID, loggedInUser.getId());
    }

    BookmarkablePageLink goAccount;
    if (loggedInUser != null && loggedInUser.isSocialNetworkEater()) {
        goAccount = new BookmarkablePageLink<String>("goAccount", EaterFacebookAccountPage.class,
                goAccountParameters);
    } else {
        goAccount = new BookmarkablePageLink<String>("goAccount", EaterAccountPage.class, goAccountParameters);
    }
    goAccount.setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
            .isInstantiationAuthorized(EaterAccountPage.class)));

    Label name = new Label("name", loggedInUser != null ? loggedInUser.toString() : "");
    goAccount.add(name);
    add(goAccount);

    BookmarkablePageLink goInfo = new BookmarkablePageLink("goInfo", AboutPage.class);
    add(goInfo);

    BookmarkablePageLink goInfoTop = new BookmarkablePageLink("goInfoTop", AboutPage.class);
    add(goInfoTop);

    BookmarkablePageLink goIndex = new BookmarkablePageLink("goIndex", IndexRistoPage.class);
    add(goIndex);

    BookmarkablePageLink goPrivacy = new BookmarkablePageLink("goPrivacy", PrivacyPage.class);
    add(goPrivacy);

    BookmarkablePageLink goFeed = new BookmarkablePageLink("goFeed", FeedPage.class);
    add(goFeed);

    BookmarkablePageLink goInfoRestaurateurPage = new BookmarkablePageLink("goInfoRestaurateur",
            InfoForRestaurateurPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible(!isAuthenticated);
        }
    };
    add(goInfoRestaurateurPage);

    add(new BookmarkablePageLink<String>("goRistoManager", RistoranteManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(RistoranteManagerPage.class)));
        }
    });

    add(new BookmarkablePageLink<String>("activitiesManager", ActivitiesManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(ActivitiesManagerPage.class)));
        }
    });

    add(new BookmarkablePageLink<String>("commentsManager", CommentsManagerPage.class) {
        @Override
        protected void onBeforeRender() {
            super.onBeforeRender();
            setVisible((getApplication().getSecuritySettings().getAuthorizationStrategy()
                    .isInstantiationAuthorized(CommentsManagerPage.class)));
        }
    });

}