BAEL-325 - adding optional construct
This commit is contained in:
parent
a9106b8e70
commit
c9073cbfa5
|
@ -3,6 +3,7 @@ package com.baeldung.patterns.intercepting.filter.commands;
|
|||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
public class LoginCommand extends FrontCommand {
|
||||
@Override
|
||||
|
@ -12,10 +13,8 @@ public class LoginCommand extends FrontCommand {
|
|||
session.setAttribute("username", request.getParameter("username"));
|
||||
response.sendRedirect(request.getParameter("redirect"));
|
||||
} else {
|
||||
String queryString = request.getQueryString();
|
||||
if (queryString == null) {
|
||||
queryString = "command=Home";
|
||||
}
|
||||
String queryString = Optional.ofNullable(request.getQueryString())
|
||||
.orElse("command=Home");
|
||||
request.setAttribute("redirect", request.getRequestURL()
|
||||
.append("?").append(queryString).toString());
|
||||
forward("login");
|
||||
|
|
|
@ -3,14 +3,17 @@ package com.baeldung.patterns.intercepting.filter.commands;
|
|||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
public class LogoutCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
super.process();
|
||||
HttpSession session = request.getSession(false);
|
||||
session.removeAttribute("username");
|
||||
session.removeAttribute("order");
|
||||
Optional.ofNullable(request.getSession(false))
|
||||
.ifPresent(session -> {
|
||||
session.removeAttribute("username");
|
||||
session.removeAttribute("order");
|
||||
});
|
||||
response.sendRedirect("/?command=Home");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import com.baeldung.patterns.intercepting.filter.data.OrderImpl;
|
|||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
public class OrderCommand extends FrontCommand {
|
||||
@Override
|
||||
|
@ -15,11 +16,10 @@ public class OrderCommand extends FrontCommand {
|
|||
super.process();
|
||||
if (request.getMethod().equals("POST")) {
|
||||
HttpSession session = request.getSession(false);
|
||||
Order order = (Order) session.getAttribute("order");
|
||||
if (order == null) {
|
||||
String username = (String) session.getAttribute("username");
|
||||
order = new OrderImpl(username);
|
||||
}
|
||||
Order order = Optional
|
||||
.ofNullable(session.getAttribute("order"))
|
||||
.map(Order.class::cast)
|
||||
.orElseGet(() -> new OrderImpl((String) session.getAttribute("username")));
|
||||
Bookshelf bookshelf = (Bookshelf) request.getServletContext()
|
||||
.getAttribute("bookshelf");
|
||||
String isbn = request.getParameter("isbn");
|
||||
|
|
|
@ -4,6 +4,7 @@ import javax.servlet.*;
|
|||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.annotation.WebInitParam;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
@WebFilter(
|
||||
servletNames = {"intercepting-filter"},
|
||||
|
@ -24,10 +25,9 @@ public class EncodingFilter extends BaseFilter {
|
|||
ServletResponse response,
|
||||
FilterChain chain
|
||||
) throws IOException, ServletException {
|
||||
String encoding = request.getParameter("encoding");
|
||||
if (encoding == null) {
|
||||
encoding = this.encoding;
|
||||
}
|
||||
String encoding = Optional
|
||||
.ofNullable(request.getParameter("encoding"))
|
||||
.orElse(this.encoding);
|
||||
response.setCharacterEncoding(encoding);
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import javax.servlet.ServletResponse;
|
|||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
@WebFilter(servletNames = "intercepting-filter")
|
||||
public class LoggingFilter extends BaseFilter {
|
||||
|
@ -23,10 +24,10 @@ public class LoggingFilter extends BaseFilter {
|
|||
) throws IOException, ServletException {
|
||||
chain.doFilter(request, response);
|
||||
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
||||
String username = (String) httpServletRequest.getAttribute("username");
|
||||
if (username == null) {
|
||||
username = "guest";
|
||||
}
|
||||
String username = Optional
|
||||
.ofNullable(httpServletRequest.getAttribute("username"))
|
||||
.map(Object::toString)
|
||||
.orElse("guest");
|
||||
log.info("Request from '{}@{}': {}?{}", username, request.getRemoteAddr(),
|
||||
httpServletRequest.getRequestURI(), request.getParameterMap());
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class VisitorCounterFilter implements Filter {
|
||||
|
@ -21,8 +22,9 @@ public class VisitorCounterFilter implements Filter {
|
|||
FilterChain chain
|
||||
) throws IOException, ServletException {
|
||||
HttpSession session = ((HttpServletRequest) request).getSession(false);
|
||||
String username = (String) session.getAttribute("username");
|
||||
users.add(username);
|
||||
Optional.ofNullable(session.getAttribute("username"))
|
||||
.map(Object::toString)
|
||||
.ifPresent(users::add);
|
||||
request.setAttribute("counter", users.size());
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue