From f136131a92b16c9a80a66f08a81a881d5312ca93 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Tue, 7 Dec 2010 08:03:26 +0000
Subject: [PATCH] LMS-1904 sessionID added as URL parameter for each link

SVN: 19022
---
 .../AbstractDatasetDownloadServlet.java       |  2 -
 .../server/DatasetDownloadServlet.java        | 18 +++++----
 .../dss/generic/server/RenderingContext.java  | 10 ++++-
 .../server/SimpleHTMLDirectoryRenderer.java   |  6 ++-
 .../openbis/dss/generic/server/Utils.java     | 35 +++++++++++++++++
 .../server/DatasetDownloadServletTest.java    | 38 ++++++++++++++-----
 6 files changed, 88 insertions(+), 21 deletions(-)
 create mode 100644 datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/Utils.java

diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/AbstractDatasetDownloadServlet.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/AbstractDatasetDownloadServlet.java
index 198c558b332..3e3f03c0fb7 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/AbstractDatasetDownloadServlet.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/AbstractDatasetDownloadServlet.java
@@ -75,8 +75,6 @@ abstract public class AbstractDatasetDownloadServlet extends HttpServlet
 
     private static final String THUMBNAIL_MODE_DISPLAY = "thumbnail";
 
-    protected static final String SESSION_ID_PARAM = "sessionID";
-
     static final String DISPLAY_MODE_PARAM = "mode";
 
     static final String BINARY_CONTENT_TYPE = "binary";
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServlet.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServlet.java
index d84bd2abfd3..737ad9a780b 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServlet.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServlet.java
@@ -228,9 +228,7 @@ public class DatasetDownloadServlet extends AbstractDatasetDownloadServlet
         File rootDir = createDataSetRootDirectory(dataSetCode, session);
         RenderingContext context =
                 new RenderingContext(rootDir, requestParams.getURLPrefix(),
-                        requestParams.getPathInfo()
-
-                );
+                        requestParams.getPathInfo(), requestParams.tryGetSessionId());
 
         return context;
     }
@@ -272,7 +270,7 @@ public class DatasetDownloadServlet extends AbstractDatasetDownloadServlet
         final String urlPrefixWithDataset =
                 requestURI.substring(0, requestURI.length() - pathInfo.length());
 
-        final String sessionIDOrNull = request.getParameter(SESSION_ID_PARAM);
+        final String sessionIDOrNull = request.getParameter(Utils.SESSION_ID_PARAM);
         String displayMode = getDisplayMode(request);
 
         Boolean autoResolveOrNull = Boolean.valueOf(request.getParameter(AUTO_RESOLVE_KEY));
@@ -387,7 +385,8 @@ public class DatasetDownloadServlet extends AbstractDatasetDownloadServlet
                             mainDataSets.get(0).getPath()));
             RenderingContext newRenderingContext =
                     new RenderingContext(renderingContext.getRootDir(),
-                            renderingContext.getUrlPrefix(), newRelativePath);
+                            renderingContext.getUrlPrefix(), newRelativePath,
+                            renderingContext.getSessionIdOrNull());
             autoResolveRedirect(response, newRenderingContext);
         } else if (AutoResolveUtils.continueAutoResolving(requestParams.tryGetMainDataSetPattern(),
                 dir))
