@Override @SuppressWarnings("unchecked") public void contextInitialized(ServletContextEvent event) { ApplicationRouteRegistry registry = ApplicationRouteRegistry .getInstance(event.getServletContext()); Stream<Class<? extends Component>> hasErrorComponents = findBySuperType( getErrorParameterPackages(), HasErrorParameter.class) .filter(Component.class::isAssignableFrom) .map(clazz -> (Class<? extends Component>) clazz); registry.setErrorNavigationTargets( hasErrorComponents.collect(Collectors.toSet())); }
/** * Set error handler navigation targets. * <p> * This can also be used to add error navigation targets that override * existing targets. Note! The overriding targets need to be extending the * existing target or they will throw. * * @param errorNavigationTargets * error handler navigation targets */ public void setErrorNavigationTargets( Set<Class<? extends Component>> errorNavigationTargets) { Map<Class<? extends Exception>, Class<? extends Component>> exceptionTargetsMap = new HashMap<>(); exceptionTargetsMap.putAll(getConfiguration().getExceptionHandlers()); errorNavigationTargets.stream() .filter(target -> !defaultErrorHandlers.contains(target)) .filter(this::allErrorFiltersMatch) .forEach(target -> addErrorTarget(target, exceptionTargetsMap)); initErrorTargets(exceptionTargetsMap); }
private void initErrorTargets( Map<Class<? extends Exception>, Class<? extends Component>> map) { if (!map.containsKey(NotFoundException.class)) { map.put(NotFoundException.class, RouteNotFoundError.class); } if (!map.containsKey(Exception.class)) { map.put(Exception.class, InternalServerError.class); } configure(configuration -> map.forEach(configuration::setErrorRoute)); }
/** * Get a registered navigation target for given exception. First we will * search for a matching cause for in the exception chain and if no match * found search by extended type. * * @param exception * exception to search error view for * @return optional error target entry corresponding to the given exception */ public Optional<ErrorTargetEntry> getErrorNavigationTarget( Exception exception) { if (getConfiguration().getExceptionHandlers().isEmpty()) { initErrorTargets(new HashMap<>()); } Optional<ErrorTargetEntry> result = searchByCause(exception); if (!result.isPresent()) { result = searchBySuperType(exception); } return result; }
@Override public void onStartup(Set<Class<?>> classSet, ServletContext servletContext) throws ServletException { try { if (classSet == null) { ApplicationRouteRegistry routeRegistry = ApplicationRouteRegistry .getInstance(servletContext); routeRegistry.clean(); return; } Set<Class<? extends Component>> routes = validateRouteClasses( classSet.stream()); ApplicationRouteRegistry routeRegistry = ApplicationRouteRegistry .getInstance(servletContext); RouteConfiguration routeConfiguration = RouteConfiguration .forRegistry(routeRegistry); routeConfiguration.update(() -> { routeConfiguration.getHandledRegistry().clean(); for (Class<? extends Component> navigationTarget : routes) { routeConfiguration.setAnnotatedRoute(navigationTarget); } }); routeRegistry.setPwaConfigurationClass(validatePwaClass( routes.stream().map(clazz -> (Class<?>) clazz))); } catch (InvalidRouteConfigurationException irce) { throw new ServletException( "Exception while registering Routes on servlet startup", irce); } }
@SuppressWarnings("unchecked") @Override public void contextInitialized(ServletContextEvent event) { ApplicationRouteRegistry registry = ApplicationRouteRegistry .getInstance(event.getServletContext()); if (registry.getRegisteredRoutes().isEmpty()) { try { List<Class<?>> routeClasses = findByAnnotation( getRoutePackages(), Route.class, RouteAlias.class).collect(Collectors.toList()); Set<Class<? extends Component>> navigationTargets = validateRouteClasses( routeClasses.stream()); RouteConfiguration routeConfiguration = RouteConfiguration .forRegistry(registry); routeConfiguration.update(() -> { routeConfiguration.getHandledRegistry().clean(); navigationTargets.forEach(routeConfiguration::setAnnotatedRoute); }); registry.setPwaConfigurationClass(validatePwaClass(routeClasses.stream())); } catch (InvalidRouteConfigurationException e) { throw new IllegalStateException(e); } } }
ApplicationRouteRegistry reg = ApplicationRouteRegistry.getInstance(servletContext); PWA pwa = reg.getPwaConfigurationClass() != null ? reg .getPwaConfigurationClass().getAnnotation(PWA.class) : null;
private void createAppServlet(ServletContext context) { if (!ApplicationRouteRegistry.getInstance(context).hasNavigationTargets()) { getLogger().info( "{} there are no navigation targets registered to the registry", SKIPPING_AUTOMATIC_SERVLET_REGISTRATION_BECAUSE); return; } ServletRegistration vaadinServlet = findVaadinServlet(context); if (vaadinServlet != null) { getLogger().info( "{} there is already a Vaadin servlet with the name {}", SKIPPING_AUTOMATIC_SERVLET_REGISTRATION_BECAUSE, vaadinServlet.getName()); return; } createServletIfNotExists(context, getClass().getName(), "/*"); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { // Verify servlet version also for SpringBoot. ServletVerifier.verifyServletVersion(); ApplicationRouteRegistry registry = ApplicationRouteRegistry .getInstance(servletContext); // If the registry is already initialized then RouteRegistryInitializer // has done its job already, skip the custom routes search if (registry.getRegisteredRoutes().isEmpty()) { /* * Don't rely on RouteRegistry.isInitialized() negative return value * here because it's not known whether RouteRegistryInitializer has * been executed already or not (the order is undefined). Postpone * this to the end of context initialization cycle. At this point * RouteRegistry is either initialized or it's not initialized * because an RouteRegistryInitializer has not been executed (end * never will). */ servletContext.addListener(new RouteServletContextListener()); } servletContext.addListener(new ErrorParameterServletContextListener()); servletContext .addListener(new AnnotationValidatorServletContextListener()); }
private static ApplicationRouteRegistry createRegistry( ServletContext context) { if (context != null && context == OSGiAccess.getInstance() .getOsgiServletContext()) { return new OSGiDataCollector(); } else if (OSGiAccess.getInstance().getOsgiServletContext() == null) { return new ApplicationRouteRegistry(); } OSGiRouteRegistry osgiRouteRegistry = new OSGiRouteRegistry(); OSGiDataCollector osgiDataCollector = (OSGiDataCollector) getInstance( OSGiAccess.getInstance().getOsgiServletContext()); osgiRouteRegistry.setRoutes(osgiDataCollector.getRegisteredRoutes()); osgiRouteRegistry.subscribeToChanges(osgiDataCollector); return osgiRouteRegistry; } }
@Override protected RouteRegistry getRouteRegistry() { return ApplicationRouteRegistry.getInstance(getServlet().getServletContext()); }
protected Optional<ErrorTargetEntry> getErrorNavigationTarget( Exception exception) { if (registry instanceof ApplicationRouteRegistry) { return ((ApplicationRouteRegistry) registry) .getErrorNavigationTarget(exception); } return Optional.empty(); } }
@Override public Optional<Class<? extends Component>> getNavigationTarget( String pathString, List<String> segments) { if (getConfiguration().hasRoute(pathString, segments)) { return getConfiguration().getRoute(pathString, segments); } return Optional.empty(); }
attribute = createRegistry(servletContext); servletContext.setAttribute(RouteRegistry.class.getName(), attribute);
private static RouteRegistry getApplicationRegistry() { VaadinService service = VaadinService.getCurrent(); if (service instanceof VaadinServletService) { return ApplicationRouteRegistry .getInstance(((VaadinServletService) service).getServlet() .getServletContext()); } else { // TODO Once we have PortletService, this will explode // we will need route registry for portlet context throw new IllegalStateException("Cannot access " + ApplicationRouteRegistry.class.getName() + ", because no " + "VaadinServletService available for " + "fetching ServletContext"); } }
@Override public Optional<ErrorTargetEntry> getErrorNavigationTarget( Exception exception) { initErrorTargets(); return super.getErrorNavigationTarget(exception); }
/** * Check if there are registered navigation targets in the registry. * * @return true if any navigation are registered */ public boolean hasNavigationTargets() { return !getConfiguration().getRoutes().isEmpty(); }
@SuppressWarnings("unchecked") @Override public void onStartup(Set<Class<?>> classSet, ServletContext servletContext) throws ServletException { if (classSet == null) { classSet = new HashSet<>(); } Set<Class<? extends Component>> routes = classSet.stream() // Liberty 18 also includes the interface itself in the set... .filter(clazz -> clazz != HasErrorParameter.class) .map(clazz -> (Class<? extends Component>) clazz) .collect(Collectors.toSet()); ApplicationRouteRegistry.getInstance(servletContext) .setErrorNavigationTargets(routes); }