Search in sources :

Example 1 with VaadinServletService

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

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

the class PushAtmosphereHandlerTest method setup.

@Before
public void setup() throws IOException {
    request = Mockito.mock(AtmosphereRequest.class);
    response = Mockito.mock(AtmosphereResponse.class);
    printWriter = Mockito.mock(PrintWriter.class);
    Mockito.when(response.getWriter()).thenReturn(printWriter);
    resource = Mockito.mock(AtmosphereResource.class);
    Mockito.when(resource.getRequest()).thenReturn(request);
    Mockito.when(resource.getResponse()).thenReturn(response);
    VaadinContext context = new MockVaadinContext();
    ApplicationConfiguration config = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(config.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(config.getContext()).thenReturn(context);
    VaadinServletService service = new VaadinServletService(null, new DefaultDeploymentConfiguration(config, getClass(), new Properties()));
    PushHandler handler = new PushHandler(service);
    atmosphereHandler = new PushAtmosphereHandler();
    atmosphereHandler.setPushHandler(handler);
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) VaadinContext(com.vaadin.flow.server.VaadinContext) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) PrintWriter(java.io.PrintWriter) Before(org.junit.Before)

Example 3 with VaadinServletService

use of com.vaadin.flow.server.VaadinServletService 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 4 with VaadinServletService

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

the class EndpointInvoker method invoke.

/**
 * Invoke the given endpoint method with the given parameters if the user
 * has access to do so.
 *
 * @param endpointName
 *            the name of the endpoint
 * @param methodName
 *            the name of the method in the endpoint
 * @param body
 *            optional request body, that should be specified if the method
 *            called has parameters
 * @param request
 *            the HTTP request which should not be here in the end
 * @return the return value of the invoked endpoint method, wrapped in a
 *         response entity
 */
