当OmniPartialViewContext #startDocument执行透明重定向时,omnifaces 1.10的错误

时间:2015-11-18 17:07:16

标签: jsf tomcat jsf-2.2 myfaces omnifaces

我正在使用以下堆栈:

  • PrimeFaces 5.3.1
  • MyFaces 2.2.8
  • OpenWebBeans 1.6.2
  • OmniFaces 1.10
  • DeltaSpike 1.5.1
  • Tomcat 8.0.28

AFAI明白,只有MyFaces和OmniFaces在那里很重要。

当会话过期的客户端执行ajax请求以及对< security-constraint>控制页面的访问时,我有一个错误。在webapp web.xml中。

在这种情况下,OmniPartialViewContext#startDocument执行"透明重定向",以提供更好的解释(参见https://github.com/omnifaces/omnifaces/blob/master/src/main/java/org/omnifaces/context/OmniPartialViewContext.java#L275)(此功能从版本1.10到当前1.11不变)。

    @Override
    public void startDocument() throws IOException {
        wrapped.startDocument();
        String loginURL = WebXml.INSTANCE.getFormLoginPage();

        if (loginURL != null) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            String loginViewId = normalizeViewId(facesContext, loginURL);

            if (loginViewId.equals(getViewId(facesContext))) {
                String originalURL = getRequestAttribute(facesContext, "javax.servlet.forward.request_uri");

                if (originalURL != null) {
                    redirect(originalURL);
                }
            }
        }
    }

这是一个问题,因为在堆栈中更高,org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartialRendering会执行第466行和第466行:

    {
        String currentEncoding = writer.getCharacterEncoding();
        writer.writePreamble("<?xml version=\"1.0\" encoding=\""+
            (currentEncoding == null ? "UTF-8" : currentEncoding) +"\"?>");
        writer.startDocument();

        writer.writeAttribute("id", viewRoot.getContainerClientId(_facesContext),"id");

因此,在尝试编写id时会引发类似以下的异常,因为当前没有元素被打开。

16-Nov-2015 16:36:35.980 SEVERE [http-apr-8444-exec-10] org.omnifaces.exceptionhandler.FullAjaxExceptionHandler.logException FullAjaxExceptionHandler: An exception occurred during rendering JSF ajax response. Error page '/error.xhtml' will be shown.
java.lang.IllegalStateException: Must be called before the start element is closed (attribute 'id')
        at org.apache.myfaces.shared.renderkit.html.HtmlResponseWriterImpl.writeAttribute(HtmlResponseWriterImpl.java:816)
        at javax.faces.context.ResponseWriterWrapper.writeAttribute(ResponseWriterWrapper.java:109)
        at org.apache.myfaces.context.PartialResponseWriterImpl.writeAttribute(PartialResponseWriterImpl.java:407)
        at javax.faces.context.ResponseWriterWrapper.writeAttribute(ResponseWriterWrapper.java:109)
        at javax.faces.context.ResponseWriterWrapper.writeAttribute(ResponseWriterWrapper.java:109)
        at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartialRendering(PartialViewContextImpl.java:473)
        at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:415)
        at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
        at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:85)
        at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:516)
        at javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:541)
        at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1891)
        at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:313)
        at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:58)
        at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:58)
        at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:58)
        at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:58)
        at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:58)
        at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:116)
        at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:267)
        at org.apache.deltaspike.jsf.impl.listener.request.DeltaSpikeLifecycleWrapper.render(DeltaSpikeLifecycleWrapper.java:111)
        at javax.faces.lifecycle.LifecycleWrapper.render(LifecycleWrapper.java:31)
        at org.apache.deltaspike.jsf.impl.listener.request.JsfClientWindowAwareLifecycleWrapper.render(JsfClientWindowAwareLifecycleWrapper.java:160)
        at org.apache.deltaspike.jsf.impl.listener.request.DeltaSpikeLifecycleWrapper.render(DeltaSpikeLifecycleWrapper.java:111)
        at javax.faces.lifecycle.LifecycleWrapper.render(LifecycleWrapper.java:31)
        at org.apache.deltaspike.jsf.impl.listener.request.JsfClientWindowAwareLifecycleWrapper.render(JsfClientWindowAwareLifecycleWrapper.java:160)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:200)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:720)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
        at org.apache.catalina.authenticator.FormAuthenticator.forwardToLoginPage(FormAuthenticator.java:384)
        at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:229)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:577)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
        at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:291)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2503)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2492)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)

