Class Application
java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
com.creativityfactory.swiftserver.app.Application
- All Implemented Interfaces:
jakarta.servlet.Servlet
,jakarta.servlet.ServletConfig
,Serializable
- Direct Known Subclasses:
ApplicationRunner
public class Application
extends jakarta.servlet.http.HttpServlet
This class is the core of this framework where it's act as your single controller.
It extends from HttpServlet
and override its functionalities to make it
more simple to use.
This class is holding a route mapping container of your application, so a robust routing system.
Here is an example of web application:
@WebServlet("/*")
public MyApp extends Application {
@Override
protected void execute() throws Exception {
get("/", (req, res) -> {
res.setHeader("Content-Type", "text/html")
.write("<h1>Hello world</h1>");
});
}
}
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
createRest
(Class<?> model) This method is responsible for creating a rest api of a given model.protected void
delete
(String urlPattern, HttpRequestHandler handler) Maps a DELETE request with the specified URL pattern to the given HTTP request handler.protected void
doDelete
(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) protected void
doGet
(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) protected void
doPatch
(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) protected void
doPost
(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) protected void
doPut
(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) protected void
execute()
protected void
This method is responsible for automate the creation of web service based on models with annotationRest
.protected void
get
(String urlPattern, HttpRequestHandler handler) Maps a GET request with the specified URL pattern to the given HTTP request handler.void
init()
void
init
(jakarta.servlet.ServletConfig servletConfig) protected void
patch
(String urlPattern, HttpRequestHandler handler) Maps a PATCH request with the specified URL pattern to the given HTTP request handler.protected void
post
(String urlPattern, HttpRequestHandler handler) Maps a PoST request with the specified URL pattern to the given HTTP request handler.protected void
process
(Map<String, List<HttpRequestHandler>> mappedMethods, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) This method is a central method which intercept every incoming http request and look for its handlers.protected void
put
(String urlPattern, HttpRequestHandler handler) Maps a PUT request with the specified URL pattern to the given HTTP request handler.protected void
service
(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) protected void
use
(String urlPattern, HttpRequestHandler handler) Methods inherited from class jakarta.servlet.http.HttpServlet
doHead, doOptions, doTrace, getLastModified, service
Methods inherited from class jakarta.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, log, log
-
Constructor Details
-
Application
public Application()
-
-
Method Details
-
init
public void init() throws jakarta.servlet.ServletException- Overrides:
init
in classjakarta.servlet.GenericServlet
- Throws:
jakarta.servlet.ServletException
-
init
public void init(jakarta.servlet.ServletConfig servletConfig) throws jakarta.servlet.ServletException - Specified by:
init
in interfacejakarta.servlet.Servlet
- Overrides:
init
in classjakarta.servlet.GenericServlet
- Throws:
jakarta.servlet.ServletException
-
process
protected void process(Map<String, List<HttpRequestHandler>> mappedMethods, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOExceptionThis method is a central method which intercept every incoming http request and look for its handlers.
It's responsible for managing middlewares for every endpoint where it starts executing them in the given order from the client code inside a loop.
- Parameters:
mappedMethods
-request
-response
-- Throws:
IOException
-
execute
- Throws:
Exception
-
generateRest
This method is responsible for automate the creation of web service based on models with annotationRest
. It scans classes and process them and do the necessary mapping, for creation of a web service for a model.- Throws:
Exception
-
createRest
protected void createRest(Class<?> model) throws InstantiationException, IllegalAccessException, IOException This method is responsible for creating a rest api of a given model.- Parameters:
model
- The model which the client want to have a rest api on it.- Throws:
InstantiationException
IllegalAccessException
IOException
-
use
-
get
Maps a GET request with the specified URL pattern to the given HTTP request handler.- Parameters:
urlPattern
- The URL pattern to map the handler to.handler
- The HTTP request handler object to handle GET requests.
-
post
Maps a PoST request with the specified URL pattern to the given HTTP request handler.- Parameters:
urlPattern
- The URL pattern to map the handler to.handler
- The HTTP request handler object to handle PoST requests.
-
put
Maps a PUT request with the specified URL pattern to the given HTTP request handler.- Parameters:
urlPattern
- The URL pattern to map the handler to.handler
- The HTTP request handler object to handle PUT requests.
-
delete
Maps a DELETE request with the specified URL pattern to the given HTTP request handler.- Parameters:
urlPattern
- The URL pattern to map the handler to.handler
- The HTTP request handler object to handle DELETE requests.
-
patch
Maps a PATCH request with the specified URL pattern to the given HTTP request handler.- Parameters:
urlPattern
- The URL pattern to map the handler to.handler
- The HTTP request handler object to handle PATCH requests.
-
service
protected void service(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) throws jakarta.servlet.ServletException, IOException - Overrides:
service
in classjakarta.servlet.http.HttpServlet
- Throws:
jakarta.servlet.ServletException
IOException
-
doGet
protected void doGet(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) throws IOException - Overrides:
doGet
in classjakarta.servlet.http.HttpServlet
- Throws:
IOException
-
doPost
protected void doPost(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) throws IOException - Overrides:
doPost
in classjakarta.servlet.http.HttpServlet
- Throws:
IOException
-
doPut
protected void doPut(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) throws IOException - Overrides:
doPut
in classjakarta.servlet.http.HttpServlet
- Throws:
IOException
-
doDelete
protected void doDelete(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) throws IOException - Overrides:
doDelete
in classjakarta.servlet.http.HttpServlet
- Throws:
IOException
-
doPatch
protected void doPatch(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) throws IOException - Throws:
IOException
-