Search in sources :

Example 21 with HttpSession

use of javax.servlet.http.HttpSession in project jetty.project by eclipse.

the class FormAuthenticator method prepareRequest.

/* ------------------------------------------------------------ */
@Override
public void prepareRequest(ServletRequest request) {
    //if this is a request resulting from a redirect after auth is complete
    //(ie its from a redirect to the original request uri) then due to 
    //browser handling of 302 redirects, the method may not be the same as
    //that of the original request. Replace the method and original post
    //params (if it was a post).
    //
    //See Servlet Spec 3.1 sec 13.6.3
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpSession session = httpRequest.getSession(false);
    if (session == null || session.getAttribute(SessionAuthentication.__J_AUTHENTICATED) == null)
        //not authenticated yet
        return;
    String juri = (String) session.getAttribute(__J_URI);
    if (juri == null || juri.length() == 0)
        //no original uri saved
        return;
    String method = (String) session.getAttribute(__J_METHOD);
    if (method == null || method.length() == 0)
        //didn't save original request method
        return;
    StringBuffer buf = httpRequest.getRequestURL();
    if (httpRequest.getQueryString() != null)
        buf.append("?").append(httpRequest.getQueryString());
    if (!juri.equals(buf.toString()))
        //this request is not for the same url as the original
        return;
    //restore the original request's method on this request
    if (LOG.isDebugEnabled())
        LOG.debug("Restoring original method {} for {} with method {}", method, juri, httpRequest.getMethod());
    Request base_request = Request.getBaseRequest(request);
    base_request.setMethod(method);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest)

Example 22 with HttpSession

use of javax.servlet.http.HttpSession in project jetty.project by eclipse.

the class ResponseTest method testResetWithNewSession.

@Test
public void testResetWithNewSession() throws Exception {
    Response response = getResponse();
    Request request = response.getHttpChannel().getRequest();
    SessionHandler session_handler = new SessionHandler();
    session_handler.setServer(_server);
    session_handler.setUsingCookies(true);
    session_handler.start();
    request.setSessionHandler(session_handler);
    HttpSession session = request.getSession(true);
    assertThat(session, not(nullValue()));
    assertTrue(session.isNew());
    HttpField set_cookie = response.getHttpFields().getField(HttpHeader.SET_COOKIE);
    assertThat(set_cookie, not(nullValue()));
    assertThat(set_cookie.getValue(), startsWith("JSESSIONID"));
    assertThat(set_cookie.getValue(), containsString(session.getId()));
    response.setHeader("Some", "Header");
    response.addCookie(new Cookie("Some", "Cookie"));
    response.getOutputStream().print("X");
    assertThat(response.getHttpFields().size(), is(4));
    response.reset();
    set_cookie = response.getHttpFields().getField(HttpHeader.SET_COOKIE);
    assertThat(set_cookie, not(nullValue()));
    assertThat(set_cookie.getValue(), startsWith("JSESSIONID"));
    assertThat(set_cookie.getValue(), containsString(session.getId()));
    assertThat(response.getHttpFields().size(), is(2));
    response.getWriter();
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) HttpCookie(java.net.HttpCookie) Cookie(javax.servlet.http.Cookie) HttpSession(javax.servlet.http.HttpSession) HttpField(org.eclipse.jetty.http.HttpField) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.junit.Test)

Example 23 with HttpSession

use of javax.servlet.http.HttpSession in project jetty.project by eclipse.

the class GetHttpSessionConfigurator method modifyHandshake.

@Override
public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
    HttpSession httpSession = (HttpSession) request.getHttpSession();
    config.getUserProperties().put(HttpSession.class.getName(), httpSession);
}
Also used : HttpSession(javax.servlet.http.HttpSession)

Example 24 with HttpSession

use of javax.servlet.http.HttpSession in project jetty.project by eclipse.

the class SessionDump method doGet.

