Package com.sodius.oslc.server.services
Class HttpFilter
- java.lang.Object
-
- com.sodius.oslc.server.services.HttpFilter
-
- All Implemented Interfaces:
Filter
- Direct Known Subclasses:
AccessLogFilter,ContentSecurityPolicyFilter,JSPCacheFilter,LicenseFilter,LocalesFilter,OAuthFilter,SameSiteCookiesFilter,XSSFilter
public abstract class HttpFilter extends Object implements Filter
A filter skeleton that ensures it is executed in an HTTP context. Subclasses must implementdoFilter(HttpServletRequest, HttpServletResponse, FilterChain)and may override other methods, if needed.This filter serializes in the HTTP response an
OslcErrorembedded in aOslcWebApplicationException, if such exception is thrown by subclasses.- Since:
- 2.0.0
-
-
Constructor Summary
Constructors Constructor Description HttpFilter()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voiddestroy()This implementation does nothing.protected abstract voiddoFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)Processes this filter and either executes or blocks the chain.voiddoFilter(ServletRequest request, ServletResponse response, FilterChain chain)Verifies the request and the response are HTTP ones and callsdoFilter(HttpServletRequest, HttpServletResponse, FilterChain).protected voidhandleOslcError(HttpServletRequest request, HttpServletResponse response, OslcWebApplicationException exception, MediaType contentType)Handles the behavior of the filter when an OSLC Error occurred at running this filter, another filter in the chain or the servlet.voidinit(FilterConfig filterConfig)This implementation does nothing.
-
-
-
Method Detail
-
init
public void init(FilterConfig filterConfig) throws ServletException
This implementation does nothing. Subclasses may override.- Specified by:
initin interfaceFilter- Parameters:
filterConfig- the configuration of the filter.- Throws:
ServletException- not thrown by this implementation.
-
doFilter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
Verifies the request and the response are HTTP ones and callsdoFilter(HttpServletRequest, HttpServletResponse, FilterChain).The method serializes in the HTTP response an
OslcErrorembedded in aOslcWebApplicationExceptionthrown bydoFilter(HttpServletRequest, HttpServletResponse, FilterChain), if any.- Specified by:
doFilterin interfaceFilter- Parameters:
request- the request.response- the response.chain- the filter chain to process.- Throws:
IOException- if thrown bydoFilter(HttpServletRequest, HttpServletResponse, FilterChain).ServletException- if not running in an HTTP context, or if thrown bydoFilter(HttpServletRequest, HttpServletResponse, FilterChain).
-
doFilter
protected abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException
Processes this filter and either executes or blocks the chain.Subclasses may throw a
OslcWebApplicationExceptionto report anOslcError. This filter will automatically serialize such error in the HTTP response.- Parameters:
request- the request.response- the response.chain- the filter chain to process.- Throws:
IOException- if an I/O error occurs.ServletException- if the filter execution fails.
-
handleOslcError
protected void handleOslcError(HttpServletRequest request, HttpServletResponse response, OslcWebApplicationException exception, MediaType contentType) throws IOException
Handles the behavior of the filter when an OSLC Error occurred at running this filter, another filter in the chain or the servlet.This implementation:
- responds the
OslcErrordescription as HTML content whentext/htmlis requested, - responds the
OslcErrorentity as JSON, JSON-LD, XML or Turtle, if such media type is requested, - responds the
OslcErrormessage as plain text if any other media type is requested.
Subclasses may override this method to adapt the response to particular cases, for example to render a different type of HTML content (or redirect to an HTML page) or to handle specific media types.
- Parameters:
request- the received requestresponse- the response to sendexception- theOslcWebApplicationExceptionthrown by the filter, another filter in the chain or the servlet. At this stage the exception is known to embed an OslcError.contentType- the desired content type for the response (e.g.application/jsonortext/html).- Throws:
IOException- exception can be thrown when writing response
- responds the
-
-