From b2eaf0af582cc64661a662005dfbf8550974859b Mon Sep 17 00:00:00 2001 From: abialas Date: Sun, 18 Mar 2018 21:02:16 +0100 Subject: [PATCH] BAEL-1567 use optional (#3849) * BAEL-1412 add java 8 spring data features * BAEL-21 new HTTP API overview * BAEL-21 fix executor * BAEL-1432 add custom gradle task * BAEL-1567 add samples of cookie and session in serlvet * BAEL-1567 use stream api * BAEL-1567 fix optional --- .../java/com/baeldung/servlets/CookieReader.java | 8 ++++---- .../java/com/baeldung/servlets/LoginServlet.java | 9 +++++---- .../java/com/baeldung/servlets/WelcomeServlet.java | 14 ++++++++------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java b/javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java index c2d875a69f..133e729c33 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java @@ -3,6 +3,7 @@ package com.baeldung.servlets; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; +import java.util.Optional; /** * Created by adam. @@ -26,14 +27,13 @@ public class CookieReader { * Reads cookie by key from request. * * @param key the key of a cookie - * @return returns cookie value (or null if cookie with given key does not exist) + * @return returns cookie value */ - public String readCookie(String key) { + public Optional readCookie(String key) { return Arrays.stream(request.getCookies()) .filter(c -> key.equals(c.getName())) .map(Cookie::getValue) - .findFirst() - .orElse(null); + .findFirst(); } } diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java index deb0e05ec4..ee4e73c956 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java @@ -5,6 +5,7 @@ import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; +import java.util.Optional; /** * Created by adam. @@ -15,12 +16,12 @@ public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { CookieReader cookieReader = new CookieReader(request); - String uiColor = cookieReader.readCookie("uiColor"); - String userName = cookieReader.readCookie("userName"); + Optional uiColor = cookieReader.readCookie("uiColor"); + Optional userName = cookieReader.readCookie("userName"); - request.setAttribute("uiColor", uiColor != null ? uiColor : "blue"); + request.setAttribute("uiColor", uiColor.isPresent() ? uiColor.get() : "blue"); - if (userName == null || userName.isEmpty()) { + if (!userName.isPresent()) { RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/login.jsp"); dispatcher.forward(request, response); } else { diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java index b35be2fc93..dd66cb458e 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java @@ -8,6 +8,7 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.Optional; /** * Created by adam. @@ -18,15 +19,16 @@ public class WelcomeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { CookieReader cookieReader = new CookieReader(request); - String uiColor = cookieReader.readCookie("uiColor"); - String userName = cookieReader.readCookie("userName"); + Optional uiColor = cookieReader.readCookie("uiColor"); + Optional userName = cookieReader.readCookie("userName"); - if (userName == null || userName.isEmpty()) { + if (!userName.isPresent()) { response.sendRedirect("/login"); } else { - request.setAttribute("uiColor", uiColor != null ? uiColor : "blue"); - request.setAttribute("userName", userName); - request.setAttribute("sessionAttribute", request.getSession().getAttribute("sampleKey")); + request.setAttribute("uiColor", uiColor.isPresent() ? uiColor.get() : "blue"); + request.setAttribute("userName", userName.get()); + request.setAttribute("sessionAttribute", request.getSession() + .getAttribute("sampleKey")); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/welcome.jsp"); dispatcher.forward(request, response);