/* ------------------------------------------------------------ */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    handleForm(request, response);
    response.setContentType("text/html");
    HttpSession session = request.getSession(getURI(request).indexOf("new") > 0);
    try {
        if (session != null)
            session.isNew();
    } catch (IllegalStateException e) {
        session = null;
    }
    PrintWriter out = response.getWriter();
    out.println("<h1>Session Dump Servlet:</h1>");
    out.println("<form action=\"" + response.encodeURL(getURI(request)) + "\" method=\"post\">");
    if (session == null) {
        out.println("<H3>No Session</H3>");
        out.println("<input type=\"submit\" name=\"Action\" value=\"New Session\"/>");
    } else {
        try {
            out.println("<b>ID:</b> " + session.getId() + "<br/>");
            out.println("<b>New:</b> " + session.isNew() + "<br/>");
            out.println("<b>Created:</b> " + new Date(session.getCreationTime()) + "<br/>");
            out.println("<b>Last:</b> " + new Date(session.getLastAccessedTime()) + "<br/>");
            out.println("<b>Max Inactive:</b> " + session.getMaxInactiveInterval() + "<br/>");
            out.println("<b>Context:</b> " + session.getServletContext() + "<br/>");
            Enumeration<String> keys = session.getAttributeNames();
            while (keys.hasMoreElements()) {
                String name = (String) keys.nextElement();
                String value = "" + session.getAttribute(name);
                out.println("<b>" + name + ":</b> " + value + "<br/>");
            }
            out.println("<b>Name:</b><input type=\"text\" name=\"Name\" /><br/>");
            out.println("<b>Value:</b><input type=\"text\" name=\"Value\" /><br/>");
            out.println("<input type=\"submit\" name=\"Action\" value=\"Set\"/>");
            out.println("<input type=\"submit\" name=\"Action\" value=\"Remove\"/>");
            out.println("<input type=\"submit\" name=\"Action\" value=\"Refresh\"/>");
            out.println("<input type=\"submit\" name=\"Action\" value=\"Invalidate\"/><br/>");
            out.println("</form><br/>");
            if (request.isRequestedSessionIdFromCookie())
                out.println("<P>Turn off cookies in your browser to try url encoding<BR>");
            if (request.isRequestedSessionIdFromURL())
                out.println("<P>Turn on cookies in your browser to try cookie encoding<BR>");
            out.println("<a href=\"" + response.encodeURL(request.getRequestURI() + "?q=0") + "\">Encoded Link</a><BR>");
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Example 25 with HttpSession

use of javax.servlet.http.HttpSession in project jetty.project by eclipse.

the class WebAppObjectInSessionServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {
    try {
        String action = request.getParameter("action");
        if ("set".equals(action)) {
            HttpSession session = request.getSession(true);
            session.setAttribute("staticAttribute", new TestSharedStatic());
            Object staticAttribute = session.getAttribute("staticAttribute");
            Assert.assertTrue(staticAttribute instanceof TestSharedStatic);
        //                session.setAttribute("objectAttribute", new TestSharedNonStatic());
        // The session itself is not shareable, since the implementation class
        // refers to the session manager via the hidden field this$0, and
        // it seems there is no way to mark the hidden field as transient.
        //                session.setAttribute("sessionAttribute", session);
        } else if ("get".equals(action)) {
            HttpSession session = request.getSession(false);
            Object staticAttribute = session.getAttribute("staticAttribute");
            Assert.assertTrue(staticAttribute instanceof TestSharedStatic);
        //                Object objectAttribute = session.getAttribute("objectAttribute");
        //                Assert.assertTrue(objectAttribute instanceof TestSharedNonStatic);
        //                Object sessionAttribute = session.getAttribute("sessionAttribute");
        //                assertTrue(sessionAttribute instanceof HttpSession);
        }
    } catch (Exception e) {
        // e.printStackTrace();
        httpServletResponse.sendError(500, e.toString());
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) HttpSession(javax.servlet.http.HttpSession) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

HttpSession (javax.servlet.http.HttpSession)730 HttpServletRequest (javax.servlet.http.HttpServletRequest)151 Test (org.junit.Test)110 IOException (java.io.IOException)80 HttpServletResponse (javax.servlet.http.HttpServletResponse)80 ServletException (javax.servlet.ServletException)75 ArrayList (java.util.ArrayList)65 RequestDispatcher (javax.servlet.RequestDispatcher)59 HashMap (java.util.HashMap)48 Map (java.util.Map)44 Locale (java.util.Locale)39 Properties (java.util.Properties)39 PrintWriter (java.io.PrintWriter)38 Cookie (javax.servlet.http.Cookie)27 List (java.util.List)24 SQLException (java.sql.SQLException)23 WebUser (org.compiere.util.WebUser)23 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)20 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)20 ModelAndView (org.springframework.web.servlet.ModelAndView)20