aboutsummaryrefslogtreecommitdiffstats
path: root/tests/testbench/com/vaadin/tests/resources/ConnectorResourceTest.java
blob: c8d711f0b2aebf1d79820ea7f104bc7b91587825 (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
package com.vaadin.tests.resources;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.vaadin.server.DynamicConnectorResource;
import com.vaadin.server.WrappedRequest;
import com.vaadin.server.WrappedResponse;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Embedded;

public class ConnectorResourceTest extends AbstractTestUI {

    private static final String DYNAMIC_IMAGE_NAME = "requestImage.png";

    @Override
    protected void setup(WrappedRequest request) {
        addComponent(new Embedded(DYNAMIC_IMAGE_NAME,
                new DynamicConnectorResource(this, DYNAMIC_IMAGE_NAME)));
        addComponent(new Embedded("Dynamic text", new DynamicConnectorResource(
                this, DYNAMIC_IMAGE_NAME, new HashMap<String, String>() {
                    {
                        put("text", "Dynamic%20text");
                    }
                })));
    }

    @Override
    protected String getTestDescription() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Integer getTicketNumber() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean handleConnectorRequest(WrappedRequest request,
            WrappedResponse response, String path) throws IOException {
        if (DYNAMIC_IMAGE_NAME.equals(path)) {
            // Create an image, draw the "text" parameter to it and output it to
            // the browser.
            String text = request.getParameter("text");
            if (text == null) {
                text = DYNAMIC_IMAGE_NAME;
            }
            BufferedImage bi = getImage(text);
            response.setContentType("image/png");
            ImageIO.write(bi, "png", response.getOutputStream());

            return true;
        } else {
            return super.handleConnectorRequest(request, response, path);
        }
    }

    private BufferedImage getImage(String text) {
        BufferedImage bi = new BufferedImage(150, 30,
                BufferedImage.TYPE_3BYTE_BGR);
        bi.getGraphics()
                .drawChars(text.toCharArray(), 0, text.length(), 10, 20);
        return bi;
    }

}