diff --git a/datastore_server/build.gradle b/datastore_server/build.gradle index a5cd41854c738fdc62757c184c4fcc835740aa03..1501e6205d54c6bfa2e1a33ebc90927f7c4ca0ca 100644 --- a/datastore_server/build.gradle +++ b/datastore_server/build.gradle @@ -32,6 +32,9 @@ dependencies { 'cisd:cisd-image-readers:r1553067167', 'hjg:pngj:0.62', 'apache:commons-fileupload:1.3.3', + 'docx4j:docx4j:6.1.2', + 'freva:ascii-table:1.2.0', + 'jsoup:jsoup:1.14.2', 'dspace:xoai-data-provider:4.2.0' runtime 'bioformats:bioformats:6.5.1' diff --git a/datastore_server/source/java/ch/ethz/sis/DOCXBuilder.java b/datastore_server/source/java/ch/ethz/sis/DOCXBuilder.java new file mode 100644 index 0000000000000000000000000000000000000000..6c0f4185b58ad39b7a78f1a606281f2d2592a8f3 --- /dev/null +++ b/datastore_server/source/java/ch/ethz/sis/DOCXBuilder.java @@ -0,0 +1,256 @@ +package ch.ethz.sis; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.util.Base64; + +import org.docx4j.jaxb.Context; +import org.docx4j.openpackaging.contenttype.ContentType; +import org.docx4j.openpackaging.packages.WordprocessingMLPackage; +import org.docx4j.openpackaging.parts.PartName; +import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart; +import org.docx4j.relationships.Relationship; +import org.docx4j.wml.CTAltChunk; +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.client.api.Request; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import ch.systemsx.cisd.common.http.JettyHttpClientFactory; +import com.github.freva.asciitable.AsciiTable; + +public class DOCXBuilder +{ + public static void main(String[] args) throws Exception + { + DOCXBuilder docx = new DOCXBuilder(); + docx.addTitle("TitleA"); + docx.addHeader("MetaA"); + docx.addProperty("PropertyA", "ValueA"); + docx.addProperty("PropertyB", "ValueB"); + docx.addProperty("PropertyC", + "<p>I am normal</p><p style=\"color:red;\">I am red</p><p style=\"color:blue;\">I am blue</p><p style=\"font-size:36px;\">I am big</p>"); + + FileOutputStream out = new FileOutputStream(new File("wordFromHTML.docx")); + out.write(docx.getDocBytes()); + out.close(); + } + + private static final String START_RICH_TEXT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<html><head></head><body>"; + + private static final String END_RICH_TEXT = "</body></html>"; + + private StringBuffer doc; + + private String closedDoc; + + private boolean closed; + + public DOCXBuilder() + { + System.setProperty("javax.xml.transform.TransformerFactory", "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); + closed = false; + doc = new StringBuffer(); + startDoc(); + } + + public void setDocument(String doc) + { + this.doc = new StringBuffer(doc); + closed = true; + } + + private void startDoc() + { + if (!closed) + { + doc.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); + doc.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); + doc.append("<head></head>"); + doc.append("<body>"); + } + } + + private void endDoc() + { + if (!closed) + { + doc.append("</body>"); + doc.append("</html>"); + closed = true; + closedDoc = fixImages(doc); + } + } + + public void addProperty(String key, String value) + { + if (!closed) + { + doc.append("<p>").append("<b>").append(key).append(": ").append("</b>").append("</p>"); + addParagraph(value); + } + } + + public void addParagraph(String value) + { + if (!closed) + { + value = cleanXMLEnvelope(value); + doc.append("<p>").append(value).append("</p>"); + } + } + + public void addTitle(String title) + { + if (!closed) + { + doc.append("<h1>").append(title).append("</h1>"); + } + } + + public void addHeader(String header) + { + if (!closed) + { + doc.append("<h2>").append(header).append("</h2>"); + } + } + + public byte[] getHTMLBytes() throws Exception + { + if (!closed) + { + endDoc(); + } + return closedDoc.getBytes(); + } + + public byte[] getDocBytes() throws Exception + { + // .. Finish Document + if (!closed) + { + endDoc(); + } + + // .. HTML Code + WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); + AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html")); + afiPart.setBinaryData(closedDoc.getBytes()); + afiPart.setContentType(new ContentType("text/html")); + Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart); + + // .. the bit in document body + CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk(); + ac.setId(altChunkRel.getId()); + wordMLPackage.getMainDocumentPart().addObject(ac); + + // .. content type + wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html"); + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + wordMLPackage.save(outStream); + + return outStream.toByteArray(); + } + + private String cleanXMLEnvelope(String value) + { + if (value.startsWith(START_RICH_TEXT) && value.endsWith(END_RICH_TEXT)) + { + value = value.substring(START_RICH_TEXT.length(), value.length() - END_RICH_TEXT.length()); + } + return value; + } + + private String fixImages(StringBuffer buffer) + { + Document jsoupDoc = Jsoup.parse(buffer.toString()); + jsoupDoc.outputSettings().syntax(Document.OutputSettings.Syntax.xml); + Elements elements = jsoupDoc.select("img"); + + // Fixes images sizes + for (Element element : elements) + { + String style = element.attr("style"); + if (style != null) + { + String[] rules = style.split(";"); + if (rules != null) + { + for (int rIdx = 0; rIdx < rules.length; rIdx++) + { + String rule = rules[rIdx]; + String[] ruleElements = rule.split(":"); + if (ruleElements != null && ruleElements.length == 2) + { + String ruleKey = ruleElements[0].trim(); + String ruleValue = ruleElements[1].trim(); + if ((ruleKey.toLowerCase().equals("width") || ruleKey.toLowerCase().equals("height")) + && ruleValue.endsWith("px")) + { + element.attr(ruleKey, ruleValue.substring(0, ruleValue.length() - 2)); + } + } + } + } + } + + // Converts to Base64 + String src = element.attr("src"); + try + { + element.attr("src", getDataUriFromUri(src)); + } catch (Exception ex) + { + + } + } + + return jsoupDoc.html(); + } + + // private String encodeImgAsBase64(String value) { + // Document doc = Jsoup.parse(value); + // doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml); + // Elements elements = doc.select("img"); + // + // for (Element element : elements) { + // String src = element.attr("src"); + // try { + // element.attr("src", getDataUriFromUri(src)); + // } catch(Exception ex) { + // + // } + // } + // + // return doc.html(); + // } + + private static String getDataUriFromUri(String url) throws Exception + { + HttpClient client = JettyHttpClientFactory.getHttpClient(); + Request requestEntity = client.newRequest(url).method("GET"); + ContentResponse contentResponse = requestEntity.send(); + return "data:" + contentResponse.getMediaType() + ";base64," + Base64.getEncoder().encodeToString(contentResponse.getContent()); + } + + // public static String convertJsonToText(final String jsonString) { + // return AsciiTable.getTable(null, jsonStringToArray(jsonString)); + // } + // + // private static String[] jsonStringToArray(final String jsonString) { + // final ArrayList<String> stringList = new ArrayList<String>(); + // + // final JSONArray jsonArray = new JSONArray(jsonString); + // + // for (int i = 0; i < jsonArray.length(); i++) { + // stringList.add(jsonArray.getString(i)); + // } + // + // return stringList.toArray(); + // } +} diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/docxbuilder-source/DOCXBuilder.java b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/docxbuilder-source/DOCXBuilder.java deleted file mode 100644 index 1543daad5c20b9c1826ec530b4c5cf19e4872b34..0000000000000000000000000000000000000000 --- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/docxbuilder-source/DOCXBuilder.java +++ /dev/null @@ -1,225 +0,0 @@ -package ch.ethz.sis; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.util.Base64; - -import org.docx4j.jaxb.Context; -import org.docx4j.openpackaging.contenttype.ContentType; -import org.docx4j.openpackaging.packages.WordprocessingMLPackage; -import org.docx4j.openpackaging.parts.PartName; -import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart; -import org.docx4j.relationships.Relationship; -import org.docx4j.wml.CTAltChunk; -import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.client.api.ContentResponse; -import org.eclipse.jetty.client.api.Request; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.select.Elements; - -import ch.systemsx.cisd.common.http.JettyHttpClientFactory; -import com.github.freva.asciitable.AsciiTable; - -public class DOCXBuilder { - public static void main(String[] args) throws Exception { - DOCXBuilder docx = new DOCXBuilder(); - docx.addTitle("TitleA"); - docx.addHeader("MetaA"); - docx.addProperty("PropertyA", "ValueA"); - docx.addProperty("PropertyB", "ValueB"); - docx.addProperty("PropertyC", - "<p>I am normal</p><p style=\"color:red;\">I am red</p><p style=\"color:blue;\">I am blue</p><p style=\"font-size:36px;\">I am big</p>"); - - FileOutputStream out = new FileOutputStream(new File("wordFromHTML.docx")); - out.write(docx.getDocBytes()); - out.close(); - } - - private static final String START_RICH_TEXT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<html><head></head><body>"; - - private static final String END_RICH_TEXT = "</body></html>"; - - private StringBuffer doc; - - private String closedDoc; - - private boolean closed; - - public DOCXBuilder() { - System.setProperty("javax.xml.transform.TransformerFactory", "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); - closed = false; - doc = new StringBuffer(); - startDoc(); - } - - public void setDocument(String doc) { - this.doc = new StringBuffer(doc); - closed = true; - } - - private void startDoc() { - if (!closed) { - doc.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); - doc.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); - doc.append("<head></head>"); - doc.append("<body>"); - } - } - - private void endDoc() { - if (!closed) { - doc.append("</body>"); - doc.append("</html>"); - closed = true; - closedDoc = fixImages(doc); - } - } - - public void addProperty(String key, String value) { - if (!closed) { - doc.append("<p>").append("<b>").append(key).append(": ").append("</b>").append("</p>"); - addParagraph(value); - } - } - - public void addParagraph(String value) - { - if (!closed) - { - value = cleanXMLEnvelope(value); - doc.append("<p>").append(value).append("</p>"); - } - } - - public void addTitle(String title) { - if (!closed) { - doc.append("<h1>").append(title).append("</h1>"); - } - } - - public void addHeader(String header) { - if (!closed) { - doc.append("<h2>").append(header).append("</h2>"); - } - } - - public byte[] getHTMLBytes() throws Exception { - if (!closed) { - endDoc(); - } - return closedDoc.getBytes(); - } - - public byte[] getDocBytes() throws Exception { - // .. Finish Document - if (!closed) { - endDoc(); - } - - // .. HTML Code - WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); - AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html")); - afiPart.setBinaryData(closedDoc.getBytes()); - afiPart.setContentType(new ContentType("text/html")); - Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart); - - // .. the bit in document body - CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk(); - ac.setId(altChunkRel.getId()); - wordMLPackage.getMainDocumentPart().addObject(ac); - - // .. content type - wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html"); - ByteArrayOutputStream outStream = new ByteArrayOutputStream(); - wordMLPackage.save(outStream); - - return outStream.toByteArray(); - } - - private String cleanXMLEnvelope(String value) { - if (value.startsWith(START_RICH_TEXT) && value.endsWith(END_RICH_TEXT)) { - value = value.substring(START_RICH_TEXT.length() + 3, value.length() - END_RICH_TEXT.length()); - } - return value; - } - - private String fixImages(StringBuffer buffer) { - Document jsoupDoc = Jsoup.parse(buffer.toString()); - jsoupDoc.outputSettings().syntax(Document.OutputSettings.Syntax.xml); - Elements elements = jsoupDoc.select("img"); - - // Fixes images sizes - for (Element element : elements) { - String style = element.attr("style"); - if (style != null) { - String[] rules = style.split(";"); - if(rules != null) { - for (int rIdx = 0; rIdx < rules.length; rIdx++) { - String rule = rules[rIdx]; - String[] ruleElements = rule.split(":"); - if (ruleElements != null && ruleElements.length == 2) { - String ruleKey = ruleElements[0].trim(); - String ruleValue = ruleElements[1].trim(); - if ((ruleKey.toLowerCase().equals("width") || ruleKey.toLowerCase().equals("height")) - && ruleValue.endsWith("px")) { - element.attr(ruleKey, ruleValue.substring(0, ruleValue.length() - 2)); - } - } - } - } - } - - // Converts to Base64 - String src = element.attr("src"); - try { - element.attr("src", getDataUriFromUri(src)); - } catch(Exception ex) { - - } - } - - return jsoupDoc.html(); - } - -// private String encodeImgAsBase64(String value) { -// Document doc = Jsoup.parse(value); -// doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml); -// Elements elements = doc.select("img"); -// -// for (Element element : elements) { -// String src = element.attr("src"); -// try { -// element.attr("src", getDataUriFromUri(src)); -// } catch(Exception ex) { -// -// } -// } -// -// return doc.html(); -// } - - private static String getDataUriFromUri(String url) throws Exception { - HttpClient client = JettyHttpClientFactory.getHttpClient(); - Request requestEntity = client.newRequest(url).method("GET"); - ContentResponse contentResponse = requestEntity.send(); - return "data:"+contentResponse.getMediaType()+";base64,"+Base64.getEncoder().encodeToString(contentResponse.getContent()); - } - -// public static String convertJsonToText(final String jsonString) { -// return AsciiTable.getTable(null, jsonStringToArray(jsonString)); -// } -// -// private static String[] jsonStringToArray(final String jsonString) { -// final ArrayList<String> stringList = new ArrayList<String>(); -// -// final JSONArray jsonArray = new JSONArray(jsonString); -// -// for (int i = 0; i < jsonArray.length(); i++) { -// stringList.add(jsonArray.getString(i)); -// } -// -// return stringList.toArray(); -// } -} diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/generalExports.py b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/generalExports.py index 085255914b26c92a1a7e76bc3bbe29c4acab1a33..b8e43ee8ee5170f8b32362913f1defe636a17206 100644 --- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/generalExports.py +++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/generalExports.py @@ -97,6 +97,10 @@ def generateZipFile(entities, includeRoot, sessionToken, tempDirPath, tempZipFil try: generateFilesInZip(zos, entities, includeRoot, sessionToken, tempDirPath) + except BaseException as e: + operationLog.error("Error occurred: %s" % e) + except Throwable as e: + operationLog.error("Error occurred: %s" % e, e) finally: zos.close() fos.close() \ No newline at end of file diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/ascii-table-1.1.0.jar b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/ascii-table-1.1.0.jar deleted file mode 100644 index df2fd3bcc3372019cb319f49eb267b5d9a06391d..0000000000000000000000000000000000000000 Binary files a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/ascii-table-1.1.0.jar and /dev/null differ diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/docx4j-3.3.3.jar b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/docx4j-3.3.3.jar deleted file mode 100644 index d7baf6c21d8bc639542552e0f0f42185b15786f2..0000000000000000000000000000000000000000 Binary files a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/docx4j-3.3.3.jar and /dev/null differ diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/docxbuilder.jar b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/docxbuilder.jar deleted file mode 100644 index d74f90a27a688fb2c5025ff99c782a733566b315..0000000000000000000000000000000000000000 Binary files a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/docxbuilder.jar and /dev/null differ diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/jsoup-1.11.3.jar b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/jsoup-1.11.3.jar deleted file mode 100644 index 80c0ca8252a0a00f0ea7156d7aed058b1ce876b4..0000000000000000000000000000000000000000 Binary files a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/jsoup-1.11.3.jar and /dev/null differ diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/serializer-2.7.2.jar b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/serializer-2.7.2.jar deleted file mode 100644 index 10c881c100ebd5ecd9ffb5ba912711f667169a76..0000000000000000000000000000000000000000 Binary files a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/serializer-2.7.2.jar and /dev/null differ diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/slf4j-api-1.7.21.jar b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/slf4j-api-1.7.21.jar deleted file mode 100644 index 2a5c33ec55491e5b4be53525d8d7242e53e020c0..0000000000000000000000000000000000000000 Binary files a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/slf4j-api-1.7.21.jar and /dev/null differ diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/xalan-2.7.2.jar b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/xalan-2.7.2.jar deleted file mode 100644 index abdabe33ec1368f910ac7012f081ad2ae9e4897b..0000000000000000000000000000000000000000 Binary files a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/dss/reporting-plugins/exports-api/lib/xalan-2.7.2.jar and /dev/null differ