Search in sources :

Example 1 with ServiceException

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

the class WebJarServerTest method init.

@Before
public void init() throws Exception {
    DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(configuration.getDevelopmentFrontendPrefix()).thenReturn(Constants.FRONTEND_URL_DEV_DEFAULT);
    VaadinServletService servletService = Mockito.mock(VaadinServletService.class);
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    servlet = new VaadinServlet() {

        @Override
        protected VaadinServletService createServletService() throws ServletException, ServiceException {
            return servletService;
        }

        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    };
    Mockito.when(servletService.getDeploymentConfiguration()).thenReturn(configuration);
    Mockito.when(configuration.areWebJarsEnabled()).thenReturn(true);
    servlet.init(Mockito.mock(ServletConfig.class));
    Field webJarServerField = VaadinServlet.class.getDeclaredField("webJarServer");
    webJarServerField.setAccessible(true);
    webJarServer = (WebJarServer) webJarServerField.get(servlet);
    // webJarServer = new WebJarServer(configuration);
    // context = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getResource(GRID_WEBJAR)).thenReturn(new URL("http://localhost:8080" + GRID_DEPENDENCY));
}
Also used : ServletException(javax.servlet.ServletException) Field(java.lang.reflect.Field) ServiceException(com.vaadin.flow.server.ServiceException) VaadinServlet(com.vaadin.flow.server.VaadinServlet) ServletConfig(javax.servlet.ServletConfig) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServletContext(javax.servlet.ServletContext) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) URL(java.net.URL) Before(org.junit.Before)

Example 2 with ServiceException

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

the class ApplicationRunnerServlet method createServletService.

@SuppressWarnings("serial")
@Override
protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
    // service doesn't use router actually. UI class is responsible to show
    // and update the content by itself with only root route available
    VaadinServletService service = new VaadinServletService(this, deploymentConfiguration) {

        @Override
        public Router getRouter() {
            Router router = new Router(getRouteRegistry()) {

                @Override
                public int navigate(UI ui, Location location, NavigationTrigger trigger, JsonValue state) {
                    ui.getPage().getHistory().pushState(null, location);
                    return HttpServletResponse.SC_OK;
                }
            };
            return router;
        }
    };
    service.init();
    final SystemMessagesProvider provider = service.getSystemMessagesProvider();
    service.setSystemMessagesProvider((SystemMessagesProvider) systemMessagesInfo -> {
        if (systemMessagesInfo.getRequest() == null) {
            return provider.getSystemMessages(systemMessagesInfo);
        }
        Object messages = systemMessagesInfo.getRequest().getAttribute(CUSTOM_SYSTEM_MESSAGES_PROPERTY);
        if (messages instanceof SystemMessages) {
            return (SystemMessages) messages;
        }
        return provider.getSystemMessages(systemMessagesInfo);
    });
    return service;
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) Proxy(java.lang.reflect.Proxy) ServletException(javax.servlet.ServletException) SystemMessagesProvider(com.vaadin.flow.server.SystemMessagesProvider) URL(java.net.URL) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Router(com.vaadin.flow.router.Router) JsonValue(elemental.json.JsonValue) HttpServletRequest(javax.servlet.http.HttpServletRequest) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Map(java.util.Map) Location(com.vaadin.flow.router.Location) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) URI(java.net.URI) UI(com.vaadin.flow.component.UI) Method(java.lang.reflect.Method) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) HttpSession(javax.servlet.http.HttpSession) VaadinSession(com.vaadin.flow.server.VaadinSession) SystemMessages(com.vaadin.flow.server.SystemMessages) ServletConfig(javax.servlet.ServletConfig) Properties(java.util.Properties) Logger(org.slf4j.Logger) Files(java.nio.file.Files) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Set(java.util.Set) IOException(java.io.IOException) Attributes(com.vaadin.flow.server.Attributes) Field(java.lang.reflect.Field) File(java.io.File) Serializable(java.io.Serializable) WebServlet(javax.servlet.annotation.WebServlet) FileVisitResult(java.nio.file.FileVisitResult) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Conf(com.vaadin.flow.uitest.servlet.CustomDeploymentConfiguration.Conf) VaadinService(com.vaadin.flow.server.VaadinService) InvocationHandler(java.lang.reflect.InvocationHandler) ServiceException(com.vaadin.flow.server.ServiceException) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) UI(com.vaadin.flow.component.UI) SystemMessagesProvider(com.vaadin.flow.server.SystemMessagesProvider) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) JsonValue(elemental.json.JsonValue) VaadinServletService(com.vaadin.flow.server.VaadinServletService) Router(com.vaadin.flow.router.Router) SystemMessages(com.vaadin.flow.server.SystemMessages) Location(com.vaadin.flow.router.Location)

Example 3 with ServiceException

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

