use of com.helger.servlet.response.StatusAwareHttpResponseWrapper in project ph-web by phax.
the class AbstractXFilter method doHttpFilter.
@Override
public void doHttpFilter(@Nonnull final HttpServletRequest aHttpRequest, @Nonnull final HttpServletResponse aHttpResponse, @Nonnull final FilterChain aChain) throws IOException, ServletException {
// Increase counter
m_aCounterRequestsTotal.increment();
// Create a wrapper around the Servlet Response that saves the status code
final StatusAwareHttpResponseWrapper aHttpResponseWrapper = StatusAwareHttpResponseWrapper.wrap(aHttpResponse);
// Create request scope
final BiFunction<? super HttpServletRequest, ? super HttpServletResponse, IRequestWebScope> aFactory;
aFactory = m_bIsMultipartEnabled ? RequestWebScopeMultipart::new : RequestWebScope::new;
// Create request scope
try (final RequestScopeInitializer aRequestScopeInitializer = RequestScopeInitializer.create(aHttpRequest, aHttpResponseWrapper, aFactory)) {
final IRequestWebScope aRequestScope = aRequestScopeInitializer.getRequestScope();
try {
if (onFilterBefore(aHttpRequest, aHttpResponseWrapper, aRequestScope).isContinue()) {
// Continue request processing
m_aCounterRequestsBeforeContinue.increment();
aChain.doFilter(aHttpRequest, aHttpResponse);
onFilterAfter(aHttpRequest, aHttpResponseWrapper, aRequestScope);
} else {
m_aCounterRequestsBeforeBreak.increment();
}
} catch (final Exception ex) {
m_aCounterRequestsWithException.increment();
if (m_aExceptionHandler.forEachBreakable(x -> x.onException(aRequestScope, ex)).isContinue()) {
// This log entry is mainly present to have an overview on how often
// this really happens
LOGGER.error("Filter exception propagated to the outside", ex);
// Ensure only exceptions with the correct type are propagated
if (ex instanceof IOException)
throw (IOException) ex;
if (ex instanceof ServletException)
throw (ServletException) ex;
throw new ServletException("Wrapped " + ex.getClass().getName(), ex);
}
}
}
}
use of com.helger.servlet.response.StatusAwareHttpResponseWrapper in project ph-web by phax.
the class XServletFilterConsistency method afterRequest.
@Override
public void afterRequest(@Nonnull final HttpServletRequest aHttpRequest, @Nonnull final HttpServletResponse aHttpResponse, @Nonnull final EHttpVersion eHttpVersion, @Nonnull final EHttpMethod eHttpMethod, final boolean bInvokeHandler, @Nullable final Throwable aCaughtException, final boolean bIsHandledAsync) {
ValueEnforcer.isTrue(aHttpResponse instanceof StatusAwareHttpResponseWrapper, "Must be a StatusAwareHttpResponseWrapper");
final String sRequestURL = RequestHelper.getURLDecoded(aHttpRequest);
final int nStatusCode = ((StatusAwareHttpResponseWrapper) aHttpResponse).getStatusCode();
final HttpHeaderMap aHeaders = ((StatusAwareHttpResponseWrapper) aHttpResponse).headerMap();
final String sCharacterEncoding = aHttpResponse.getCharacterEncoding();
final String sContentType = aHttpResponse.getContentType();
checkStatusCode(sRequestURL, nStatusCode, eHttpMethod);
checkCharacterEncoding(sRequestURL, sCharacterEncoding, nStatusCode, eHttpMethod);
if (!bIsHandledAsync)
checkContentType(sRequestURL, sContentType, nStatusCode, eHttpMethod);
checkHeaders(sRequestURL, aHeaders, nStatusCode, eHttpMethod);
}
use of com.helger.servlet.response.StatusAwareHttpResponseWrapper in project ph-web by phax.
the class AbstractXServlet method service.
/**
* Dispatches client requests to the protected <code>service</code> method.
* There's no need to override this method.
*
* @param aHttpRequest
* the {@link HttpServletRequest} object that contains the request the
* client made of the servlet
* @param aHttpResponse
* the {@link HttpServletResponse} object that contains the response
* the servlet returns to the client
* @exception IOException
* if an input or output error occurs while the servlet is handling
* the HTTP request
* @exception ServletException
* if the HTTP request cannot be handled
* @see javax.servlet.Servlet#service
*/
@Override
protected final void service(@Nonnull final HttpServletRequest aHttpRequest, @Nonnull final HttpServletResponse aHttpResponse) throws ServletException, IOException {
// Increase counter
m_aCounterRequestsTotal.increment();
// Increase per servlet invocation
m_aStatusMgr.onServletInvocation(getClass());
// Ensure a valid HTTP version is provided
final String sProtocol = aHttpRequest.getProtocol();
final EHttpVersion eHttpVersion = EHttpVersion.getFromNameOrNull(sProtocol);
if (eHttpVersion == null) {
// HTTP version disallowed
logInvalidRequestSetup("Request has unsupported HTTP version (" + sProtocol + ")!", aHttpRequest);
aHttpResponse.sendError(CHttp.HTTP_VERSION_NOT_SUPPORTED);
return;
}
m_aCounterRequestsPerVersionAccepted.increment(eHttpVersion.getName());
// Ensure a valid HTTP method is provided
final String sMethod = aHttpRequest.getMethod();
final EHttpMethod eHttpMethod = EHttpMethod.getFromNameOrNull(sMethod);
if (eHttpMethod == null) {
// HTTP method unknown
logInvalidRequestSetup("Request has unsupported HTTP method (" + sMethod + ")!", aHttpRequest);
aHttpResponse.sendError(CHttp.HTTP_NOT_IMPLEMENTED);
return;
}
m_aCounterRequestsPerMethodAccepted.increment(eHttpMethod.getName());
// May already be set in test cases!
if (FIRST_REQUEST.getAndSet(false) && !StaticServerInfo.isSet()) {
// First set the default web server info
StaticServerInfo.init(aHttpRequest.getScheme(), aHttpRequest.getServerName(), aHttpRequest.getServerPort(), ServletContextPathHolder.getContextPath());
}
// Create a wrapper around the Servlet Response that saves the status code
final StatusAwareHttpResponseWrapper aHttpResponseWrapper = StatusAwareHttpResponseWrapper.wrap(aHttpResponse);
// Create effective filter list with all internal filters as well
final ICommonsList<IXServletLowLevelFilter> aEffectiveFilterList = new CommonsArrayList<>(4 + m_aFilterLowLevelList.size());
// Add internal filters - always first
aEffectiveFilterList.add(XServletFilterSecurityPoxy.INSTANCE);
aEffectiveFilterList.add(XServletFilterConsistency.INSTANCE);
if (m_aSettings.hasHttpReferrerPolicy())
aEffectiveFilterList.add(new XServletFilterSecurityHttpReferrerPolicy(m_aSettings.getHttpReferrerPolicy()));
if (m_aSettings.hasXFrameOptions())
aEffectiveFilterList.add(new XServletFilterSecurityXFrameOptions(m_aSettings.getXFrameOptionsType(), m_aSettings.getXFrameOptionsDomain()));
// Add custom filters
aEffectiveFilterList.addAll(m_aFilterLowLevelList);
// Filter before request scope is created!
boolean bInvokeHandler = true;
for (final IXServletLowLevelFilter aFilter : aEffectiveFilterList) if (aFilter.beforeRequest(aHttpRequest, aHttpResponseWrapper, eHttpVersion, eHttpMethod).isBreak()) {
bInvokeHandler = false;
return;
}
boolean bIsHandledAsync = false;
Exception aCaughtException = null;
try {
if (bInvokeHandler) {
// Create request scope
final BiFunction<? super HttpServletRequest, ? super HttpServletResponse, IRequestWebScope> aFactory;
aFactory = m_aSettings.isMultipartEnabled() ? RequestWebScopeMultipart::new : RequestWebScope::new;
try (final RequestScopeInitializer aRequestScopeInitializer = RequestScopeInitializer.create(aHttpRequest, aHttpResponseWrapper, aFactory)) {
final IRequestWebScope aRequestScope = aRequestScopeInitializer.getRequestScope();
aRequestScope.attrs().putIn(REQUEST_ATTR_SCOPE_CREATED, aRequestScopeInitializer.isNew());
// Find and invoke handler
_invokeHandler(aHttpRequest, aHttpResponseWrapper, eHttpVersion, eHttpMethod, aRequestScope);
bIsHandledAsync = aRequestScope.attrs().getAsBoolean(AbstractXServlet.REQUEST_ATTR_HANDLED_ASYNC, false);
if (bIsHandledAsync) {
// The request scope is needed in the async handler!
aRequestScopeInitializer.internalSetDontDestroyRequestScope();
}
}
}
} catch (final Exception ex) {
// Remember
aCaughtException = ex;
// This log entry is mainly present to have an overview on how often
// this really happens
log("Servlet exception propagated to the outside", ex);
// Ensure only exceptions with the correct type are propagated
if (ex instanceof IOException)
throw (IOException) ex;
if (ex instanceof ServletException)
throw (ServletException) ex;
throw new ServletException("Wrapped " + ex.getClass().getName(), ex);
} finally {
// Filter after
for (final IXServletLowLevelFilter aFilter : aEffectiveFilterList) try {
aFilter.afterRequest(aHttpRequest, aHttpResponseWrapper, eHttpVersion, eHttpMethod, bInvokeHandler, aCaughtException, bIsHandledAsync);
} catch (final ServletException | IOException ex) {
LOGGER.error("Exception in low-level filter afterRequest of " + aFilter + " - re-thrown", ex);
// Don't re-throw in finally
// throw ex;
}
}
}
Aggregations