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