我怎样才能纠正这个问题(放弃OmniFaces,这真的很酷,我真的想保留。:-))?

1 个答案:

答案 0 :(得分:2)

最终,我使用以下解决方案,可以看作是一种解决方法,因为修补MyFaces或OmniFaces是我无法实现的......

我在处理链的顶部添加了自己的PartialViewContext。这样,当我看到它正在尝试获取登录页面时,我可以对ajax请求执行干净重定向。

要做到这一点,必须:

  • 实现PartialViewContextFactory
  • 实现PartialViewContext
  • 在faces-config.xml文件中声明PartialViewContextFactory

所以,在我的webapp faces-config.xml中,我把:

<factory>
    <partial-view-context-factory>fr.senat.context.SenatPartialViewContextFactory</partial-view-context-factory>
</factory>    

我的PartialViewContextFactory很简单:

package fr.senat.context;

import javax.faces.context.FacesContext;
import javax.faces.context.PartialViewContext;
import javax.faces.context.PartialViewContextFactory;
import lombok.Getter;

/**
*
* @author lpenet
*/
public class SenatPartialViewContextFactory extends PartialViewContextFactory {
    @Getter
    private final PartialViewContextFactory wrapped;

    public SenatPartialViewContextFactory(PartialViewContextFactory wrapped) {
            this.wrapped = wrapped;
    }

    @Override
    public PartialViewContext getPartialViewContext(FacesContext context) {
            return new SenatPartialViewContext(wrapped.getPartialViewContext(context));
    }
}

,PartialViewContext也很简单:

package fr.senat.context;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.PartialResponseWriter;
import javax.faces.context.PartialViewContext;
import javax.faces.context.PartialViewContextWrapper;
import javax.faces.event.PhaseId;
import javax.servlet.http.HttpServletRequest;
import lombok.Getter;
import org.apache.myfaces.context.servlet.PartialViewContextImpl;
import org.omnifaces.config.WebXml;
import static org.omnifaces.util.FacesLocal.getRequestAttribute;
import static org.omnifaces.util.FacesLocal.getViewId;
import static org.omnifaces.util.FacesLocal.normalizeViewId;

/**
 *
 * @author lpenet
 */
public class SenatPartialViewContext  extends PartialViewContextWrapper {

    @Getter
    private final PartialViewContext wrapped;

    public SenatPartialViewContext(PartialViewContext wrapped) {
            this.wrapped = wrapped;
    }

    private void processPartialRendering() throws IOException
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        UIViewRoot viewRoot = facesContext.getViewRoot();

        String loginURL = WebXml.INSTANCE.getFormLoginPage();
        if (loginURL != null) {
            String loginViewId = normalizeViewId(facesContext, loginURL);

            if (loginViewId.equals(getViewId(facesContext))) {
                    String originalURL = getRequestAttribute(facesContext, "javax.servlet.forward.request_uri");

                    if (originalURL != null) {
                        PartialResponseWriter writer = facesContext.getPartialViewContext().getPartialResponseWriter();
                        writer.startDocument();
                        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
                        writer.redirect(request.getContextPath() + loginURL);
                        writer.endDocument();
                        return;
                    }
            }
        }

        wrapped.processPartial(PhaseId.RENDER_RESPONSE);
    }

    @Override
    public void processPartial(PhaseId phaseId)
    {
        if (phaseId == PhaseId.RENDER_RESPONSE)
        {
            try {
                processPartialRendering();
            }
        catch (IOException ex)
        {
            Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
            if (log.isLoggable(Level.SEVERE))
            {
                log.log(Level.SEVERE, "", ex);
            }

        }
        } else {
            wrapped.processPartial(phaseId);
        }
    }

}