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