diff --git a/wise-webapp/src/main/java/com/wisemapping/exporter/ExportFormat.java b/wise-webapp/src/main/java/com/wisemapping/exporter/ExportFormat.java deleted file mode 100644 index f7d7d8e9..00000000 --- a/wise-webapp/src/main/java/com/wisemapping/exporter/ExportFormat.java +++ /dev/null @@ -1,57 +0,0 @@ -/* -* Copyright [2015] [wisemapping] -* -* Licensed under WiseMapping Public License, Version 1.0 (the "License"). -* It is basically the Apache License, Version 2.0 (the "License") plus the -* "powered by wisemapping" text requirement on every single page; -* you may not use this file except in compliance with the License. -* You may obtain a copy of the license at -* -* http://www.wisemapping.org/license -* -* 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 com.wisemapping.exporter; - -import org.jetbrains.annotations.NotNull; - -public enum ExportFormat { - FREEMIND("application/freemind", "mm"), - TEXT("text/plain", "txt"), - MICROSOFT_EXCEL("application/vnd.ms-excel", "xls"), - MICROSOFT_WORD("application/msword", "doc"), - MINDJET("application/vnd.mindjet.mindmanager", "mmap"), - WISEMAPPING("application/wisemapping+xml", "wxml"); - - - private final String contentType; - private final String fileExtension; - - ExportFormat(String contentType, String fileExtension) { - this.contentType = contentType; - this.fileExtension = fileExtension; - } - - public String getFileExtension() { - return fileExtension; - } - - public String getContentType() { - return contentType; - } - - public static ExportFormat fromContentType(@NotNull final String contentType) { - final ExportFormat[] values = ExportFormat.values(); - for (ExportFormat value : values) { - if (value.getContentType().equals(contentType)) { - return value; - } - } - throw new IllegalStateException("ComponentType could not be mapped:" + contentType); - } -} diff --git a/wise-webapp/src/main/java/com/wisemapping/exporter/ExportProperties.java b/wise-webapp/src/main/java/com/wisemapping/exporter/ExportProperties.java deleted file mode 100644 index 8a663759..00000000 --- a/wise-webapp/src/main/java/com/wisemapping/exporter/ExportProperties.java +++ /dev/null @@ -1,51 +0,0 @@ -/* -* Copyright [2015] [wisemapping] -* -* Licensed under WiseMapping Public License, Version 1.0 (the "License"). -* It is basically the Apache License, Version 2.0 (the "License") plus the -* "powered by wisemapping" text requirement on every single page; -* you may not use this file except in compliance with the License. -* You may obtain a copy of the license at -* -* http://www.wisemapping.org/license -* -* 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 com.wisemapping.exporter; - -public class ExportProperties { - private final ExportFormat format; - private String version; - - public ExportFormat getFormat() { - return format; - } - - private ExportProperties(final ExportFormat format) { - this.format = format; - } - - public static ExportProperties create(final ExportFormat format) { - return new GenericProperties(format); - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - private static class GenericProperties extends ExportProperties { - private GenericProperties(ExportFormat format) { - super(format); - } - } - -} diff --git a/wise-webapp/src/main/java/com/wisemapping/exporter/ExporterFactory.java b/wise-webapp/src/main/java/com/wisemapping/exporter/ExporterFactory.java deleted file mode 100644 index c8a40143..00000000 --- a/wise-webapp/src/main/java/com/wisemapping/exporter/ExporterFactory.java +++ /dev/null @@ -1,217 +0,0 @@ -/* -* Copyright [2015] [wisemapping] -* -* Licensed under WiseMapping Public License, Version 1.0 (the "License"). -* It is basically the Apache License, Version 2.0 (the "License") plus the -* "powered by wisemapping" text requirement on every single page; -* you may not use this file except in compliance with the License. -* You may obtain a copy of the license at -* -* http://www.wisemapping.org/license -* -* 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 com.wisemapping.exporter; - -import com.wisemapping.importer.VersionNumber; -import org.apache.commons.io.IOUtils; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.w3c.dom.*; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import javax.servlet.ServletContext; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import javax.xml.xpath.*; -import java.awt.geom.AffineTransform; -import java.io.*; -import java.nio.charset.StandardCharsets; -import java.util.Base64; -import java.util.regex.Pattern; - -public class ExporterFactory { - - private static final String GROUP_NODE_NAME = "g"; - private static final String IMAGE_NODE_NAME = "image"; - private static final int MANGING = 50; - private final File baseImgDir; - - public ExporterFactory(@NotNull final ServletContext servletContext) { - this.baseImgDir = new File(servletContext.getRealPath("/")); - } - - public ExporterFactory(@NotNull final File baseImgDir) { - this.baseImgDir = baseImgDir; - } - - public void export(@NotNull ExportProperties properties, @Nullable String xml, @NotNull OutputStream output, @Nullable String mapSvg) throws ExportException, IOException { - final ExportFormat format = properties.getFormat(); - - - switch (format) { - case TEXT: { - final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.TEXT); - exporter.export(xml.getBytes(StandardCharsets.UTF_8), output); - break; - } - case MICROSOFT_EXCEL: { - final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.MICROSOFT_EXCEL); - exporter.export(xml.getBytes(StandardCharsets.UTF_8), output); - break; - } - case FREEMIND: { - final FreemindExporter exporter = new FreemindExporter(); - exporter.setVersion(new VersionNumber(properties.getVersion())); - exporter.export(xml.getBytes(StandardCharsets.UTF_8), output); - break; - } - case MINDJET: { - final Exporter exporter = XSLTExporter.create(XSLTExporter.Type.MINDJET); - exporter.export(xml.getBytes(StandardCharsets.UTF_8), output); - break; - } - default: - throw new UnsupportedOperationException("Export method not supported."); - } - - output.flush(); - output.close(); - } - - private static String domToString(@NotNull Document document) throws TransformerException { - DOMSource domSource = new DOMSource(document); - - // Create a string writer - final CharArrayWriter result = new CharArrayWriter(); - - // Create the stream stream for the transform - StreamResult stream = new StreamResult(result); - - // Create a Transformer to serialize the document - TransformerFactory tFactory = TransformerFactory.newInstance(); - - Transformer transformer = tFactory.newTransformer(); - transformer.setOutputProperty("indent", "yes"); - - // Transform the document to the stream stream - transformer.transform(domSource, stream); - - return result.toString(); - } - - private void inlineImages(@NotNull Document document, @NotNull Element element) { - - final NodeList list = element.getChildNodes(); - - for (int i = 0; i < list.getLength(); i++) { - final Node node = list.item(i); - // find all groups - if (GROUP_NODE_NAME.equals(node.getNodeName())) { - // Must continue looking .... - inlineImages(document, (Element) node); - - } else if (IMAGE_NODE_NAME.equals(node.getNodeName())) { - - Element elem = (Element) node; - - final String imgUrl = fixHref(elem); - if (!imgUrl.isEmpty() && (!imgUrl.startsWith("image/png;base64") || !imgUrl.startsWith("data:image/png;base64"))) { - elem.removeAttribute("href"); - - InputStream fis = null; - // Obtains file name ... - try { - final File iconFile = iconFile(imgUrl); - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - fis = new FileInputStream(iconFile); - Base64.Encoder enc = Base64.getEncoder(); - bos.write(enc.encode(IOUtils.toByteArray(fis))); - - elem.setAttribute("xlink:href", "data:image/png;base64," + bos.toString("8859_1")); - elem.appendChild(document.createTextNode(" ")); - - } catch (IOException e) { - e.printStackTrace(); - } finally { - close(fis); - } - } - } - } - } - - @NotNull - private String fixHref(@NotNull Element elem) { - // Fix href attribute ... - // Hack for IE: If the image is a external URL, embeed it... - String result = elem.getAttribute("href"); - if (result.isEmpty()) { - - - // Bug WISE-422: This seems to be a bug in Safari. For some reason, img add prefixed with NS1 - // values = new HashMap(); - - final User user = Utils.getUser(); - values.put("mindmap", new RestMindmap(mindMap, user)); - values.put("filename", mindMap.getTitle()); - return new ModelAndView("transformViewWise", values); - } - - @RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"application/freemind"}, params = {"download=mm"}) - @ResponseBody - public ModelAndView retrieveDocumentAsFreemind(@PathVariable int id) throws IOException, MapCouldNotFoundException { - final Mindmap mindMap = findMindmapById(id); - final Map values = new HashMap(); - values.put("content", mindMap.getXmlStr()); - values.put("filename", mindMap.getTitle()); - return new ModelAndView("transformViewFreemind", values); - } - - @RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"text/plain"}, params = {"download=txt"}) - @ResponseBody - public ModelAndView retrieveDocumentAsText(@PathVariable int id) throws IOException, MapCouldNotFoundException { - final Mindmap mindMap = findMindmapById(id); - final Map values = new HashMap(); - values.put("content", mindMap.getXmlStr()); - values.put("filename", mindMap.getTitle()); - return new ModelAndView("transformViewTxt", values); - } - - @RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"application/vnd.mindjet.mindmanager"}, params = {"download=mmap"}) - @ResponseBody - public ModelAndView retrieveDocumentAsMindJet(@PathVariable int id) throws IOException, MapCouldNotFoundException { - final Mindmap mindMap = findMindmapById(id); - final Map values = new HashMap(); - values.put("content", mindMap.getXmlStr()); - values.put("filename", mindMap.getTitle()); - return new ModelAndView("transformViewMMap", values); - } - - @RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"application/vnd.ms-excel"}, params = {"download=xls"}) - @ResponseBody - public ModelAndView retrieveDocumentAsExcel(@PathVariable int id) throws IOException, MapCouldNotFoundException { - final Mindmap mindMap = findMindmapById(id); - final Map values = new HashMap(); - values.put("content", mindMap.getXmlStr()); - values.put("filename", mindMap.getTitle()); - return new ModelAndView("transformViewXls", values); - } - - @RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"application/vnd.oasis.opendocument.text"}, params = {"download=odt"}) - @ResponseBody - public ModelAndView retrieveDocumentAsOdt(@PathVariable int id) throws IOException, MapCouldNotFoundException { - final Mindmap mindMap = findMindmapById(id); - final Map values = new HashMap(); - values.put("content", mindMap.getXmlStr()); - values.put("filename", mindMap.getTitle()); - return new ModelAndView("transformViewOdt", values); - } - @RequestMapping(method = RequestMethod.GET, value = "/maps/", produces = {"application/json", "application/xml"}) public RestMindmapList retrieveList(@RequestParam(required = false) String q) throws IOException { final User user = Utils.getUser(); diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/TransformerController.java b/wise-webapp/src/main/java/com/wisemapping/rest/TransformerController.java index 7f12becf..cc21b4f2 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/TransformerController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/TransformerController.java @@ -18,10 +18,6 @@ package com.wisemapping.rest; - -import com.wisemapping.exporter.ExportFormat; -import com.wisemapping.exporter.ExportProperties; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; @@ -30,7 +26,6 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; -import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -38,11 +33,7 @@ import java.util.Map; @Controller public class TransformerController extends BaseController { - private static final String PARAM_SVG_XML = "svgXml"; - private static final String PARAM_WISE_MAP_XML = "mapXml"; - private static final String PARAM_FILENAME = "filename"; - - @RequestMapping(method = RequestMethod.POST, value = "/transform", produces = {"application/freemind"}, consumes = {"application/xml"}) + @RequestMapping(method = RequestMethod.POST, value = "/transform", produces = {"application/freemind"}, consumes = {"application/xml"}) @ResponseBody public ModelAndView transformFreemind(@RequestBody @Nullable final String content) throws IOException { final Map values = new HashMap(); diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/view/TransformView.java b/wise-webapp/src/main/java/com/wisemapping/rest/view/TransformView.java deleted file mode 100644 index 0c510232..00000000 --- a/wise-webapp/src/main/java/com/wisemapping/rest/view/TransformView.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright [2015] [wisemapping] - * - * Licensed under WiseMapping Public License, Version 1.0 (the "License"). - * It is basically the Apache License, Version 2.0 (the "License") plus the - * "powered by wisemapping" text requirement on every single page; - * you may not use this file except in compliance with the License. - * You may obtain a copy of the license at - * - * http://www.wisemapping.org/license - * - * 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 com.wisemapping.rest.view; - -import com.wisemapping.exporter.ExportFormat; -import com.wisemapping.exporter.ExportProperties; -import com.wisemapping.exporter.ExporterFactory; -import com.wisemapping.mail.NotificationService; -import com.wisemapping.security.Utils; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.oxm.jaxb.Jaxb2Marshaller; -import org.springframework.web.servlet.view.AbstractView; - -import javax.servlet.ServletContext; -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.xml.transform.stream.StreamResult; -import java.io.UnsupportedEncodingException; -import java.util.Map; - -public class TransformView extends AbstractView { - - @NonNls - private static final String DEFAULT_ENCODING = "UTF-8"; - private final String contentType; - private final ExportFormat exportFormat; - private final NotificationService notificationService; - - @Autowired - private Jaxb2Marshaller jaxbMarshaller; - - public TransformView(@NotNull final String contentType, @NotNull NotificationService notificationService) { - this.contentType = contentType; - this.notificationService = notificationService; - this.exportFormat = ExportFormat.fromContentType(contentType); - } - - @Override - protected void renderMergedOutputModel(@NotNull Map viewMap, @NotNull HttpServletRequest request, @NotNull final HttpServletResponse response) { - - final String content = (String) viewMap.get("content"); - final String filename = (String) viewMap.get("filename"); - final String version = (String) viewMap.get("version"); - - // Build format properties ... - final ExportProperties properties = ExportProperties.create(exportFormat); - if (version != null) { - properties.setVersion(version); - } - - // Set format content type... - final String contentType = exportFormat.getContentType(); - response.setContentType(contentType); - - // Set file name...:http://stackoverflow.com/questions/5325322/java-servlet-download-filename-special-characters/13359949#13359949 - final String fileName = (filename != null ? filename : "map") + "." + exportFormat.getFileExtension(); - this.setContentDisposition(request, response, fileName); - - // Change image link URL. - final ServletContext servletContext = getServletContext(); - final ExporterFactory factory = new ExporterFactory(servletContext); - try { - // Write the conversion content ... - final ServletOutputStream outputStream = response.getOutputStream(); - if (exportFormat == ExportFormat.FREEMIND) { - response.setContentType(String.format("%s; charset=%s", contentType, "ASCII")); - factory.export(properties, content, outputStream, null); - } else if (exportFormat == ExportFormat.WISEMAPPING) { - response.setContentType(String.format("%s; charset=%s", contentType, DEFAULT_ENCODING)); - final Object mindmap = viewMap.get("mindmap"); - final StreamResult result = new StreamResult(outputStream); - jaxbMarshaller.marshal(mindmap, result); - } else if (exportFormat == ExportFormat.MICROSOFT_EXCEL || exportFormat == ExportFormat.TEXT || exportFormat == ExportFormat.MINDJET) { - - response.setContentType(String.format("%s; charset=%s", contentType, DEFAULT_ENCODING)); - factory.export(properties, content, outputStream, null); - } else { - // Image export ... - factory.export(properties, null, outputStream, content); - } - } catch (Throwable e) { - notificationService.reportJavaException(e, Utils.getUser(), content, request); - } - } - - private void setContentDisposition(HttpServletRequest request, HttpServletResponse response, String fileName) { - final String userAgent = request.getHeader("user-agent"); - boolean isInternetExplorer = (userAgent.contains("MSIE")); - - String disposition = fileName; - try { - byte[] fileNameBytes = fileName.getBytes((isInternetExplorer) ? ("windows-1250") : ("utf-8")); - final StringBuilder dispositionFileName = new StringBuilder(); - for (byte b : fileNameBytes) { - dispositionFileName.append((char) (b & 0xff)); - } - disposition = "attachment; filename=\"" + dispositionFileName + "\""; - } catch (UnsupportedEncodingException ence) { - // ... handle exception ... - } - response.setHeader("Content-disposition", disposition); - } - - @Override - public String getContentType() { - return contentType; - } -} diff --git a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml index 5c907d51..10fa4708 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml @@ -93,59 +93,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/wise-webapp/src/test/java/com/wisemapping/test/importer/FreeMindImportExportTest.java b/wise-webapp/src/test/java/com/wisemapping/test/importer/FreeMindImportExportTest.java index 26d26f50..5945d4f0 100644 --- a/wise-webapp/src/test/java/com/wisemapping/test/importer/FreeMindImportExportTest.java +++ b/wise-webapp/src/test/java/com/wisemapping/test/importer/FreeMindImportExportTest.java @@ -27,7 +27,6 @@ public class FreeMindImportExportTest { ImporterFactory exporterFactory = ImporterFactory.getInstance(); importer = exporterFactory.getImporter(ImportFormat.FREEMIND); exporter = new FreemindExporter(); - }