From 55d31dd68e64a5502fb7a81e1d519c197ce65b2b Mon Sep 17 00:00:00 2001
From: jakubs <jakubs>
Date: Wed, 24 Jun 2015 11:03:30 +0000
Subject: [PATCH] SSDM-1813 - create a new service. V3 api for data store
 service. work in progress

SVN: 34272
---
 .../dss/api/v3/DataStoreServerApi.java        | 121 ++++++++++++++++++
 .../dss/api/v3/DataStoreServerApiLogger.java  |  37 ++++++
 .../dss/api/v3/DataStoreServerApiServer.java  |  59 +++++++++
 .../dss/generic/server/DataStoreServer.java   |  25 +++-
 .../OpenBISAuthenticationInterceptor.java     |   2 +-
 .../dss/generic/shared/ServiceProvider.java   |  24 ++--
 .../source/java/dssApplicationContext.xml     |  14 +-
 7 files changed, 262 insertions(+), 20 deletions(-)
 create mode 100644 datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApi.java
 create mode 100644 datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApiLogger.java
 create mode 100644 datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApiServer.java

diff --git a/datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApi.java b/datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApi.java
new file mode 100644
index 00000000000..5ce0bfcf2b8
--- /dev/null
+++ b/datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApi.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2015 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ch.ethz.sis.openbis.generic.dss.api.v3;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+
+import ch.ethz.sis.openbis.generic.dss.api.v3.dto.entity.datasetfile.DataSetFile;
+import ch.ethz.sis.openbis.generic.dss.api.v3.dto.search.FileSearchCriterion;
+import ch.systemsx.cisd.common.filesystem.IFreeSpaceProvider;
+import ch.systemsx.cisd.common.filesystem.SimpleFreeSpaceProvider;
+import ch.systemsx.cisd.common.logging.LogCategory;
+import ch.systemsx.cisd.common.logging.LogFactory;
+import ch.systemsx.cisd.etlserver.api.v1.PutDataSetService;
+import ch.systemsx.cisd.openbis.common.spring.IInvocationLoggerContext;
+import ch.systemsx.cisd.openbis.dss.generic.server.AbstractDssServiceRpc;
+import ch.systemsx.cisd.openbis.dss.generic.server.IStreamRepository;
+import ch.systemsx.cisd.openbis.dss.generic.server.plugins.tasks.IPluginTaskInfoProvider;
+import ch.systemsx.cisd.openbis.dss.generic.shared.IEncapsulatedOpenBISService;
+import ch.systemsx.cisd.openbis.dss.generic.shared.IHierarchicalContentProvider;
+import ch.systemsx.cisd.openbis.dss.generic.shared.IShareIdManager;
+import ch.systemsx.cisd.openbis.generic.server.authorization.annotation.RolesAllowed;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.RoleWithHierarchy;
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.IQueryApiServer;
+
+/**
+ * @author Jakub Straszewski
+ */
+@Component(IDataStoreServerApi.INTERNAL_SERVICE_NAME)
+public class DataStoreServerApi extends AbstractDssServiceRpc<IDataStoreServerApi>
+        implements IDataStoreServerApi
+{
+    /**
+     * Logger with {@link LogCategory#OPERATION} with name of the concrete class, needs to be static for our purpose.
+     */
+    @SuppressWarnings("hiding")
+    protected static Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION,
+            DataStoreServerApi.class);
+
+    public String DSS_SERVICE_NAME = "DSS Service";
+
+    /**
+     * The designated constructor.
+     */
+    @Autowired
+    public DataStoreServerApi(IEncapsulatedOpenBISService openBISService,
+            IQueryApiServer apiServer, IPluginTaskInfoProvider infoProvider)
+    {
+        // NOTE: IShareIdManager and IHierarchicalContentProvider will be lazily created by spring
+        this(openBISService, apiServer, infoProvider, new SimpleFreeSpaceProvider(), null, null);
+    }
+
+    DataStoreServerApi(IEncapsulatedOpenBISService openBISService, IQueryApiServer apiServer,
+            IPluginTaskInfoProvider infoProvider, IFreeSpaceProvider freeSpaceProvider,
+            IShareIdManager shareIdManager, IHierarchicalContentProvider contentProvider)
+    {
+        this(openBISService, apiServer, infoProvider, null, freeSpaceProvider, shareIdManager,
+                contentProvider, new PutDataSetService(openBISService, operationLog));
+    }
+
+    /**
+     * A constructor for testing.
+     */
+    public DataStoreServerApi(IEncapsulatedOpenBISService openBISService,
+            IQueryApiServer apiServer, IPluginTaskInfoProvider infoProvider,
+            IStreamRepository streamRepository, IFreeSpaceProvider freeSpaceProvider,
+            IShareIdManager shareIdManager, IHierarchicalContentProvider contentProvider,
+            PutDataSetService service)
+    {
+        super(openBISService, streamRepository, shareIdManager, contentProvider);
+        // queryApiServer = apiServer;
+        // this.freeSpaceProvider = freeSpaceProvider;
+        // putService = service;
+        // this.sessionWorkspaceRootDirectory = infoProvider.getSessionWorkspaceRootDir();
+        operationLog.info("[rpc] Started DSS API V1 service.");
+    }
+
+    @Transactional(readOnly = true)
+    @RolesAllowed({ RoleWithHierarchy.SPACE_OBSERVER, RoleWithHierarchy.SPACE_ETL_SERVER })
+    @Override
+    public List<DataSetFile> searchFiles(String sessionToken, FileSearchCriterion searchCriterion)
+    {
+        return null;
+    }
+
+    @Override
+    public int getMajorVersion()
+    {
+        return 3;
+    }
+
+    @Override
+    public int getMinorVersion()
+    {
+        return 0;
+    }
+
+    @Override
+    public IDataStoreServerApi createLogger(IInvocationLoggerContext context)
+    {
+        return new DataStoreServerApiLogger(context);
+    }
+
+}
diff --git a/datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApiLogger.java b/datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApiLogger.java
new file mode 100644
index 00000000000..d362bee2339
--- /dev/null
+++ b/datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApiLogger.java
@@ -0,0 +1,37 @@
+package ch.ethz.sis.openbis.generic.dss.api.v3;
+
+import java.util.List;
+
+import ch.ethz.sis.openbis.generic.dss.api.v3.dto.entity.datasetfile.DataSetFile;
+import ch.ethz.sis.openbis.generic.dss.api.v3.dto.search.FileSearchCriterion;
+import ch.systemsx.cisd.openbis.common.spring.IInvocationLoggerContext;
+import ch.systemsx.cisd.openbis.generic.shared.AbstractServerLogger;
+
+public class DataStoreServerApiLogger extends AbstractServerLogger implements
+        IDataStoreServerApi
+{
+    DataStoreServerApiLogger(IInvocationLoggerContext context)
+    {
+        super(null, context);
+    }
+
+    @Override
+    public int getMajorVersion()
+    {
+        return 0;
+    }
+
+    @Override
+    public int getMinorVersion()
+    {
+        return 0;
+    }
+
+    @Override
+    public List<DataSetFile> searchFiles(String sessionToken, FileSearchCriterion searchCriterion)
+    {
+        System.out.println("search files");
+        return null; // TODO kuba implement this method
+    }
+
+}
\ No newline at end of file
diff --git a/datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApiServer.java b/datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApiServer.java
new file mode 100644
index 00000000000..cf622df51b9
--- /dev/null
+++ b/datastore_server/source/java/ch/ethz/sis/openbis/generic/dss/api/v3/DataStoreServerApiServer.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2015 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.ethz.sis.openbis.generic.dss.api.v3;
+
+import java.io.IOException;
+
+import javax.annotation.Resource;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import ch.systemsx.cisd.openbis.common.api.server.AbstractApiServiceExporter;
+
+/**
+ * @author Jakub Straszewski
+ */
+@Controller(value = DataStoreServerApiServer.INTERNAL_BEAN_NAME)
+public class DataStoreServerApiServer extends AbstractApiServiceExporter
+{
+    public static final String INTERNAL_BEAN_NAME = "v3-exporter";
+
+    @Resource(name = IDataStoreServerApi.INTERNAL_SERVICE_NAME)
+    private IDataStoreServerApi service;
+
+    @Override
+    public void afterPropertiesSet()
+    {
+        System.err.println("After properties set of datastore server server " + service);
+        establishService(IDataStoreServerApi.class, service, IDataStoreServerApi.SERVICE_NAME,
+                IDataStoreServerApi.SERVICE_URL);
+        super.afterPropertiesSet();
+    }
+
+    @RequestMapping(
+    { IDataStoreServerApi.SERVICE_URL, "/datastore_server" + IDataStoreServerApi.SERVICE_URL })
+    @Override
+    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
+            throws ServletException, IOException
+    {
+        super.handleRequest(request, response);
+    }
+}
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java
index 8843b8254e0..c5f4459272b 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServer.java
@@ -33,7 +33,6 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.log4j.Logger;
-import org.eclipse.jetty.server.Connector;
 import org.eclipse.jetty.server.HttpConfiguration;
 import org.eclipse.jetty.server.HttpConnectionFactory;
 import org.eclipse.jetty.server.SecureRequestCustomizer;
