Search in sources :

Example 1 with Pair

use of com.vaadin.flow.internal.Pair in project flow by vaadin.

the class LitTemplateParserImpl method getTemplateContent.

@Override
public TemplateData getTemplateContent(Class<? extends LitTemplate> clazz, String tag, VaadinService service) {
    List<Dependency> dependencies = AnnotationReader.getAnnotationsFor(clazz, JsModule.class).stream().map(jsModule -> new Dependency(Dependency.Type.JS_MODULE, jsModule.value(), // load mode doesn't
    LoadMode.EAGER)).collect(Collectors.toList());
    for (DependencyFilter filter : service.getDependencyFilters()) {
        dependencies = filter.filter(new ArrayList<>(dependencies), service);
    }
    Pair<Dependency, String> chosenDep = null;
    for (Dependency dependency : dependencies) {
        if (dependency.getType() != Dependency.Type.JS_MODULE) {
            continue;
        }
        String url = dependency.getUrl();
        String source = getSourcesFromTemplate(service, tag, url);
        if (source == null) {
            continue;
        }
        if (chosenDep == null) {
            chosenDep = new Pair<>(dependency, source);
        }
        if (dependencyHasTagName(dependency, tag)) {
            chosenDep = new Pair<>(dependency, source);
            break;
        }
    }
    Element templateElement = null;
    if (chosenDep != null) {
        templateElement = BundleLitParser.parseLitTemplateElement(chosenDep.getFirst().getUrl(), chosenDep.getSecond());
    }
    if (templateElement != null) {
        // Template needs to be wrapped in an element with id, to look
        // like a P2 template
        Element parent = new Element(tag);
        parent.attr("id", tag);
        templateElement.appendTo(parent);
        return new TemplateData(chosenDep.getFirst().getUrl(), templateElement);
    }
    getLogger().info("Couldn't find the " + "definition of the element with tag '{}' " + "in any lit template file declared using '@{}' annotations. " + "In a Spring Boot project, please ensure that the template's " + "groupId is added to the vaadin.whitelisted-packages " + "property. Otherwise, please Check the availability of the " + "template files in your WAR file or provide alternative " + "implementation of the method " + "LitTemplateParser.getTemplateContent() which should return " + "an element representing the content of the template file", tag, JsModule.class.getSimpleName());
    return null;
}
Also used : LitTemplateParser(com.vaadin.flow.component.littemplate.LitTemplateParser) URL(java.net.URL) BundleLitParser(com.vaadin.flow.component.littemplate.BundleLitParser) LoggerFactory(org.slf4j.LoggerFactory) Dependency(com.vaadin.flow.shared.ui.Dependency) FrontendUtils(com.vaadin.flow.server.frontend.FrontendUtils) ArrayList(java.util.ArrayList) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Lookup(com.vaadin.flow.di.Lookup) Constants(com.vaadin.flow.server.Constants) Logger(org.slf4j.Logger) Pair(com.vaadin.flow.internal.Pair) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) IOException(java.io.IOException) ResourceProvider(com.vaadin.flow.di.ResourceProvider) LitTemplate(com.vaadin.flow.component.littemplate.LitTemplate) Collectors(java.util.stream.Collectors) List(java.util.List) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) JsModule(com.vaadin.flow.component.dependency.JsModule) FilenameUtils(org.apache.commons.io.FilenameUtils) InputStream(java.io.InputStream) JsModule(com.vaadin.flow.component.dependency.JsModule) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency)

Example 2 with Pair

use of com.vaadin.flow.internal.Pair in project flow by vaadin.

the class StreamReceiverHandler method streamToReceiver.

private final Pair<Boolean, UploadStatus> streamToReceiver(VaadinSession session, final InputStream in, StreamReceiver streamReceiver, String filename, String type, long contentLength) throws UploadException {
    StreamVariable streamVariable = streamReceiver.getStreamVariable();
    if (streamVariable == null) {
        throw new IllegalStateException("StreamVariable for the post not found");
    }
    OutputStream out = null;
    long totalBytes = 0;
    StreamingStartEventImpl startedEvent = new StreamingStartEventImpl(filename, type, contentLength);
    boolean success = false;
    try {
        boolean listenProgress;
        session.lock();
        try {
            streamVariable.streamingStarted(startedEvent);
            out = streamVariable.getOutputStream();
            listenProgress = streamVariable.listenProgress();
        } finally {
            session.unlock();
        }
        // Gets the output target stream
        if (out == null) {
            throw new NoOutputStreamException();
        }
        if (null == in) {
            // No file, for instance non-existent filename in html upload
            throw new NoInputStreamException();
        }
        final byte[] buffer = new byte[MAX_UPLOAD_BUFFER_SIZE];
        long lastStreamingEvent = 0;
        int bytesReadToBuffer;
        do {
            bytesReadToBuffer = in.read(buffer);
            if (bytesReadToBuffer > 0) {
                out.write(buffer, 0, bytesReadToBuffer);
                totalBytes += bytesReadToBuffer;
            }
            if (listenProgress) {
                StreamingProgressEventImpl progressEvent = new StreamingProgressEventImpl(filename, type, contentLength, totalBytes);
                lastStreamingEvent = updateProgress(session, streamVariable, progressEvent, lastStreamingEvent, bytesReadToBuffer);
            }
            if (streamVariable.isInterrupted()) {
                throw new UploadInterruptedException();
            }
        } while (bytesReadToBuffer > 0);
        // upload successful
        out.close();
        StreamVariable.StreamingEndEvent event = new StreamingEndEventImpl(filename, type, totalBytes);
        session.lock();
        try {
            streamVariable.streamingFinished(event);
        } finally {
            session.unlock();
        }
        success = true;
    } catch (UploadInterruptedException | IOException e) {
        // Download is either interrupted by application code or some
        // IOException happens
        onStreamingFailed(session, filename, type, contentLength, streamVariable, out, totalBytes, e);
    // Interrupted exception and IOEXception are not thrown forward:
    // it's enough to fire them via streamVariable
    } catch (final Exception e) {
        onStreamingFailed(session, filename, type, contentLength, streamVariable, out, totalBytes, e);
        // I/O operations).
        throw new UploadException(e);
    }
    return new Pair<>(startedEvent.isDisposed(), success ? UploadStatus.OK : UploadStatus.ERROR);
}
Also used : OutputStream(java.io.OutputStream) UploadException(com.vaadin.flow.server.UploadException) FileUploadException(org.apache.commons.fileupload.FileUploadException) StreamingStartEventImpl(com.vaadin.flow.server.communication.streaming.StreamingStartEventImpl) NoOutputStreamException(com.vaadin.flow.server.NoOutputStreamException) IOException(java.io.IOException) NoOutputStreamException(com.vaadin.flow.server.NoOutputStreamException) IOException(java.io.IOException) NoInputStreamException(com.vaadin.flow.server.NoInputStreamException) UploadException(com.vaadin.flow.server.UploadException) FileUploadException(org.apache.commons.fileupload.FileUploadException) StreamVariable(com.vaadin.flow.server.StreamVariable) StreamingEndEventImpl(com.vaadin.flow.server.communication.streaming.StreamingEndEventImpl) NoInputStreamException(com.vaadin.flow.server.NoInputStreamException) StreamingProgressEventImpl(com.vaadin.flow.server.communication.streaming.StreamingProgressEventImpl) Pair(com.vaadin.flow.internal.Pair)

