aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/minitutorials/v7a1/DynamicImageUI.java
blob: 632eda24911b954b81f7baf319fefee7774cd22a (plain)
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
package com.vaadin.tests.minitutorials.v7a1;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import com.vaadin.server.ExternalResource;
import com.vaadin.server.RequestHandler;
import com.vaadin.server.VaadinSession;
import com.vaadin.server.WrappedRequest;
import com.vaadin.server.WrappedResponse;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Embedded;

public class DynamicImageUI extends AbstractTestUI {

    @Override
    public void setup(WrappedRequest request) {
        // Add the request handler that handles our dynamic image
        getSession().addRequestHandler(new DynamicImageRequestHandler());

        // Create a URL that we can handle in DynamicImageRequestHandler
        URL imageUrl;
        try {
            imageUrl = new URL(getSession().getURL(),
                    DynamicImageRequestHandler.IMAGE_URL + "?text=Hello!");
        } catch (MalformedURLException e) {
            // This should never happen
            throw new RuntimeException(e);
        }

        // Add an embedded using the created URL
        Embedded embedded = new Embedded("A dynamically generated image",
                new ExternalResource(imageUrl));
        embedded.setType(Embedded.TYPE_IMAGE);
        getContent().addComponent(embedded);

    }

    @Override
    protected String getTestDescription() {
        return "Mini tutorial for https://vaadin.com/wiki/-/wiki/Main/Generating%20dynamic%20resources%20based%20on%20URI%20or%20parameters";
    }

    @Override
    protected Integer getTicketNumber() {
        return null;
    }
}

class DynamicImageRequestHandler implements RequestHandler {

    public static final String IMAGE_URL = "myimage.png";

    @Override
    public boolean handleRequest(VaadinSession application,
            WrappedRequest request, WrappedResponse response)
            throws IOException {
        String pathInfo = request.getRequestPathInfo();
        if (("/" + IMAGE_URL).equals(pathInfo)) {
            // 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;
    }
}