PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/servlet-printout/src/main/java/fi/nls/oskari/printout/ws/jaxrs/format/StreamingDOCXImpl.java

https://github.com/nls-oskari/oskari-server
Java | 146 lines | 106 code | 35 blank | 5 comment | 8 complexity | e6c0ddd87921a1487250d68e48227284 MD5 | raw file
  1. package fi.nls.oskari.printout.ws.jaxrs.format;
  2. import java.awt.image.BufferedImage;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.net.URISyntaxException;
  8. import java.text.ParseException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import javax.imageio.ImageIO;
  12. import javax.ws.rs.WebApplicationException;
  13. import javax.ws.rs.core.StreamingOutput;
  14. import javax.xml.stream.FactoryConfigurationError;
  15. import javax.xml.stream.XMLStreamException;
  16. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  17. import org.apache.poi.util.Units;
  18. import org.apache.poi.xwpf.usermodel.Document;
  19. import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
  20. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  21. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  22. import org.apache.poi.xwpf.usermodel.XWPFRun;
  23. import org.geowebcache.GeoWebCacheException;
  24. import org.geowebcache.filter.request.RequestFilterException;
  25. import org.opengis.referencing.operation.TransformException;
  26. import com.vividsolutions.jts.geom.Envelope;
  27. import com.vividsolutions.jts.geom.Point;
  28. import fi.nls.oskari.printout.imaging.ScaleOps;
  29. import fi.nls.oskari.printout.input.layers.LayerDefinition;
  30. import fi.nls.oskari.printout.input.maplink.MapLink;
  31. import fi.nls.oskari.printout.output.layer.AsyncLayerProcessor;
  32. import fi.nls.oskari.printout.output.map.MapProducer;
  33. /**
  34. * support for M$ format documents - this does not work atm
  35. */
  36. public class StreamingDOCXImpl implements StreamingOutput {
  37. private MapLink mapLink;
  38. private MapProducer producer;
  39. ScaleOps scaleOps = new ScaleOps();
  40. private BufferedImage image;
  41. public StreamingDOCXImpl(MapProducer producer, MapLink mapLink) {
  42. this.producer = producer;
  43. this.mapLink = mapLink;
  44. }
  45. public void write(OutputStream outs) throws IOException,
  46. WebApplicationException {
  47. XWPFDocument doc = new XWPFDocument();
  48. XWPFParagraph p1 = doc.createParagraph();
  49. p1.setAlignment(ParagraphAlignment.CENTER);
  50. XWPFRun r1 = p1.createRun();
  51. int width = mapLink.getWidth();
  52. int height = mapLink.getHeight();
  53. ByteArrayOutputStream buf = new ByteArrayOutputStream();
  54. ImageIO.write(image, "png", buf);
  55. buf.close();
  56. byte[] bytes = buf.toByteArray();
  57. try {
  58. r1.setText("png");
  59. r1.addBreak();
  60. r1.addPicture(new ByteArrayInputStream(bytes),
  61. Document.PICTURE_TYPE_PNG, "pic.png", Units.toEMU(width),
  62. Units.toEMU(height));
  63. } catch (InvalidFormatException e) {
  64. throw new IOException(e);
  65. }
  66. // r1.addBreak(BreakType.PAGE);
  67. doc.write(outs);
  68. }
  69. public void underflow() throws ParseException, IOException,
  70. GeoWebCacheException, XMLStreamException,
  71. FactoryConfigurationError, RequestFilterException,
  72. TransformException, URISyntaxException {
  73. final List<LayerDefinition> selectedLayers = new ArrayList<LayerDefinition>();
  74. for (LayerDefinition ldef : mapLink.getMapLinkLayers()) {
  75. LayerDefinition inScale = mapLink
  76. .selectLayerDefinitionForScale(ldef);
  77. if (inScale == null) {
  78. continue;
  79. }
  80. selectedLayers.add(inScale);
  81. }
  82. Point centre = mapLink.getCentre();
  83. int zoom = mapLink.getZoom();
  84. int width = mapLink.getWidth();
  85. int height = mapLink.getHeight();
  86. Envelope env = producer.getProcessor().getEnvFromPointZoomAndExtent(
  87. centre, zoom, width, height);
  88. AsyncLayerProcessor asyncProc = new AsyncLayerProcessor();
  89. asyncProc.start();
  90. try {
  91. BufferedImage producedImage = producer.getMap(asyncProc, env, zoom,
  92. width, height, selectedLayers, MapProducer.ImageType.ARGB);
  93. String scaledWidth = mapLink.getValues().get("SCALEDWIDTH");
  94. String scaledHeight = mapLink.getValues().get("SCALEDHEIGHT");
  95. if (scaledWidth != null && scaledHeight != null) {
  96. image = scaleOps.doScaleWithFilters(producedImage,
  97. Integer.valueOf(scaledWidth, 10),
  98. Integer.valueOf(scaledHeight, 10));
  99. } else {
  100. image = producedImage;
  101. }
  102. } finally {
  103. try {
  104. asyncProc.shutdown();
  105. } catch (InterruptedException e) {
  106. // TODO Auto-generated catch block
  107. throw new IOException(e);
  108. }
  109. }
  110. }
  111. }