- REST PDF/PNG/Freemind transform working...

This commit is contained in:
Paulo Gustavo Veiga 2012-02-17 10:42:20 -03:00
parent a786c0de09
commit 6edb3d46a5
8 changed files with 883 additions and 567 deletions

View File

@ -102,7 +102,7 @@ public class ExportController extends BaseMultiActionController {
// Change image link URL.
setBaseBaseImgUrl(format, properties);
ExporterFactory.export(properties, mindMap, bos, mapSvg);
ExporterFactory.export(properties, mindMap.getUnzippedXml(), bos, mapSvg);
// If the export goes ok, write the map to the stream ...

View File

@ -18,13 +18,14 @@
package com.wisemapping.exporter;
import org.jetbrains.annotations.NotNull;
public enum ExportFormat {
SVG("image/svg+xml", "svg"),
JPEG("image/jpeg", "jpg"),
PNG("image/png", "png"),
MINDJET("text/xml", "xml"),
PDF("application/pdf", "pdf"),
FREEMIND("text/xml", "mm");
FREEMIND("application/freemind", "mm");
private String contentType;
private String fileExtension;
@ -41,4 +42,14 @@ public enum ExportFormat {
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);
}
}

View File

@ -55,7 +55,7 @@ public class ExporterFactory {
}
public static void export(@NotNull ExportProperties properties, @Nullable MindMap map, @NotNull OutputStream output, @NotNull String mapSvg) throws TranscoderException, IOException, ParserConfigurationException, SAXException, XMLStreamException, TransformerException, JAXBException, ExportException {
public static void export(@NotNull ExportProperties properties, @NotNull String xml, @NotNull OutputStream output, @NotNull String mapSvg) throws TranscoderException, IOException, ParserConfigurationException, SAXException, XMLStreamException, TransformerException, JAXBException, ExportException {
final ExportFormat format = properties.getFormat();
final String imgPath = properties.getBaseImgPath();
@ -122,7 +122,7 @@ public class ExporterFactory {
}
case FREEMIND: {
final FreemindExporter exporter = new FreemindExporter();
exporter.export(map.getUnzippedXml().getBytes(), output);
exporter.export(xml.getBytes(), output);
break;
}
default:

View File

@ -2,8 +2,11 @@ package com.wisemapping.rest;
import com.wisemapping.model.MindMap;
import com.wisemapping.model.MindmapUser;
import com.wisemapping.model.User;
import com.wisemapping.rest.model.RestMindMap;
import com.wisemapping.service.MindmapService;
import com.wisemapping.validator.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
@ -14,20 +17,30 @@ import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/map")
public class MindmapController {
@Autowired
private MindmapService mindmapService;
@RequestMapping(method = RequestMethod.GET, value = "/{id}")
@RequestMapping(method = RequestMethod.GET, value = "/map/{id}")
@ResponseBody
public ModelAndView getMindmap(@PathVariable int id) throws IOException {
final MindMap mindMap = mindmapService.getMindmapById(id);
final RestMindMap map = new RestMindMap(mindMap);
return new ModelAndView("mapView", "map", map);
}
@RequestMapping(method = RequestMethod.GET, value = "/maps")
public ModelAndView getMindmaps() throws IOException {
final User user = com.wisemapping.security.Utils.getUser();
final List<MindmapUser> list = mindmapService.getMindmapUserByUser(user);
// final RestMindMap map = new RestMindMap(mindMap);
// return new ModelAndView("mapView", "map", map);
return null;
}
}

View File

@ -0,0 +1,63 @@
package com.wisemapping.rest;
import com.wisemapping.exporter.ExportProperties;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Controller
public class TransformerController {
@RequestMapping(method = RequestMethod.POST, value = "/transform", produces = {"application/pdf"}, consumes = {"image/svg+xml"})
@ResponseBody
public ModelAndView transformPdf(@RequestBody @Nullable final String content) throws IOException {
final Map<String, Object> values = new HashMap<String, Object>();
if (content == null || content.length() == 0) {
throw new IllegalArgumentException("Body can not be null.");
}
values.put("content", content);
return new ModelAndView("transformViewPdf", values);
}
@RequestMapping(method = RequestMethod.POST, value = "/transform", produces = {"image/png"}, consumes = {"image/svg+xml"})
@ResponseBody
public ModelAndView transformPng(@RequestBody @Nullable final String content) throws IOException {
final Map<String, Object> values = new HashMap<String, Object>();
if (content == null || content.length() == 0) {
throw new IllegalArgumentException("Body can not be null.");
}
values.put("content", content);
values.put("imageSize", ExportProperties.ImageProperties.Size.LARGE);
return new ModelAndView("transformViewPng", values);
}
@RequestMapping(method = RequestMethod.POST, value = "/transform", produces = {"image/jpeg"}, consumes = {"image/svg+xml"})
@ResponseBody
public ModelAndView transformJpeg(@RequestBody @Nullable final String content) throws IOException {
final Map<String, Object> values = new HashMap<String, Object>();
if (content == null || content.length() == 0) {
throw new IllegalArgumentException("Body can not be null.");
}
values.put("content", content);
values.put("imageSize", ExportProperties.ImageProperties.Size.LARGE);
return new ModelAndView("transformViewJpg", values);
}
@RequestMapping(method = RequestMethod.POST, value = "/transform", produces = {"application/freemind"}, consumes = {"text/xml"})
@ResponseBody
public ModelAndView transformFreemind(@RequestBody @Nullable final String content) throws IOException {
final Map<String, Object> values = new HashMap<String, Object>();
if (content == null || content.length() == 0) {
throw new IllegalArgumentException("Body can not be null.");
}
values.put("content", content);
return new ModelAndView("transformViewFreemind", values);
}
}

View File

@ -0,0 +1,123 @@
package com.wisemapping.rest.model;
import com.wisemapping.model.MindMap;
import com.wisemapping.model.User;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.jetbrains.annotations.NotNull;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
@XmlRootElement(name = "map")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class RestMindMap {
@JsonIgnore
private MindMap mindmap;
public RestMindMap() {
this(null);
}
public RestMindMap(@NotNull MindMap mindmap) {
this.mindmap = mindmap;
}
public String getOwner() {
return mindmap.getOwner().getUsername();
}
public Calendar getCreationTime() {
return mindmap.getCreationTime();
}
public String getDescription() {
return mindmap.getDescription();
}
public String getTags() {
return mindmap.getTags();
}
public String getTitle() {
return mindmap.getTitle();
}
public int getId() {
return mindmap.getId();
}
public String getCreator() {
return mindmap.getCreator();
}
public String getLastModifierUser() {
return mindmap.getLastModifierUser();
}
public Date getLastModificationDate() {
return mindmap.getLastModificationDate();
}
public boolean isPublic() {
return mindmap.isPublic();
}
public String getXml() throws IOException {
return mindmap.getNativeXml();
}
public void setXml(String xml) throws IOException {
mindmap.setNativeXml(xml);
}
public void setId(int id) {
mindmap.setId(id);
}
public void setTitle(String title) {
mindmap.setTitle(title);
}
public void setTags(String tags) {
mindmap.setTags(tags);
}
public void setDescription(String description) {
mindmap.setDescription(description);
}
public void setOwner(User owner) {
mindmap.setOwner(owner);
}
public void setCreator(String creatorUser) {
mindmap.setCreator(creatorUser);
}
public void setProperties(String properties) {
mindmap.setProperties(properties);
}
public void setLastModificationTime(Calendar lastModificationTime) {
mindmap.setLastModificationTime(lastModificationTime);
}
public void setLastModifierUser(String lastModifierUser) {
mindmap.setLastModifierUser(lastModifierUser);
}
@JsonIgnore
public MindMap getDelegated() {
return this.mindmap;
}
}

View File

@ -0,0 +1,88 @@
package com.wisemapping.rest.view;
import com.wisemapping.exporter.ExportFormat;
import com.wisemapping.exporter.ExportProperties;
import com.wisemapping.exporter.ExporterFactory;
import org.jetbrains.annotations.NotNull;
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 java.io.ByteArrayOutputStream;
import java.util.Map;
public class TransformView extends AbstractView {
private String contentType;
private ExportFormat exportFormat;
public TransformView(@NotNull final String contentType) {
this.contentType = contentType;
this.exportFormat = ExportFormat.fromContentType(contentType);
}
@Override
protected void renderMergedOutputModel(@NotNull Map<String, Object> viewMap, @NotNull HttpServletRequest request, @NotNull final HttpServletResponse response) throws Exception {
final String content = (String) viewMap.get("content");
// Build format properties ...
final ExportProperties properties = ExportProperties.create(exportFormat);
if (properties instanceof ExportProperties.ImageProperties) {
final String sizeStr = request.getParameter(IMG_SIZE_PARAMETER);
final ExportProperties.ImageProperties imageProperties = (ExportProperties.ImageProperties) properties;
if (sizeStr != null) {
final ExportProperties.ImageProperties.Size size = ExportProperties.ImageProperties.Size.valueOf(sizeStr);
imageProperties.setSize(size);
} else {
imageProperties.setSize(ExportProperties.ImageProperties.Size.LARGE);
}
}
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
// Change image link URL.
setBaseBaseImgUrl(exportFormat, properties);
if (exportFormat == ExportFormat.FREEMIND) {
ExporterFactory.export(properties, content, bos, null);
} else {
ExporterFactory.export(properties, null, bos, content);
}
// Set format content type...
final String contentType = exportFormat.getContentType();
response.setContentType(contentType);
// Set file name...
final String fileName = "map" + "." + exportFormat.getFileExtension();
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
// Write content ...
final ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(bos.toByteArray());
}
@Override
public String getContentType() {
return contentType;
}
private void setBaseBaseImgUrl(@NotNull ExportFormat format, @NotNull ExportProperties properties) {
final String baseUrl;
if (format == ExportFormat.SVG) {
baseUrl = "http://www.wisemapping.com/images";
} else {
final ServletContext servletContext = this.getServletContext();
baseUrl = "file://" + servletContext.getRealPath("/icons/") + "/";
}
properties.setBaseImagePath(baseUrl);
}
private static final String IMG_SIZE_PARAMETER = "imgSize";
}

View File

@ -28,21 +28,22 @@
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="defaultContentType" value="application/json"/>
<property name="favorPathExtension" value="true"/>
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml"/>
<entry key="xml" value="text/xml"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
<entry key="pdf" value="application/pdf"/>
<entry key="png" value="application/pdf"/>
<entry key="png" value="application/png"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp-rest/"/>
<property name="suffix" value=".jsp"/>
@ -59,4 +60,21 @@
</property>
</bean>
<bean id="transformViewPdf" class="com.wisemapping.rest.view.TransformView">
<constructor-arg value="application/pdf"/>
</bean>
<bean id="transformViewPng" class="com.wisemapping.rest.view.TransformView">
<constructor-arg value="image/png"/>
</bean>
<bean id="transformViewJpeg" class="com.wisemapping.rest.view.TransformView">
<constructor-arg value="image/jpeg"/>
</bean>
<bean id="transformViewFreemind" class="com.wisemapping.rest.view.TransformView">
<constructor-arg value="application/freemind"/>
</bean>
</beans>