Search in sources :

Example 1 with IPortalCookie

use of org.apereo.portal.portlet.om.IPortalCookie in project uPortal by Jasig.

the class PortletCookieServiceImpl method getAllPortletCookies.

@Override
public Cookie[] getAllPortletCookies(HttpServletRequest request, IPortletWindowId portletWindowId) {
    final IPortalCookie portalCookie = this.getPortalCookie(request);
    //Get the cookies from the servlet request
    Cookie[] servletCookies = request.getCookies();
    if (servletCookies == null) {
        servletCookies = new Cookie[0];
    } else if (portalCookie != null) {
        for (int i = 0; i < servletCookies.length; i++) {
            if (servletCookies[i].getName().equals(this.cookieName)) {
                // replace cookie in the array with converted IPortalCookie (so secure, domain, path, maxAge are set)
                servletCookies[i] = convertToCookie(portalCookie, this.portalCookieAlwaysSecure || request.isSecure());
            }
        }
    }
    //Get cookies that have been set by portlets, suppressing expired
    Set<IPortletCookie> portletCookies = new HashSet<IPortletCookie>();
    if (portalCookie != null) {
        for (IPortletCookie portletCookie : portalCookie.getPortletCookies()) {
            if (portletCookie.getExpires().isAfterNow()) {
                portletCookies.add(portletCookie);
            }
        }
    }
    // finally get portlet cookies from session (all maxAge -1)
    Map<String, SessionOnlyPortletCookieImpl> sessionOnlyPortletCookieMap = getSessionOnlyPortletCookieMap(request);
    Collection<SessionOnlyPortletCookieImpl> sessionOnlyCookies = sessionOnlyPortletCookieMap.values();
    //Merge into a single array
    final Cookie[] cookies = new Cookie[servletCookies.length + portletCookies.size() + sessionOnlyCookies.size()];
    System.arraycopy(servletCookies, 0, cookies, 0, servletCookies.length);
    int cookieIdx = servletCookies.length;
    for (final IPortletCookie portletCookie : portletCookies) {
        final Cookie cookie = portletCookie.toCookie();
        cookies[cookieIdx++] = cookie;
    }
    for (SessionOnlyPortletCookieImpl sessionOnlyCookie : sessionOnlyCookies) {
        cookies[cookieIdx++] = sessionOnlyCookie.toCookie();
    }
    return cookies;
}
Also used : IPortalCookie(org.apereo.portal.portlet.om.IPortalCookie) Cookie(javax.servlet.http.Cookie) IPortletCookie(org.apereo.portal.portlet.om.IPortletCookie) IPortalCookie(org.apereo.portal.portlet.om.IPortalCookie) IPortletCookie(org.apereo.portal.portlet.om.IPortletCookie) HashSet(java.util.HashSet)

Example 2 with IPortalCookie

use of org.apereo.portal.portlet.om.IPortalCookie in project uPortal by Jasig.

the class PortletCookieServiceImpl method addCookie.

@Override
public void addCookie(HttpServletRequest request, IPortletWindowId portletWindowId, Cookie cookie) {
    final IPortalCookie portalCookie = this.getOrCreatePortalCookie(request);
    if (cookie.getMaxAge() < 0) {
        // persist only in the session
        Map<String, SessionOnlyPortletCookieImpl> sessionOnlyPortletCookies = getSessionOnlyPortletCookieMap(request);
        SessionOnlyPortletCookieImpl sessionOnlyCookie = new SessionOnlyPortletCookieImpl(cookie);
        sessionOnlyPortletCookies.put(cookie.getName(), sessionOnlyCookie);
    } else if (cookie.getMaxAge() == 0) {
        // delete the cookie from the session, if present
        Map<String, SessionOnlyPortletCookieImpl> sessionOnlyPortletCookies = getSessionOnlyPortletCookieMap(request);
        SessionOnlyPortletCookieImpl existing = sessionOnlyPortletCookies.remove(cookie.getName());
        if (null == existing) {
            // returning null from map#remove means cookie wasn't in the session, trigger portletCookieDao update
            this.portletCookieDao.addOrUpdatePortletCookie(portalCookie, cookie);
        }
    } else {
        Map<String, SessionOnlyPortletCookieImpl> sessionOnlyPortletCookies = getSessionOnlyPortletCookieMap(request);
        sessionOnlyPortletCookies.remove(cookie.getName());
        // update the portletCookieDao regardless
        this.portletCookieDao.addOrUpdatePortletCookie(portalCookie, cookie);
    }
}
Also used : IPortalCookie(org.apereo.portal.portlet.om.IPortalCookie) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with IPortalCookie

use of org.apereo.portal.portlet.om.IPortalCookie in project uPortal by Jasig.

the class JpaPortletCookieDaoImpl method createPortalCookie.