public ResponseEntity<String> invoke(String endpointName, String methodName, ObjectNode body, HttpServletRequest request) {
    VaadinEndpointData vaadinEndpointData = endpointRegistry.get(endpointName);
    if (vaadinEndpointData == null) {
        getLogger().debug("Endpoint '{}' not found", endpointName);
        return ResponseEntity.notFound().build();
    }
    Method methodToInvoke = vaadinEndpointData.getMethod(methodName).orElse(null);
    if (methodToInvoke == null) {
        getLogger().debug("Method '{}' not found in endpoint '{}'", methodName, endpointName);
        return ResponseEntity.notFound().build();
    }
    try {
        // Put a VaadinRequest in the instances object so as the request is
        // available in the end-point method
        VaadinServletService service = (VaadinServletService) VaadinService.getCurrent();
        CurrentInstance.set(VaadinRequest.class, new VaadinServletRequest(request, service));
        return invokeVaadinEndpointMethod(endpointName, methodName, methodToInvoke, body, vaadinEndpointData, request);
    } catch (JsonProcessingException e) {
        String errorMessage = String.format("Failed to serialize endpoint '%s' method '%s' response. " + "Double check method's return type or specify a custom mapper bean with qualifier '%s'", endpointName, methodName, EndpointController.VAADIN_ENDPOINT_MAPPER_BEAN_QUALIFIER);
        getLogger().error(errorMessage, e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(createResponseErrorObject(errorMessage));
    } finally {
        CurrentInstance.set(VaadinRequest.class, null);
    }
}
Also used : VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServletService(com.vaadin.flow.server.VaadinServletService) Method(java.lang.reflect.Method) VaadinEndpointData(dev.hilla.EndpointRegistry.VaadinEndpointData) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with VaadinServletService

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

the class SpringInstantiatorTest method getService.

public static VaadinService getService(ApplicationContext context, Properties configProperties, boolean rootMapping) throws ServletException {
    SpringServlet servlet = new SpringServlet(context, rootMapping) {

        @Override
        protected DeploymentConfiguration createDeploymentConfiguration(Properties initParameters) {
            if (configProperties != null) {
                configProperties.putAll(initParameters);
                return super.createDeploymentConfiguration(configProperties);
            }
            return super.createDeploymentConfiguration(initParameters);
        }
    };
    ServletConfig config = Mockito.mock(ServletConfig.class);
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getClassLoader()).thenReturn(servlet.getClass().getClassLoader());
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(appConfig.getContext()).thenReturn(new VaadinServletContext(servletContext));
    Mockito.when(servletContext.getAttribute(ApplicationConfiguration.class.getName())).thenReturn(appConfig);
    Lookup lookup = Mockito.mock(Lookup.class);
    ResourceProvider provider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(provider);
    StaticFileHandlerFactory staticFileHandlerFactory = vaadinService -> new StaticFileServer((VaadinServletService) vaadinService);
    Mockito.when(lookup.lookup(StaticFileHandlerFactory.class)).thenReturn(staticFileHandlerFactory);
    Mockito.when(servletContext.getAttribute(Lookup.class.getName())).thenReturn(lookup);
    Mockito.when(config.getServletContext()).thenReturn(servletContext);
    Mockito.when(config.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(servletContext.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    servlet.init(config);
    return servlet.getService();
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) BeanInstantiationException(org.springframework.beans.BeanInstantiationException) ServletException(javax.servlet.ServletException) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) Div(com.vaadin.flow.component.html.Div) StaticFileServer(com.vaadin.flow.server.StaticFileServer) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Scope(org.springframework.context.annotation.Scope) Locale(java.util.Locale) Lookup(com.vaadin.flow.di.Lookup) SpringServlet(com.vaadin.flow.spring.SpringServlet) SpringRunner(org.springframework.test.context.junit4.SpringRunner) I18NProvider(com.vaadin.flow.i18n.I18NProvider) ServiceInitEvent(com.vaadin.flow.server.ServiceInitEvent) Instantiator(com.vaadin.flow.di.Instantiator) ServletConfig(javax.servlet.ServletConfig) Properties(java.util.Properties) SpringInstantiator(com.vaadin.flow.spring.SpringInstantiator) Set(java.util.Set) Import(org.springframework.context.annotation.Import) Test(org.junit.Test) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) ComponentScan(org.springframework.context.annotation.ComponentScan) VaadinServiceInitListener(com.vaadin.flow.server.VaadinServiceInitListener) Configuration(org.springframework.context.annotation.Configuration) Mockito(org.mockito.Mockito) List(java.util.List) Component(org.springframework.stereotype.Component) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) StaticFileHandlerFactory(com.vaadin.flow.server.StaticFileHandlerFactory) VaadinService(com.vaadin.flow.server.VaadinService) ServletContext(javax.servlet.ServletContext) Assert(org.junit.Assert) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) StaticFileHandlerFactory(com.vaadin.flow.server.StaticFileHandlerFactory) StaticFileServer(com.vaadin.flow.server.StaticFileServer) ResourceProvider(com.vaadin.flow.di.ResourceProvider) ServletConfig(javax.servlet.ServletConfig) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) SpringServlet(com.vaadin.flow.spring.SpringServlet)

Aggregations

VaadinServletService (com.vaadin.flow.server.VaadinServletService)23 Test (org.junit.Test)14 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)10 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)9 VaadinSession (com.vaadin.flow.server.VaadinSession)8 Properties (java.util.Properties)7 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)6 VaadinServlet (com.vaadin.flow.server.VaadinServlet)6 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)5 VaadinService (com.vaadin.flow.server.VaadinService)5 Before (org.junit.Before)5 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)4 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)4 URL (java.net.URL)4 Collections (java.util.Collections)4 ServletContext (javax.servlet.ServletContext)4 Lookup (com.vaadin.flow.di.Lookup)3 ResourceProvider (com.vaadin.flow.di.ResourceProvider)3 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)3 VaadinResponse (com.vaadin.flow.server.VaadinResponse)3