BAEL-325: Massive extensions to the example from BAEL-305.
This commit is contained in:
parent
2b9470eed0
commit
ae3d9760c9
|
@ -37,7 +37,7 @@
|
|||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>9.3.11.v20160721</version>
|
||||
<version>9.4.0.M1</version>
|
||||
<configuration>
|
||||
<webApp>
|
||||
<contextPath>/intercepting-filter</contextPath>
|
||||
|
|
|
@ -2,7 +2,10 @@ package com.baeldung.enterprise.patterns.front.controller;
|
|||
|
||||
import com.baeldung.enterprise.patterns.front.controller.commands.FrontCommand;
|
||||
import com.baeldung.enterprise.patterns.front.controller.commands.UnknownCommand;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Bookshelf;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.BookshelfImpl;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
|
@ -10,8 +13,16 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet(name = "front-controller", urlPatterns = "/")
|
||||
@WebServlet(name = "front-controller", urlPatterns = "/index")
|
||||
public class FrontControllerServlet extends HttpServlet {
|
||||
@Override
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
super.init(config);
|
||||
Bookshelf bookshelf = new BookshelfImpl();
|
||||
bookshelf.init();
|
||||
getServletContext().setAttribute("bookshelf", bookshelf);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(
|
||||
HttpServletRequest request,
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Order;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CheckoutCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
super.process();
|
||||
HttpSession session = request.getSession(false);
|
||||
if (request.getMethod().equals("POST")) {
|
||||
session.removeAttribute("order");
|
||||
response.sendRedirect("index?command=Home&message=Thank you for buying!");
|
||||
} else {
|
||||
Order order = (Order) session.getAttribute("order");
|
||||
Double total = order.getItems().entrySet().stream()
|
||||
.map(entry -> entry.getKey().getPrice() * entry.getValue())
|
||||
.reduce((p1, p2) -> p1 + p2)
|
||||
.orElse(0.00);
|
||||
request.setAttribute("total", total);
|
||||
forward("shopping-cart");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Bookshelf;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class HomeCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
super.process();
|
||||
Bookshelf bookshelf = (Bookshelf) request.getServletContext()
|
||||
.getAttribute("bookshelf");
|
||||
request.setAttribute("books", bookshelf);
|
||||
forward("home");
|
||||
}
|
||||
}
|
|
@ -7,14 +7,14 @@ import java.io.IOException;
|
|||
public class LoginCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
if (request.getMethod().equals("GET")) {
|
||||
request.setAttribute("redirect", request.getRequestURL()
|
||||
.append("?").append(request.getQueryString()).toString());
|
||||
forward("login");
|
||||
} else {
|
||||
if (request.getMethod().equals("POST")) {
|
||||
HttpSession session = request.getSession(true);
|
||||
session.setAttribute("username", request.getParameter("username"));
|
||||
response.sendRedirect(request.getParameter("redirect"));
|
||||
} else {
|
||||
request.setAttribute("redirect", request.getRequestURL()
|
||||
.append("?").append(request.getQueryString()).toString());
|
||||
forward("login");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
|
||||
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");
|
||||
response.sendRedirect("index?command=Home");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Book;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Bookshelf;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Order;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.OrderImpl;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
|
||||
public class OrderCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
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);
|
||||
}
|
||||
Bookshelf bookshelf = (Bookshelf) request.getServletContext()
|
||||
.getAttribute("bookshelf");
|
||||
String isbn = request.getParameter("isbn");
|
||||
Integer quantity = Integer.parseInt(request.getParameter("quantity"));
|
||||
Book book = bookshelf.get(isbn);
|
||||
order.add(book, quantity);
|
||||
session.setAttribute("order", order);
|
||||
response.sendRedirect(String.format("index?command=Show&isbn=%s", isbn));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,19 +1,22 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Book;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.BookshelfImpl;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Bookshelf;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class SearchCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
super.process();
|
||||
Book book = new BookshelfImpl().getInstance()
|
||||
.findByTitle(request.getParameter("title"));
|
||||
if (book != null) {
|
||||
request.setAttribute("book", book);
|
||||
Bookshelf bookshelf = (Bookshelf) request.getServletContext()
|
||||
.getAttribute("bookshelf");
|
||||
String q = request.getParameter("q");
|
||||
List<Book> books = bookshelf.find(q);
|
||||
if (books.size() > 0) {
|
||||
request.setAttribute("books", books);
|
||||
forward("book-found");
|
||||
} else {
|
||||
forward("book-notfound");
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Book;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Bookshelf;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
public class ShowCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
super.process();
|
||||
Bookshelf bookshelf = (Bookshelf) request.getServletContext()
|
||||
.getAttribute("bookshelf");
|
||||
String title = request.getParameter("isbn");
|
||||
Book book = bookshelf.get(title);
|
||||
request.setAttribute("books", Collections.singletonList(book));
|
||||
forward("book-found");
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import java.io.IOException;
|
|||
public class UnknownCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
super.process();
|
||||
forward("unknown");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
public interface Book {
|
||||
String getIsbn();
|
||||
|
||||
void setIsbn(String isbn);
|
||||
|
||||
String getAuthor();
|
||||
|
||||
void setAuthor(String author);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
public class BookImpl implements Book {
|
||||
private String isbn;
|
||||
private String author;
|
||||
private String title;
|
||||
private Double price;
|
||||
|
@ -8,12 +9,23 @@ public class BookImpl implements Book {
|
|||
public BookImpl() {
|
||||
}
|
||||
|
||||
public BookImpl(String author, String title, Double price) {
|
||||
public BookImpl(String isbn, String author, String title, Double price) {
|
||||
this.isbn = isbn;
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Bookshelf {
|
||||
|
||||
default void init() {
|
||||
add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
|
||||
add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
|
||||
add(new BookImpl("Unknown", "Something about German Umlauts (äüö) and ß", 5.49));
|
||||
add(new BookImpl("001", "Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
|
||||
add(new BookImpl("002", "Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
|
||||
add(new BookImpl("003", "Unknown", "Something about German Umlauts (äüö) and ß", 5.49));
|
||||
}
|
||||
|
||||
Bookshelf getInstance();
|
||||
|
||||
<E extends Book> boolean add(E book);
|
||||
|
||||
Book findByTitle(String title);
|
||||
Book get(String isbn);
|
||||
|
||||
List<Book> find(String q);
|
||||
}
|
||||
|
|
|
@ -1,24 +1,23 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BookshelfImpl extends ArrayList<Book> implements Bookshelf {
|
||||
private static Bookshelf INSTANCE;
|
||||
|
||||
@Override
|
||||
public Bookshelf getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new BookshelfImpl();
|
||||
INSTANCE.init();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book findByTitle(String title) {
|
||||
public Book get(String isbn) {
|
||||
return this.stream()
|
||||
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
|
||||
.filter(book -> book.getIsbn().equals(isbn))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> find(String q) {
|
||||
return this.stream()
|
||||
.filter(book -> book.getTitle().toLowerCase().contains(q.toLowerCase())
|
||||
|| book.getAuthor().toLowerCase().contains(q.toLowerCase()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface Order {
|
||||
String getUsername();
|
||||
|
||||
Map<Book, Integer> getItems();
|
||||
|
||||
void add(Book item, Integer quantity);
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class OrderImpl implements Order {
|
||||
private String username;
|
||||
private Map<Book, Integer> items = new HashMap<>();
|
||||
|
||||
public OrderImpl(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Book, Integer> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(Map<Book, Integer> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Book item, Integer quantity) {
|
||||
Integer q = 0;
|
||||
if (this.items.containsKey(item)) {
|
||||
q = this.items.get(item);
|
||||
}
|
||||
this.items.put(item, quantity + q);
|
||||
}
|
||||
}
|
|
@ -29,10 +29,10 @@ public class AuthenticationFilter extends BaseFilter {
|
|||
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
|
||||
HttpSession session = httpServletRequest.getSession(false);
|
||||
if (session == null || session.getAttribute("username") == null) {
|
||||
callback.intercept();
|
||||
FrontCommand command = new LoginCommand();
|
||||
command.init(httpServletRequest, httpServletResponse);
|
||||
command.process();
|
||||
callback.intercept();
|
||||
} else {
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ public class FilterManager {
|
|||
) throws ServletException, IOException {
|
||||
FilterChain filterChain = new FilterChainImpl(
|
||||
new AuthenticationFilter(callback),
|
||||
new AuditFilter()
|
||||
new VisitorCounterFilter()
|
||||
);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
|
|
|
@ -24,16 +24,10 @@ public class LoggingFilter extends BaseFilter {
|
|||
chain.doFilter(request, response);
|
||||
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
||||
String username = (String) httpServletRequest.getAttribute("username");
|
||||
if (username != null && username.length() > 0) {
|
||||
username = username.concat("@");
|
||||
} else {
|
||||
username = "";
|
||||
if (username == null) {
|
||||
username = "guest";
|
||||
}
|
||||
String intro = "Request";
|
||||
if (username.length() > 0) {
|
||||
intro = "Authenticated request";
|
||||
}
|
||||
log.info("{} from '{}{}': {}?{}", intro, username, request.getRemoteAddr(),
|
||||
log.info("Request from '{}@{}': {}?{}", username, request.getRemoteAddr(),
|
||||
httpServletRequest.getRequestURI(), request.getParameterMap());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,24 @@
|
|||
<%@ page import="com.baeldung.enterprise.patterns.front.controller.data.Book" %>
|
||||
<%@ page import="java.util.List" %>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bookshelf: Title found</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Our Bookshelf contains this title:</p>
|
||||
<h2>${book.getTitle()}</h2>
|
||||
<p>Author: ${book.getAuthor()}</p>
|
||||
<input type="submit" value="Buy it: ${book.getPrice()}$">
|
||||
<p>Our Bookshelf contains following titles:</p>
|
||||
<% for (Book book : (List<Book>) request.getAttribute("books")) { %>
|
||||
<h2><%= book.getTitle() %></h2>
|
||||
<p>Author: <%= book.getAuthor() %></p>
|
||||
<form action="index?command=Order" method="POST">
|
||||
<label for="quantity">Quantity:</label>
|
||||
<input type="text" id="quantity" name="quantity" value="1" required>
|
||||
<input type="hidden" name="isbn" value="<%= book.getIsbn() %>">
|
||||
<input type="submit" value="Buy it: <%= book.getPrice() %>$">
|
||||
<a href="index?command=Home">Go back...</a>
|
||||
</form>
|
||||
<% } %>
|
||||
<%@ include file="shopping-cart-hint.jsp"%>
|
||||
<%@ include file="visitor-counter.jsp"%>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<p>Our Bookshelf doesn't contains this title:</p>
|
||||
<h2>${param.get("title")}</h2>
|
||||
<h2><%= request.getParameter("q") %></h2>
|
||||
<%@ include file="visitor-counter.jsp"%>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
<%@ page import="com.baeldung.enterprise.patterns.front.controller.data.Book" %>
|
||||
<%@ page import="java.util.List" %>
|
||||
<!DOCTYPE>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bookshelf: Home</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="index?command=Search" method="POST">
|
||||
<input type="text" id="search" name="q">
|
||||
<input type="submit" value="Go!">
|
||||
</form>
|
||||
<% if (request.getParameter("message") != null) { %>
|
||||
<h2><%= request.getParameter("message") %></h2>
|
||||
<% } else { %>
|
||||
<h2>Welcome to the Bookshelf!</h2>
|
||||
<% } %>
|
||||
<% for (Book book : (List<Book>) request.getAttribute("books")) { %>
|
||||
<hr/>
|
||||
<p><b><%= book.getAuthor() %>:</b></p>
|
||||
<h3><%= book.getTitle() %></h3>
|
||||
<a href="index?command=Show&isbn=<%= book.getIsbn() %>">More...</a>
|
||||
<% } %>
|
||||
<%@ include file="shopping-cart-hint.jsp"%>
|
||||
<%@ include file="visitor-counter.jsp"%>
|
||||
</body>
|
||||
</html>
|
|
@ -6,10 +6,11 @@
|
|||
<body>
|
||||
<p>Please input a username:</p>
|
||||
<h2>Login</h2>
|
||||
<form action="/intercepting-filter/?command=Login" method="POST">
|
||||
<form action="index?command=Login" method="POST">
|
||||
<input type="text" name="username" placeholder="Username">
|
||||
<input type="hidden" name="redirect" value="${redirect}">
|
||||
<input type="hidden" name="redirect" value="<%= (String) request.getAttribute("redirect") %>">
|
||||
<input type="submit" value="Proceed">
|
||||
</form>
|
||||
<%@ include file="visitor-counter.jsp" %>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
<%@ page import="com.baeldung.enterprise.patterns.front.controller.data.Order" %>
|
||||
<% if (session != null && session.getAttribute("order") != null) { %>
|
||||
<% Order order = ((Order) session.getAttribute("order")); %>
|
||||
<% if (order != null && order.getItems().size() > 0) { %>
|
||||
<hr/>
|
||||
<p>
|
||||
Your shopping cart is holding
|
||||
<% if (order.getItems().size() == 1) { %>
|
||||
1 item.
|
||||
<% } else { %>
|
||||
<%= (order.getItems().size()) %> items.
|
||||
<% } %>
|
||||
<a href="index?command=Checkout">Checkout</a>
|
||||
</p>
|
||||
<% } %>
|
||||
<% } %>
|
|
@ -0,0 +1,29 @@
|
|||
<%@ page import="com.baeldung.enterprise.patterns.front.controller.data.Book" %>
|
||||
<%@ page import="com.baeldung.enterprise.patterns.front.controller.data.Order" %>
|
||||
<%@ page import="java.util.Map" %>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bookshelf: Checkout</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>You are about to buy the following books:</p>
|
||||
<h2>Shopping Cart</h2>
|
||||
<% Order order = (Order) session.getAttribute("order"); %>
|
||||
<ul>
|
||||
<% for (Map.Entry<Book, Integer> entry : order.getItems().entrySet()) { %>
|
||||
<li>
|
||||
<b><%= entry.getValue() %> x <%= entry.getKey().getPrice() %></b>
|
||||
<h3><%= entry.getKey().getTitle() %></h3>
|
||||
<i> by <%= entry.getKey().getAuthor()%></i>
|
||||
</li>
|
||||
<% } %>
|
||||
</ul>
|
||||
<p>
|
||||
<b>Total: <%= request.getAttribute("total") %></b>
|
||||
</p>
|
||||
<form action="index?command=Checkout" method="POST">
|
||||
<input type="submit" value="Donate :)">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
|
@ -5,6 +5,5 @@
|
|||
</head>
|
||||
<body>
|
||||
<p>Sorry, this command is not known!</p>
|
||||
<%@include file="visitor-counter.jsp"%>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
<% Integer counter = (Integer) request.getAttribute("counter"); %>
|
||||
<% if (counter != null && counter > 0) { %>
|
||||
<hr/>
|
||||
<p>You are visitor #${counter}!</p>
|
||||
<p>You are visitor #<%= counter %>. <a href="index?command=Logout">Logout</a></p>
|
||||
<% } %>
|
||||
|
|
Loading…
Reference in New Issue