@@ -400,7 +399,8 @@ public class DatasetDownloadServlet extends AbstractDatasetDownloadServlet
             String newRelativePath = pathPrefix + childName;
             RenderingContext newRenderingContext =
                     new RenderingContext(renderingContext.getRootDir(),
-                            renderingContext.getUrlPrefix(), newRelativePath);
+                            renderingContext.getUrlPrefix(), newRelativePath,
+                            renderingContext.getSessionIdOrNull());
             autoResolveRedirect(response, newRenderingContext);
         } else
         {
@@ -411,8 +411,12 @@ public class DatasetDownloadServlet extends AbstractDatasetDownloadServlet
     private static void autoResolveRedirect(HttpServletResponse response,
             RenderingContext newContext) throws IOException
     {
+        String urlPrefix = newContext.getUrlPrefix();
+        String relativePathOrNull = newContext.getRelativePathOrNull();
+        String sessionIdOrNull = newContext.getSessionIdOrNull();
         final String newLocation =
-                DOWNLOAD_URL + newContext.getUrlPrefix() + "/" + newContext.getRelativePathOrNull();
+                DOWNLOAD_URL + urlPrefix + "/" + relativePathOrNull
+                        + Utils.createUrlParameterForSessionId("?", sessionIdOrNull);
         if (operationLog.isInfoEnabled())
         {
             operationLog.info(String.format("Auto resolve redirect: '%s', context: %s",
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/RenderingContext.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/RenderingContext.java
index 93a67937696..aaee62874cc 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/RenderingContext.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/RenderingContext.java
@@ -34,12 +34,15 @@ final class RenderingContext
 
     private String relativeParentPath;
 
-    RenderingContext(File rootDir, String urlPrefix, String relativePathOrNull)
+    private final String sessionIdOrNull;
+
+    RenderingContext(File rootDir, String urlPrefix, String relativePathOrNull, String sessionIdOrNull)
     {
         this.rootDir = rootDir;
         this.relativePathOrNull = relativePathOrNull;
         this.file = rootDir;
         this.urlPrefix = urlPrefix;
+        this.sessionIdOrNull = sessionIdOrNull;
         if (relativePathOrNull != null && relativePathOrNull.length() > 0)
         {
             file = new File(rootDir, relativePathOrNull);
@@ -76,6 +79,11 @@ final class RenderingContext
         return relativeParentPath;
     }
 
+    public String getSessionIdOrNull()
+    {
+        return sessionIdOrNull;
+    }
+
     @Override
     public String toString()
     {
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/SimpleHTMLDirectoryRenderer.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/SimpleHTMLDirectoryRenderer.java
index 7c740bd30fd..78f458ee8d9 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/SimpleHTMLDirectoryRenderer.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/SimpleHTMLDirectoryRenderer.java
@@ -51,7 +51,7 @@ final class SimpleHTMLDirectoryRenderer implements IDirectoryRenderer
 
     private static final Template ROW_TEMPLATE =
             new Template(
-                    "<tr><td class='td_file'><a href='${path}?mode=simpleHtml'>${name}</td><td>${size}</td></tr>");
+                    "<tr><td class='td_file'><a href='${path}?mode=simpleHtml${sessionId}'>${name}</td><td>${size}</td></tr>");
 
     private static final Template HEADER_TEMPLATE =
             new Template("<html><head>" + CSS + "</head><body>" + "<table> " + "${folder}" + "");
@@ -64,9 +64,12 @@ final class SimpleHTMLDirectoryRenderer implements IDirectoryRenderer
 
     private final String relativePathOrNull;
 
+    private final String sessionIdOrNull;
+
     SimpleHTMLDirectoryRenderer(final RenderingContext context)
     {
         this.relativePathOrNull = context.getRelativePathOrNull();
+        sessionIdOrNull = context.getSessionIdOrNull();
         final String prefix = context.getUrlPrefix();
         this.urlPrefix = prefix.endsWith("/") ? prefix : prefix + "/";
     }
@@ -111,6 +114,7 @@ final class SimpleHTMLDirectoryRenderer implements IDirectoryRenderer
         template.bind("path", urlPrefix + encodeURL(relativePath));
         template.bind("name", name);
         template.bind("size", fileSize);
+        template.bind("sessionId", Utils.createUrlParameterForSessionId("&", sessionIdOrNull));
         writer.println(template.createText());
     }
 
diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/Utils.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/Utils.java
new file mode 100644
index 00000000000..02473b7b375
--- /dev/null
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/Utils.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2010 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.systemsx.cisd.openbis.dss.generic.server;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class Utils
+{
+
+    public static final String SESSION_ID_PARAM = "sessionID";
+    
+    static String createUrlParameterForSessionId(String prefix, String sessionIdOrNull)
+    {
+        return sessionIdOrNull == null ? "" : prefix + Utils.SESSION_ID_PARAM + "="
+                + sessionIdOrNull;
+    }
+
+}
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServletTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServletTest.java
index 9ff6ddb75e7..8ca295bdcfa 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServletTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/DatasetDownloadServletTest.java
@@ -187,13 +187,31 @@ public class DatasetDownloadServletTest
         DatasetDownloadServlet servlet = createServlet();
         servlet.doGet(request, response);
         assertEquals(
-                "<html><head><style type='text/css'> * { margin: 3px; }html { height: 100%;  }body { height: 100%; font-family: verdana, tahoma, helvetica; font-size: 11px; text-align:left; }h1 { text-align: center; padding: 1em; color: #1E4E8F;}.td_hd { border: 1px solid #FFFFFF; padding 3px; background-color: #DDDDDD; height: 1.5em; }.div_hd { background-color: #1E4E8F; color: white; font-weight: bold; padding: 3px; }table { border-collapse: collapse; padding: 1em; }tr, td { font-family: verdana, tahoma, helvetica; font-size: 11px; }.td_file { font-family: verdana, tahoma, helvetica; font-size: 11px; height: 1.5em }.wrapper { min-height: 100%; height: auto !important; height: 100%; margin: 0em auto -4em; }.footer { height: 4em; text-align: center; }</style></head><body><table> "
+                "<html><head><style type='text/css'> * { margin: 3px; }html { height: 100%;  }"
+                        + "body { height: 100%; font-family: verdana, tahoma, helvetica; "
+                        + "font-size: 11px; text-align:left; }"
+                        + "h1 { text-align: center; padding: 1em; color: #1E4E8F;}"
+                        + ".td_hd { border: 1px solid #FFFFFF; padding 3px; "
+                        + "background-color: #DDDDDD; height: 1.5em; }"
+                        + ".div_hd { background-color: #1E4E8F; color: white; "
+                        + "font-weight: bold; padding: 3px; }"
+                        + "table { border-collapse: collapse; padding: 1em; }"
+                        + "tr, td { font-family: verdana, tahoma, helvetica; font-size: 11px; }"
+                        + ".td_file { font-family: verdana, tahoma, helvetica; "
+                        + "font-size: 11px; height: 1.5em }"
+                        + ".wrapper { min-height: 100%; height: auto !important; height: 100%;"
+                        + " margin: 0em auto -4em; }"
+                        + ".footer { height: 4em; text-align: center; }</style></head>"
+                        + "<body><table> "
                         + OSUtilities.LINE_SEPARATOR
-                        + "<tr><td class='td_file'><a href='/datastore_server/1234-1/%2B+s+%25+%21+%23+%40?mode=simpleHtml'>+ s % ! # @</td><td></td></tr>"
+                        + "<tr><td class='td_file'>"
+                        + "<a href='/datastore_server/1234-1/%2B+s+%25+%21+%23+%40?mode=simpleHtml&sessionID=AV76CF'>"
+                        + "+ s % ! # @</td><td></td></tr>"
                         + OSUtilities.LINE_SEPARATOR
-                        + "<tr><td class='td_file'><a href='/datastore_server/1234-1/read+me+%40home.txt?mode=simpleHtml'>read me @home.txt</td><td>12 bytes</td></tr>"
-                        + OSUtilities.LINE_SEPARATOR
-                        + "</table> </div> </body></html>"
+                        + "<tr><td class='td_file'>"
+                        + "<a href='/datastore_server/1234-1/read+me+%40home.txt?mode=simpleHtml&sessionID=AV76CF'>"
+                        + "read me @home.txt</td><td>12 bytes</td></tr>"
+                        + OSUtilities.LINE_SEPARATOR + "</table> </div> </body></html>"
                         + OSUtilities.LINE_SEPARATOR + "", writer.toString());
         assertEquals(getSessionCreationLogMessage() + OSUtilities.LINE_SEPARATOR + LOG_INFO
                 + "Data set '1234-1' obtained from openBIS server." + OSUtilities.LINE_SEPARATOR
@@ -208,7 +226,7 @@ public class DatasetDownloadServletTest
         context.checking(new Expectations()
             {
                 {
-                    one(request).getParameter(DatasetDownloadServlet.SESSION_ID_PARAM);
+                    one(request).getParameter(Utils.SESSION_ID_PARAM);
                     will(returnValue(EXAMPLE_SESSION_ID));
 
                     one(request).getParameter(DatasetDownloadServlet.DISPLAY_MODE_PARAM);
@@ -577,7 +595,7 @@ public class DatasetDownloadServletTest
 
     private void prepareParseRequestURLNoSession(Expectations exp)
     {
-        exp.one(request).getParameter(DatasetDownloadServlet.SESSION_ID_PARAM);
+        exp.one(request).getParameter(Utils.SESSION_ID_PARAM);
         exp.will(Expectations.returnValue(null));
 
         exp.allowing(request).getParameter(DatasetDownloadServlet.DISPLAY_MODE_PARAM);
@@ -604,7 +622,7 @@ public class DatasetDownloadServletTest
         context.checking(new Expectations()
             {
                 {
-                    one(request).getParameter(DatasetDownloadServlet.SESSION_ID_PARAM);
+                    one(request).getParameter(Utils.SESSION_ID_PARAM);
                     will(returnValue(EXAMPLE_SESSION_ID));
 
                     one(request).getParameter(DatasetDownloadServlet.DISPLAY_MODE_PARAM);
@@ -646,10 +664,10 @@ public class DatasetDownloadServletTest
 
                     // For the logging of session-creating requests
                     Vector<String> parameterNames = new Vector<String>();
-                    parameterNames.add(DatasetDownloadServlet.SESSION_ID_PARAM);
+                    parameterNames.add(Utils.SESSION_ID_PARAM);
                     one(request).getParameterNames();
                     will(Expectations.returnValue(parameterNames.elements()));
-                    one(request).getParameter(DatasetDownloadServlet.SESSION_ID_PARAM);
+                    one(request).getParameter(Utils.SESSION_ID_PARAM);
                     will(returnValue(EXAMPLE_SESSION_ID));
 
                     DatabaseInstance databaseInstance = new DatabaseInstance();
-- 
GitLab