BAEL-325: Added intercepting filters.
This commit is contained in:
committed by
slavisa-baeldung
parent
ae3d9760c9
commit
dcc1889909
+23
-9
@@ -4,7 +4,13 @@
|
||||
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>
|
||||
|
||||
<groupId>com.baeldung.enterprise.patterns</groupId>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.baeldung.patterns</groupId>
|
||||
<artifactId>patterns</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
@@ -15,14 +21,14 @@
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -34,13 +40,21 @@
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>9.4.0.M1</version>
|
||||
<version>9.3.11.v20160721</version>
|
||||
<configuration>
|
||||
<webApp>
|
||||
<contextPath>/front-controller</contextPath>
|
||||
<contextPath>/</contextPath>
|
||||
</webApp>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
-38
@@ -1,38 +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 javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FrontControllerServlet extends HttpServlet {
|
||||
@Override
|
||||
protected void doGet(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response
|
||||
) throws ServletException, IOException {
|
||||
FrontCommand command = getCommand(request);
|
||||
command.init(getServletContext(), 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class FrontCommand {
|
||||
protected ServletContext context;
|
||||
protected HttpServletRequest request;
|
||||
protected HttpServletResponse response;
|
||||
|
||||
public void init(
|
||||
ServletContext servletContext,
|
||||
HttpServletRequest servletRequest,
|
||||
HttpServletResponse servletResponse
|
||||
) {
|
||||
this.context = servletContext;
|
||||
this.request = servletRequest;
|
||||
this.response = servletResponse;
|
||||
}
|
||||
|
||||
public abstract void process() throws ServletException, IOException;
|
||||
|
||||
protected void forward(String target) throws ServletException, IOException {
|
||||
target = String.format("/WEB-INF/jsp/%s.jsp", target);
|
||||
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
|
||||
dispatcher.forward(request, response);
|
||||
}
|
||||
}
|
||||
-21
@@ -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.BookshelfImpl;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SearchCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
Book book = new BookshelfImpl().getInstance()
|
||||
.findByTitle(request.getParameter("title"));
|
||||
if (book != null) {
|
||||
request.setAttribute("book", book);
|
||||
forward("book-found");
|
||||
} else {
|
||||
forward("book-notfound");
|
||||
}
|
||||
}
|
||||
}
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
public interface Bookshelf {
|
||||
|
||||
default void init() {
|
||||
add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
|
||||
add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
|
||||
}
|
||||
|
||||
Bookshelf getInstance();
|
||||
|
||||
<E extends Book> boolean add(E book);
|
||||
|
||||
Book findByTitle(String title);
|
||||
}
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
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) {
|
||||
return this.stream()
|
||||
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.patterns;
|
||||
|
||||
import com.baeldung.patterns.commands.FrontCommand;
|
||||
import com.baeldung.patterns.commands.UnknownCommand;
|
||||
import com.baeldung.patterns.data.Bookshelf;
|
||||
import com.baeldung.patterns.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.patterns.commands.%sCommand",
|
||||
request.getParameter("command")
|
||||
)
|
||||
);
|
||||
return (FrontCommand) type
|
||||
.asSubclass(FrontCommand.class)
|
||||
.newInstance();
|
||||
} catch (Exception e) {
|
||||
return new UnknownCommand();
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user