@@ -57,6 +56,7 @@ import org.springframework.web.context.WebApplicationContext;
 import com.googlecode.jsonrpc4j.spring.JsonServiceExporter;
 import com.marathon.util.spring.StreamSupportingHttpInvokerServiceExporter;
 
+import ch.ethz.sis.openbis.generic.dss.api.v3.IDataStoreServerApi;
 import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
 import ch.systemsx.cisd.common.api.IRpcServiceNameServer;
 import ch.systemsx.cisd.common.api.RpcServiceInterfaceVersionDTO;
@@ -318,6 +318,17 @@ public class DataStoreServer
         jsonV1ServiceExporter
                 .setApplicationContext((org.springframework.context.ApplicationContext) ServiceProvider
                         .getApplicationContext());
+
+        //
+        // export the V3 API
+        //
+        String rpcV3Path = DataStoreApiUrlUtilities.getUrlForRpcService(IDataStoreServerApi.SERVICE_URL);
+        HttpInvokerServiceExporter v3ServiceExporter = ServiceProvider.getDssServiceV3();
+        // TODO: 24.06.2015 Include the V3 service in name-server
+        // IDataStoreServerApi serviceV3 = (IDataStoreServerApi) v3ServiceExporter .getService();
+        context.addServlet(new ServletHolder(new HttpInvokerServlet(v3ServiceExporter, rpcV3Path)),
+                rpcV3Path);
+
         try
         {
             jsonV1ServiceExporter.afterPropertiesSet();
@@ -431,19 +442,19 @@ public class DataStoreServer
         HttpConfiguration httpConfig = new HttpConfiguration();
         httpConfig.setSecureScheme("https");
 
-    	if (configParams.isUseSSL())
+        if (configParams.isUseSSL())
         {
             final SslContextFactory sslContextFactory = new SslContextFactory();
             sslContextFactory.setKeyStorePath(configParams.getKeystorePath());
             sslContextFactory.setKeyStorePassword(configParams.getKeystorePassword());
             sslContextFactory.setKeyManagerPassword(configParams.getKeystoreKeyPassword());
-            
+
             HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
             httpsConfig.addCustomizer(new SecureRequestCustomizer());
-            
-            return new ServerConnector(thisServer, 
-            	       new SslConnectionFactory(sslContextFactory, "http/1.1"),
-            	       new HttpConnectionFactory(httpsConfig));
+
+            return new ServerConnector(thisServer,
+                    new SslConnectionFactory(sslContextFactory, "http/1.1"),
+                    new HttpConnectionFactory(httpsConfig));
         } else
         {
             operationLog.warn("creating connector to openBIS without SSL");
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/openbisauth/OpenBISAuthenticationInterceptor.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/openbisauth/OpenBISAuthenticationInterceptor.java
index 59f96b33184..974f6f64667 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/openbisauth/OpenBISAuthenticationInterceptor.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/openbisauth/OpenBISAuthenticationInterceptor.java
@@ -64,7 +64,7 @@ public class OpenBISAuthenticationInterceptor implements MethodInterceptor
 {
 
     private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION,
-            OpenBISAuthenticationAdvisor.class);
+            OpenBISAuthenticationInterceptor.class);
 
     private final SessionTokenManager sessionTokenManager;
 
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProvider.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProvider.java
index 9672f0d8203..e7122e9fc52 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProvider.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProvider.java
@@ -24,6 +24,7 @@ import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
 
 import com.marathon.util.spring.StreamSupportingHttpInvokerServiceExporter;
 
+import ch.ethz.sis.openbis.generic.dss.api.v3.DataStoreServerApiServer;
 import ch.ethz.sis.openbis.generic.shared.api.v3.IApplicationServerApi;
 import ch.systemsx.cisd.common.logging.LogCategory;
 import ch.systemsx.cisd.common.logging.LogFactory;
@@ -57,8 +58,8 @@ public class ServiceProvider
     private static boolean buildingApplicationContext;
 
     /**
-     * @deprecated This method should only be used from {@link ServiceProviderTestWrapper} to avoid
-     *             leaving the application context uncleaned after test execution.
+     * @deprecated This method should only be used from {@link ServiceProviderTestWrapper} to avoid leaving the application context uncleaned after
+     *             test execution.
      */
     @Deprecated
     public static void setBeanFactory(BeanFactory applicationContext)
@@ -69,8 +70,7 @@ public class ServiceProvider
     /**
      * Return the application context
      * 
-     * @param create <code>true</code> if the application context should be created when it does not
-     *            exist.
+     * @param create <code>true</code> if the application context should be created when it does not exist.
      */
     public static BeanFactory tryGetApplicationContext(boolean create)
     {
@@ -88,12 +88,12 @@ public class ServiceProvider
                     }
                     buildingApplicationContext = true;
                     applicationContext = new ClassPathXmlApplicationContext(new String[]
-                        { "dssApplicationContext.xml" }, true)
-                    {
+                    { "dssApplicationContext.xml" }, true)
                         {
-                            setDisplayName("Application Context from { dssApplicationContext.xml }");
-                        }
-                    };
+                            {
+                                setDisplayName("Application Context from { dssApplicationContext.xml }");
+                            }
+                        };
                     buildingApplicationContext = false;
                 }
             }
