Search in sources :

Example 6 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_elementIsNotAttachedAndHasAttribute_elementHasAttribute.

@Test
public void setResourceAttribute_elementIsNotAttachedAndHasAttribute_elementHasAttribute() {
    Element element = ElementFactory.createDiv();
    element.setAttribute("foo", "bar");
    String resName = "resource";
    StreamResource resource = createEmptyResource(resName);
    element.setAttribute("foo", resource);
    Assert.assertTrue(element.hasAttribute("foo"));
    Assert.assertTrue(element.getAttribute("foo").endsWith(resName));
}
Also used : StreamResource(com.vaadin.flow.server.StreamResource) Element(com.vaadin.flow.dom.Element) Test(org.junit.Test)

Example 7 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_attachElement_setRawAttribute.

@Test
public void setResourceAttribute_attachElement_setRawAttribute() throws URISyntaxException, InterruptedException {
    UI ui = createUI();
    UI.setCurrent(ui);
    StreamResource resource = createEmptyResource("resource");
    Element element = ElementFactory.createDiv();
    element.setAttribute("foo", resource);
    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;
    element.setAttribute("foo", "bar");
    TestUtil.isGarbageCollected(ref);
    ui.getElement().appendChild(element);
    Assert.assertTrue(element.hasAttribute("foo"));
    Assert.assertEquals("bar", element.getAttribute("foo"));
}
Also used : UI(com.vaadin.flow.component.UI) StreamResource(com.vaadin.flow.server.StreamResource) Element(com.vaadin.flow.dom.Element) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Example 8 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_elementIsAttached_setRawAttribute.

@Test
public void setResourceAttribute_elementIsAttached_setRawAttribute() throws URISyntaxException, InterruptedException {
    UI ui = createUI();
    UI.setCurrent(ui);
    StreamResource resource = createEmptyResource("resource");
    ui.getElement().setAttribute("foo", resource);
    String uri = ui.getElement().getAttribute("foo");
    Optional<StreamResource> res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, new URI(uri));
    Assert.assertTrue(res.isPresent());
    res = null;
    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;
    ui.getElement().setAttribute("foo", "bar");
    TestUtil.isGarbageCollected(ref);
    res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, new URI(uri));
    Assert.assertFalse(res.isPresent());
    Assert.assertTrue(ui.getElement().hasAttribute("foo"));
    Assert.assertTrue(ui.getElement().getAttribute("foo").equals("bar"));
}
Also used : UI(com.vaadin.flow.component.UI) StreamResource(com.vaadin.flow.server.StreamResource) WeakReference(java.lang.ref.WeakReference) URI(java.net.URI) Test(org.junit.Test)

Example 9 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_elementIsAttached_setAnotherResource.

@Test
public void setResourceAttribute_elementIsAttached_setAnotherResource() throws URISyntaxException {
    UI ui = createUI();
    UI.setCurrent(ui);
    StreamResource resource = createEmptyResource("resource1");
    ui.getElement().setAttribute("foo", resource);
    String uri = ui.getElement().getAttribute("foo");
    Optional<StreamResource> res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, new URI(uri));
    Assert.assertTrue(res.isPresent());
    String resName = "resource2";
    ui.getElement().setAttribute("foo", createEmptyResource(resName));
    res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, new URI(uri));
    Assert.assertFalse(res.isPresent());
    Assert.assertTrue(ui.getElement().hasAttribute("foo"));
    Assert.assertTrue(ui.getElement().getAttribute("foo").endsWith(resName));
}
Also used : UI(com.vaadin.flow.component.UI) StreamResource(com.vaadin.flow.server.StreamResource) URI(java.net.URI) Test(org.junit.Test)

Example 10 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class StreamRequestHandler method handleRequest.

@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    String pathInfo = request.getPathInfo();
    if (pathInfo == null) {
        return false;
    }
    // remove leading '/'
    assert pathInfo.startsWith(Character.toString(PATH_SEPARATOR));
    pathInfo = pathInfo.substring(1);
    if (!pathInfo.startsWith(DYN_RES_PREFIX)) {
        return false;
    }
    Optional<AbstractStreamResource> abstractStreamResource;
    session.lock();
    try {
        abstractStreamResource = StreamRequestHandler.getPathUri(pathInfo).flatMap(session.getResourceRegistry()::getResource);
        if (!abstractStreamResource.isPresent()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource is not found for path=" + pathInfo);
            return true;
        }
    } finally {
        session.unlock();
    }
    if (abstractStreamResource.isPresent()) {
        AbstractStreamResource resource = abstractStreamResource.get();
        if (resource instanceof StreamResource) {
            resourceHandler.handleRequest(session, request, response, (StreamResource) resource);
        } else if (resource instanceof StreamReceiver) {
            StreamReceiver streamReceiver = (StreamReceiver) resource;
            String[] parts = parsePath(pathInfo);
            receiverHandler.handleRequest(session, request, response, streamReceiver, parts[0], parts[1]);
        } else {
            getLogger().warn("Received unknown stream resource.");
        }
    }
    return true;
}
Also used : AbstractStreamResource(com.vaadin.flow.server.AbstractStreamResource) StreamResource(com.vaadin.flow.server.StreamResource) StreamReceiver(com.vaadin.flow.server.StreamReceiver) AbstractStreamResource(com.vaadin.flow.server.AbstractStreamResource)

Aggregations

StreamResource (com.vaadin.flow.server.StreamResource)26 Test (org.junit.Test)24 UI (com.vaadin.flow.component.UI)14 Element (com.vaadin.flow.dom.Element)13 URI (java.net.URI)12 WeakReference (java.lang.ref.WeakReference)9 StreamRegistration (com.vaadin.flow.server.StreamRegistration)4 StreamResourceRegistry (com.vaadin.flow.server.StreamResourceRegistry)4 Anchor (com.vaadin.flow.component.html.Anchor)1 ContentTypeResolver (com.vaadin.flow.function.ContentTypeResolver)1 AbstractStreamResource (com.vaadin.flow.server.AbstractStreamResource)1 StreamReceiver (com.vaadin.flow.server.StreamReceiver)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1