Example usage for javax.servlet.http HttpServletResponse addCookie

List of usage examples for javax.servlet.http HttpServletResponse addCookie

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse addCookie.

Prototype

public void addCookie(Cookie cookie);

Source Link

Document

Adds the specified cookie to the response.

Usage

From source file:org.osmsurround.ae.oauth.OauthRequestController.java

@RequestMapping("/oauth")
public String receiveOauthToken(
        @SuppressWarnings("unused") @RequestParam(value = "oauth_token", defaultValue = "") String oAuthToken,
        @RequestParam(value = "oauth_verifier", defaultValue = "") String oAuthVerifier,
        HttpServletResponse response) {
    OauthTokens oauthTokens = oauthService.retrieveAccessToken(oAuthVerifier);
    response.addCookie(oauthCookieService.createOauthCookie(oauthTokens));
    return "redirect:index";
}

From source file:ai.susi.server.AbstractAPIHandler.java

/**
 * Checks a request for valid login data, either a existing session, a cookie or an access token
 * @return user identity if some login is active, anonymous identity otherwise
 *///from   ww w.j  av  a2  s .com
public static ClientIdentity getIdentity(HttpServletRequest request, HttpServletResponse response,
        Query query) {

    if (getLoginCookie(request) != null) { // check if login cookie is set

        Cookie loginCookie = getLoginCookie(request);

        ClientCredential credential = new ClientCredential(ClientCredential.Type.cookie,
                loginCookie.getValue());
        Authentication authentication = new Authentication(credential, DAO.authentication);

        if (authentication.getIdentity() != null && authentication.checkExpireTime()) {

            //reset cookie validity time
            authentication.setExpireTime(defaultCookieTime);
            loginCookie.setMaxAge(defaultCookieTime.intValue());
            loginCookie.setPath("/"); // bug. The path gets reset
            response.addCookie(loginCookie);

            return authentication.getIdentity();
        }

        authentication.delete();

        // delete cookie if set
        deleteLoginCookie(response);

        Log.getLog().info("Invalid login try via cookie from host: " + query.getClientHost());
    } else if (request.getSession().getAttribute("identity") != null) { // check session is set
        return (ClientIdentity) request.getSession().getAttribute("identity");
    } else if (request.getParameter("access_token") != null) { // access tokens can be used by api calls, somehow the stateless equivalent of sessions for browsers
        ClientCredential credential = new ClientCredential(ClientCredential.Type.access_token,
                request.getParameter("access_token"));
        Authentication authentication = new Authentication(credential, DAO.authentication);

        // check if access_token is valid
        if (authentication.getIdentity() != null) {
            ClientIdentity identity = authentication.getIdentity();

            if (authentication.checkExpireTime()) {
                Log.getLog().info("login for user: " + identity.getName() + " via access token from host: "
                        + query.getClientHost());

                if ("true".equals(request.getParameter("request_session"))) {
                    request.getSession().setAttribute("identity", identity);
                }
                if (authentication.has("one_time") && authentication.getBoolean("one_time")) {
                    authentication.delete();
                }
                return identity;
            }
        }
        Log.getLog().info("Invalid access token from host: " + query.getClientHost());
        return getAnonymousIdentity(query.getClientHost());
    }

    return getAnonymousIdentity(query.getClientHost());
}

From source file:shiver.me.timbers.spring.security.CookieJwtAuthenticationApplier.java

@Override
public void apply(Authentication authentication, HttpServletResponse response) {
    final String token = tokenParser.create(authentication);
    response.setHeader(tokenName, token);
    response.addCookie(bakery.bake(tokenName, token));
}

From source file:shiver.me.timbers.spring.security.CookieJwtLogoutHandler.java

@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
    final Cookie cookie = bakery.bake(tokenName, "");
    cookie.setMaxAge(0);/*ww w  . j a  v a 2 s . c om*/
    response.addCookie(cookie);
}

From source file:webim.service.WebimVisitorManager.java

private void createCookie(HttpServletResponse response, String vid) {
    Cookie cookie = new Cookie(COOKIE_VID, vid);
    response.addCookie(cookie);
}

From source file:org.owasp.dependencytrack.controller.LoginController.java

/**
 * Login action.//from   w  w w . ja va 2s . c  o  m
 *
 * @param request a HttpServletRequest object
 * @param response a HttpServletResponse object
 * @return a String
 */
@RequestMapping(value = "/login", method = { RequestMethod.GET, RequestMethod.HEAD })
public String login(HttpServletRequest request, HttpServletResponse response) {
    setLdapStatus(request, false);
    response.addCookie(new Cookie("CONTEXTPATH", getServletContext().getContextPath()));
    final String s = "loginPage";
    if (SecurityUtils.getSubject().isAuthenticated()) {
        return "redirect:/dashboard";
    }
    return s;
}

From source file:org.apache.ofbiz.securityext.login.LoginEvents.java

public static void setUsername(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String domain = EntityUtilProperties.getPropertyValue("url", "cookie.domain", delegator);
    // first try to get the username from the cookie
    synchronized (session) {
        if (UtilValidate.isEmpty(getUsername(request))) {
            // create the cookie and send it back
            Cookie cookie = new Cookie(usernameCookieName, request.getParameter("USERNAME"));
            cookie.setMaxAge(60 * 60 * 24 * 365);
            cookie.setPath("/");
            cookie.setDomain(domain);//from w ww. j a va 2 s  .c  o m
            response.addCookie(cookie);
        }
    }
}

From source file:AddCookieServlet.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String data = request.getParameter("data");
    Cookie cookie = new Cookie("MyCookie", data);
    response.addCookie(cookie);
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("<B>MyCookie has been set to");
    pw.println(data);/*from  w w w.j a  va  2  s. c  o m*/
    pw.close();
}

From source file:org.codehaus.groovy.grails.plugins.springsecurity.facebook.FacebookLogoutHandler.java

private void cancelCookie(final String name, final String path, final HttpServletResponse response) {
    Cookie cookie = new Cookie(name, null);
    cookie.setMaxAge(0);//from   w  w  w. j av a 2 s  . c  o  m
    cookie.setPath(path);
    response.addCookie(cookie);
}

From source file:nl.surfnet.coin.teams.control.LandingPageController.java

@RequestMapping(value = "/landingpage.shtml", method = RequestMethod.POST)
public void storeCookie(HttpServletResponse response) {
    Cookie cookie = new Cookie(LoginInterceptor.TEAMS_COOKIE, "skipLanding=true");
    cookie.setMaxAge(Integer.MAX_VALUE);
    response.addCookie(cookie);
}