use of com.helger.xservlet.filter.IXServletHighLevelFilter in project ph-web by phax.
the class AbstractXServlet method _invokeHandler.
private void _invokeHandler(@Nonnull final HttpServletRequest aHttpRequest, @Nonnull final HttpServletResponse aHttpResponse, @Nonnull final EHttpVersion eHttpVersion, @Nonnull final EHttpMethod eHttpMethod, @Nonnull final IRequestWebScope aRequestScope) throws ServletException, IOException {
// HTTP version and method are valid
m_aCounterRequestsAccepted.increment();
// Find the handler for the HTTP method
// Important: must be done inside this method to handle "HEAD" requests
// properly!
final IXServletHandler aServletHandler = m_aHandlerRegistry.getHandler(eHttpMethod);
if (aServletHandler == null) {
// HTTP method is not supported by this servlet!
m_aCounterHttpMethodUnhandled.increment(eHttpMethod.getName());
aHttpResponse.setHeader(CHttpHeader.ALLOW, m_aHandlerRegistry.getAllowedHttpMethodsString());
if (eHttpVersion.is10())
aHttpResponse.sendError(CHttp.HTTP_BAD_REQUEST);
else
aHttpResponse.sendError(CHttp.HTTP_METHOD_NOT_ALLOWED);
return;
}
// HTTP method is supported by this servlet implementation
final ICommonsList<IXServletHighLevelFilter> aEffectiveFilters = new CommonsArrayList<>(2 + m_aFilterHighLevelList.size());
// Add new instance all the time!
aEffectiveFilters.add(new XServletFilterTimer(this));
// Add new instance all the time!
aEffectiveFilters.add(new XServletFilterTrackRequest());
aEffectiveFilters.addAll(m_aFilterHighLevelList);
try {
// High level filters before
for (final IXServletHighLevelFilter aFilter : aEffectiveFilters) aFilter.beforeRequest(aRequestScope);
// This may indirectly call "_internalService" again (e.g. for HEAD
// requests, which calls GET internally)
aServletHandler.onRequest(aHttpRequest, aHttpResponse, eHttpVersion, eHttpMethod, aRequestScope);
// Handled and no exception
m_aCounterRequestsHandled.increment();
m_aCounterRequestsPerVersionHandled.increment(eHttpVersion.getName());
m_aCounterRequestsPerMethodHandled.increment(eHttpMethod.getName());
} catch (final ForcedRedirectException ex) {
// Handle Post-Redirect-Get here
m_aCounterRequestsPRG.increment();
// Remember the content
ForcedRedirectManager.getInstance().createForcedRedirect(ex);
// And set the redirect
if (eHttpVersion.is10()) {
// For HTTP 1.0 send 302
aHttpResponse.setStatus(CHttp.HTTP_MOVED_TEMPORARY);
} else {
// For HTTP 1.1 send 303
aHttpResponse.setStatus(CHttp.HTTP_SEE_OTHER);
}
// Set the location header
String sTargetURL = ex.getRedirectTargetURL().getAsStringWithEncodedParameters();
if (ServletSettings.isEncodeURLs())
sTargetURL = aHttpResponse.encodeRedirectURL(sTargetURL);
if (LOGGER.isDebugEnabled())
LOGGER.debug("Sending redirect to '" + sTargetURL + "'");
aHttpResponse.addHeader(CHttpHeader.LOCATION, sTargetURL);
} catch (final Exception ex) {
m_aCounterRequestsWithException.increment();
// Invoke exception handler
if (m_aExceptionHandler.forEachBreakable(x -> x.onException(aRequestScope, ex)).isContinue()) {
// No handler handled it - propagate
throw ex;
}
// One exception handled did it - no need to propagate
} finally {
// High level filters after
for (final IXServletHighLevelFilter aFilter : aEffectiveFilters) try {
aFilter.afterRequest(aRequestScope);
} catch (final Exception ex) {
if (LOGGER.isErrorEnabled())
LOGGER.error("Exception in high-level filter afterRequest of " + aFilter + " - caught and ignored", ex);
}
}
}
Aggregations