重定向上的IllegalStateException

时间:2010-11-20 10:02:34

标签: java web-applications jsf

我有一个PhaseListener,它侦听phaseId RENDER_RESPONSE。这个faceListener调用这个方法:

   public void doLogin(ServletRequest request) throws IOException {
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpServletRequest req = (HttpServletRequest) request;
        String code = req.getParameter("code");
        if (StringUtil.isNotBlankString(code)) {
            String authURL = Facebook.getAuthURL(code);
            URL url = new URL(authURL);
            try {
                ....
                if (accessToken != null && expires != null) {
                    boolean isLoginOk = service.authFacebookLogin(accessToken);
                    if (isLoginOk) {
                        fc.getApplication().getNavigationHandler().handleNavigation(fc, "/welcome.xhtml", "logged-in");
                    }
                } else {
                    throw new RuntimeException("Access token and expires not found");
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (FacebookException e) {
                Logger.getLogger(FBOauth.class.getName()).log(Level.SEVERE, "Facebook error", e);
            }
        }
    }

    private String readURL(URL url) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = url.openStream();
        int r;
        while ((r = is.read()) != -1) {
            baos.write(r);
        }
        return new String(baos.toByteArray());
    }

当它重定向时,我得到以下异常,我无法找到任何解决方案。根据我的理解,它被抛出,因为响应已经被评估,但为什么它已经被评估?

java.lang.IllegalStateException at 
org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:522)
                at com.sun.faces.context.ExternalContextImpl.redirect(ExternalContextImpl.java:572)
                at com.sun.faces.application.NavigationHandlerImpl.handleNavigation(NavigationHandlerImpl.java:182)
                at wmc.web.facebook.FBOauth.doLogin(FBOauth.java:57)
                at wmc.web.listeners.FacebookSignInListener.afterPhase(FacebookSignInListener.java:56)
                at com.sun.faces.lifecycle.Phase.handleAfterPhase(Phase.java:189)
                at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:107)
                at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
                at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)

顺便说一下,我非常感谢我在这里得到的所有帮助:)

2 个答案:

答案 0 :(得分:0)

我的猜测是,在执行sendRedirect()之前,您的代码的某些部分已经将文本流式传输到servlet repsonse,可能会发送一些通用信息发送给所有响应?

答案 1 :(得分:0)

你的phaselistener显然是在afterPhase的{​​{1}}上挂钩。现在改变响应为时已晚。响应已发送给客户端。而是挂钩`RENDER_RESPONSE的RENDER_RESPONSE