Example 3 with Pair

use of com.vaadin.flow.internal.Pair in project flow by vaadin.

the class NpmTemplateParser method getTemplateContent.

@Override
public TemplateData getTemplateContent(Class<? extends PolymerTemplate<?>> clazz, String tag, VaadinService service) {
    List<Dependency> dependencies = AnnotationReader.getAnnotationsFor(clazz, JsModule.class).stream().map(jsModule -> new Dependency(Dependency.Type.JS_MODULE, jsModule.value(), // load mode doesn't
    LoadMode.EAGER)).collect(Collectors.toList());
    for (DependencyFilter filter : service.getDependencyFilters()) {
        dependencies = filter.filter(new ArrayList<>(dependencies), service);
    }
    Pair<Dependency, String> chosenDep = null;
    for (Dependency dependency : dependencies) {
        if (dependency.getType() != Dependency.Type.JS_MODULE) {
            continue;
        }
        String url = dependency.getUrl();
        String source = getSourcesFromTemplate(service, tag, url);
        if (source == null) {
            continue;
        }
        if (chosenDep == null) {
            chosenDep = new Pair<>(dependency, source);
        }
        if (dependencyHasTagName(dependency, tag)) {
            chosenDep = new Pair<>(dependency, source);
            break;
        }
    }
    if (chosenDep != null) {
        Element templateElement = BundleParser.parseTemplateElement(chosenDep.getFirst().getUrl(), chosenDep.getSecond());
        if (!JsoupUtils.getDomModule(templateElement, null).isPresent()) {
            // Template needs to be wrapped in an element with id, to look
            // like a P2 template
            Element parent = new Element(tag);
            parent.attr("id", tag);
            templateElement.appendTo(parent);
        }
        return new TemplateData(chosenDep.getFirst().getUrl(), templateElement);
    }
    throw new IllegalStateException(String.format("Couldn't find the " + "definition of the element with tag '%s' " + "in any template file declared using '@%s' annotations. " + "In a Spring Boot project, please ensure that the template's " + "groupId is added to the vaadin.whitelisted-packages " + "property. Otherwise, please Check the availability of the " + "template files in your WAR file or provide alternative " + "implementation of the method getTemplateContent() which " + "should return an element representing the content of the " + "template file", tag, JsModule.class.getSimpleName()));
}
Also used : Logger(org.slf4j.Logger) URL(java.net.URL) Pair(com.vaadin.flow.internal.Pair) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Dependency(com.vaadin.flow.shared.ui.Dependency) Collectors(java.util.stream.Collectors) FrontendUtils(com.vaadin.flow.server.frontend.FrontendUtils) ArrayList(java.util.ArrayList) List(java.util.List) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Lookup(com.vaadin.flow.di.Lookup) Constants(com.vaadin.flow.server.Constants) JsModule(com.vaadin.flow.component.dependency.JsModule) FilenameUtils(org.apache.commons.io.FilenameUtils) InputStream(java.io.InputStream) JsModule(com.vaadin.flow.component.dependency.JsModule) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency)

Aggregations

Pair (com.vaadin.flow.internal.Pair)3 IOException (java.io.IOException)3 JsModule (com.vaadin.flow.component.dependency.JsModule)2 Lookup (com.vaadin.flow.di.Lookup)2 ResourceProvider (com.vaadin.flow.di.ResourceProvider)2 AnnotationReader (com.vaadin.flow.internal.AnnotationReader)2 Constants (com.vaadin.flow.server.Constants)2 DependencyFilter (com.vaadin.flow.server.DependencyFilter)2 VaadinService (com.vaadin.flow.server.VaadinService)2 FrontendUtils (com.vaadin.flow.server.frontend.FrontendUtils)2 Dependency (com.vaadin.flow.shared.ui.Dependency)2 LoadMode (com.vaadin.flow.shared.ui.LoadMode)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Locale (java.util.Locale)2 Collectors (java.util.stream.Collectors)2 FilenameUtils (org.apache.commons.io.FilenameUtils)2 Element (org.jsoup.nodes.Element)2