PageRenderTime 84ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/nls-oskari/oskari-server
Java | 160 lines | 95 code | 27 blank | 38 comment | 8 complexity | e9cd50395eb04e2236447d20d7ac7ccb MD5 | raw file
  1. package fi.nls.oskari.printout.ws.jaxrs.format;
  2. import java.awt.Rectangle;
  3. import java.awt.image.BufferedImage;
  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.xslf.usermodel.XMLSlideShow;
  17. import org.apache.poi.xslf.usermodel.XSLFPictureData;
  18. import org.apache.poi.xslf.usermodel.XSLFPictureShape;
  19. import org.apache.poi.xslf.usermodel.XSLFSlide;
  20. import org.geowebcache.GeoWebCacheException;
  21. import org.geowebcache.filter.request.RequestFilterException;
  22. import org.opengis.referencing.operation.TransformException;
  23. import com.vividsolutions.jts.geom.Envelope;
  24. import com.vividsolutions.jts.geom.Point;
  25. import fi.nls.oskari.printout.imaging.ScaleOps;
  26. import fi.nls.oskari.printout.input.layers.LayerDefinition;
  27. import fi.nls.oskari.printout.input.maplink.MapLink;
  28. import fi.nls.oskari.printout.output.layer.AsyncLayerProcessor;
  29. import fi.nls.oskari.printout.output.map.MapProducer;
  30. /**
  31. * Outputs and creates a PPTX slideshow with an embedded map image as a result for
  32. * JAX-RS map image request.
  33. */
  34. public class StreamingPPTXImpl implements StreamingOutput {
  35. ScaleOps scaleOps = new ScaleOps();
  36. private MapProducer producer;
  37. private MapLink mapLink;
  38. private BufferedImage image;
  39. public StreamingPPTXImpl(MapProducer producer, MapLink mapLink) {
  40. this.producer = producer;
  41. this.mapLink = mapLink;
  42. }
  43. public void write(OutputStream outs) throws IOException,
  44. WebApplicationException {
  45. XMLSlideShow ppt = new XMLSlideShow();
  46. XSLFSlide slide = ppt.createSlide();
  47. /*
  48. * XSLFTextBox shape = slide.createTextBox(); shape.setAnchor(new
  49. * Rectangle(50, 50, 400, 200));
  50. *
  51. * XSLFTextParagraph p1 = shape.addNewTextParagraph(); p1.setLevel(0);
  52. * p1.setBullet(true); XSLFTextRun r1 = p1.addNewTextRun();
  53. * r1.setText("Bullet1");
  54. *
  55. * XSLFTextParagraph p2 = shape.addNewTextParagraph(); // indentation
  56. * before text p2.setLeftMargin(60); // the bullet is set 40 pt before
  57. * the text p2.setIndent(-40); p2.setBullet(true); // customize bullets
  58. * p2.setBulletFontColor(Color.red); p2.setBulletFont("Wingdings");
  59. * p2.setBulletCharacter("\u0075"); p2.setLevel(1); XSLFTextRun r2 =
  60. * p2.addNewTextRun(); r2.setText("Bullet2");
  61. *
  62. * // the next three paragraphs form an auto-numbered list
  63. * XSLFTextParagraph p3 = shape.addNewTextParagraph();
  64. * p3.setBulletAutoNumber(ListAutoNumber.ALPHA_LC_PARENT_R, 1);
  65. * p3.setLevel(2); XSLFTextRun r3 = p3.addNewTextRun();
  66. * r3.setText("Numbered List Item - 1");
  67. *
  68. * XSLFTextParagraph p4 = shape.addNewTextParagraph();
  69. * p4.setBulletAutoNumber(ListAutoNumber.ALPHA_LC_PARENT_R, 2);
  70. * p4.setLevel(2); XSLFTextRun r4 = p4.addNewTextRun();
  71. * r4.setText("Numbered List Item - 2");
  72. *
  73. * XSLFTextParagraph p5 = shape.addNewTextParagraph();
  74. * p5.setBulletAutoNumber(ListAutoNumber.ALPHA_LC_PARENT_R, 3);
  75. * p5.setLevel(2); XSLFTextRun r5 = p5.addNewTextRun();
  76. * r5.setText("Numbered List Item - 3");
  77. *
  78. * shape.resizeToFitText();
  79. */
  80. ByteArrayOutputStream buf = new ByteArrayOutputStream();
  81. ImageIO.write(image, "png", buf);
  82. buf.close();
  83. byte[] data = buf.toByteArray();
  84. int pictureIndex = ppt.addPicture(data,
  85. XSLFPictureData.PICTURE_TYPE_PNG);
  86. XSLFPictureShape shape = slide.createPicture(pictureIndex);
  87. shape.setAnchor(new Rectangle(5, 5, mapLink.getWidth(), mapLink
  88. .getHeight()));
  89. ppt.write(outs);
  90. }
  91. public void underflow() throws ParseException, IOException,
  92. GeoWebCacheException, XMLStreamException,
  93. FactoryConfigurationError, RequestFilterException,
  94. TransformException, URISyntaxException {
  95. final List<LayerDefinition> selectedLayers = new ArrayList<LayerDefinition>();
  96. for (LayerDefinition ldef : mapLink.getMapLinkLayers()) {
  97. LayerDefinition inScale = mapLink
  98. .selectLayerDefinitionForScale(ldef);
  99. if (inScale == null) {
  100. continue;
  101. }
  102. selectedLayers.add(inScale);
  103. }
  104. Point centre = mapLink.getCentre();
  105. int zoom = mapLink.getZoom();
  106. int width = mapLink.getWidth();
  107. int height = mapLink.getHeight();
  108. Envelope env = producer.getProcessor().getEnvFromPointZoomAndExtent(
  109. centre, zoom, width, height);
  110. AsyncLayerProcessor asyncProc = new AsyncLayerProcessor();
  111. asyncProc.start();
  112. try {
  113. BufferedImage producedImage = producer.getMap(asyncProc, env, zoom,
  114. width, height, selectedLayers, MapProducer.ImageType.ARGB);
  115. String scaledWidth = mapLink.getValues().get("SCALEDWIDTH");
  116. String scaledHeight = mapLink.getValues().get("SCALEDHEIGHT");
  117. if (scaledWidth != null && scaledHeight != null) {
  118. image = scaleOps.doScaleWithFilters(producedImage,
  119. Integer.valueOf(scaledWidth, 10),
  120. Integer.valueOf(scaledHeight, 10));
  121. } else {
  122. image = producedImage;
  123. }
  124. } finally {
  125. try {
  126. asyncProc.shutdown();
  127. } catch (InterruptedException e) {
  128. // TODO Auto-generated catch block
  129. throw new IOException(e);
  130. }
  131. }
  132. }
  133. }