From d7a79221269da81f5cca9090cf7a4f836bd2d108 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Mon, 27 Jun 2016 14:58:01 +0000 Subject: [PATCH] Logout from ckan through new servlet git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/gcube-ckan-datacatalog@129484 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/CkanEventHandlerManager.java | 35 ++++- .../client/GCubeCkanDataCatalog.java | 4 +- .../server/CkanLogout.java | 147 ++++++++++++++++++ .../GcubeCkanDataCatalogServiceImpl.java | 44 ++++-- src/main/webapp/WEB-INF/web.xml | 22 ++- 5 files changed, 231 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/CkanLogout.java diff --git a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/client/CkanEventHandlerManager.java b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/client/CkanEventHandlerManager.java index e82b63b..ec5c8d8 100644 --- a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/client/CkanEventHandlerManager.java +++ b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/client/CkanEventHandlerManager.java @@ -25,6 +25,11 @@ import org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui.CreateDatase import com.github.gwtbootstrap.client.ui.Modal; import com.google.gwt.event.shared.HandlerManager; +import com.google.gwt.http.client.Request; +import com.google.gwt.http.client.RequestBuilder; +import com.google.gwt.http.client.RequestCallback; +import com.google.gwt.http.client.RequestException; +import com.google.gwt.http.client.Response; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; @@ -173,7 +178,33 @@ public class CkanEventHandlerManager { @Override public void onLogout(NotifyLogoutEvent editMetadataEvent) { - GCubeCkanDataCatalog.service.logoutFromCkan(new AsyncCallback() { +// com.google.gwt.user.client.Window.open(GCubeCkanDataCatalog.CKAN_LOGUT_SERVICE, "_blank", ""); +// com.google.gwt.user.client.Window.open("https://ckan-d-d4s.d4science.org:443/ckan-connector/gcube/service/disconnect?gcube-token=4620e6d0-2313-4f48-9d54-eb3efd01a810", "_blank", ""); + + RequestBuilder logutRequestBuilder = new RequestBuilder(RequestBuilder.GET, GCubeCkanDataCatalog.CKAN_LOGUT_SERVICE); + + logutRequestBuilder.setCallback(new RequestCallback() { + + @Override + public void onResponseReceived(Request request, Response response) { + +// Window.alert("Logout: "+response.getHeadersAsString()); + } + + @Override + public void onError(Request request, Throwable exception) { + + // TODO Auto-generated method stub + } + }); + + try { + logutRequestBuilder.send(); + } catch ( RequestException e ) { + Window.alert("Error on logout"); + } + + /*GCubeCkanDataCatalog.service.logoutFromCkan(new AsyncCallback() { @Override public void onFailure(Throwable caught) { @@ -188,7 +219,7 @@ public class CkanEventHandlerManager { // TODO Auto-generated method stub } - }); + });*/ } }); diff --git a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/client/GCubeCkanDataCatalog.java b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/client/GCubeCkanDataCatalog.java index 730cabb..97398b8 100644 --- a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/client/GCubeCkanDataCatalog.java +++ b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/client/GCubeCkanDataCatalog.java @@ -5,7 +5,7 @@ import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.event.NotifyLogo import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.view.GCubeCkanDataCatalogPanel; import com.google.gwt.core.client.EntryPoint; -import com.google.gwt.core.shared.GWT; +import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.Window.ClosingEvent; import com.google.gwt.user.client.ui.RootPanel; @@ -21,6 +21,8 @@ public class GCubeCkanDataCatalog implements EntryPoint { * service. */ public static final GcubeCkanDataCatalogServiceAsync service = GWT.create(GcubeCkanDataCatalogService.class); + public static final String CKAN_LOGUT_SERVICE = GWT.getModuleBaseURL() + "gcubeckanlogout"; + private final String DIV_PORTLET_ID = "gCubeCkanDataCatalog"; private CkanEventHandlerManager eventManager = new CkanEventHandlerManager(); public static final String GET_PATH_PARAMETER = "path"; diff --git a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/CkanLogout.java b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/CkanLogout.java new file mode 100644 index 0000000..52be1e9 --- /dev/null +++ b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/CkanLogout.java @@ -0,0 +1,147 @@ +/** + * + */ +package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.gcube.application.framework.core.session.ASLSession; +import org.gcube.application.framework.core.session.SessionManager; +import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.CkanConnectorAccessPoint; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpStatus; +import eu.trentorise.opendata.traceprov.internal.org.apache.commons.io.IOUtils; + + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 27, 2016 + */ +public class CkanLogout extends HttpServlet { + + /** + * + */ + private static final long serialVersionUID = 2793892309831716065L; + + private static Logger logger = LoggerFactory.getLogger(CkanLogout.class); + + /* (non-Javadoc) + * @see javax.servlet.http.HttpServlet#doDelete(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + */ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + logger.info("CkanLogout performing..."); + HttpSession httpSession = req.getSession(); + ASLSession session = getASLSession(httpSession); + String username = session.getUsername(); + CkanConnectorAccessPoint ckanAP = SessionUtil.getCkanAccessPoint(req.getSession()); + // String token = getGcubeSecurityToken(); + logger.info("Logout from CKAN for: "+username +" by token: "+ckanAP.getGcubeTokenValue()); + + String ckanConnectorLogut = getServletContext().getInitParameter(GcubeCkanDataCatalogServiceImpl.CKANCONNECTORLOGOUT); + logger.debug(GcubeCkanDataCatalogServiceImpl.CKANCONNECTORLOGOUT + " is: "+ckanConnectorLogut); + + CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(ckanAP.getBaseUrl()); + ckan.addPathInfo(ckanConnectorLogut); + ckan.addGubeToken(ckanAP.getGcubeTokenValue()); + + URL url; + try { + String deleteURI = ckan.buildURI(); + logger.debug("Perfoming HTTP delete to URI: "+deleteURI); + url = new URL(deleteURI); + HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); +// httpCon.setDoOutput(true); +// httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded" ); +// httpCon.setRequestProperty(key, value); + httpCon.setRequestMethod("GET"); +// httpCon.setRequestMethod("DELETE"); + httpCon.connect(); + + HttpServletResponse response = resp; + + if(httpCon.getResponseCode()==HttpStatus.SC_OK){ + response.setContentLength(httpCon.getContentLength()); + Map> map = httpCon.getHeaderFields(); + for (String key : map.keySet()) { + String hf = httpCon.getHeaderField(key); + logger.trace("key: "+key +", value: "+hf); + if(key==null){ + logger.trace("skip key: "+key +", value: "+hf); + }else + response.setHeader(key,hf); + } + + response.setContentLength(httpCon.getContentLength()); + String encoding = httpCon.getContentEncoding(); + encoding = encoding == null ? GcubeCkanDataCatalogServiceImpl.UTF_8 : encoding; + response.setCharacterEncoding(encoding); + response.setStatus(HttpStatus.SC_OK); + response.setContentType(httpCon.getContentType()); + +// Cookie cookie = new Cookie("user", null); // Not necessary, but saves bandwidth. +// cookie.setPath("/MyApplication"); +// cookie.setHttpOnly(true); +// cookie.setMaxAge(0); // Don't set to -1 or it will become a session cookie! +// response.addCookie(cookie); + + InputStream in = httpCon.getInputStream(); + ServletOutputStream out = response.getOutputStream(); + IOUtils.copy(in, out); + logger.info("Logout Completed, response code: "+HttpStatus.SC_OK); + + }else{ + logger.warn("An error occurred during perfoming CKAN logout, Response status is: "+httpCon.getResponseCode()); + logger.warn(IOUtils.toString(httpCon.getErrorStream())); + } + } + catch (IOException e) { + logger.error("An error occured during performing Logout from CKAN for: "+username +" by token: "+ckanAP.getGcubeTokenValue(), e); + } + } + + /** + * Gets the ASL session. + * + * @param httpSession the http session + * @return the ASL session + */ + protected ASLSession getASLSession(HttpSession httpSession) + { + String sessionID = httpSession.getId(); + String user = (String) httpSession.getAttribute(GcubeCkanDataCatalogServiceImpl.USERNAME_ATTRIBUTE); + + if (user == null) { + + logger.warn("****** STARTING IN TEST MODE - NO USER FOUND *******"); + //for test only + user = GcubeCkanDataCatalogServiceImpl.TEST_USER; + httpSession.setAttribute(GcubeCkanDataCatalogServiceImpl.USERNAME_ATTRIBUTE, user); + ASLSession session = SessionManager.getInstance().getASLSession(sessionID, user); + session.setScope(GcubeCkanDataCatalogServiceImpl.TEST_SCOPE); + session.setUserEmailAddress(GcubeCkanDataCatalogServiceImpl.TEST_MAIL); + //session.setScope("/gcube/devsec/devVRE"); + + return session; + } else logger.trace("user found in session "+user); + return SessionManager.getInstance().getASLSession(sessionID, user); + } + + +} diff --git a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/GcubeCkanDataCatalogServiceImpl.java b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/GcubeCkanDataCatalogServiceImpl.java index 52bfabd..21881ff 100644 --- a/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/GcubeCkanDataCatalogServiceImpl.java +++ b/src/main/java/org/gcube/portlets/gcubeckan/gcubeckandatacatalog/server/GcubeCkanDataCatalogServiceImpl.java @@ -10,6 +10,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.codec.binary.Base64; @@ -46,6 +48,10 @@ import eu.trentorise.opendata.traceprov.internal.org.apache.commons.io.IOUtils; @SuppressWarnings("serial") public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implements GcubeCkanDataCatalogService { + /** + * + */ + public static final String UTF_8 = "UTF-8"; private static final String PORT_HTTP = ":80"; private static final String PORT_HTTPS = ":443"; private static final String HTTPS = "https"; @@ -57,10 +63,10 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem private static Logger logger = LoggerFactory.getLogger(GcubeCkanDataCatalogServiceImpl.class); private final static String DEFAULT_ROLE = "OrganizationMember"; - private final static String TEST_USER = "francesco.mangiacrapa"; - private final static String TEST_SCOPE = "/gcube/devsec/devVRE"; - private final static String TEST_MAIL = "francesco.mangiacrapa@isti.cnr.it"; - private final static String TEST_SEC_TOKEN = "4620e6d0-2313-4f48-9d54-eb3efd01a810"; + public final static String TEST_USER = "francesco.mangiacrapa"; + public final static String TEST_SCOPE = "/gcube/devsec/devVRE"; + public final static String TEST_MAIL = "francesco.mangiacrapa@isti.cnr.it"; + public final static String TEST_SEC_TOKEN = "4620e6d0-2313-4f48-9d54-eb3efd01a810"; // private final static String TEST_SEC_TOKEN = "f539884c-8697-4ac0-9bbf-2f4d595281f5"; public static final String CKAN_TOKEN_KEY = "ckanToken"; @@ -409,17 +415,31 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem httpCon.setRequestMethod("DELETE"); httpCon.connect(); + HttpServletResponse response = this.getThreadLocalResponse(); + if(httpCon.getResponseCode()==HttpStatus.SC_OK){ + response.setContentLength(httpCon.getContentLength()); Map> map = httpCon.getHeaderFields(); for (String key : map.keySet()) { - logger.trace("key: "+key +", value: "+map.get(key)); + String hf = httpCon.getHeaderField(key); + logger.trace("key: "+key +", value: "+hf); + if(key==null){ + logger.trace("skyp key: "+key +", value: "+hf); + }else + response.setHeader(key,hf); } - InputStream in = httpCon.getInputStream(); + + response.setContentLength(httpCon.getContentLength()); String encoding = httpCon.getContentEncoding(); - encoding = encoding == null ? "UTF-8" : encoding; - String body = IOUtils.toString(in, encoding); - logger.debug("response: "+body); + encoding = encoding == null ? UTF_8 : encoding; + response.setCharacterEncoding(encoding); + response.setStatus(HttpStatus.SC_OK); + + InputStream in = httpCon.getInputStream(); + ServletOutputStream out = response.getOutputStream(); + IOUtils.copy(in, out); logger.info("Logout Completed, response code: "+HttpStatus.SC_OK); + }else{ logger.warn("An error occurred during perfoming CKAN logout, Response status is: "+httpCon.getResponseCode()); } @@ -427,10 +447,9 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem catch (IOException e) { logger.error("An error occured during performing Logout from CKAN for: "+username +" by token: "+ckanAP.getGcubeTokenValue(), e); } - } - + /* public static void main(String[] args) { URL url; @@ -462,6 +481,5 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem catch (IOException e) { logger.error("An error occured during performing Logout from CKAN", e); } - - } + }*/ } diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 6c0021a..973b10a 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -10,11 +10,12 @@ /gcube/service/connect - - - - - + + + + + ckan connector logout from CKAN @@ -28,6 +29,12 @@ org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server.GcubeCkanDataCatalogServiceImpl + + gcubeckanlogout + org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server.CkanLogout + + + ckanpublisherservices org.gcube.portlets.widgets.ckandatapublisherwidget.server.CKANPublisherServicesImpl @@ -44,6 +51,11 @@ /gCubeCkanDataCatalog/ckanservices + + gcubeckanlogout + /gCubeCkanDataCatalog/gcubeckanlogout + +