diff --git a/.classpath b/.classpath index 0c14c4a..cdda5cd 100644 --- a/.classpath +++ b/.classpath @@ -1,12 +1,12 @@ - + - + @@ -40,5 +40,5 @@ - + diff --git a/distro/changelog.xml b/distro/changelog.xml index 573247e..c7d003b 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,19 +1,25 @@ + + Updated to new PortalContext [ticket #6548] + Added fix for support new portal theme - Added different colors for columns of different type [issue #251] + Added different colors for columns of different type [issue + #251] Updated Inline Filter to support soundex, text begin and text - end + end + Updated to GWT 2.6.1 Updated to GXT 3.1.1 Strengthened tabular-data-widgetx now support more rows [issue - #3238] + #3238] + Fixed SelectionModel to select rows on different pages diff --git a/pom.xml b/pom.xml index d0f90dd..154c3eb 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.portlets.user tabular-data-widgetx - 1.9.0-SNAPSHOT + 1.10.0-SNAPSHOT tabular-data-widgetx Widget that allows visualization of tabular data @@ -24,7 +24,7 @@ 3.3.2 3.1.1 - + ${env.KEYS} @@ -82,17 +82,49 @@ ${gxtVersion} - + + - org.gcube.portal - custom-portal-handler + org.gcube.core + common-scope-maps + compile - org.gcube.applicationsupportlayer - aslcore + org.gcube.common.portal + portal-manager + provided + + org.gcube.dvos + usermanagement-core + provided + + + + org.gcube.portal + client-context-library + [1.0.0-SNAPSHOT,) + compile + + + + + + org.gcube.common + common-authorization + provided + + + + org.gcube.common + authorization-client + provided + + + + commons-io commons-io @@ -169,8 +201,8 @@ org.gcube.portlets.user.tdwx.TabularDataWidgetX - - + + org.apache.maven.plugins maven-assembly-plugin @@ -190,7 +222,7 @@ - + diff --git a/src/main/java/org/gcube/portlets/user/tdwx/client/TabularDataXGridPanel.java b/src/main/java/org/gcube/portlets/user/tdwx/client/TabularDataXGridPanel.java index 8dc9158..8ed2aa4 100644 --- a/src/main/java/org/gcube/portlets/user/tdwx/client/TabularDataXGridPanel.java +++ b/src/main/java/org/gcube/portlets/user/tdwx/client/TabularDataXGridPanel.java @@ -9,6 +9,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.gcube.portal.clientcontext.client.GCubeClientContext; import org.gcube.portlets.user.td.widgetcommonevent.client.event.GridHeaderColumnMenuItemEvent; import org.gcube.portlets.user.td.widgetcommonevent.client.type.GridHeaderColumnMenuItemType; import org.gcube.portlets.user.td.widgetcommonevent.shared.CellData; @@ -33,6 +34,7 @@ import org.gcube.portlets.user.tdwx.client.model.util.ColumnConfigGenerator; import org.gcube.portlets.user.tdwx.client.style.DefaultRowStyle; import org.gcube.portlets.user.tdwx.client.util.ColumnPositionComparator; import org.gcube.portlets.user.tdwx.client.util.PagingLoadUrlEncoder; +import org.gcube.portlets.user.tdwx.shared.Constants; import org.gcube.portlets.user.tdwx.shared.ServletParameters; import org.gcube.portlets.user.tdwx.shared.StaticFilterInformation; import org.gcube.portlets.user.tdwx.shared.model.ColumnDefinition; @@ -77,7 +79,6 @@ import com.sencha.gxt.widget.core.client.menu.Menu; import com.sencha.gxt.widget.core.client.menu.MenuItem; import com.sencha.gxt.widget.core.client.menu.SeparatorMenuItem; import com.sencha.gxt.widget.core.client.selection.CellSelection; -import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar; /** * @@ -101,12 +102,12 @@ public class TabularDataXGridPanel extends ContentPanel { private ListStore store; private Grid grid; - + private TableDefinition tableDefinition; private VerticalLayoutContainer container; - //private ExtendedLiveGridView liveGridView; + // private ExtendedLiveGridView liveGridView; private DataRowPagingReader reader; @@ -132,7 +133,6 @@ public class TabularDataXGridPanel extends ContentPanel { private PagingToolBarX pagingToolBar; - /** * @param eventBus */ @@ -149,7 +149,7 @@ public class TabularDataXGridPanel extends ContentPanel { container = new VerticalLayoutContainer(); container.setBorders(false); add(container, new MarginData(0)); - + } /** @@ -447,14 +447,14 @@ public class TabularDataXGridPanel extends ContentPanel { List> columns = columnModel.getColumns(); List> columnsNew = new ArrayList>(); - + DataRowColumnConfig columnTarget = null; for (ColumnConfig col : columns) { columnTarget = (DataRowColumnConfig) col; String columnLocal = columnTarget.getDefinition() .getColumnLocalId(); if (visibleOnlyColumn.compareTo(columnLocal) == 0) { - + columnTarget.setHidden(false); columnsNew.add(columnTarget); } else { @@ -683,64 +683,55 @@ public class TabularDataXGridPanel extends ContentPanel { .generateConfiguration(columnDefinition)); } - columnModel = new ColumnModel(columnsConfig); columnModel = checkOnlyColumn(columnModel); /* - columnModel - .addColumnMoveHandler(new ColumnMoveEvent.ColumnMoveHandler() { - - @Override - public void onColumnMove(ColumnMoveEvent event) { - int columnIndex = event.getIndex(); - @SuppressWarnings("unchecked") - ColumnConfig col = (ColumnConfig) event - .getColumnConfig(); - DataRowColumnConfig columnDataRow = (DataRowColumnConfig) col; - ColumnDefinition colDef = columnDataRow.getDefinition(); - - Log.debug("Column Reordering", "Index: " + columnIndex - + " Label: " + colDef.getLabel() - + " Position: " + colDef.getPosition() - + " ColumnId: " + colDef.getColumnLocalId()); - - if (colDef.isViewColumn()) { - Info.display("Attention", - "The view columns can not be moved"); - // ColumnHeader - // colHeader=grid.getView().getHeader(); - - // event.getSource().moveColumn(columnIndex, - // colDef.getPosition()); - } else { - ColumnsReorderingConfig columnsReorderingConfig = new ColumnsReorderingConfig( - columnIndex, colDef); - ColumnsReorderingEvent columnsReorderingEvent = new ColumnsReorderingEvent( - columnsReorderingConfig); - eventBus.fireEvent(columnsReorderingEvent); - } - } - }); - */ + * columnModel .addColumnMoveHandler(new + * ColumnMoveEvent.ColumnMoveHandler() { + * + * @Override public void onColumnMove(ColumnMoveEvent event) { int + * columnIndex = event.getIndex(); + * + * @SuppressWarnings("unchecked") ColumnConfig col = + * (ColumnConfig) event .getColumnConfig(); + * DataRowColumnConfig columnDataRow = (DataRowColumnConfig) col; + * ColumnDefinition colDef = columnDataRow.getDefinition(); + * + * Log.debug("Column Reordering", "Index: " + columnIndex + " Label: " + + * colDef.getLabel() + " Position: " + colDef.getPosition() + + * " ColumnId: " + colDef.getColumnLocalId()); + * + * if (colDef.isViewColumn()) { Info.display("Attention", + * "The view columns can not be moved"); // ColumnHeader // + * colHeader=grid.getView().getHeader(); + * + * // event.getSource().moveColumn(columnIndex, // + * colDef.getPosition()); } else { ColumnsReorderingConfig + * columnsReorderingConfig = new ColumnsReorderingConfig( columnIndex, + * colDef); ColumnsReorderingEvent columnsReorderingEvent = new + * ColumnsReorderingEvent( columnsReorderingConfig); + * eventBus.fireEvent(columnsReorderingEvent); } } }); + */ if (grid == null) { Log.debug("Setup reader"); reader = new DataRowPagingReader(tableDefinition); - String path = GWT.getModuleBaseURL() + "tdwxdata"; + String path = GWT.getModuleBaseURL() + + Constants.TABULAR_DATA_X_SERVLET; RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, path); builder.setHeader(ServletParameters.TD_SESSION_ID, String.valueOf(tdSessionId)); - + builder.setHeader(Constants.CURR_GROUP_ID, + GCubeClientContext.getCurrentContextId()); HttpProxy proxy = new HttpProxy( builder); proxy.setWriter(new PagingLoadUrlEncoder(staticFilters)); - loader = new PagingLoader>( proxy, reader) { @Override @@ -756,12 +747,11 @@ public class TabularDataXGridPanel extends ContentPanel { pagingToolBar = new PagingToolBarX(PAGINGDIMENSION); pagingToolBar.getElement().getStyle() - .setProperty("borderBottom", "none"); + .setProperty("borderBottom", "none"); pagingToolBar.fixPageTextWidth(); pagingToolBar.setItemId("TDMPagingToolBarX"); pagingToolBar.bind(loader); - grid = new Grid(store, columnModel) { @Override protected void onAfterFirstAttach() { @@ -779,7 +769,7 @@ public class TabularDataXGridPanel extends ContentPanel { grid.setSelectionModel(sm); grid.setLoadMask(true); grid.setLoader(loader); - + grid.setBorders(false); grid.setColumnReordering(false); @@ -799,7 +789,6 @@ public class TabularDataXGridPanel extends ContentPanel { container.add(grid, new VerticalLayoutData(1, 1, new Margins(0))); container.add(pagingToolBar, new VerticalLayoutData(1, -1)); - if (contextMenu != null) { grid.setContextMenu(contextMenu); } else @@ -811,7 +800,7 @@ public class TabularDataXGridPanel extends ContentPanel { } else { Log.debug("Setup grid not null"); - + reader.setDefinition(tableDefinition); loader.clearSortInfo(); @@ -819,20 +808,18 @@ public class TabularDataXGridPanel extends ContentPanel { store)); grid.reconfigure(store, columnModel); - // Filter ArrayList> filters = FiltersGenerator .generate(columnModel); - Log.debug("Filters: " + filters.size()); for (Filter filterGeneric : filters) { gridFilters.addFilter(filterGeneric); } loader.load(); - + } if (tableViewConfig != null) { @@ -852,7 +839,7 @@ public class TabularDataXGridPanel extends ContentPanel { grid.getView().setViewConfig( new TabularDataGridViewConfig(tableViewConfig, tableDefinition)); - + } container.forceLayout(); diff --git a/src/main/java/org/gcube/portlets/user/tdwx/server/SessionUtil.java b/src/main/java/org/gcube/portlets/user/tdwx/server/SessionUtil.java new file mode 100644 index 0000000..8fd2ee0 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/server/SessionUtil.java @@ -0,0 +1,251 @@ +/** + * + */ +package org.gcube.portlets.user.tdwx.server; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.portal.PortalContext; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceX; +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXException; +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXFactory; +import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXFactoryRegistry; +import org.gcube.portlets.user.tdwx.server.session.TDSession; +import org.gcube.portlets.user.tdwx.server.session.TDSessionList; +import org.gcube.portlets.user.tdwx.server.util.ServiceCredentials; +import org.gcube.portlets.user.tdwx.shared.Constants; +import org.gcube.portlets.user.tdwx.shared.model.TableId; +import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; +import org.gcube.vomanagement.usermanagement.model.GCubeGroup; +import org.gcube.vomanagement.usermanagement.model.GCubeUser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author "Giancarlo Panichi" g.panichi@isti.cnr.it + * + */ +public class SessionUtil { + + private static final Logger logger = LoggerFactory + .getLogger(SessionUtil.class); + + public static final String TDWX_SESSIONS_ATTRIBUTE_NAME = "TDWX.SESSIONS"; + + /** + * + * @param httpServletRequest + * @return + * @throws TDGWTServiceException + */ + public static ServiceCredentials getServiceCredentials( + HttpServletRequest httpServletRequest) throws Exception { + return getServiceCredentials(httpServletRequest, null); + } + + /** + * + * @param httpServletRequest + * @param scopeGroupId + * @return + * @throws TDGWTServiceException + */ + public static ServiceCredentials getServiceCredentials( + HttpServletRequest httpServletRequest, String scopeGroupId) + throws Exception { + + ServiceCredentials sCredentials = null; + String userName = null; + String scope = null; + String token = null; + String groupId = null; + String groupName = null; + + if (Constants.DEBUG_MODE) { + logger.info("No credential found in session, use test user!"); + + userName = Constants.DEFAULT_USER; + scope = Constants.DEFAULT_SCOPE; + token = Constants.DEFAULT_TOKEN; + + logger.info("Set SecurityToken: " + token); + SecurityTokenProvider.instance.set(token); + logger.info("Set ScopeProvider: " + scope); + ScopeProvider.instance.set(scope); + + sCredentials = new ServiceCredentials(userName, scope, token); + + } else { + logger.info("Retrieving credential in session!"); + PortalContext pContext = PortalContext.getConfiguration(); + boolean hasScopeGroupId = false; + + if (scopeGroupId != null && !scopeGroupId.isEmpty()) { + hasScopeGroupId = true; + + } else { + hasScopeGroupId = false; + } + + if (hasScopeGroupId) { + scope = pContext.getCurrentScope(scopeGroupId); + } else { + scope = pContext.getCurrentScope(httpServletRequest); + } + + if (scope == null || scope.isEmpty()) { + String error = "Error retrieving scope: " + scope; + logger.error(error); + throw new Exception(error); + } + + GCubeUser gCubeUser = pContext.getCurrentUser(httpServletRequest); + + if (gCubeUser == null) { + String error = "Error retrieving gCubeUser in scope " + scope + + ": " + gCubeUser; + logger.error(error); + throw new Exception(error); + } + + userName = gCubeUser.getUsername(); + + if (userName == null || userName.isEmpty()) { + String error = "Error retrieving username in scope " + scope + + ": " + userName; + logger.error(error); + throw new Exception(error); + } + + token = pContext.getCurrentUserToken(scope, userName); + + if (token == null || token.isEmpty()) { + String error = "Error retrieving token for " + userName + + " in " + scope + ": " + token; + logger.error(error); + throw new Exception(error); + } + + String name = gCubeUser.getFirstName(); + String lastName = gCubeUser.getLastName(); + String fullName = gCubeUser.getFullname(); + + String userAvatarURL = gCubeUser.getUserAvatarURL(); + + String email = gCubeUser.getEmail(); + + if (hasScopeGroupId) { + logger.info("Set SecurityToken: " + token); + SecurityTokenProvider.instance.set(token); + logger.info("Set ScopeProvider: " + scope); + ScopeProvider.instance.set(scope); + + groupId = scopeGroupId; + + long gId; + + try { + gId = Long.parseLong(scopeGroupId); + } catch (Throwable e) { + String error = "Error retrieving groupId: " + scopeGroupId; + logger.error(error, e); + throw new Exception(error); + } + + GCubeGroup group; + try { + group = new LiferayGroupManager().getGroup(gId); + } catch (Throwable e) { + String error = "Error retrieving group: " + groupName; + logger.error(error); + throw new Exception(error); + } + + groupName = group.getGroupName(); + + } else { + + groupId = String.valueOf(pContext + .getCurrentGroupId(httpServletRequest)); + + groupName = pContext.getCurrentGroupName(httpServletRequest); + + } + + sCredentials = new ServiceCredentials(userName, fullName, name, + lastName, email, scope, groupId, groupName, userAvatarURL, + token); + } + + logger.info("ServiceCredentials: " + sCredentials); + + return sCredentials; + } + + public static DataSourceX getDataSource(HttpServletRequest httpRequest, + ServiceCredentials serviceCredentials, int tdSessionId) { + TDSession tdSession = getSession(httpRequest, serviceCredentials, + tdSessionId); + return tdSession.getDataSource(); + } + + public static void setDataSource(HttpServletRequest httpRequest, + ServiceCredentials serviceCredentials, int tdSessionId, + DataSourceX datasource) { + TDSession tdSession = getSession(httpRequest, serviceCredentials, + tdSessionId); + tdSession.setDataSource(datasource); + logger.trace("datasource " + datasource + " set in session " + + tdSessionId); + } + + protected static TDSession getSession(HttpServletRequest httpRequest, + ServiceCredentials serviceCredentials, int tdSessionId) { + // workaround to TDSession object loaded from different class loader + HttpSession httpSession = httpRequest.getSession(); + Object tsSession = httpSession + .getAttribute(TDWX_SESSIONS_ATTRIBUTE_NAME); + TDSessionList tdSessions = (tsSession instanceof TDSessionList) ? ((TDSessionList) tsSession) + : null; + if (tdSessions == null) { + tdSessions = new TDSessionList(); + httpSession.setAttribute(TDWX_SESSIONS_ATTRIBUTE_NAME, tdSessions); + } + if (tdSessions.get(tdSessionId) == null) { + tdSessions.set(tdSessionId, new TDSession(tdSessionId)); + logger.trace("created new td sessions " + tdSessionId); + } + return tdSessions.get(tdSessionId); + } + + public static DataSourceX openDataSource(HttpServletRequest httpRequest, + ServiceCredentials serviceCredentials, TableId tableId) + throws DataSourceXException { + DataSourceXFactoryRegistry dataSourceFactoryRegistry = DataSourceXFactoryRegistry + .getInstance(); + DataSourceXFactory factory = dataSourceFactoryRegistry.get(tableId + .getDataSourceFactoryId()); + if (factory == null) + throw new DataSourceXException("DataSourceFactory with id " + + tableId.getDataSourceFactoryId() + " don't exists"); + return factory.openDataSource(serviceCredentials, tableId); + } + + public static void closeDataSource(HttpServletRequest httpRequest,ServiceCredentials serviceCredentials, int tdSessionId) + throws DataSourceXException { + DataSourceX currentDataSource = getDataSource(httpRequest, serviceCredentials, tdSessionId); + if (currentDataSource != null) { + DataSourceXFactoryRegistry dataSourceFactoryRegistry = DataSourceXFactoryRegistry + .getInstance(); + DataSourceXFactory factory = dataSourceFactoryRegistry + .get(currentDataSource.getDataSourceFactoryId()); + factory.closeDataSource(serviceCredentials, currentDataSource); + } + } + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/server/TabularDataXServiceImpl.java b/src/main/java/org/gcube/portlets/user/tdwx/server/TabularDataXServiceImpl.java index fb5ace7..7857664 100644 --- a/src/main/java/org/gcube/portlets/user/tdwx/server/TabularDataXServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/tdwx/server/TabularDataXServiceImpl.java @@ -3,57 +3,62 @@ */ package org.gcube.portlets.user.tdwx.server; -import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpServletRequest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.gcube.portlets.user.tdwx.client.rpc.TabularDataXService; import org.gcube.portlets.user.tdwx.client.rpc.TabularDataXServiceException; import org.gcube.portlets.user.tdwx.server.datasource.DataSourceX; import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXException; -import org.gcube.portlets.user.tdwx.server.util.SessionUtil; +import org.gcube.portlets.user.tdwx.server.util.ServiceCredentials; import org.gcube.portlets.user.tdwx.shared.ColumnsReorderingConfig; import org.gcube.portlets.user.tdwx.shared.model.TableDefinition; import org.gcube.portlets.user.tdwx.shared.model.TableId; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.google.gwt.user.server.rpc.RemoteServiceServlet; /** * - * @author "Giancarlo Panichi" - * g.panichi@isti.cnr.it + * @author "Giancarlo Panichi" g.panichi@isti.cnr.it * */ -public class TabularDataXServiceImpl extends RemoteServiceServlet implements TabularDataXService { +public class TabularDataXServiceImpl extends RemoteServiceServlet implements + TabularDataXService { private static final long serialVersionUID = 193560783723693864L; - protected static Logger logger = LoggerFactory.getLogger(TabularDataXServiceImpl.class); - - protected DataSourceX getDataSource(int tdSessionId) - { - HttpSession httpSession = this.getThreadLocalRequest().getSession(); - return SessionUtil.getDataSource(httpSession, tdSessionId); - } + private static Logger logger = LoggerFactory + .getLogger(TabularDataXServiceImpl.class); /** * {@inheritDoc} */ @Override - public TableDefinition openTable(int tdSessionId, TableId tableId) throws TabularDataXServiceException { - logger.debug("openTable tdSessionId: "+tdSessionId+" tableId: "+tableId); + public TableDefinition openTable(int tdSessionId, TableId tableId) + throws TabularDataXServiceException { + logger.debug("openTable tdSessionId: " + tdSessionId + " tableId: " + + tableId); - try{ - closeCurrentTable(tdSessionId, true); - - HttpSession httpSession = this.getThreadLocalRequest().getSession(); - DataSourceX dataSource = SessionUtil.openDataSource(httpSession, tableId); - SessionUtil.setDataSource(httpSession, tdSessionId, dataSource); + try { + HttpServletRequest httpRequest = this.getThreadLocalRequest(); + ServiceCredentials serviceCredentials = SessionUtil + .getServiceCredentials(httpRequest); + closeCurrentTable(httpRequest, serviceCredentials, tdSessionId, + true); + DataSourceX dataSource = SessionUtil.openDataSource(httpRequest, + serviceCredentials, tableId); + SessionUtil.setDataSource(httpRequest, serviceCredentials, + tdSessionId, dataSource); logger.debug("Open table get table definition"); return dataSource.getTableDefinition(); - }catch (Exception e) { - logger.error("An error occurred opening the specified table "+tableId+" in session "+tdSessionId, e); - throw new TabularDataXServiceException("An error occurred opening the specified table: "+e.getMessage()); + } catch (Exception e) { + logger.error("An error occurred opening the specified table " + + tableId + " in session " + tdSessionId, e); + throw new TabularDataXServiceException( + "An error occurred opening the specified table: " + + e.getMessage()); } } @@ -61,54 +66,75 @@ public class TabularDataXServiceImpl extends RemoteServiceServlet implements Tab * {@inheritDoc} */ @Override - public TableDefinition getCurrentTableDefinition(int tdSessionId) throws TabularDataXServiceException { - logger.debug("getCurrentTableDefinition tdSessionId: "+tdSessionId); + public TableDefinition getCurrentTableDefinition(int tdSessionId) + throws TabularDataXServiceException { + logger.debug("getCurrentTableDefinition tdSessionId: " + tdSessionId); - try{ - DataSourceX dataSource = getDataSource(tdSessionId); + try { + HttpServletRequest httpRequest = this.getThreadLocalRequest(); + ServiceCredentials serviceCredentials = SessionUtil + .getServiceCredentials(httpRequest); + DataSourceX dataSource = SessionUtil.getDataSource(httpRequest, + serviceCredentials, tdSessionId); logger.debug("Service get current table definition"); return dataSource.getTableDefinition(); - }catch (Exception e) { + } catch (Exception e) { logger.error("An error occurred retrieving the table definition", e); - throw new TabularDataXServiceException("An error occurred retrieving the table definition: "+e.getMessage()); + throw new TabularDataXServiceException( + "An error occurred retrieving the table definition: " + + e.getMessage()); } } - - /** * {@inheritDoc} */ @Override - public TableDefinition setCurrentTableColumnsReordering(int tdSessionId, ColumnsReorderingConfig columnReorderingConfig) throws TabularDataXServiceException { - logger.debug("setCurrentTableColumnsReordering tdSessionId: "+tdSessionId); + public TableDefinition setCurrentTableColumnsReordering(int tdSessionId, + ColumnsReorderingConfig columnReorderingConfig) + throws TabularDataXServiceException { + logger.debug("setCurrentTableColumnsReordering tdSessionId: " + + tdSessionId); - try{ - DataSourceX dataSource = getDataSource(tdSessionId); + try { + HttpServletRequest httpRequest = this.getThreadLocalRequest(); + ServiceCredentials serviceCredentials = SessionUtil + .getServiceCredentials(httpRequest); + DataSourceX dataSource = SessionUtil.getDataSource(httpRequest, + serviceCredentials, tdSessionId); logger.debug("Service get current table definition"); - TableDefinition tableDefinition=dataSource.setColumnReordering(columnReorderingConfig); + TableDefinition tableDefinition = dataSource + .setColumnReordering(columnReorderingConfig); return tableDefinition; - }catch (Exception e) { + } catch (Exception e) { logger.error("An error occurred setting columns reordering", e); - throw new TabularDataXServiceException("An error occurred setting columns reordering: "+e.getMessage()); + throw new TabularDataXServiceException( + "An error occurred setting columns reordering: " + + e.getMessage()); } } - + /** * {@inheritDoc} */ @Override - public TableDefinition getTableDefinition(TableId id) throws TabularDataXServiceException { - logger.debug("getTableDefinition TableId: "+id); + public TableDefinition getTableDefinition(TableId id) + throws TabularDataXServiceException { + logger.debug("getTableDefinition TableId: " + id); - try{ - HttpSession httpSession = this.getThreadLocalRequest().getSession(); - DataSourceX dataSource = SessionUtil.openDataSource(httpSession,id); + try { + HttpServletRequest httpRequest = this.getThreadLocalRequest(); + ServiceCredentials serviceCredentials = SessionUtil + .getServiceCredentials(httpRequest); + DataSourceX dataSource = SessionUtil.openDataSource(httpRequest, + serviceCredentials, id); logger.debug("Service get table definition"); return dataSource.getTableDefinition(); - }catch (Exception e) { + } catch (Exception e) { logger.error("An error occurred getting the table definition", e); - throw new TabularDataXServiceException("An error occurred getting the table definition: "+e.getMessage()); + throw new TabularDataXServiceException( + "An error occurred getting the table definition: " + + e.getMessage()); } } @@ -118,21 +144,28 @@ public class TabularDataXServiceImpl extends RemoteServiceServlet implements Tab @Override public void closeTable(int tdSessionId) throws TabularDataXServiceException { try { - closeCurrentTable(tdSessionId, false); - } catch (DataSourceXException e) { + HttpServletRequest httpRequest = this.getThreadLocalRequest(); + ServiceCredentials serviceCredentials = SessionUtil + .getServiceCredentials(httpRequest); + closeCurrentTable(httpRequest, serviceCredentials, tdSessionId, + false); + } catch (Exception e) { logger.error("An error occurred closing the current table", e); - throw new TabularDataXServiceException("An error occurred closing the current table: "+e.getMessage()); + throw new TabularDataXServiceException( + "An error occurred closing the current table: " + + e.getMessage()); } } - - - protected void closeCurrentTable(int tdSessionId, boolean silent) throws DataSourceXException { + + protected void closeCurrentTable(HttpServletRequest httpRequest, + ServiceCredentials serviceCredentials, int tdSessionId, + boolean silent) throws DataSourceXException { try { - HttpSession httpSession = this.getThreadLocalRequest().getSession(); - SessionUtil.closeDataSource(httpSession, tdSessionId); - } catch (DataSourceXException e) - { - if (!silent) throw e; + SessionUtil.closeDataSource(httpRequest, serviceCredentials, + tdSessionId); + } catch (DataSourceXException e) { + if (!silent) + throw e; } } diff --git a/src/main/java/org/gcube/portlets/user/tdwx/server/TabularDataXServlet.java b/src/main/java/org/gcube/portlets/user/tdwx/server/TabularDataXServlet.java index f21c6dc..3e0bfcb 100644 --- a/src/main/java/org/gcube/portlets/user/tdwx/server/TabularDataXServlet.java +++ b/src/main/java/org/gcube/portlets/user/tdwx/server/TabularDataXServlet.java @@ -13,7 +13,8 @@ import javax.servlet.http.HttpServletResponse; import org.gcube.portlets.user.tdwx.server.datasource.DataSourceX; import org.gcube.portlets.user.tdwx.server.datasource.Direction; -import org.gcube.portlets.user.tdwx.server.util.SessionUtil; +import org.gcube.portlets.user.tdwx.server.util.ServiceCredentials; +import org.gcube.portlets.user.tdwx.shared.Constants; import org.gcube.portlets.user.tdwx.shared.FilterInformation; import org.gcube.portlets.user.tdwx.shared.ServletParameters; import org.gcube.portlets.user.tdwx.shared.SortInformation; @@ -52,6 +53,32 @@ public class TabularDataXServlet extends HttpServlet { logger.debug("TabularDataServlet handleRequest"); try { + + + ServiceCredentials serviceCredentials; + + String scopeGroupId = request.getHeader(Constants.CURR_GROUP_ID); + if (scopeGroupId == null || scopeGroupId.isEmpty()) { + scopeGroupId = request.getParameter(Constants.CURR_GROUP_ID); + if (scopeGroupId == null || scopeGroupId.isEmpty()) { + logger.error("CURR_GROUP_ID is null, it is a mandatory parameter in custom servlet: " + + scopeGroupId); + throw new ServletException( + "CURR_GROUP_ID is null, it is a mandatory parameter in custom servlet: " + + scopeGroupId); + } + } + + try { + serviceCredentials = SessionUtil.getServiceCredentials(request, + scopeGroupId); + + } catch (Exception e) { + logger.error( + "Error retrieving credentials:" + e.getLocalizedMessage(), + e); + throw new ServletException(e.getLocalizedMessage()); + } // TD SESSION ID String tdSessionIdParameter = request @@ -153,7 +180,7 @@ public class TabularDataXServlet extends HttpServlet { DataSourceX dataSource = SessionUtil.getDataSource( - request.getSession(), tdSessionId); + request,serviceCredentials, tdSessionId); String json = dataSource.getDataAsJSon(start, limit, sortColumn, direction, filtersList, staticFiltersList); diff --git a/src/main/java/org/gcube/portlets/user/tdwx/server/datasource/DataSourceXFactory.java b/src/main/java/org/gcube/portlets/user/tdwx/server/datasource/DataSourceXFactory.java index 4df38aa..e35b0bb 100644 --- a/src/main/java/org/gcube/portlets/user/tdwx/server/datasource/DataSourceXFactory.java +++ b/src/main/java/org/gcube/portlets/user/tdwx/server/datasource/DataSourceXFactory.java @@ -1,6 +1,6 @@ package org.gcube.portlets.user.tdwx.server.datasource; -import org.gcube.application.framework.core.session.ASLSession; +import org.gcube.portlets.user.tdwx.server.util.ServiceCredentials; import org.gcube.portlets.user.tdwx.shared.model.TableId; @@ -24,18 +24,21 @@ public interface DataSourceXFactory { /** * Open the specified datasource {@link DataSourceX}. + * + * @param serviceCredentials credentials * @param id the datasource id. * @return the retrieved datasource. * @throws DataSourceXException if an error occurs retrieving the datasource. */ - public DataSourceX openDataSource(ASLSession session, TableId id) throws DataSourceXException; + public DataSourceX openDataSource(ServiceCredentials serviceCredentials, TableId id) throws DataSourceXException; /** * Close the specified DataSource releasing allocated resources. - * @param session + * + * @param serviceCredentials * @param dataSource * @throws DataSourceXException */ - public void closeDataSource(ASLSession session, DataSourceX dataSource) throws DataSourceXException; + public void closeDataSource(ServiceCredentials serviceCredentials, DataSourceX dataSource) throws DataSourceXException; } diff --git a/src/main/java/org/gcube/portlets/user/tdwx/server/session/TDSession.java b/src/main/java/org/gcube/portlets/user/tdwx/server/session/TDSession.java index 56faaab..4f0e436 100644 --- a/src/main/java/org/gcube/portlets/user/tdwx/server/session/TDSession.java +++ b/src/main/java/org/gcube/portlets/user/tdwx/server/session/TDSession.java @@ -13,8 +13,8 @@ import org.gcube.portlets.user.tdwx.server.datasource.DataSourceX; */ public class TDSession { - protected int id; - protected DataSourceX dataSource; + private int id; + private DataSourceX dataSource; /** * @param id diff --git a/src/main/java/org/gcube/portlets/user/tdwx/server/util/ServiceCredentials.java b/src/main/java/org/gcube/portlets/user/tdwx/server/util/ServiceCredentials.java new file mode 100644 index 0000000..97e48d3 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/tdwx/server/util/ServiceCredentials.java @@ -0,0 +1,141 @@ +package org.gcube.portlets.user.tdwx.server.util; + +import java.io.Serializable; + +/** + * + * @author Giancarlo Panichi email: g.panichi@isti.cnr.it + * + */ +public class ServiceCredentials implements Serializable { + + private static final long serialVersionUID = 3560918948310315680L; + private String userName; + private String fullName; + private String name; + private String lastName; + private String email; + private String scope; + private String groupId; + private String groupName; + private String userAvatarURL; + private String token; + + public ServiceCredentials() { + super(); + } + + public ServiceCredentials(String userName, String scope, String token) { + super(); + this.userName = userName; + this.scope = scope; + this.token = token; + } + + public ServiceCredentials(String userName, String fullName, String name, + String lastName, String email, String scope, String groupId, + String groupName, String userAvatarURL, String token) { + super(); + this.userName = userName; + this.fullName = fullName; + this.name = name; + this.lastName = lastName; + this.email = email; + this.scope = scope; + this.groupId = groupId; + this.groupName = groupName; + this.userAvatarURL = userAvatarURL; + this.token = token; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getFullName() { + return fullName; + } + + public void setFullName(String fullName) { + this.fullName = fullName; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getScope() { + return scope; + } + + public void setScope(String scope) { + this.scope = scope; + } + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getGroupName() { + return groupName; + } + + public void setGroupName(String groupName) { + this.groupName = groupName; + } + + public String getUserAvatarURL() { + return userAvatarURL; + } + + public void setUserAvatarURL(String userAvatarURL) { + this.userAvatarURL = userAvatarURL; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + @Override + public String toString() { + return "ServiceCredentials [userName=" + userName + ", fullName=" + + fullName + ", name=" + name + ", lastName=" + lastName + + ", email=" + email + ", scope=" + scope + ", groupId=" + + groupId + ", groupName=" + groupName + ", userAvatarURL=" + + userAvatarURL + ", token=" + token + "]"; + } + +} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/server/util/SessionUtil.java b/src/main/java/org/gcube/portlets/user/tdwx/server/util/SessionUtil.java deleted file mode 100644 index 59a7331..0000000 --- a/src/main/java/org/gcube/portlets/user/tdwx/server/util/SessionUtil.java +++ /dev/null @@ -1,110 +0,0 @@ -/** - * - */ -package org.gcube.portlets.user.tdwx.server.util; - -import javax.servlet.http.HttpSession; - -import org.gcube.application.framework.core.session.ASLSession; -import org.gcube.application.framework.core.session.SessionManager; -import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper; -import org.gcube.portlets.user.tdwx.server.datasource.DataSourceX; -import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXException; -import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXFactory; -import org.gcube.portlets.user.tdwx.server.datasource.DataSourceXFactoryRegistry; -import org.gcube.portlets.user.tdwx.server.session.TDSession; -import org.gcube.portlets.user.tdwx.server.session.TDSessionList; -import org.gcube.portlets.user.tdwx.shared.Constants; -import org.gcube.portlets.user.tdwx.shared.model.TableId; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - * @author "Giancarlo Panichi" - * g.panichi@isti.cnr.it - * - */ -public class SessionUtil { - - - private static final Logger log = LoggerFactory.getLogger(SessionUtil.class); - - public static final String TDWX_SESSIONS_ATTRIBUTE_NAME = "TDWX.SESSIONS"; - - protected static ASLSession getSession(HttpSession httpSession) - { - String username = (String) httpSession - .getAttribute(ScopeHelper.USERNAME_ATTRIBUTE); - ASLSession session; - if (username == null) { - log.warn("no user found in session, using test one"); - username = Constants.DEFAULT_USER; - String scope = Constants.DEFAULT_SCOPE; - - httpSession.setAttribute(ScopeHelper.USERNAME_ATTRIBUTE, username); - session = SessionManager.getInstance().getASLSession( - httpSession.getId(), username); - session.setScope(scope); - - } else { - session= SessionManager.getInstance().getASLSession( - httpSession.getId(), username); - - } - - return session; - } - - public static DataSourceX getDataSource(HttpSession httpSession, int tdSessionId) - { - ASLSession session = getSession(httpSession); - TDSession tdSession = getSession(session, tdSessionId); - return tdSession.getDataSource(); - } - - public static void setDataSource(HttpSession httpSession, int tdSessionId, DataSourceX datasource) - { - ASLSession session = getSession(httpSession); - TDSession tdSession = getSession(session, tdSessionId); - tdSession.setDataSource(datasource); - log.trace("datasource "+datasource+" set in session "+tdSessionId); - } - - protected static TDSession getSession(ASLSession session, int tdSessionId) - { - //workaround to TDSession object loaded from different class loader - Object tsSession = session.getAttribute(TDWX_SESSIONS_ATTRIBUTE_NAME); - TDSessionList sessions = (tsSession instanceof TDSessionList)?((TDSessionList) tsSession):null; - if (sessions == null) { - sessions = new TDSessionList(); - session.setAttribute(TDWX_SESSIONS_ATTRIBUTE_NAME, sessions); - } - if (sessions.get(tdSessionId)==null) { - sessions.set(tdSessionId, new TDSession(tdSessionId)); - log.trace("created new sessions "+tdSessionId); - } - return sessions.get(tdSessionId); - } - - public static DataSourceX openDataSource(HttpSession httpSession, TableId tableId) throws DataSourceXException - { - DataSourceXFactoryRegistry dataSourceFactoryRegistry = DataSourceXFactoryRegistry.getInstance(); - DataSourceXFactory factory = dataSourceFactoryRegistry.get(tableId.getDataSourceFactoryId()); - if (factory==null) throw new DataSourceXException("DataSourceFactory with id "+tableId.getDataSourceFactoryId()+" don't exists"); - ASLSession session = getSession(httpSession); - return factory.openDataSource(session, tableId); - } - - public static void closeDataSource(HttpSession httpSession, int tdSessionId) throws DataSourceXException - { - DataSourceX currentDataSource = getDataSource(httpSession, tdSessionId); - if (currentDataSource != null) { - DataSourceXFactoryRegistry dataSourceFactoryRegistry = DataSourceXFactoryRegistry.getInstance(); - DataSourceXFactory factory = dataSourceFactoryRegistry.get(currentDataSource.getDataSourceFactoryId()); - ASLSession session = getSession(httpSession); - factory.closeDataSource(session, currentDataSource); - } - } - -} diff --git a/src/main/java/org/gcube/portlets/user/tdwx/shared/Constants.java b/src/main/java/org/gcube/portlets/user/tdwx/shared/Constants.java index 56fc71e..2a0233e 100644 --- a/src/main/java/org/gcube/portlets/user/tdwx/shared/Constants.java +++ b/src/main/java/org/gcube/portlets/user/tdwx/shared/Constants.java @@ -3,20 +3,28 @@ */ package org.gcube.portlets.user.tdwx.shared; - - /** * - * @author "Giancarlo Panichi" - * g.panichi@isti.cnr.it + * @author "Giancarlo Panichi" g.panichi@isti.cnr.it * */ public class Constants { - public final static String VERSION = "1.0.0"; - public final static String DEFAULT_USER = "giancarlo.panichi"; - public final static String DEFAULT_SCOPE = "/gcube/devsec"; - public final static String REMOTE_SERVICE_RELATIVE_PATH= "tdwx"; - + public final static String VERSION = "1.0.0"; + + public static final boolean DEBUG_MODE = false; + public static final boolean TEST_ENABLE = false; + + public static final String CURR_GROUP_ID = "CURR_GROUP_ID"; + // public static final String CURR_USER_ID = "CURR_USER_ID"; + + public static final String DEFAULT_USER = "giancarlo.panichi"; + public static final String DEFAULT_SCOPE = "/gcube/devNext/NextNext"; + public static final String DEFAULT_TOKEN = "ae1208f0-210d-47c9-9b24-d3f2dfcce05f-98187548"; + + public final static String REMOTE_SERVICE_RELATIVE_PATH = "tdwx"; + + public static final String TABULAR_DATA_X_SERVLET = "tdwxdata"; }