the class DefaultTemplateParserTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    VaadinSession session = Mockito.mock(VaadinSession.class);
    factory = (VaadinUriResolverFactory) vaadinRequest -> resolver;
    Mockito.when(session.getAttribute(VaadinUriResolverFactory.class)).thenReturn(factory);
    Mockito.when(service.getDependencyFilters()).thenReturn(Collections.emptyList());
    WrappedHttpSession wrappedSession = Mockito.mock(WrappedHttpSession.class);
    HttpSession httpSession = Mockito.mock(HttpSession.class);
    Mockito.when(wrappedSession.getHttpSession()).thenReturn(httpSession);
    servlet = new VaadinServlet() {

        @Override
        protected VaadinServletService createServletService() throws ServletException, ServiceException {
            return service;
        }

        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    };
    Mockito.when(service.getServlet()).thenReturn(servlet);
    Mockito.when(resolver.resolveVaadinUri("/bar.html")).thenReturn("bar.html");
    Mockito.when(resolver.resolveVaadinUri("/bar1.html")).thenReturn("bar1.html");
    Mockito.when(resolver.resolveVaadinUri("/bundle.html")).thenReturn("bundle.html");
    Mockito.when(request.getWrappedSession()).thenReturn(wrappedSession);
    Mockito.when(request.getServletPath()).thenReturn("");
    Mockito.when(servletContext.getResourceAsStream("/bar.html")).thenReturn(new ByteArrayInputStream("<dom-module id='bar'></dom-module>".getBytes(StandardCharsets.UTF_8)));
    Mockito.when(servletContext.getResourceAsStream("/bar1.html")).thenReturn(new ByteArrayInputStream("<dom-module id='foo'></dom-module>".getBytes(StandardCharsets.UTF_8)));
    Mockito.when(servletContext.getResourceAsStream("/bundle.html")).thenReturn(getBundle(), getBundle(), getBundle());
    CurrentInstance.set(VaadinRequest.class, request);
    CurrentInstance.set(VaadinSession.class, session);
    CurrentInstance.set(VaadinService.class, service);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) ServletException(javax.servlet.ServletException) Mock(org.mockito.Mock) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) Comment(org.jsoup.nodes.Comment) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) Type(com.vaadin.flow.shared.ui.Dependency.Type) Assert.assertThat(org.junit.Assert.assertThat) MockitoAnnotations(org.mockito.MockitoAnnotations) Tag(com.vaadin.flow.component.Tag) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(org.jsoup.nodes.Element) After(org.junit.After) DependencyFilter(com.vaadin.flow.server.DependencyFilter) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Before(org.junit.Before) HttpSession(javax.servlet.http.HttpSession) VaadinSession(com.vaadin.flow.server.VaadinSession) Matchers.empty(org.hamcrest.Matchers.empty) VaadinServlet(com.vaadin.flow.server.VaadinServlet) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) NotThreadSafe(net.jcip.annotations.NotThreadSafe) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Node(org.jsoup.nodes.Node) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) ServletContext(javax.servlet.ServletContext) ModelClass(com.vaadin.flow.component.polymertemplate.PolymerTemplateTest.ModelClass) ServiceException(com.vaadin.flow.server.ServiceException) Assert(org.junit.Assert) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServletException(javax.servlet.ServletException) VaadinSession(com.vaadin.flow.server.VaadinSession) ServiceException(com.vaadin.flow.server.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServletContext(javax.servlet.ServletContext) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) Before(org.junit.Before)

Example 4 with ServiceException

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

the class SpringVaadinServletService method loadInstantiators.

@Override
protected Optional<Instantiator> loadInstantiators() throws ServiceException {
    Optional<Instantiator> spiInstantiator = super.loadInstantiators();
    List<Instantiator> springInstantiators = context.getBeansOfType(Instantiator.class).values().stream().filter(instantiator -> instantiator.init(this)).collect(Collectors.toList());
    if (spiInstantiator.isPresent() && !springInstantiators.isEmpty()) {
        throw new ServiceException("Cannot init VaadinService because there are multiple eligible " + "instantiator implementations: Java SPI registered instantiator " + spiInstantiator.get() + " and Spring instantiator beans: " + springInstantiators);
    }
    if (!spiInstantiator.isPresent() && springInstantiators.isEmpty()) {
        Instantiator defaultInstantiator = new SpringInstantiator(this, context);
        defaultInstantiator.init(this);
        return Optional.of(defaultInstantiator);
    }
    return spiInstantiator.isPresent() ? spiInstantiator : springInstantiators.stream().findFirst();
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) Instantiator(com.vaadin.flow.di.Instantiator) URL(java.net.URL) SessionDestroyListener(com.vaadin.flow.server.SessionDestroyListener) VaadinServlet(com.vaadin.flow.server.VaadinServlet) Registration(com.vaadin.flow.shared.Registration) IOException(java.io.IOException) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) ServiceException(com.vaadin.flow.server.ServiceException) UIInitListener(com.vaadin.flow.server.UIInitListener) VaadinServletService(com.vaadin.flow.server.VaadinServletService) Resource(org.springframework.core.io.Resource) ServiceException(com.vaadin.flow.server.ServiceException) Instantiator(com.vaadin.flow.di.Instantiator)

Aggregations

ServiceException (com.vaadin.flow.server.ServiceException)4 VaadinServlet (com.vaadin.flow.server.VaadinServlet)4 VaadinServletService (com.vaadin.flow.server.VaadinServletService)4 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)3 VaadinSession (com.vaadin.flow.server.VaadinSession)3 URL (java.net.URL)3 ServletException (javax.servlet.ServletException)3 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)2 VaadinRequest (com.vaadin.flow.server.VaadinRequest)2 VaadinService (com.vaadin.flow.server.VaadinService)2 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 ServletConfig (javax.servlet.ServletConfig)2 Tag (com.vaadin.flow.component.Tag)1 UI (com.vaadin.flow.component.UI)1