1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package com.vaadin.tests.tickets;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import com.vaadin.Application;
import com.vaadin.server.DownloadStream;
import com.vaadin.server.ExternalResource;
import com.vaadin.server.RequestHandler;
import com.vaadin.server.WrappedRequest;
import com.vaadin.server.WrappedResponse;
import com.vaadin.ui.Link;
import com.vaadin.ui.UI.LegacyWindow;
public class Ticket1589 extends Application.LegacyApplication {
@Override
public void init() {
LegacyWindow w = new LegacyWindow(getClass().getSimpleName());
setMainWindow(w);
MyDynamicResource res = new MyDynamicResource();
Application.getCurrent().addRequestHandler(res);
w.addComponent(new Link(
"Test (without Content-Disposition, should suggest generatedFile.png when saving, browser default for actual disposition)",
new ExternalResource("myresource")));
w.addComponent(new Link(
"Test (with Content-Disposition, should popup download dialog that suggests filename downloadedPNG.png)",
new ExternalResource("myresource_download")));
}
}
class MyDynamicResource implements RequestHandler {
String textToDisplay = (new Date()).toString();
/**
* Provides the dynamic resource if the URI matches the resource URI. The
* matching URI is "/myresource" under the application URI context.
*
* Returns null if the URI does not match. Otherwise returns a download
* stream that contains the response from the server.
*/
@Override
public boolean handleRequest(Application application,
WrappedRequest request, WrappedResponse response)
throws IOException {
String relativeUri = request.getRequestPathInfo();
// Catch the given URI that identifies the resource, otherwise let other
// URI handlers or the Application to handle the response.
if (!relativeUri.startsWith("myresource")) {
return false;
}
// Create an image and draw some background on it.
BufferedImage image = new BufferedImage(200, 200,
BufferedImage.TYPE_INT_RGB);
Graphics drawable = image.getGraphics();
drawable.setColor(Color.lightGray);
drawable.fillRect(0, 0, 200, 200);
drawable.setColor(Color.yellow);
drawable.fillOval(25, 25, 150, 150);
drawable.setColor(Color.blue);
drawable.drawRect(0, 0, 199, 199);
// Use the parameter to create dynamic content.
drawable.setColor(Color.black);
drawable.drawString("Time: " + textToDisplay, 75, 100);
try {
// Write the image to a buffer.
ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();
ImageIO.write(image, "png", imagebuffer);
// Return a stream from the buffer.
ByteArrayInputStream istream = new ByteArrayInputStream(
imagebuffer.toByteArray());
DownloadStream downloadStream = new DownloadStream(istream,
"image/png", "generatedFile.png");
if (relativeUri.startsWith("myresource_download")) {
downloadStream.setParameter("Content-Disposition",
"attachment; filename=\"downloadedPNG.png\"");
}
downloadStream.writeResponse(request, response);
return true;
} catch (IOException e) {
return false;
}
}
}
|