- Remove old ExportController

- Add support for simple REST transformation
This commit is contained in:
Paulo Gustavo Veiga
2012-02-19 21:07:24 -03:00
parent e033e8552c
commit 81538f049b
11 changed files with 115 additions and 422 deletions

View File

@@ -4,9 +4,8 @@ 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.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;
@@ -16,21 +15,18 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class MindmapController {
@Autowired
private MindmapService mindmapService;
@RequestMapping(method = RequestMethod.GET, value = "/map/{id}")
@RequestMapping(method = RequestMethod.GET, value = "/map/{id}", produces = {"text/xml", "application/json", "text/html"})
@ResponseBody
public ModelAndView getMindmap(@PathVariable int id) throws IOException {
final MindMap mindMap = mindmapService.getMindmapById(id);
final RestMindMap map = new RestMindMap(mindMap);
final RestMindmap map = new RestMindmap(mindMap);
return new ModelAndView("mapView", "map", map);
}
@@ -39,7 +35,7 @@ public class MindmapController {
final User user = com.wisemapping.security.Utils.getUser();
final List<MindmapUser> list = mindmapService.getMindmapUserByUser(user);
// final RestMindMap map = new RestMindMap(mindMap);
// final RestMindMap map = new RestMindmap(mindMap);
// return new ModelAndView("mapView", "map", map);
return null;
}

View File

@@ -1,12 +1,16 @@
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.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@@ -14,6 +18,10 @@ import java.util.Map;
@Controller
public class TransformerController {
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/pdf"}, consumes = {"image/svg+xml"})
@ResponseBody
public ModelAndView transformPdf(@RequestBody @Nullable final String content) throws IOException {
@@ -26,6 +34,18 @@ public class TransformerController {
return new ModelAndView("transformViewPdf", values);
}
@RequestMapping(method = RequestMethod.POST, value = "/transform", produces = {"image/svg+xml"}, consumes = {"image/svg+xml"})
@ResponseBody
public ModelAndView transformSvg(@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("transformViewSvg", 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 {
@@ -47,7 +67,7 @@ public class TransformerController {
}
values.put("content", content);
values.put("imageSize", ExportProperties.ImageProperties.Size.LARGE);
return new ModelAndView("transformViewJpg", values);
return new ModelAndView("transformViewJpeg", values);
}
@RequestMapping(method = RequestMethod.POST, value = "/transform", produces = {"application/freemind"}, consumes = {"text/xml"})
@@ -60,4 +80,42 @@ public class TransformerController {
values.put("content", content);
return new ModelAndView("transformViewFreemind", values);
}
@RequestMapping(method = RequestMethod.POST, value = "/transform", consumes = {"application/x-www-form-urlencoded"})
public ModelAndView transform(@NotNull HttpServletRequest request,
@NotNull HttpServletResponse response) throws IOException {
final String svg = request.getParameter(PARAM_SVG_XML);
final String mapXml = request.getParameter(PARAM_WISE_MAP_XML);
final String filename = request.getParameter(PARAM_FILENAME);
// Obtains transformation type based on the last part of the URL ...
final String requestURI = request.getRequestURI();
final String format = requestURI.substring(requestURI.lastIndexOf(".") + 1, requestURI.length());
final ExportFormat exportFormat = ExportFormat.valueOf(format.toUpperCase());
ModelAndView result;
switch (exportFormat) {
case PNG:
result = this.transformPng(svg);
break;
case JPEG:
result = this.transformJpeg(svg);
break;
case PDF:
result = this.transformPdf(svg);
break;
case SVG:
result = this.transformSvg(svg);
break;
case FREEMIND:
result = this.transformFreemind(mapXml);
break;
default:
throw new IllegalArgumentException("Unsupported export format");
}
result.getModelMap().put("filename", filename);
return result;
}
}

View File

@@ -17,17 +17,17 @@ import java.util.Date;
@XmlRootElement(name = "map")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class RestMindMap {
public class RestMindmap {
@JsonIgnore
private MindMap mindmap;
public RestMindMap() {
public RestMindmap() {
this(null);
}
public RestMindMap(@NotNull MindMap mindmap) {
public RestMindmap(@NotNull MindMap mindmap) {
this.mindmap = mindmap;
}

View File

@@ -28,6 +28,7 @@ public class TransformView extends AbstractView {
protected void renderMergedOutputModel(@NotNull Map<String, Object> viewMap, @NotNull HttpServletRequest request, @NotNull final HttpServletResponse response) throws Exception {
final String content = (String) viewMap.get("content");
final String filename = (String) viewMap.get("filename");
// Build format properties ...
final ExportProperties properties = ExportProperties.create(exportFormat);
@@ -57,7 +58,7 @@ public class TransformView extends AbstractView {
response.setContentType(contentType);
// Set file name...
final String fileName = "map" + "." + exportFormat.getFileExtension();
final String fileName = (filename != null ? filename : "map" + ".") + exportFormat.getFileExtension();
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
// Write content ...