Search in sources :

Example 1 with FakeHttpSession

use of org.atmosphere.util.FakeHttpSession in project atmosphere by Atmosphere.

the class SessionTest method basicSessionTest.

@Test
public void basicSessionTest() throws IOException, ServletException, ExecutionException, InterruptedException {
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().build();
    assertNull(request.getSession(false));
    assertNotNull(request.getSession());
    assertNotNull(request.getSession(true));
    assertNotNull(request.getSession());
    request = new AtmosphereRequestImpl.Builder().session(new FakeHttpSession("-1", null, System.currentTimeMillis(), -1)).build();
    assertNotNull(request.getSession());
    assertNotNull(request.getSession(true));
}
Also used : FakeHttpSession(org.atmosphere.util.FakeHttpSession) Test(org.testng.annotations.Test)

Example 2 with FakeHttpSession

use of org.atmosphere.util.FakeHttpSession in project atmosphere by Atmosphere.

the class SessionTest method basicAtmosphereResourceSessionTest.

@Test
public void basicAtmosphereResourceSessionTest() throws IOException, ServletException, ExecutionException, InterruptedException {
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().build();
    AtmosphereResponse response = new AtmosphereResponseImpl.Builder().build();
    AtmosphereConfig config = new AtmosphereFramework().getAtmosphereConfig();
    AtmosphereResource r = config.resourcesFactory().create(new AtmosphereFramework().getAtmosphereConfig(), request, response, mock(AsyncSupport.class));
    r.getAtmosphereConfig().setSupportSession(true);
    assertNull(r.session(false));
    assertNotNull(r.session());
    assertNotNull(r.session(true));
    assertNotNull(r.session());
    request = new AtmosphereRequestImpl.Builder().session(new FakeHttpSession("-1", null, System.currentTimeMillis(), -1)).build();
    response = new AtmosphereResponseImpl.Builder().build();
    r = config.resourcesFactory().create(new AtmosphereFramework().getAtmosphereConfig(), request, response, mock(AsyncSupport.class));
    r.getAtmosphereConfig().setSupportSession(true);
    assertNotNull(r.session());
    assertNotNull(r.session(true));
}
Also used : FakeHttpSession(org.atmosphere.util.FakeHttpSession) Test(org.testng.annotations.Test)

Example 3 with FakeHttpSession

use of org.atmosphere.util.FakeHttpSession in project atmosphere by Atmosphere.

the class AtmosphereRequestImpl method cloneRequest.

/**
 * Copy the HttpServletRequest content inside an AtmosphereRequest. By default the returned AtmosphereRequest
 * is not destroyable.
 *
 * @param request {@link HttpServletRequest}
 * @return an {@link AtmosphereRequest}
 */
