diff --git a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadServlet.java b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadServlet.java index 02dd90b7f560c3b6898dd7d44c819a3904c1858a..07743e7393fb1d75e141903cfa1f4969283d59c0 100644 --- a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadServlet.java +++ b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadServlet.java @@ -114,6 +114,7 @@ public class DatasetDownloadServlet extends HttpServlet protected final void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { + IRendererFactory rendererFactory = new HTMLRendererFactory(); try { String requestURI = URLDecoder.decode(request.getRequestURI(), "UTF-8"); @@ -152,17 +153,17 @@ public class DatasetDownloadServlet extends HttpServlet } File rootDir = createDataSetRootDirectory(dataSet); RenderingContext context = new RenderingContext(rootDir, requestURI, pathInfo); - renderPage(response, dataSet, context); + renderPage(rendererFactory, response, dataSet, context); } } catch (Exception e) { - printError(request, response, e); + printError(rendererFactory, request, response, e); } } - private void printError(final HttpServletRequest request, final HttpServletResponse response, - Exception exception) throws IOException + private void printError(IRendererFactory rendererFactory, final HttpServletRequest request, + final HttpServletResponse response, Exception exception) throws IOException { if (exception instanceof UserFailureException == false) { @@ -179,16 +180,17 @@ public class DatasetDownloadServlet extends HttpServlet } String message = exception.getMessage(); String errorText = StringUtils.isBlank(message) ? exception.toString() : message; + IErrorRenderer errorRenderer = rendererFactory.createErrorRenderer(); + response.setContentType(rendererFactory.getContentType()); PrintWriter writer = response.getWriter(); - writer.println("<html><body><h1>Error</h1>"); - writer.println(errorText); - writer.println("</body></html>"); + errorRenderer.setWriter(writer); + errorRenderer.printErrorMessage(errorText); writer.flush(); writer.close(); } - private void renderPage(HttpServletResponse response, ExternalData dataSet, - RenderingContext renderingContext) throws IOException + private void renderPage(IRendererFactory rendererFactory, HttpServletResponse response, + ExternalData dataSet, RenderingContext renderingContext) throws IOException { File file = renderingContext.getFile(); if (file.exists() == false) @@ -197,23 +199,24 @@ public class DatasetDownloadServlet extends HttpServlet } if (file.isDirectory()) { - createPage(response, dataSet, renderingContext, file); + createPage(rendererFactory, response, dataSet, renderingContext, file); } else { deliverFile(response, dataSet, file); } } - private void createPage(HttpServletResponse response, ExternalData dataSet, - RenderingContext renderingContext, File file) throws IOException + private void createPage(IRendererFactory rendererFactory, HttpServletResponse response, + ExternalData dataSet, RenderingContext renderingContext, File file) throws IOException { if (operationLog.isInfoEnabled()) { operationLog.info("For data set '" + dataSet.getCode() + "' show directory " + file.getAbsolutePath()); } - IDirectoryRenderer directoryRenderer = new HTMLDirectoryRenderer(renderingContext); - response.setContentType(directoryRenderer.getContentType()); + IDirectoryRenderer directoryRenderer = + rendererFactory.createDirectoryRenderer(renderingContext); + response.setContentType(rendererFactory.getContentType()); PrintWriter writer = null; try { diff --git a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/HTMLDirectoryRenderer.java b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/HTMLDirectoryRenderer.java index 046c9b24ebfaffc9d1f5df27d6018b1f752384e2..4335aec1f5a3225679e9132da60ea5a59f3808f9 100644 --- a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/HTMLDirectoryRenderer.java +++ b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/HTMLDirectoryRenderer.java @@ -96,11 +96,6 @@ final class HTMLDirectoryRenderer implements IDirectoryRenderer this.writer = writer; } - public String getContentType() - { - return "text/html"; - } - public void printHeader(final ExternalData dataSet) { final String datasetCode = dataSet.getCode(); diff --git a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/HTMLErrorRenderer.java b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/HTMLErrorRenderer.java new file mode 100644 index 0000000000000000000000000000000000000000..9b177e8a6ef8b6ab0e084b19f079468fcb14ef32 --- /dev/null +++ b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/HTMLErrorRenderer.java @@ -0,0 +1,42 @@ +/* + * Copyright 2008 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.datasetdownload; + +import java.io.PrintWriter; + +/** + * Error renderer for HTML. + * + * @author Franz-Josef Elmer + */ +public class HTMLErrorRenderer implements IErrorRenderer +{ + private PrintWriter writer; + + public void setWriter(PrintWriter writer) + { + this.writer = writer; + } + + public void printErrorMessage(String errorMessage) + { + writer.println("<html><body><h1>Error</h1>"); + writer.println(errorMessage); + writer.println("</body></html>"); + } + +} diff --git a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/HTMLRendererFactory.java b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/HTMLRendererFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..dc88b330224f64959902fa643a13c69acdb74151 --- /dev/null +++ b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/HTMLRendererFactory.java @@ -0,0 +1,42 @@ +/* + * Copyright 2008 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.datasetdownload; + +/** + * Renderer factory for HTML. + * + * @author Franz-Josef Elmer + */ +public class HTMLRendererFactory implements IRendererFactory +{ + public String getContentType() + { + return "text/html"; + } + + + public IDirectoryRenderer createDirectoryRenderer(RenderingContext context) + { + return new HTMLDirectoryRenderer(context); + } + + public IErrorRenderer createErrorRenderer() + { + return new HTMLErrorRenderer(); + } + +} diff --git a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IDirectoryRenderer.java b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IDirectoryRenderer.java index 31197c7c13069aaed39d4fb8073080fa2badfcf8..9c808f16271822d34954c8fcf436283a12973ba3 100644 --- a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IDirectoryRenderer.java +++ b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IDirectoryRenderer.java @@ -16,19 +16,16 @@ package ch.systemsx.cisd.openbis.datasetdownload; -import java.io.PrintWriter; import ch.systemsx.cisd.lims.base.ExternalData; /** + * Interface of a renderer of a directory. + * * @author Franz-Josef Elmer */ -public interface IDirectoryRenderer +public interface IDirectoryRenderer extends IWriterInjector { - public String getContentType(); - - public void setWriter(PrintWriter writer); - public void printHeader(ExternalData dataSet); public void printLinkToParentDirectory(String relativePath); diff --git a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IErrorRenderer.java b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IErrorRenderer.java new file mode 100644 index 0000000000000000000000000000000000000000..43a23682c7738098af4b05e11d726630406f83bb --- /dev/null +++ b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IErrorRenderer.java @@ -0,0 +1,31 @@ +/* + * Copyright 2008 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.datasetdownload; + +/** + * Interface for a renderer of an error messages. + * + * @author Franz-Josef Elmer + */ +public interface IErrorRenderer extends IWriterInjector +{ + /** + * Prints the error message set by + * {@link IWriterInjector#setWriter(java.io.PrintWriter)}. + */ + public void printErrorMessage(String errorMessage); +} diff --git a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IRendererFactory.java b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IRendererFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..0218ada2fe24cb271503d5188013b81a75a6dbec --- /dev/null +++ b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IRendererFactory.java @@ -0,0 +1,31 @@ +/* + * Copyright 2008 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.datasetdownload; + +/** + * Interface of a factory of renderers. + * + * @author Franz-Josef Elmer + */ +public interface IRendererFactory +{ + public String getContentType(); + + public IDirectoryRenderer createDirectoryRenderer(RenderingContext context); + + public IErrorRenderer createErrorRenderer(); +} diff --git a/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IWriterInjector.java b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IWriterInjector.java new file mode 100644 index 0000000000000000000000000000000000000000..ff3d0c4d0405acbac639e05d65e227af58930a89 --- /dev/null +++ b/dataset_download/source/java/ch/systemsx/cisd/openbis/datasetdownload/IWriterInjector.java @@ -0,0 +1,33 @@ +/* + * Copyright 2008 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.datasetdownload; + +import java.io.PrintWriter; + +/** + * Interface of objects which allows injection of a {@link PrintWriter}. + * + * @author Franz-Josef Elmer + */ +public interface IWriterInjector +{ + /** + * Sets the writer. + */ + public void setWriter(PrintWriter writer); + +} \ No newline at end of file diff --git a/dataset_download/sourceTest/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadServletTest.java b/dataset_download/sourceTest/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadServletTest.java index e308819a5c9ecd34f0c9a17e850d4af41a2edea1..abe8739c905a87099fc6cec08b589decd37a8ff1 100644 --- a/dataset_download/sourceTest/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadServletTest.java +++ b/dataset_download/sourceTest/java/ch/systemsx/cisd/openbis/datasetdownload/DatasetDownloadServletTest.java @@ -165,10 +165,10 @@ public class DatasetDownloadServletTest DatasetDownloadServlet servlet = createServlet(); servlet.doGet(request, response); assertEquals( - "<html><head><title> Data Set Download Service: GROUP-G/PROJECT-P/EPERIMENT-E/SAMPLE-S/1234-1</title><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><div class=\'wrapper\'><h1>Data Set Download Service</h1><div class=\'div_hd\'>Information about data set</div><table><tr><td class=\'td_hd\'>Group:</td><td>GROUP-G</td></tr><tr><td class=\'td_hd\'>Project:</td><td>PROJECT-P</td></tr><tr><td class=\'td_hd\'>Experiment:</td><td>EPERIMENT-E</td></tr><tr><td class=\'td_hd\'>Sample:</td><td>SAMPLE-S</td></tr><tr><td class=\'td_hd\'>Data Set Code:</td><td>1234-1</td></tr></table> <div class=\'div_hd\'>Files</div><table> \n" - + "<tr><td class=\'td_file\'><a href=\'/download/1234-1/%2B+s+%25+%21+%23+%40\'>+ s % ! # @</td><td></td></tr>\n" - + "<tr><td class=\'td_file\'><a href=\'/download/1234-1/read+me+%40home.txt\'>read me @home.txt</td><td>12 bytes</td></tr>\n" - + "</table> </div> <div class=\'footer\'>Copyright © 2008 ETHZ - <a href=\'http://www.cisd.systemsx.ethz.ch/\'>CISD</a> </div> </body></html>\n" + "<html><head><title> Data Set Download Service: GROUP-G/PROJECT-P/EPERIMENT-E/SAMPLE-S/1234-1</title><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><div class=\'wrapper\'><h1>Data Set Download Service</h1><div class=\'div_hd\'>Information about data set</div><table><tr><td class=\'td_hd\'>Group:</td><td>GROUP-G</td></tr><tr><td class=\'td_hd\'>Project:</td><td>PROJECT-P</td></tr><tr><td class=\'td_hd\'>Experiment:</td><td>EPERIMENT-E</td></tr><tr><td class=\'td_hd\'>Sample:</td><td>SAMPLE-S</td></tr><tr><td class=\'td_hd\'>Data Set Code:</td><td>1234-1</td></tr></table> <div class=\'div_hd\'>Files</div><table> " + OSUtilities.LINE_SEPARATOR + + "<tr><td class=\'td_file\'><a href=\'/download/1234-1/%2B+s+%25+%21+%23+%40\'>+ s % ! # @</td><td></td></tr>" + OSUtilities.LINE_SEPARATOR + + "<tr><td class=\'td_file\'><a href=\'/download/1234-1/read+me+%40home.txt\'>read me @home.txt</td><td>12 bytes</td></tr>" + OSUtilities.LINE_SEPARATOR + + "</table> </div> <div class=\'footer\'>Copyright © 2008 ETHZ - <a href=\'http://www.cisd.systemsx.ethz.ch/\'>CISD</a> </div> </body></html>" + OSUtilities.LINE_SEPARATOR + "", writer.toString()); assertEquals(LOG_INFO + "Data set '1234-1' obtained from openBIS server." + OSUtilities.LINE_SEPARATOR + LOG_INFO @@ -189,6 +189,7 @@ public class DatasetDownloadServletTest context.checking(new Expectations() { { + one(response).setContentType("text/html"); one(response).getWriter(); will(returnValue(new PrintWriter(writer))); } @@ -227,6 +228,7 @@ public class DatasetDownloadServletTest String codeAndPath = externalData.getCode(); will(returnValue(REQUEST_URI_PREFIX + codeAndPath)); + one(response).setContentType("text/html"); one(response).getWriter(); will(returnValue(new PrintWriter(writer))); } @@ -257,9 +259,9 @@ public class DatasetDownloadServletTest DatasetDownloadServlet servlet = createServlet(); servlet.doGet(request, response); assertEquals( - "<html><head><title> Data Set Download Service: GROUP-G/PROJECT-P/EPERIMENT-E/SAMPLE-S/1234-1</title><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><div class=\'wrapper\'><h1>Data Set Download Service</h1><div class=\'div_hd\'>Information about data set</div><table><tr><td class=\'td_hd\'>Group:</td><td>GROUP-G</td></tr><tr><td class=\'td_hd\'>Project:</td><td>PROJECT-P</td></tr><tr><td class=\'td_hd\'>Experiment:</td><td>EPERIMENT-E</td></tr><tr><td class=\'td_hd\'>Sample:</td><td>SAMPLE-S</td></tr><tr><td class=\'td_hd\'>Data Set Code:</td><td>1234-1</td></tr></table> <div class=\'div_hd\'>Files</div><table> <tr><td class=\'td_hd\'>Folder:</td><td>+ s % ! # @</td></tr>\n" - + "<tr><td class=\'td_file\'><a href=\'/download/1234-1/\'>..</td><td></td></tr>\n" - + "</table> </div> <div class=\'footer\'>Copyright © 2008 ETHZ - <a href=\'http://www.cisd.systemsx.ethz.ch/\'>CISD</a> </div> </body></html>\n", + "<html><head><title> Data Set Download Service: GROUP-G/PROJECT-P/EPERIMENT-E/SAMPLE-S/1234-1</title><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><div class=\'wrapper\'><h1>Data Set Download Service</h1><div class=\'div_hd\'>Information about data set</div><table><tr><td class=\'td_hd\'>Group:</td><td>GROUP-G</td></tr><tr><td class=\'td_hd\'>Project:</td><td>PROJECT-P</td></tr><tr><td class=\'td_hd\'>Experiment:</td><td>EPERIMENT-E</td></tr><tr><td class=\'td_hd\'>Sample:</td><td>SAMPLE-S</td></tr><tr><td class=\'td_hd\'>Data Set Code:</td><td>1234-1</td></tr></table> <div class=\'div_hd\'>Files</div><table> <tr><td class=\'td_hd\'>Folder:</td><td>+ s % ! # @</td></tr>" + OSUtilities.LINE_SEPARATOR + + "<tr><td class=\'td_file\'><a href=\'/download/1234-1/\'>..</td><td></td></tr>" + OSUtilities.LINE_SEPARATOR + + "</table> </div> <div class=\'footer\'>Copyright © 2008 ETHZ - <a href=\'http://www.cisd.systemsx.ethz.ch/\'>CISD</a> </div> </body></html>" + OSUtilities.LINE_SEPARATOR, writer.toString()); assertEquals(LOG_INFO + "For data set '1234-1' show directory <wd>/data set #123/" + EXAMPLE_DATA_SET_SUB_FOLDER_NAME, getNormalizedLogContent()); @@ -317,6 +319,7 @@ public class DatasetDownloadServletTest one(request).getQueryString(); will(returnValue("queryString")); + one(response).setContentType("text/html"); one(response).getWriter(); will(returnValue(new PrintWriter(writer))); } @@ -377,7 +380,8 @@ public class DatasetDownloadServletTest one(request).getQueryString(); will(returnValue("query")); - + + one(response).setContentType("text/html"); one(response).getWriter(); will(returnValue(new PrintWriter(writer))); }