Search in sources :

Example 1 with AbstractStreamResource

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

the class AnchorTest method disabledAnchor_setResource_hrefIsRemoved_enableAnchor_hrefIsRestored.

@Test
public void disabledAnchor_setResource_hrefIsRemoved_enableAnchor_hrefIsRestored() {
    mockUI();
    AbstractStreamResource resource = new AbstractStreamResource() {

        @Override
        public String getName() {
            return "foo";
        }
    };
    Anchor anchor = new Anchor(resource, "bar");
    String href = anchor.getHref();
    anchor.setEnabled(false);
    Assert.assertFalse(anchor.getElement().hasAttribute("href"));
    Assert.assertEquals(href, anchor.getHref());
    anchor.setEnabled(true);
    Assert.assertEquals(href, anchor.getHref());
}
Also used : AbstractStreamResource(com.vaadin.flow.server.AbstractStreamResource) Test(org.junit.Test)

Example 2 with AbstractStreamResource

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

the class AnchorTest method disabledAnchor_setResourceWhenDisabled_enableAnchor_resourceIsPreserved.

@Test
public void disabledAnchor_setResourceWhenDisabled_enableAnchor_resourceIsPreserved() {
    Anchor anchor = new Anchor("foo", "bar");
    anchor.setEnabled(false);
    mockUI();
    anchor.setHref(new AbstractStreamResource() {

        @Override
        public String getName() {
            return "baz";
        }
    });
    anchor.setEnabled(true);
    Assert.assertTrue(anchor.getElement().hasAttribute("href"));
}
Also used : AbstractStreamResource(com.vaadin.flow.server.AbstractStreamResource) Test(org.junit.Test)

Example 3 with AbstractStreamResource

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

the class AnchorTest method disabledAnchor_setResourceWhenDisabled_hrefIsPreserved.

@Test
public void disabledAnchor_setResourceWhenDisabled_hrefIsPreserved() {
    mockUI();
    AbstractStreamResource resource = new AbstractStreamResource() {

        @Override
        public String getName() {
            return "foo";
        }
    };
    Anchor anchor = new Anchor(resource, "bar");
    String href = anchor.getHref();
    anchor.setEnabled(false);
    anchor.setHref(new AbstractStreamResource() {

        @Override
        public String getName() {
            return "baz";
        }
    });
    anchor.setEnabled(true);
    Assert.assertTrue(anchor.getElement().hasAttribute("href"));
    Assert.assertNotEquals(href, anchor.getHref());
}
Also used : AbstractStreamResource(com.vaadin.flow.server.AbstractStreamResource) Test(org.junit.Test)

Example 4 with AbstractStreamResource

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

the class ElementAttributeMap method unsetResource.

private void unsetResource(String attribute) {
    ensureResourceRegistrations();
    StreamRegistration registration = resourceRegistrations.get(attribute);
    Optional<AbstractStreamResource> resource = Optional.empty();
    if (registration != null) {
        resource = Optional.ofNullable(registration.getResource());
    }
    unregisterResource(attribute);
    resource.ifPresent(res -> deferRegistration(attribute, res));
}
Also used : StreamRegistration(com.vaadin.flow.server.StreamRegistration) AbstractStreamResource(com.vaadin.flow.server.AbstractStreamResource)

Example 5 with AbstractStreamResource

use of com.vaadin.flow.server.AbstractStreamResource 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

AbstractStreamResource (com.vaadin.flow.server.AbstractStreamResource)5 Test (org.junit.Test)3 StreamReceiver (com.vaadin.flow.server.StreamReceiver)1 StreamRegistration (com.vaadin.flow.server.StreamRegistration)1 StreamResource (com.vaadin.flow.server.StreamResource)1