List of usage examples for java.net CookieHandler getDefault
public static synchronized CookieHandler getDefault()
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_Sessions01() { final AtomicBoolean valueBound = new AtomicBoolean(false); final AtomicBoolean valueUnbound = new AtomicBoolean(false); final HttpSessionBindingListener bindingListener = new HttpSessionBindingListener() { @Override/*from ww w. jav a 2 s . c o m*/ public void valueUnbound(HttpSessionBindingEvent event) { valueUnbound.set(true); } @Override public void valueBound(HttpSessionBindingEvent event) { valueBound.set(true); } }; final AtomicBoolean sessionCreated = new AtomicBoolean(false); final AtomicBoolean sessionDestroyed = new AtomicBoolean(false); HttpSessionListener sessionListener = new HttpSessionListener() { @Override public void sessionDestroyed(HttpSessionEvent se) { sessionDestroyed.set(true); } @Override public void sessionCreated(HttpSessionEvent se) { sessionCreated.set(true); } }; HttpServlet sessionServlet = new HttpServlet() { private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); if (session.getAttribute("test.attribute") == null) { session.setAttribute("test.attribute", bindingListener); response.getWriter().print("created"); } else { session.invalidate(); response.getWriter().print("invalidated"); } } }; ServiceRegistration<Servlet> servletReg = null; ServiceRegistration<HttpSessionListener> sessionListenerReg = null; Dictionary<String, Object> servletProps = new Hashtable<String, Object>(); servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/sessions"); String actual = null; CookieHandler previous = CookieHandler.getDefault(); CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); try { servletReg = getBundleContext().registerService(Servlet.class, sessionServlet, servletProps); Dictionary<String, String> listenerProps = new Hashtable<String, String>(); listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true"); sessionListenerReg = getBundleContext().registerService(HttpSessionListener.class, sessionListener, listenerProps); sessionCreated.set(false); valueBound.set(false); sessionDestroyed.set(false); valueUnbound.set(false); // first call will create the session actual = requestAdvisor.request("sessions"); assertEquals("Wrong result", "created", actual); assertTrue("No sessionCreated called", sessionCreated.get()); assertTrue("No valueBound called", valueBound.get()); assertFalse("sessionDestroyed was called", sessionDestroyed.get()); assertFalse("valueUnbound was called", valueUnbound.get()); sessionCreated.set(false); valueBound.set(false); sessionDestroyed.set(false); valueUnbound.set(false); // second call will invalidate the session actual = requestAdvisor.request("sessions"); assertEquals("Wrong result", "invalidated", actual); assertFalse("sessionCreated was called", sessionCreated.get()); assertFalse("valueBound was called", valueBound.get()); assertTrue("No sessionDestroyed called", sessionDestroyed.get()); assertTrue("No valueUnbound called", valueUnbound.get()); sessionCreated.set(false); sessionDestroyed.set(false); valueBound.set(false); valueUnbound.set(false); // calling again should create the session again actual = requestAdvisor.request("sessions"); assertEquals("Wrong result", "created", actual); assertTrue("No sessionCreated called", sessionCreated.get()); assertTrue("No valueBound called", valueBound.get()); } catch (Exception e) { fail("Unexpected exception: " + e); } finally { if (servletReg != null) { servletReg.unregister(); } if (sessionListenerReg != null) { sessionListenerReg.unregister(); } CookieHandler.setDefault(previous); } }