public static final AtmosphereRequest cloneRequest(HttpServletRequest request, boolean loadInMemory, boolean copySession, boolean isDestroyable, boolean createSession) {
    Builder b;
    HttpServletRequest r;
    Cookie[] cs = request.getCookies();
    Set<Cookie> hs = Collections.synchronizedSet(new HashSet<>());
    if (cs != null) {
        Collections.addAll(hs, cs);
    }
    boolean isWrapped = false;
    if (AtmosphereRequestImpl.class.isAssignableFrom(request.getClass())) {
        b = ((AtmosphereRequestImpl) request).b;
        isWrapped = true;
    } else {
        b = new Builder();
        b.request(request);
    }
    HttpSession session = request.getSession(false);
    if (copySession) {
        session = request.getSession(createSession);
        if (session != null) {
            session = new FakeHttpSession(session);
        } else {
            session = new FakeHttpSession("", null, System.currentTimeMillis(), -1);
        }
    }
    b.servletPath(request.getServletPath()).pathInfo(request.getPathInfo()).contextPath(request.getContextPath()).requestURI(request.getRequestURI()).requestURL(request.getRequestURL().toString()).method(request.getMethod()).serverName(request.getServerName()).serverPort(request.getServerPort()).remoteAddr(request.getRemoteAddr()).remoteHost(request.getRemoteHost()).remotePort(request.getRemotePort()).destroyable(isDestroyable).cookies(hs).session(session).principal(request.getUserPrincipal()).authType(request.getAuthType()).isSSecure(request.isSecure());
    if (loadInMemory) {
        String s = (String) attributeWithoutException(request, FrameworkConfig.THROW_EXCEPTION_ON_CLONED_REQUEST);
        boolean throwException = Boolean.parseBoolean(s);
        r = new NoOpsRequest(throwException);
        if (isWrapped) {
            load(b.request, b);
        } else {
            load(request, b);
        }
        b.request(r);
    }
    return isWrapped ? (AtmosphereRequestImpl) request : b.build();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Cookie(jakarta.servlet.http.Cookie) FakeHttpSession(org.atmosphere.util.FakeHttpSession) HttpSession(jakarta.servlet.http.HttpSession) FakeHttpSession(org.atmosphere.util.FakeHttpSession)

Example 4 with FakeHttpSession

use of org.atmosphere.util.FakeHttpSession in project atmosphere by Atmosphere.

the class AtmosphereRequestImpl method cloneRequest.

/**
     * Copy the HttpServletRequest content inside an AtmosphereRequest. By default the returned AtmosphereRequest
     * is not destroyable.
     *
     * @param request {@link HttpServletRequest}
     * @return an {@link AtmosphereRequest}
     */
public static final AtmosphereRequest cloneRequest(HttpServletRequest request, boolean loadInMemory, boolean copySession, boolean isDestroyable, boolean createSession) {
    Builder b;
    HttpServletRequest r;
    Cookie[] cs = request.getCookies();
    Set<Cookie> hs = Collections.synchronizedSet(new HashSet());
    if (cs != null) {
        for (Cookie c : cs) {
            hs.add(c);
        }
    }
    boolean isWrapped = false;
    if (AtmosphereRequestImpl.class.isAssignableFrom(request.getClass())) {
        b = AtmosphereRequestImpl.class.cast(request).b;
        isWrapped = true;
    } else {
        b = new Builder();
        b.request(request);
    }
    HttpSession session = request.getSession(false);
    if (copySession) {
        session = request.getSession(createSession);
        if (session != null) {
            session = new FakeHttpSession(session);
        } else {
            session = new FakeHttpSession("", null, System.currentTimeMillis(), -1);
        }
    }
    b.servletPath(request.getServletPath()).pathInfo(request.getPathInfo()).contextPath(request.getContextPath()).requestURI(request.getRequestURI()).requestURL(request.getRequestURL().toString()).method(request.getMethod()).serverName(request.getServerName()).serverPort(request.getServerPort()).remoteAddr(request.getRemoteAddr()).remoteHost(request.getRemoteHost()).remotePort(request.getRemotePort()).destroyable(isDestroyable).cookies(hs).session(session).principal(request.getUserPrincipal()).authType(request.getAuthType()).isSSecure(request.isSecure());
    if (loadInMemory) {
        String s = (String) attributeWithoutException(request, FrameworkConfig.THROW_EXCEPTION_ON_CLONED_REQUEST);
        boolean throwException = s != null && Boolean.parseBoolean(s);
        r = new NoOpsRequest(throwException);
        if (isWrapped) {
            load(b.request, b);
        } else {
            load(request, b);
        }
        b.request(r);
    }
    return isWrapped ? AtmosphereRequestImpl.class.cast(request) : b.build();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) FakeHttpSession(org.atmosphere.util.FakeHttpSession) HttpSession(javax.servlet.http.HttpSession) FakeHttpSession(org.atmosphere.util.FakeHttpSession) HashSet(java.util.HashSet)

Example 5 with FakeHttpSession

use of org.atmosphere.util.FakeHttpSession in project atmosphere by Atmosphere.

the class SessionTest method basicSessionTest.

@Test
public void basicSessionTest() throws IOException, ServletException, ExecutionException, InterruptedException {
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().build();
    assertNull(request.getSession(false));
    assertNotNull(request.getSession());
    assertNotNull(request.getSession(true));
    assertNotNull(request.getSession());
    request = new AtmosphereRequestImpl.Builder().session(new FakeHttpSession("-1", null, System.currentTimeMillis(), -1)).build();
    assertNotNull(request.getSession());
    assertNotNull(request.getSession(true));
}
Also used : FakeHttpSession(org.atmosphere.util.FakeHttpSession) Test(org.testng.annotations.Test)

Aggregations

FakeHttpSession (org.atmosphere.util.FakeHttpSession)6 Test (org.testng.annotations.Test)4 Cookie (jakarta.servlet.http.Cookie)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpSession (jakarta.servlet.http.HttpSession)1 HashSet (java.util.HashSet)1 Cookie (javax.servlet.http.Cookie)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1