diff options
author | Erik Lumme <erik@vaadin.com> | 2017-09-11 14:02:42 +0300 |
---|---|---|
committer | Erik Lumme <erik@vaadin.com> | 2017-09-11 14:02:42 +0300 |
commit | 6a03ddc414dd850e30eecfcee608143a6e01cc99 (patch) | |
tree | 599dbba16d5e2a974a41599ab229c2fae10ae630 /documentation/articles/GeneratingDynamicResourcesBasedOnURIOrParameters.asciidoc | |
parent | b75136b70f6a2fa0613ebf250c3dfe426543e0f2 (diff) | |
download | vaadin-framework-6a03ddc414dd850e30eecfcee608143a6e01cc99.tar.gz vaadin-framework-6a03ddc414dd850e30eecfcee608143a6e01cc99.zip |
Migrate documentation files and change typo
Diffstat (limited to 'documentation/articles/GeneratingDynamicResourcesBasedOnURIOrParameters.asciidoc')
-rw-r--r-- | documentation/articles/GeneratingDynamicResourcesBasedOnURIOrParameters.asciidoc | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/documentation/articles/GeneratingDynamicResourcesBasedOnURIOrParameters.asciidoc b/documentation/articles/GeneratingDynamicResourcesBasedOnURIOrParameters.asciidoc new file mode 100644 index 0000000000..9cfc756bc7 --- /dev/null +++ b/documentation/articles/GeneratingDynamicResourcesBasedOnURIOrParameters.asciidoc @@ -0,0 +1,61 @@ +[[generating-dynamic-resources-based-on-uri-or-parameters]] +Generating dynamic resources based on URI or parameters +------------------------------------------------------- + +You can dynamically generate responses based on e.g. query parameters by +creating your own `RequestHandler` and registering it with the session. + +In this way, you can for instance create an image that draws a text, +given as a parameter to the image. This has been done in the example +below: + +[source,java] +.... +public class DynamicImageUI extends UI { + public static final String IMAGE_URL = "myimage.png"; + + private final RequestHandler requestHandler = new RequestHandler() { + @Override + public boolean handleRequest(VaadinSession session, + VaadinRequest request, VaadinResponse response) + throws IOException { + if (("/" + IMAGE_URL).equals(request.getPathInfo())) { + // Create an image, draw the "text" parameter to it and output + // it to the browser. + String text = request.getParameter("text"); + BufferedImage bi = new BufferedImage(100, 30, + BufferedImage.TYPE_3BYTE_BGR); + bi.getGraphics().drawChars(text.toCharArray(), 0, + text.length(), 10, 20); + response.setContentType("image/png"); + ImageIO.write(bi, "png", response.getOutputStream()); + + return true; + } + // If the URL did not match our image URL, let the other request + // handlers handle it + return false; + } + }; + + @Override + public void init(VaadinRequest request) { + Resource resource = new ExternalResource(IMAGE_URL + "?text=Hello!"); + + getSession().addRequestHandler(requestHandler); + + // Add an image using the resource + Image image = new Image("A dynamically generated image", resource); + + setContent(image); + } + + @Override + public void detach() { + super.detach(); + + // Clean up + getSession().removeRequestHandler(requestHandler); + } +} +.... |