List of usage examples for javax.servlet.http Cookie Cookie
public Cookie(String name, String value)
From source file:de.hska.ld.core.controller.HomeController.java
@RequestMapping("/logout") public String logout(HttpServletRequest request, HttpServletResponse response, Principal p) throws ServletException { request.logout();//from w w w . ja v a2s . c o m javax.servlet.http.Cookie cookie = new Cookie("sessionID", ""); cookie.setPath("/"); if (!"localhost".equals(env.getProperty("module.core.oidc.server.endpoint.main.domain"))) { cookie.setDomain(env.getProperty("module.core.oidc.server.endpoint.main.domain")); } cookie.setMaxAge(0); response.addCookie(cookie); return "redirect:"; }
From source file:net.yacy.cora.protocol.ResponseHeader.java
/** * Sets Cookie on the client machine./* ww w .j av a2s .com*/ * * @param name Cookie name * @param value Cookie value * @param maxage time to live in seconds, none negative number, according to https://tools.ietf.org/html/rfc2109, 0=discard in https://tools.ietf.org/html/rfc2965 * @param path Path the cookie belongs to. Default - "/". Can be <b>null</b>. * @param domain Domain this cookie belongs to. Default - domain name. Can be <b>null</b>. * @param secure If true cookie will be send only over safe connection such as https * @see further documentation: <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a> */ public void setCookie(final String name, final String value, final Integer maxage, final String path, final String domain, final boolean secure) { /* * TODO:Here every value can be validated for correctness if needed * For example semicolon should be not in any of the values * However an exception in this case would be an overhead IMHO. */ if (!name.isEmpty()) { if (this.cookieStore == null) this.cookieStore = new ArrayList<Cookie>(); Cookie c = new Cookie(name, value); if (maxage != null && maxage >= 0) c.setMaxAge(maxage); if (path != null) c.setPath(path); if (domain != null) c.setDomain(domain); if (secure) c.setSecure(secure); this.cookieStore.add(c); } }
From source file:au.gov.dto.springframework.security.web.context.CookieSecurityContextRepository.java
private Cookie createExpireAuthenticationCookie(HttpServletRequest request) { Cookie removeSessionCookie = new Cookie(authenticationCookieName, ""); removeSessionCookie.setPath(authenticationCookiePath); removeSessionCookie.setMaxAge(0);/*from ww w. j a v a2 s. com*/ removeSessionCookie.setHttpOnly(true); removeSessionCookie.setSecure(request.isSecure()); return removeSessionCookie; }
From source file:course.BlogController.java
private void initializeRoutes() throws IOException { // this is the blog home page get(new FreemarkerBasedRoute("/", "blog_template.ftl") { @Override//from w w w . j a v a 2 s. c o m public void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String username = sessionDAO.findUserNameBySessionId(getSessionCookie(request)); // this is where we would normally load up the blog data // but this week, we just display a placeholder. HashMap<String, String> root = new HashMap<String, String>(); template.process(root, writer); } }); // handle the signup post post(new FreemarkerBasedRoute("/signup", "signup.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String email = request.queryParams("email"); String username = request.queryParams("username"); String password = request.queryParams("password"); String verify = request.queryParams("verify"); HashMap<String, String> root = new HashMap<String, String>(); root.put("username", StringEscapeUtils.escapeHtml4(username)); root.put("email", StringEscapeUtils.escapeHtml4(email)); if (validateSignup(username, password, verify, email, root)) { // good user System.out.println("Signup: Creating user with: " + username + " " + password); if (!userDAO.addUser(username, password, email)) { // duplicate user root.put("username_error", "Username already in use, Please choose another"); template.process(root, writer); } else { // good user, let's start a session String sessionID = sessionDAO.startSession(username); System.out.println("Session ID is" + sessionID); response.raw().addCookie(new Cookie("session", sessionID)); response.redirect("/welcome"); } } else { // bad signup System.out.println("User Registration did not validate"); template.process(root, writer); } } }); // present signup form for blog get(new FreemarkerBasedRoute("/signup", "signup.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { SimpleHash root = new SimpleHash(); // initialize values for the form. root.put("username", ""); root.put("password", ""); root.put("email", ""); root.put("password_error", ""); root.put("username_error", ""); root.put("email_error", ""); root.put("verify_error", ""); template.process(root, writer); } }); get(new FreemarkerBasedRoute("/welcome", "welcome.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String cookie = getSessionCookie(request); String username = sessionDAO.findUserNameBySessionId(cookie); if (username == null) { System.out.println("welcome() can't identify the user, redirecting to signup"); response.redirect("/signup"); } else { SimpleHash root = new SimpleHash(); root.put("username", username); template.process(root, writer); } } }); // present the login page get(new FreemarkerBasedRoute("/login", "login.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { SimpleHash root = new SimpleHash(); root.put("username", ""); root.put("login_error", ""); template.process(root, writer); } }); // process output coming from login form. On success redirect folks to // the welcome page // on failure, just return an error and let them try again. post(new FreemarkerBasedRoute("/login", "login.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String username = request.queryParams("username"); String password = request.queryParams("password"); System.out.println("Login: User submitted: " + username + " " + password); Document user = userDAO.validateLogin(username, password); if (user != null) { // valid user, let's log them in String sessionID = sessionDAO.startSession(user.get("_id").toString()); if (sessionID == null) { response.redirect("/internal_error"); } else { // set the cookie for the user's browser response.raw().addCookie(new Cookie("session", sessionID)); response.redirect("/welcome"); } } else { SimpleHash root = new SimpleHash(); root.put("username", StringEscapeUtils.escapeHtml4(username)); root.put("password", ""); root.put("login_error", "Invalid Login"); template.process(root, writer); } } }); // allows the user to logout of the blog get(new FreemarkerBasedRoute("/logout", "signup.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { String sessionID = getSessionCookie(request); if (sessionID == null) { // no session to end response.redirect("/login"); } else { // deletes from session table sessionDAO.endSession(sessionID); // this should delete the cookie Cookie c = getSessionCookieActual(request); c.setMaxAge(0); response.raw().addCookie(c); response.redirect("/login"); } } }); // used to process internal errors get(new FreemarkerBasedRoute("/internal_error", "error_template.ftl") { @Override protected void doHandle(Request request, Response response, Writer writer) throws IOException, TemplateException { SimpleHash root = new SimpleHash(); root.put("error", "System has encountered an error."); template.process(root, writer); } }); }
From source file:io.stallion.plugins.flatBlog.comments.tests.TestEndpoints.java
@Test public void testCommentCrud() throws IOException { Stubbing.stub(EmailSender.class, "executeSend"); MockResponse response;/*from www.j a v a 2s . co m*/ // Create a comment Map<String, Object> params = new HashMap<String, Object>(); Comment comment = new Comment(); comment.setAuthorDisplayName("Emperor Nero"); comment.setAuthorWebSite("http://emperor.com"); comment.setBodyMarkdown("You will submit my submit, you will submit to me! " + new Date().getTime()); comment.setAuthorEmail("nero@stallion.io"); comment.setThreadId(910L); comment.setParentPermalink("http://localhost:8090/randoms"); comment.setParentTitle("The random stuff"); //params.put("author", author); //params.put("webSite", webSite); //params.put("body", body); //params.put("email", email); response = client.post("/_stx/flatBlog/comments/submit", comment); assertResponseContains(response, comment.getBodyHtml()); String authorSecret = response.getCookie("stCommentAuthorKey").getValue(); Comment resultComment = JSON.parse(response.getContent(), Comment.class); String newBody = "New body"; resultComment.setBodyMarkdown(newBody); // Update a comment MockRequest request = new MockRequest("/_stx/flatBlog/comments/" + resultComment.getId() + "/revise", "POST"); request.setDataObject(resultComment); request.setCookies(new Cookie(Constants.AUTHOR_SECRET_COOKIE, authorSecret)); response = client.request(request); Log.finer("Revise response: {0}", response.getContent()); Assert.assertEquals(200, response.getStatus()); Comment retrieved = CommentsController.instance().forId(resultComment.getId()); Assert.assertEquals("<p>" + newBody + "</p>", retrieved.getBodyHtml().trim()); // Disapprove // Approve Stubbing.verifyAndReset(); }
From source file:io.syndesis.rest.v1.handler.credential.CredentialHandler.java
static void removeCredentialCookies(final HttpServletRequest request, final HttpServletResponse response) { Arrays.stream(request.getCookies()) .filter(c -> c.getName().startsWith(CredentialFlowState.CREDENTIAL_PREFIX)).forEach(c -> { final Cookie removal = new Cookie(c.getName(), ""); removal.setPath("/"); removal.setMaxAge(0);/*ww w . j a va 2s . co m*/ removal.setHttpOnly(true); removal.setSecure(true); response.addCookie(removal); }); }
From source file:com.rantop.web.util.web.ServletUtils.java
/** * Convenience method to set a cookie//from w w w.j a va2 s . co m * * @param response * @param name * @param value * @param path */ public static void setCookie(HttpServletResponse response, String name, String value, String path) { if (log.isDebugEnabled()) { log.debug("Setting cookie '" + name + "' on path '" + path + "'"); } Cookie cookie = new Cookie(name, value); cookie.setSecure(false); cookie.setPath(path); cookie.setMaxAge(3600 * 24 * 30); // 30 days response.addCookie(cookie); }
From source file:com.atlassian.jira.security.xsrf.SimpleXsrfTokenGenerator.java
private void addNewCookie(HttpServletRequest httpServletRequest, String token, HttpServletResponse httpServletResponse) { final Cookie cookie = new Cookie(TOKEN_HTTP_SESSION_KEY, token); cookie.setPath(getRequestContext(httpServletRequest)); cookie.setMaxAge(-1); // expire with the browser exit cookie.setSecure(httpServletRequest.isSecure()); httpServletResponse.addCookie(cookie); httpServletRequest.setAttribute(SET_COOKIE_PENDING, token); }
From source file:com.mawujun.util.web.CookieGenerator.java
/** * Create a cookie with the given value, using the cookie descriptor * settings of this generator (except for "cookieMaxAge"). * @param cookieValue the value of the cookie to crate * @return the cookie//w w w .ja va 2 s.c o m * @see #setCookieName * @see #setCookieDomain * @see #setCookiePath */ protected Cookie createCookie(String cookieValue) { Cookie cookie = new Cookie(getCookieName(), cookieValue); if (getCookieDomain() != null) { cookie.setDomain(getCookieDomain()); } cookie.setPath(getCookiePath()); return cookie; }