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;
}
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);
}
}
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;
}
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);
}
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);
}
Aggregations