BAEL-325: Removed old project folder.
This commit is contained in:
parent
dcc1889909
commit
67e4090b57
|
@ -1,49 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>intercepting-filter-pattern</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>enterprise-patterns-parent</artifactId>
|
||||
<groupId>com.baeldung.enterprise.patterns</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.21</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>9.4.0.M1</version>
|
||||
<configuration>
|
||||
<webApp>
|
||||
<contextPath>/intercepting-filter</contextPath>
|
||||
</webApp>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -1,66 +0,0 @@
|
|||
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;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@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,
|
||||
HttpServletResponse response
|
||||
) throws ServletException, IOException {
|
||||
doCommand(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response
|
||||
) throws ServletException, IOException {
|
||||
doCommand(request, response);
|
||||
}
|
||||
|
||||
private void doCommand(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response
|
||||
) throws ServletException, IOException {
|
||||
FrontCommand command = getCommand(request);
|
||||
command.init(request, response);
|
||||
command.process();
|
||||
}
|
||||
|
||||
private FrontCommand getCommand(HttpServletRequest request) {
|
||||
try {
|
||||
Class type = Class.forName(
|
||||
String.format(
|
||||
"com.baeldung.enterprise.patterns.front.controller.commands.%sCommand",
|
||||
request.getParameter("command")
|
||||
)
|
||||
);
|
||||
return (FrontCommand) type
|
||||
.asSubclass(FrontCommand.class)
|
||||
.newInstance();
|
||||
} catch (Exception e) {
|
||||
return new UnknownCommand();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import com.baeldung.enterprise.patterns.front.controller.filters.FilterManager;
|
||||
import com.baeldung.enterprise.patterns.front.controller.filters.OnIntercept;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class FrontCommand implements OnIntercept {
|
||||
protected HttpServletRequest request;
|
||||
protected HttpServletResponse response;
|
||||
private boolean intercept;
|
||||
|
||||
public FrontCommand() {
|
||||
}
|
||||
|
||||
public void init(HttpServletRequest request, HttpServletResponse response) {
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public void process() throws ServletException, IOException {
|
||||
FilterManager.process(request, response, this);
|
||||
}
|
||||
|
||||
public void forward(String target) throws ServletException, IOException {
|
||||
if (intercept) {
|
||||
return;
|
||||
}
|
||||
String path = String.format("/WEB-INF/jsp/%s.jsp", target);
|
||||
RequestDispatcher dispatcher = request.getServletContext()
|
||||
.getRequestDispatcher(path);
|
||||
dispatcher.forward(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intercept() {
|
||||
intercept = true;
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
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");
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
|
||||
public class LoginCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
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");
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
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,25 +0,0 @@
|
|||
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.List;
|
||||
|
||||
public class SearchCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
super.process();
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
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");
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class UnknownCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
super.process();
|
||||
forward("unknown");
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
public interface Book {
|
||||
String getIsbn();
|
||||
|
||||
void setIsbn(String isbn);
|
||||
|
||||
String getAuthor();
|
||||
|
||||
void setAuthor(String author);
|
||||
|
||||
String getTitle();
|
||||
|
||||
void setTitle(String title);
|
||||
|
||||
Double getPrice();
|
||||
|
||||
void setPrice(Double price);
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
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;
|
||||
|
||||
public BookImpl() {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Bookshelf {
|
||||
|
||||
default void init() {
|
||||
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));
|
||||
}
|
||||
|
||||
<E extends Book> boolean add(E book);
|
||||
|
||||
Book get(String isbn);
|
||||
|
||||
List<Book> find(String q);
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
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 {
|
||||
@Override
|
||||
public Book get(String isbn) {
|
||||
return this.stream()
|
||||
.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());
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
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);
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.filters;
|
||||
|
||||
import com.baeldung.enterprise.patterns.front.controller.commands.FrontCommand;
|
||||
import com.baeldung.enterprise.patterns.front.controller.commands.LoginCommand;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AuthenticationFilter extends BaseFilter {
|
||||
private OnIntercept callback;
|
||||
|
||||
public AuthenticationFilter(OnIntercept callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(
|
||||
ServletRequest request,
|
||||
ServletResponse response,
|
||||
FilterChain chain
|
||||
) throws IOException, ServletException {
|
||||
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
||||
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();
|
||||
} else {
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.filters;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
public abstract class BaseFilter implements Filter {
|
||||
private static final Logger log = LoggerFactory.getLogger(BaseFilter.class);
|
||||
|
||||
protected FilterConfig filterConfig;
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
log.info("Initialize filter: {}", getClass().getSimpleName());
|
||||
this.filterConfig = filterConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
log.info("Destroy filter: {}", getClass().getSimpleName());
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.filters;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebFilter(servletNames = "front-controller")
|
||||
public class EncodingFilter extends BaseFilter {
|
||||
private String encoding;
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
super.init(filterConfig);
|
||||
this.encoding = filterConfig.getInitParameter("encoding");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(
|
||||
ServletRequest request,
|
||||
ServletResponse response,
|
||||
FilterChain chain
|
||||
) throws IOException, ServletException {
|
||||
String encoding = request.getParameter("encoding");
|
||||
if (encoding == null) {
|
||||
encoding = this.encoding;
|
||||
}
|
||||
response.setCharacterEncoding(encoding);
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.filters;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class FilterChainImpl implements javax.servlet.FilterChain {
|
||||
private Iterator<Filter> filters;
|
||||
|
||||
public FilterChainImpl(Filter... filters) {
|
||||
this.filters = Arrays.asList(filters).iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(
|
||||
ServletRequest request,
|
||||
ServletResponse response
|
||||
) throws IOException, ServletException {
|
||||
if (filters.hasNext()) {
|
||||
Filter filter = filters.next();
|
||||
filter.doFilter(request, response, this);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.filters;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FilterManager {
|
||||
public static void process(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
OnIntercept callback
|
||||
) throws ServletException, IOException {
|
||||
FilterChain filterChain = new FilterChainImpl(
|
||||
new AuthenticationFilter(callback),
|
||||
new VisitorCounterFilter()
|
||||
);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.filters;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebFilter(servletNames = "front-controller")
|
||||
public class LoggingFilter extends BaseFilter {
|
||||
private static final Logger log = LoggerFactory.getLogger(LoggingFilter.class);
|
||||
|
||||
@Override
|
||||
public void doFilter(
|
||||
ServletRequest request,
|
||||
ServletResponse response,
|
||||
FilterChain chain
|
||||
) throws IOException, ServletException {
|
||||
chain.doFilter(request, response);
|
||||
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
||||
String username = (String) httpServletRequest.getAttribute("username");
|
||||
if (username == null) {
|
||||
username = "guest";
|
||||
}
|
||||
log.info("Request from '{}@{}': {}?{}", username, request.getRemoteAddr(),
|
||||
httpServletRequest.getRequestURI(), request.getParameterMap());
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.filters;
|
||||
|
||||
public interface OnIntercept {
|
||||
void intercept();
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
<%@ 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 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>
|
|
@ -1,11 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bookshelf: Title not found</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Our Bookshelf doesn't contains this title:</p>
|
||||
<h2><%= request.getParameter("q") %></h2>
|
||||
<%@ include file="visitor-counter.jsp"%>
|
||||
</body>
|
||||
</html>
|
|
@ -1,27 +0,0 @@
|
|||
<%@ 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>
|
|
@ -1,16 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bookshelf: Login</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Please input a username:</p>
|
||||
<h2>Login</h2>
|
||||
<form action="index?command=Login" method="POST">
|
||||
<input type="text" name="username" placeholder="Username">
|
||||
<input type="hidden" name="redirect" value="<%= (String) request.getAttribute("redirect") %>">
|
||||
<input type="submit" value="Proceed">
|
||||
</form>
|
||||
<%@ include file="visitor-counter.jsp" %>
|
||||
</body>
|
||||
</html>
|
|
@ -1,16 +0,0 @@
|
|||
<%@ 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>
|
||||
<% } %>
|
||||
<% } %>
|
|
@ -1,29 +0,0 @@
|
|||
<%@ 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>
|
|
@ -1,9 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bookshelf: Command unknown</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Sorry, this command is not known!</p>
|
||||
</body>
|
||||
</html>
|
|
@ -1,5 +0,0 @@
|
|||
<% Integer counter = (Integer) request.getAttribute("counter"); %>
|
||||
<% if (counter != null && counter > 0) { %>
|
||||
<hr/>
|
||||
<p>You are visitor #<%= counter %>. <a href="index?command=Logout">Logout</a></p>
|
||||
<% } %>
|
Loading…
Reference in New Issue