--- /dev/null
+package com.itmill.toolkit.tests.tickets;\r
+\r
+import java.awt.Color;\r
+import java.awt.Graphics;\r
+import java.awt.image.BufferedImage;\r
+import java.io.ByteArrayInputStream;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
+import java.net.URL;\r
+import java.util.Date;\r
+\r
+import javax.imageio.ImageIO;\r
+\r
+import com.itmill.toolkit.Application;\r
+import com.itmill.toolkit.terminal.DownloadStream;\r
+import com.itmill.toolkit.terminal.ExternalResource;\r
+import com.itmill.toolkit.terminal.URIHandler;\r
+import com.itmill.toolkit.ui.Link;\r
+import com.itmill.toolkit.ui.Window;\r
+\r
+public class Ticket1589 extends Application {\r
+\r
+ public void init() {\r
+ Window w = new Window(getClass().getSimpleName());\r
+ setMainWindow(w);\r
+\r
+ MyDynamicResource res = new MyDynamicResource();\r
+\r
+ w.addURIHandler(res);\r
+\r
+ w\r
+ .addComponent(new Link(\r
+ "Test (without Content-Disposition, should suggest generatedFile.png when saving, browser default for actual disposition)",\r
+ new ExternalResource("myresource")));\r
+\r
+ w\r
+ .addComponent(new Link(\r
+ "Test (with Content-Disposition, should popup download dialog that suggests filename downloadedPNG.png)",\r
+ new ExternalResource("myresource_download")));\r
+ }\r
+}\r
+\r
+class MyDynamicResource implements URIHandler {\r
+ String textToDisplay = (new Date()).toString();\r
+\r
+ /**\r
+ * Provides the dynamic resource if the URI matches the resource URI. The\r
+ * matching URI is "/myresource" under the application URI context.\r
+ * \r
+ * Returns null if the URI does not match. Otherwise returns a download\r
+ * stream that contains the response from the server.\r
+ */\r
+ public DownloadStream handleURI(URL context, String relativeUri) {\r
+ // Catch the given URI that identifies the resource, otherwise let other\r
+ // URI handlers or the Application to handle the response.\r
+ if (!relativeUri.startsWith("myresource")) {\r
+ return null;\r
+ }\r
+\r
+ // Create an image and draw some background on it.\r
+ BufferedImage image = new BufferedImage(200, 200,\r
+ BufferedImage.TYPE_INT_RGB);\r
+ Graphics drawable = image.getGraphics();\r
+ drawable.setColor(Color.lightGray);\r
+ drawable.fillRect(0, 0, 200, 200);\r
+ drawable.setColor(Color.yellow);\r
+ drawable.fillOval(25, 25, 150, 150);\r
+ drawable.setColor(Color.blue);\r
+ drawable.drawRect(0, 0, 199, 199);\r
+\r
+ // Use the parameter to create dynamic content.\r
+ drawable.setColor(Color.black);\r
+ drawable.drawString("Time: " + textToDisplay, 75, 100);\r
+\r
+ try {\r
+ // Write the image to a buffer.\r
+ ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();\r
+ ImageIO.write(image, "png", imagebuffer);\r
+\r
+ // Return a stream from the buffer.\r
+ ByteArrayInputStream istream = new ByteArrayInputStream(imagebuffer\r
+ .toByteArray());\r
+ DownloadStream downloadStream = new DownloadStream(istream,\r
+ "image/png", "generatedFile.png");\r
+\r
+ if (relativeUri.startsWith("myresource_download")) {\r
+ downloadStream.setParameter("Content-Disposition",\r
+ "attachment; filename=\"downloadedPNG.png\"");\r
+ }\r
+ return downloadStream;\r
+ } catch (IOException e) {\r
+ return null;\r
+ }\r
+ }\r
+}
\ No newline at end of file