@Override protected void startUp() throws Exception { LOG.info("Starting Transaction HTTP Service..."); httpService.start(); // Register the service cancelDiscovery = discoveryService.register( ResolvingDiscoverable.of(new Discoverable(Constants.Service.TRANSACTION_HTTP, httpService.getBindAddress()))); LOG.info("Transaction HTTP started successfully on {}", httpService.getBindAddress()); }
/** * Stops the HTTP service gracefully and release all resources. Same as calling {@link #stop(long, long, TimeUnit)} * with {@code 0} second quiet period and {@code 5} seconds timeout. * * @throws Exception if there is exception raised during shutdown. */ public void stop() throws Exception { stop(0, 5, TimeUnit.SECONDS); }
@Override protected void startUp() throws Exception { httpService.startAndWait(); LOG.info("Started successfully on {}", httpService.getBindAddress()); }
NettyHttpService service = NettyHttpService.builder("test-headers") .setHttpHandlers(httpHandler) .build(); service.start(); try { InetSocketAddress bindAddress = service.getBindAddress(); Assert.assertEquals(ImmutableList.of("v2"), headers.get("k2")); } finally { service.stop();
/** * Get the address the service has bound to. * * @return Address the service has bound to. */ public InetSocketAddress getBindAddress() { return httpService.getBindAddress(); } }
@BeforeClass public static void setUp() throws Exception { CConfiguration conf = CConfiguration.create(); conf.set(Constants.Security.Store.FILE_PATH, TEMP_FOLDER.newFolder().getAbsolutePath()); SConfiguration sConf = SConfiguration.create(); sConf.set(Constants.Security.Store.FILE_PASSWORD, "secret"); InMemoryNamespaceAdmin namespaceClient = new InMemoryNamespaceAdmin(); NamespaceMeta namespaceMeta = new NamespaceMeta.Builder() .setName(NAMESPACE1) .build(); namespaceClient.create(namespaceMeta); FileSecureStoreService fileSecureStoreService = new FileSecureStoreService(conf, sConf, namespaceClient); // Starts a mock server to handle remote secure store requests httpService = NettyHttpService.builder("remoteSecureStoreTest") .setHttpHandlers(new SecureStoreHandler(fileSecureStoreService, fileSecureStoreService)) .setExceptionHandler(new HttpExceptionHandler()) .build(); httpService.start(); InMemoryDiscoveryService discoveryService = new InMemoryDiscoveryService(); discoveryService.register(new Discoverable(Constants.Service.SECURE_STORE_SERVICE, httpService.getBindAddress())); remoteSecureStore = new RemoteSecureStore(discoveryService); }
@Inject public LogSaverStatusService(CConfiguration cConf, DiscoveryService discoveryService, @Named(Constants.LogSaver.LOG_SAVER_STATUS_HANDLER) Set<HttpHandler> handlers, MetricsCollectionService metricsCollectionService) { this.discoveryService = discoveryService; this.httpService = NettyHttpService.builder(LogSaverStatusService.class.getName()) .setHttpHandlers(handlers) .setHandlerHooks(ImmutableList.of(new MetricsReporterHook(metricsCollectionService, Constants.LogSaver.LOG_SAVER_STATUS_HANDLER))) .setHost(cConf.get(Constants.LogSaver.ADDRESS)) .build(); }
@Override protected void startUp() throws Exception { httpServer.start(); }
@Before public void beforeTest() throws Exception { httpService = NettyHttpService.builder("test") .setHttpHandlers(new TestHandler()) .build(); httpService.start(); sshSession = new TestSSHSession(getSSHConfig()); proxyServer = new MonitorSocksProxy(CConfiguration.create(), host -> Optional.ofNullable(sshSession).orElseThrow(() -> new IllegalArgumentException("No SSH session available for " + host))); proxyServer.startAndWait(); Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyServer.getBindAddress()); defaultProxySelector = ProxySelector.getDefault(); // Set the proxy for URLConnection ProxySelector.setDefault(new ProxySelector() { @Override public List<Proxy> select(URI uri) { return Collections.singletonList(proxy); } @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { LOG.error("Connect failed {} {}", uri, sa, ioe); } }); }
@Override protected void shutDown() throws Exception { httpService.stopAndWait(); }
NettyHttpService service = NettyHttpService.builder("test-handler-generator") .setHttpHandlers(httpHandler, httpHandlerWithoutAnnotation) .build(); service.start(); try { InetSocketAddress bindAddress = service.getBindAddress(); service.stop();
@Override public String toString() { return Objects.toStringHelper(this) .add("bindAddress", httpService.getBindAddress()) .toString(); } }
/** * Validates the given set of user service handlers. * * @param handlers set of service handlers to validate. * @param <T> type of the handler * @throws IllegalArgumentException if any of the service handler is not valid */ public <T> void validateHttpHandler(Iterable<T> handlers) { List<HttpHandler> httpHandlers = new ArrayList<>(); NoopMetricsContext metricsContext = new NoopMetricsContext(); for (T handler : handlers) { try { @SuppressWarnings("unchecked") TypeToken<T> type = (TypeToken<T>) TypeToken.of(handler.getClass()); httpHandlers.add(createHttpHandler(type, new VerificationDelegateContext<>(handler), metricsContext)); } catch (Exception e) { throw new IllegalArgumentException("Invalid http handler class " + handler.getClass().getName()); } } try { // Constructs a NettyHttpService, to verify that the handlers passed in by the user are valid. NettyHttpService.builder("service-configurer") .setHttpHandlers(httpHandlers) .build(); } catch (Exception e) { throw new IllegalArgumentException("Invalid http handler", e); } }
@Override protected void startUp() throws Exception { httpServer.start(); }
@Override protected void shutDown() throws Exception { httpService.stopAndWait(); }
@Test public void testContentConsumer() throws Exception { HttpHandlerFactory factory = new HttpHandlerFactory("/content", TransactionControl.IMPLICIT); // Create the file upload handler and starts a netty server with it final File outputDir = TEMP_FOLDER.newFolder(); HttpHandler httpHandler = factory.createHttpHandler( TypeToken.of(FileHandler.class), new AbstractDelegatorContext<FileHandler>() { @Override protected FileHandler createHandler() { return new FileHandler(outputDir); } }, new NoopMetricsContext()); // Creates a Netty http server with 1K request buffer NettyHttpService service = NettyHttpService.builder("test-content-consumer") .setHttpHandlers(httpHandler) .setHttpChunkLimit(1024) .build(); service.start(); try { InetSocketAddress bindAddress = service.getBindAddress(); testUpload(outputDir, bindAddress, ""); testUpload(outputDir, bindAddress, "-no-tx"); } finally { service.stop(); } }
@Override protected void startUp() throws Exception { LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(NamespaceId.SYSTEM.getEntityName(), Constants.Logging.COMPONENT_NAME, Constants.Service.DATASET_EXECUTOR)); LOG.info("Starting DatasetOpExecutorService..."); httpService.start(); cancellable = discoveryService.register( ResolvingDiscoverable.of(new Discoverable(Constants.Service.DATASET_EXECUTOR, httpService.getBindAddress()))); LOG.info("DatasetOpExecutorService started successfully on {}", httpService.getBindAddress()); }
@Override protected void shutDown() throws Exception { httpServer.stop(); }
@Override public String toString() { return Objects.toStringHelper(this) .add("bindAddress", httpService.getBindAddress()) .toString(); }
/** * Validates the given set of user service handlers. * * @param handlers set of service handlers to validate. * @param <T> type of the handler * @throws IllegalArgumentException if any of the service handler is not valid */ public <T> void validateHttpHandler(Iterable<T> handlers) { List<HttpHandler> httpHandlers = new ArrayList<>(); NoopMetricsContext metricsContext = new NoopMetricsContext(); for (T handler : handlers) { try { @SuppressWarnings("unchecked") TypeToken<T> type = (TypeToken<T>) TypeToken.of(handler.getClass()); httpHandlers.add(createHttpHandler(type, new VerificationDelegateContext<>(handler), metricsContext)); } catch (Exception e) { throw new IllegalArgumentException("Invalid http handler class " + handler.getClass().getName()); } } try { // Constructs a NettyHttpService, to verify that the handlers passed in by the user are valid. NettyHttpService.builder("service-configurer") .setHttpHandlers(httpHandlers) .build(); } catch (Exception e) { throw new IllegalArgumentException("Invalid http handler", e); } }