@Override
@PortalTransactional
public IPortalCookie createPortalCookie(int maxAge) {
    //Make sure our unique ID doesn't already exist by really small random chance
    String uniqueId;
    do {
        uniqueId = generateNewCookieId();
    } while (this.getPortalCookie(uniqueId) != null);
    //Calculate the expiration date for the cookie
    final DateTime expiration = DateTime.now().plusSeconds(maxAge);
    //Create and persist
    final IPortalCookie portalCookie = new PortalCookieImpl(uniqueId, expiration);
    this.getEntityManager().persist(portalCookie);
    return portalCookie;
}
Also used : IPortalCookie(org.apereo.portal.portlet.om.IPortalCookie) DateTime(org.joda.time.DateTime)

Example 4 with IPortalCookie

use of org.apereo.portal.portlet.om.IPortalCookie in project uPortal by Jasig.

the class PortletCookieServiceImplTest method testGetOrCreatePortalCookieGetExistingFromRequestCookies.

/**
     * Test {@link
     * PortletCookieServiceImpl#getOrCreatePortalCookie(javax.servlet.http.HttpServletRequest)}.
     * that results in returning an existing portalcookie from the request cookies
     */
@Test
public void testGetOrCreatePortalCookieGetExistingFromRequestCookies() {
    IPortletCookieDao portletCookieDao = EasyMock.createMock(IPortletCookieDao.class);
    MockPortalCookie portalCookie = new MockPortalCookie();
    portalCookie.setValue("ABCDEF");
    EasyMock.expect(portletCookieDao.getPortalCookie("ABCDEF")).andReturn(portalCookie);
    EasyMock.replay(portletCookieDao);
    PortletCookieServiceImpl cookieService = new PortletCookieServiceImpl();
    cookieService.setPortletCookieDao(portletCookieDao);
    MockHttpServletRequest request = new MockHttpServletRequest();
    Cookie[] cookies = new Cookie[1];
    Cookie cookie = new Cookie(IPortletCookieService.DEFAULT_PORTAL_COOKIE_NAME, "ABCDEF");
    cookies[0] = cookie;
    request.setCookies(cookies);
    IPortalCookie result = cookieService.getOrCreatePortalCookie(request);
    Assert.assertEquals(portalCookie, result);
    EasyMock.verify(portletCookieDao);
}
Also used : IPortletCookie(org.apereo.portal.portlet.om.IPortletCookie) IPortalCookie(org.apereo.portal.portlet.om.IPortalCookie) Cookie(javax.servlet.http.Cookie) IPortalCookie(org.apereo.portal.portlet.om.IPortalCookie) IPortletCookieDao(org.apereo.portal.portlet.dao.IPortletCookieDao) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Example 5 with IPortalCookie

use of org.apereo.portal.portlet.om.IPortalCookie in project uPortal by Jasig.

the class PortletCookieServiceImplTest method testGetOrCreatePortalCookieGetExistingFromSession.

/**
     * Test {@link
     * PortletCookieServiceImpl#getOrCreatePortalCookie(javax.servlet.http.HttpServletRequest)}.
     * that results in returning an existing portalcookie from the id stored in the session.
     */
@Test
public void testGetOrCreatePortalCookieGetExistingFromSession() {
    IPortletCookieDao portletCookieDao = EasyMock.createMock(IPortletCookieDao.class);
    MockPortalCookie portalCookie = new MockPortalCookie();
    portalCookie.setValue("ABCDEF");
    EasyMock.expect(portletCookieDao.getPortalCookie("ABCDEF")).andReturn(portalCookie);
    EasyMock.replay(portletCookieDao);
    PortletCookieServiceImpl cookieService = new PortletCookieServiceImpl();
    cookieService.setPortletCookieDao(portletCookieDao);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.getSession().setAttribute(PortletCookieServiceImpl.SESSION_ATTRIBUTE__PORTAL_COOKIE_ID, "ABCDEF");
    IPortalCookie result = cookieService.getOrCreatePortalCookie(request);
    Assert.assertEquals(portalCookie, result);
    EasyMock.verify(portletCookieDao);
}
Also used : IPortalCookie(org.apereo.portal.portlet.om.IPortalCookie) IPortletCookieDao(org.apereo.portal.portlet.dao.IPortletCookieDao) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

IPortalCookie (org.apereo.portal.portlet.om.IPortalCookie)12 Cookie (javax.servlet.http.Cookie)8 IPortletCookie (org.apereo.portal.portlet.om.IPortletCookie)7 Test (org.junit.Test)4 IPortletCookieDao (org.apereo.portal.portlet.dao.IPortletCookieDao)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 DateTime (org.joda.time.DateTime)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 HttpSession (javax.servlet.http.HttpSession)1 IPortletWindowId (org.apereo.portal.portlet.om.IPortletWindowId)1 BasePortalJpaDaoTest (org.apereo.portal.test.BasePortalJpaDaoTest)1 HibernateOptimisticLockingFailureException (org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException)1