use of com.helger.xservlet.forcedredirect.ForcedRedirectException in project phoss-smp by phax.
the class SMPLayoutHTMLProvider method fillBody.
@Override
protected void fillBody(@Nonnull final ISimpleWebExecutionContext aSWEC, @Nonnull final HCHtml aHtml) {
final IRequestWebScopeWithoutResponse aRequestScope = aSWEC.getRequestScope();
final Locale aDisplayLocale = aSWEC.getDisplayLocale();
final IMenuItemPage aMenuItem = RequestSettings.getMenuItem(aRequestScope);
final LayoutExecutionContext aLEC = new LayoutExecutionContext(aSWEC, aMenuItem);
final HCHead aHead = aHtml.head();
final HCBody aBody = aHtml.body();
// Add menu item in page title
aHead.setPageTitle(StringHelper.getConcatenatedOnDemand(CSMP.getApplicationTitle(), " - ", aMenuItem.getDisplayText(aDisplayLocale)));
try {
final IHCNode aNode = m_aFactory.apply(aLEC);
aBody.addChild(aNode);
} catch (final ForcedRedirectException ex) {
throw ex;
} catch (final RuntimeException ex) {
new InternalErrorBuilder().setDisplayLocale(aDisplayLocale).setRequestScope(aRequestScope).setThrowable(ex).setUIErrorHandlerFor(aBody).handle();
}
}
use of com.helger.xservlet.forcedredirect.ForcedRedirectException in project ph-web by phax.
the class XServletHandlerToSimpleHandler method onRequest.
public void onRequest(@Nonnull final HttpServletRequest aHttpRequest, @Nonnull final HttpServletResponse aHttpResponse, @Nonnull final EHttpVersion eHttpVersion, @Nonnull final EHttpMethod eHttpMethod, @Nonnull final IRequestWebScope aRequestScope) throws ServletException, IOException {
final UnifiedResponse aUnifiedResponse = m_aSimpleHandler.createUnifiedResponse(eHttpVersion, eHttpMethod, aHttpRequest, aRequestScope);
if (m_aSimpleHandler.initRequestState(aRequestScope, aUnifiedResponse).isBreak()) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Cancelled request after initRequestState with response " + aUnifiedResponse);
// May e.g. be an 404 error for some not-found resource
} else {
// Init was successful
// Check for last-modification on GET and HEAD
boolean bExecute = true;
if (eHttpMethod == EHttpMethod.GET || eHttpMethod == EHttpMethod.HEAD)
if (_handleETag(aHttpRequest, aRequestScope, aUnifiedResponse).isBreak()) {
// ETag present in request
aUnifiedResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
bExecute = false;
}
if (bExecute) {
// On request begin
try {
m_aSimpleHandler.onRequestBegin(aRequestScope);
} catch (final Exception ex) {
_onException(aRequestScope, aUnifiedResponse, ex);
}
Throwable aCaughtException = null;
try {
// main servlet handling
m_aSimpleHandler.handleRequest(aRequestScope, aUnifiedResponse);
if (LOGGER.isDebugEnabled())
LOGGER.debug("Successfully handled request: " + aRequestScope.getPathWithinServlet());
} catch (final ForcedRedirectException ex) {
// Pass through
throw ex;
} catch (final Exception ex) {
// Invoke exception handler
// This internally re-throws the exception if needed
aCaughtException = ex;
_onException(aRequestScope, aUnifiedResponse, ex);
} finally {
// On request end
try {
m_aSimpleHandler.onRequestEnd(aCaughtException);
} catch (final Exception ex) {
LOGGER.error("onRequestEnd failed", ex);
// Don't throw anything here
}
}
}
}
aUnifiedResponse.applyToResponse(aHttpResponse);
}
use of com.helger.xservlet.forcedredirect.ForcedRedirectException 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