/** * Finds the {@link List} of {@link VirtualHost}s that contains the specified {@link Service}. If there's * no match, an empty {@link List} is returned. Note that this is potentially an expensive operation and * thus should not be used in a performance-sensitive path. */ public List<VirtualHost> findVirtualHosts(Service<?, ?> service) { requireNonNull(service, "service"); @SuppressWarnings("rawtypes") final Class<? extends Service> serviceType = service.getClass(); final List<VirtualHost> res = new ArrayList<>(); for (VirtualHost h : virtualHosts) { for (ServiceConfig c : h.serviceConfigs()) { // Consider the case where the specified service is decorated before being added. final Service<?, ?> s = c.service(); @SuppressWarnings("rawtypes") final Optional<? extends Service> sOpt = s.as(serviceType); if (!sOpt.isPresent()) { continue; } if (sOpt.get() == service) { res.add(c.virtualHost()); break; } } } return res; }
private static ServiceConfig findServiceConfig(Server server, String path, Service<?, ?> service) { for (ServiceConfig cfg : server.config().defaultVirtualHost().serviceConfigs()) { final Optional<String> exactPath = cfg.pathMapping().exactPath(); if (!exactPath.isPresent()) { continue; } if (!path.equals(exactPath.get())) { continue; } if (cfg.service().as(service.getClass()).isPresent()) { return cfg; } } throw new Error(); // Never reaches here. } }
@Override public String toString() { String strVal = this.strVal; if (strVal == null) { this.strVal = strVal = toString( getClass(), defaultHostname(), hostnamePattern(), sslContext(), serviceConfigs()); } return strVal; }
private VirtualHost normalizeDefaultVirtualHost(VirtualHost h, @Nullable SslContext defaultSslContext) { final SslContext sslCtx = h.sslContext() != null ? h.sslContext() : defaultSslContext; return new VirtualHost( h.defaultHostname(), "*", sslCtx, h.serviceConfigs().stream().map( e -> new ServiceConfig(e.pathMapping(), e.service(), e.loggerName().orElse(null))) .collect(Collectors.toList()), h.producibleMediaTypes(), rejectedPathMappingHandler, host -> h.accessLogger()); }
if (virtualHostsCopy.stream().allMatch(h -> h.serviceConfigs().isEmpty())) { throw new IllegalArgumentException("no services in the server"); .flatMap(h -> h.serviceConfigs().stream()) .collect(toImmutableList());
virtualHosts.forEach(c -> { buf.append(VirtualHost.toString(null, c.defaultHostname(), c.hostnamePattern(), c.sslContext(), c.serviceConfigs())); buf.append(", "); }); buf.append(VirtualHost.toString(null, defaultVirtualHost.defaultHostname(), "*", defaultVirtualHost.sslContext(), defaultVirtualHost.serviceConfigs())); } else { buf.setLength(buf.length() - 2); buf.append(VirtualHost.toString(null, defaultVirtualHost.defaultHostname(), "*", defaultVirtualHost.sslContext(), defaultVirtualHost.serviceConfigs()));
private static Service<?, ?> service(ServerBuilder sb) { final Server server = sb.build(); return server.config().defaultVirtualHost().serviceConfigs().get(0).service(); }