Search in sources :

Example 1 with ForcedRedirectException

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();
    }
}
Also used : Locale(java.util.Locale) IRequestWebScopeWithoutResponse(com.helger.web.scope.IRequestWebScopeWithoutResponse) LayoutExecutionContext(com.helger.photon.core.execcontext.LayoutExecutionContext) HCBody(com.helger.html.hc.html.sections.HCBody) InternalErrorBuilder(com.helger.photon.core.interror.InternalErrorBuilder) HCHead(com.helger.html.hc.html.metadata.HCHead) IMenuItemPage(com.helger.photon.core.menu.IMenuItemPage) ForcedRedirectException(com.helger.xservlet.forcedredirect.ForcedRedirectException) IHCNode(com.helger.html.hc.IHCNode)

Example 2 with ForcedRedirectException

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);
}
Also used : UnifiedResponse(com.helger.servlet.response.UnifiedResponse) ForcedRedirectException(com.helger.xservlet.forcedredirect.ForcedRedirectException) ServletException(javax.servlet.ServletException) ForcedRedirectException(com.helger.xservlet.forcedredirect.ForcedRedirectException) IOException(java.io.IOException)

Example 3 with ForcedRedirectException

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);
        }
    }
}
Also used : IXServletHighLevelFilter(com.helger.xservlet.filter.IXServletHighLevelFilter) XServletFilterTrackRequest(com.helger.xservlet.filter.XServletFilterTrackRequest) IXServletHandler(com.helger.xservlet.handler.IXServletHandler) XServletFilterTimer(com.helger.xservlet.filter.XServletFilterTimer) ForcedRedirectException(com.helger.xservlet.forcedredirect.ForcedRedirectException) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ServletException(javax.servlet.ServletException) ForcedRedirectException(com.helger.xservlet.forcedredirect.ForcedRedirectException) IOException(java.io.IOException)

Aggregations

ForcedRedirectException (com.helger.xservlet.forcedredirect.ForcedRedirectException)3 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 IHCNode (com.helger.html.hc.IHCNode)1 HCHead (com.helger.html.hc.html.metadata.HCHead)1 HCBody (com.helger.html.hc.html.sections.HCBody)1 LayoutExecutionContext (com.helger.photon.core.execcontext.LayoutExecutionContext)1 InternalErrorBuilder (com.helger.photon.core.interror.InternalErrorBuilder)1 IMenuItemPage (com.helger.photon.core.menu.IMenuItemPage)1 UnifiedResponse (com.helger.servlet.response.UnifiedResponse)1 IRequestWebScopeWithoutResponse (com.helger.web.scope.IRequestWebScopeWithoutResponse)1 IXServletHighLevelFilter (com.helger.xservlet.filter.IXServletHighLevelFilter)1 XServletFilterTimer (com.helger.xservlet.filter.XServletFilterTimer)1 XServletFilterTrackRequest (com.helger.xservlet.filter.XServletFilterTrackRequest)1 IXServletHandler (com.helger.xservlet.handler.IXServletHandler)1 Locale (java.util.Locale)1