@@ -174,6 +174,12 @@ public class ServiceProvider
                 "data-store-rpc-service-generic"));
     }
 
+    public static HttpInvokerServiceExporter getDssServiceV3()
+    {
+        return ((HttpInvokerServiceExporter) getApplicationContext().getBean(
+                DataStoreServerApiServer.INTERNAL_BEAN_NAME));
+    }
+
     public static IDataSourceProvider getDataSourceProvider()
     {
         return ((IDataSourceProvider) getApplicationContext().getBean("data-source-provider"));
diff --git a/datastore_server/source/java/dssApplicationContext.xml b/datastore_server/source/java/dssApplicationContext.xml
index c96950ed90e..13e36eb2d56 100644
--- a/datastore_server/source/java/dssApplicationContext.xml
+++ b/datastore_server/source/java/dssApplicationContext.xml
@@ -2,15 +2,23 @@
 <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:tx="http://www.springframework.org/schema/tx"
-    xmlns:plugins="http://www.springframework.org/schema/context"
+    xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
 			http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 			http://www.springframework.org/schema/tx
 			http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
 			http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd">
-      
-    <plugins:annotation-config />
+    <context:annotation-config />
+
+    <context:component-scan base-package="ch.ethz.sis.openbis.generic.dss.api.v3" use-default-filters="false">
+        <context:include-filter type="annotation"
+            expression="org.springframework.stereotype.Controller" />
+    </context:component-scan>
+    
+    <context:component-scan base-package="ch.ethz.sis.openbis.generic.dss.api.v3" annotation-config="false">
+        <context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />
+    </context:component-scan>
     
     <!--
         // Replaces ${...} with corresponding values found in 'location' property. A value
-- 
GitLab