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));
}
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"));
}
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"));
}
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));
}
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;
}
Aggregations