users@glassfish.java.net

Re: How to retrieve the original requested URI before login

From: <glassfish_at_javadesktop.org>
Date: Thu, 13 Nov 2008 23:37:25 PST

thank you for your answers. I know the implementation about this is container specific and may change in future releases. This is no problem to me, the of code is part of a specific login module and container specific by default.

Made this one:

private SavedRequest getRequestBeforeLogin(final HttpServletRequest request) {

                if (_logger.isLoggable(Level.FINEST)) {
                        _logger.finest(">>> ENTER");
                }

                CoyoteRequest req = null;
                SavedRequest savedReq = null;
                ServletRequest servletRequest = request;

                try {
                        ServletRequest prevRequest = null;
                        while (servletRequest != prevRequest
                                        && servletRequest instanceof ServletRequestWrapper) {
                                prevRequest = servletRequest;
                                servletRequest = ((ServletRequestWrapper) servletRequest)
                                                .getRequest();
                        }

                        if (servletRequest instanceof CoyoteRequestFacade) {
                                req = ((CoyoteRequestFacade) servletRequest)
                                                .getUnwrappedCoyoteRequest();
                        }

                        final HttpSession session = request.getSession(false);

                        if (session != null) {
                                Context context = req.getContext();
                                if (context != null) {
                                        Manager manager = context.getManager();
                                        if (manager != null) {
                                                Session realSession = manager.findSession(session
                                                                .getId());
                                                if (realSession != null) {
                                                        savedReq = (SavedRequest) realSession
                                                                        .getNote(Constants.FORM_REQUEST_NOTE);
                                                }
                                        }
                                }
                        }
                } catch (Exception ex) {
                        _logger.warning("could not retrieve saved request: " + ex);
                }
                if (_logger.isLoggable(Level.FINEST)) {
                        _logger.finest("<<< LEAVE");
                }

                return savedReq;
        }


invoking this way:

                                                SavedRequest origin = this
                                                                .getRequestBeforeLogin(request);
                                                String fwd = "/";
                                                if (origin != null) {
                                                        fwd = origin.getRequestURI();
                                                        if (origin.getQueryString() != null) {
                                                                fwd = fwd + "?" + origin.getQueryString();
                                                        }
                                                }

                                                request.getRequestDispatcher(fwd).forward(request,
                                                                response);

I'm not able to add request parameters (POST) to the redirect, but for the moment this is no problem....

thank you
[Message sent by forum member 'jdrive' (jdrive)]

http://forums.java.net/jive/thread.jspa?messageID=316716