From 87f1f38af752a3302eca99a92022f089dd54e9f2 Mon Sep 17 00:00:00 2001 From: Giancarlo Panichi Date: Thu, 19 Jan 2017 17:57:32 +0000 Subject: [PATCH] Updated to new PortalContext git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-gwt-service@141655 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../user/td/gwtservice/server/SessionOp.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/main/java/org/gcube/portlets/user/td/gwtservice/server/SessionOp.java diff --git a/src/main/java/org/gcube/portlets/user/td/gwtservice/server/SessionOp.java b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/SessionOp.java new file mode 100644 index 0000000..711f07f --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/td/gwtservice/server/SessionOp.java @@ -0,0 +1,106 @@ +package org.gcube.portlets.user.td.gwtservice.server; + +import java.util.HashMap; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.gcube.portlets.user.td.gwtservice.server.util.ServiceCredentials; +import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTServiceException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Giancarlo Panichi email: g.panichi@isti.cnr.it + * + * @param + */ +public class SessionOp { + + private static Logger logger = LoggerFactory.getLogger(SessionOp.class); + + public T get(HttpServletRequest httpRequest, + ServiceCredentials serviceCredentials, String attribute, + Class cls) throws TDGWTServiceException { + HttpSession httpSession = httpRequest.getSession(); + T value = null; + + @SuppressWarnings("unchecked") + HashMap map = (HashMap) httpSession + .getAttribute(attribute); + + if (map != null) { + if (map.containsKey(serviceCredentials.getScope())) { + value = map.get(serviceCredentials.getScope()); + } else { + logger.error("" + attribute + " was not acquired"); + try { + value = cls.newInstance(); + map.put(serviceCredentials.getScope(), value); + } catch (InstantiationException | IllegalAccessException e) { + String error = "Error reading session attribute: " + + e.getLocalizedMessage(); + logger.error(error, e); + throw new TDGWTServiceException(error, e); + } + + } + } else { + logger.error("" + attribute + " was not acquired"); + map = new HashMap<>(); + try { + value = cls.newInstance(); + map.put(serviceCredentials.getScope(), value); + httpSession.setAttribute(attribute, map); + } catch (InstantiationException | IllegalAccessException e) { + String error = "Error reading session attribute: " + + e.getLocalizedMessage(); + logger.error(error, e); + throw new TDGWTServiceException(error, e); + } + + } + return value; + } + + public T get(HttpServletRequest httpRequest, + ServiceCredentials serviceCredentials, String attribute) { + HttpSession httpSession = httpRequest.getSession(); + T value = null; + + @SuppressWarnings("unchecked") + HashMap map = (HashMap) httpSession + .getAttribute(attribute); + + if (map != null) { + if (map.containsKey(serviceCredentials.getScope())) { + value = map.get(serviceCredentials.getScope()); + } else { + logger.error("" + attribute + " was not acquired"); + } + } else { + logger.error("" + attribute + " was not acquired"); + } + return value; + } + + public void set(HttpServletRequest httpRequest, + ServiceCredentials serviceCredentials, String attribute, T value) { + HttpSession httpSession = httpRequest.getSession(); + + @SuppressWarnings("unchecked") + HashMap map = (HashMap) httpSession + .getAttribute(attribute); + + if (map != null) { + map.put(serviceCredentials.getScope(), value); + } else { + map = new HashMap<>(); + map.put(serviceCredentials.getScope(), value); + httpSession.setAttribute(attribute, map); + } + } + +}