Example usage for org.apache.wicket.request.http WebResponse addCookie

List of usage examples for org.apache.wicket.request.http WebResponse addCookie

Introduction

In this page you can find the example usage for org.apache.wicket.request.http WebResponse addCookie.

Prototype

public abstract void addCookie(final Cookie cookie);

Source Link

Document

Add a cookie to the web response

Usage

From source file:net.databinder.auth.AuthDataSessionBase.java

License:Open Source License

/**
 * Sets cookie to remember the currently signed-in user. Sets max age to
 * value from getSignInCookieMaxAge().//  w  w  w.  jav  a 2 s.  c o m
 * @see AuthDataSessionBase#getSignInCookieMaxAge()
 */
protected void setCookie() {
    if (userModel == null)
        throw new WicketRuntimeException("User must be signed in when calling this method");

    T cookieUser = getUser();

    Cookie name, auth;
    try {
        name = new Cookie(getUserCookieName(), URLEncoder.encode(cookieUser.getUsername(), CHARACTER_ENCODING));
        auth = new Cookie(getAuthCookieName(), getApp().getToken(cookieUser));
    } catch (UnsupportedEncodingException e) {
        throw new WicketRuntimeException(e);
    }

    int maxAge = (int) getSignInCookieMaxAge().seconds();
    name.setMaxAge(maxAge);
    auth.setMaxAge(maxAge);

    WebResponse webResponse = (WebResponse) RequestCycle.get().getResponse();
    webResponse.addCookie(name);
    webResponse.addCookie(auth);
}

From source file:org.artifactory.common.wicket.util.CookieUtils.java

License:Open Source License

public static void setCookie(String name, Object value) {
    if (value == null) {
        clearCookie(name);/*from  w  ww . ja  v  a  2 s.c o  m*/
        return;
    }

    WebResponse response = (WebResponse) RequestCycle.get().getResponse();
    response.addCookie(new Cookie(name, value.toString()));
}

From source file:org.brixcms.web.BrixRequestCycleProcessor.java

License:Apache License

public String getWorkspace() {
    String workspace = getWorkspaceFromUrl();

    if (workspace != null) {
        return workspace;
    }/* w w  w . java2  s  .  c  o  m*/

    RequestCycle rc = RequestCycle.get();
    workspace = rc.getMetaData(WORKSPACE_METADATA);
    if (workspace == null) {
        WebRequest req = (WebRequest) RequestCycle.get().getRequest();
        WebResponse resp = (WebResponse) RequestCycle.get().getResponse();
        Cookie cookie = req.getCookie(COOKIE_NAME);
        workspace = getDefaultWorkspaceName();
        if (cookie != null) {
            if (cookie.getValue() != null)
                workspace = cookie.getValue();
        }
        if (!checkSession(workspace)) {
            workspace = getDefaultWorkspaceName();
        }
        if (workspace == null) {
            throw new IllegalStateException("Could not resolve jcr workspace to use for this request");
        }
        Cookie c = new Cookie(COOKIE_NAME, workspace);
        c.setPath("/");
        if (workspace.toString().equals(getDefaultWorkspaceName()) == false)
            resp.addCookie(c);
        else if (cookie != null)
            resp.clearCookie(cookie);
        rc.setMetaData(WORKSPACE_METADATA, workspace);
    }
    return workspace;
}

From source file:org.brixcms.web.BrixRequestMapper.java

License:Apache License

public String getWorkspace() {
    String workspace = getWorkspaceFromUrl();

    if (workspace != null) {
        return workspace;
    }/*ww  w.j a v a2  s. c  o  m*/

    RequestCycle rc = RequestCycle.get();
    workspace = rc.getMetaData(WORKSPACE_METADATA);
    if (workspace == null) {
        WebRequest req = (WebRequest) RequestCycle.get().getRequest();
        WebResponse resp = (WebResponse) RequestCycle.get().getResponse();
        Cookie cookie = req.getCookie(COOKIE_NAME);
        workspace = getDefaultWorkspaceName();
        if (cookie != null) {
            if (cookie.getValue() != null) {
                workspace = cookie.getValue();
            }
        }
        if (!checkSession(workspace)) {
            workspace = getDefaultWorkspaceName();
        }
        if (workspace == null) {
            throw new IllegalStateException("Could not resolve jcr workspace to use for this request");
        }
        Cookie c = new Cookie(COOKIE_NAME, workspace);
        c.setPath("/");
        if (workspace.toString().equals(getDefaultWorkspaceName()) == false) {
            resp.addCookie(c);
        } else if (cookie != null) {
            resp.clearCookie(cookie);
        }
        rc.setMetaData(WORKSPACE_METADATA, workspace);
    }
    return workspace;
}

From source file:org.brixcms.workspace.WorkspaceUtils.java

License:Apache License

public static String getWorkspace() {
    String workspace = getWorkspaceFromUrl();

    if (workspace != null) {
        return workspace;
    }// w  w w  .j a v a  2s. c  o  m

    RequestCycle rc = RequestCycle.get();
    workspace = rc.getMetaData(WORKSPACE_METADATA);
    if (workspace == null) {
        WebRequest req = (WebRequest) RequestCycle.get().getRequest();
        WebResponse resp = (WebResponse) RequestCycle.get().getResponse();
        Cookie cookie = req.getCookie(COOKIE_NAME);
        workspace = getDefaultWorkspaceName();
        if (cookie != null) {
            if (cookie.getValue() != null)
                workspace = cookie.getValue();
        }
        if (!checkSession(workspace)) {
            workspace = getDefaultWorkspaceName();
        }
        if (workspace == null) {
            throw new IllegalStateException("Could not resolve jcr workspace to use for this request");
        }
        Cookie c = new Cookie(COOKIE_NAME, workspace);
        c.setPath("/");
        if (workspace.toString().equals(getDefaultWorkspaceName()) == false)
            resp.addCookie(c);
        else if (cookie != null)
            resp.clearCookie(cookie);
        rc.setMetaData(WORKSPACE_METADATA, workspace);
    }
    return workspace;
}

From source file:org.cyclop.service.common.CookieStorage.java

License:Apache License

protected void storeCookie(@NotNull CookieName name, @NotNull String value) {
    RequestCycle requestCycle = RequestCycle.get();
    if (requestCycle == null) {
        LOG.warn("RequestCycle is null - cannot read cookies");
        return;//w  w  w .  j av a  2 s.  co m
    }
    WebResponse response = (WebResponse) requestCycle.getResponse();
    Cookie cookie = new Cookie(name.name(), value);
    cookie.setMaxAge(appConfig.cookie.expirySeconds);
    response.addCookie(cookie);
}

From source file:org.projectforge.web.LoginPage.java

License:Open Source License

public static void logout(final MySession mySession, final WebRequest request, final WebResponse response,
        final UserXmlPreferencesCache userXmlPreferencesCache, final MenuBuilder menuBuilder) {
    final PFUserDO user = mySession.getUser();
    if (user != null) {
        userXmlPreferencesCache.flushToDB(user.getId());
        userXmlPreferencesCache.clear(user.getId());
        if (menuBuilder != null) {
            menuBuilder.expireMenu(user.getId());
        }/* www .j  av  a 2  s .  co m*/
    }
    mySession.logout();
    final Cookie stayLoggedInCookie = UserFilter
            .getStayLoggedInCookie(WicketUtils.getHttpServletRequest(request));
    if (stayLoggedInCookie != null) {
        stayLoggedInCookie.setMaxAge(0);
        stayLoggedInCookie.setValue(null);
        stayLoggedInCookie.setPath("/");
        response.addCookie(stayLoggedInCookie);
    }
}