开发者

IllegalStateException on redirect

I have a PhaseListener which listens on phaseId RENDER_RESPONSE. This faceListener calls this method:

   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());
    }

When it redirects I get the following exception which I cant really find any solution to. From what I understand it is thrown because response is already comitted but why is it already comitted?

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.fa开发者_JAVA百科ces.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)

By the way I really appreciate all the help I get in here :)


Your phaselistener is apparently hooking on afterPhase of RENDER_RESPONSE. It's too late to change the response then. The response is already been sent to the client. Rather hook on beforePhase() of the `RENDER_RESPONSE.


My guess would be that before the sendRedirect() is executed some part of your code has already streamed text to the servlet repsonse, maybe some generic information that is send to all responses?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