io.dropwizard.jetty.HttpConnectorFactory Java Examples

The following examples show how to use io.dropwizard.jetty.HttpConnectorFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComplianceToolModeConfigurationFactory.java    From verify-service-provider with MIT License 6 votes vote down vote up
private ComplianceToolModeConfiguration createComplianceToolModeConfiguration() throws IOException {
    String serviceEntityId = UUID.randomUUID().toString();
    KeysAndCert signingKeysAndCert = createKeysAndCert(serviceEntityId);
    KeysAndCert encryptionKeysAndCert = createKeysAndCert(serviceEntityId);

    ComplianceToolModeConfiguration complianceToolModeConfiguration = new ComplianceToolModeConfiguration(serviceEntityId, signingKeysAndCert, encryptionKeysAndCert);

    HttpConnectorFactory httpConnectorFactory = new HttpConnectorFactory();
    httpConnectorFactory.setPort(port);
    httpConnectorFactory.setBindHost(bindHost);
    SimpleServerFactory simpleServerFactory = new SimpleServerFactory();
    simpleServerFactory.setApplicationContextPath("/");
    simpleServerFactory.setConnector(httpConnectorFactory);
    complianceToolModeConfiguration.setServerFactory(simpleServerFactory);
    complianceToolModeConfiguration.setLoggingFactory(new DefaultLoggingFactory());

    return complianceToolModeConfiguration;
}
 
Example #2
Source File: SelfHostAndPortModule.java    From emodb with Apache License 2.0 6 votes vote down vote up
private HostAndPort getHostAndPortFromConnectorFactories(List<ConnectorFactory> connectors) {
    // find the first connector that matches and return it host/port information (in practice there should
    // be one, and just one, match)
    try {
        HttpConnectorFactory httpConnectorFactory = (HttpConnectorFactory) Iterables.find(connectors, Predicates.instanceOf(HttpConnectorFactory.class));

        String host = httpConnectorFactory.getBindHost();
        if (host == null) {
            host = getLocalHost().getHostAddress();
        }
        int port = httpConnectorFactory.getPort();
        return HostAndPort.fromParts(host, port);
    } catch (NoSuchElementException ex) {
        throw new IllegalStateException("Did not find a valid HttpConnector for the server", ex);
    }
}
 
Example #3
Source File: ServiceUtil.java    From helios with Apache License 2.0 6 votes vote down vote up
public static DefaultServerFactory createServerFactory(final InetSocketAddress httpEndpoint,
                                                       final InetSocketAddress adminEndpoint,
                                                       final boolean noHttp) {
  final DefaultServerFactory serverFactory = new DefaultServerFactory();
  if (noHttp) {
    serverFactory.setApplicationConnectors(Collections.<ConnectorFactory>emptyList());
    serverFactory.setAdminConnectors(Collections.<ConnectorFactory>emptyList());
  } else {
    final HttpConnectorFactory serviceConnector = new HttpConnectorFactory();
    serviceConnector.setPort(httpEndpoint.getPort());
    serviceConnector.setBindHost(httpEndpoint.getHostString());
    serverFactory.setApplicationConnectors(ImmutableList.<ConnectorFactory>of(serviceConnector));

    final HttpConnectorFactory adminConnector = new HttpConnectorFactory();
    adminConnector.setPort(adminEndpoint.getPort());
    adminConnector.setBindHost(adminEndpoint.getHostString());
    serverFactory.setAdminConnectors(ImmutableList.<ConnectorFactory>of(adminConnector));
  }
  return serverFactory;
}
 
Example #4
Source File: ServerUtils.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
public static int port(ServerFactory serverFactory) {
    if(serverFactory instanceof SimpleServerFactory) {
        SimpleServerFactory simpleServerFactory = (SimpleServerFactory)serverFactory;
        return getPortFromConnector(simpleServerFactory.getConnector());

    }
    if(serverFactory instanceof DefaultServerFactory) {
        DefaultServerFactory defaultServerFactory = (DefaultServerFactory)serverFactory;
        for(ConnectorFactory connectorFactory : defaultServerFactory.getApplicationConnectors()) {
            if(connectorFactory instanceof HttpConnectorFactory) {
                return getPortFromConnector(connectorFactory);
            }
        }

    }
    throw new PortExtractionException("Cannot extract port from connector");
}
 
Example #5
Source File: BaragonAgentServiceModule.java    From Baragon with Apache License 2.0 6 votes vote down vote up
@Provides
@Singleton
public BaragonAgentMetadata providesAgentMetadata(BaragonAgentConfiguration config) throws Exception {
  final SimpleServerFactory simpleServerFactory = (SimpleServerFactory) config.getServerFactory();
  final HttpConnectorFactory httpFactory = (HttpConnectorFactory) simpleServerFactory.getConnector();

  final int httpPort = httpFactory.getPort();
  final String hostname = config.getHostname().or(JavaUtils.getHostAddress());
  final Optional<String> domain = config.getLoadBalancerConfiguration().getDomain();
  final String appRoot = simpleServerFactory.getApplicationContextPath();

  final String baseAgentUri = String.format(config.getBaseUrlTemplate(), hostname, httpPort, appRoot);
  final String agentId = String.format("%s:%s", hostname, httpPort);

  return new BaragonAgentMetadata(baseAgentUri, agentId, domain, BaragonAgentEc2Metadata.fromEnvironment(config.getPrivateIp(), config.isSkipPrivateIp()), config.getGcloudMetadata(), config.getExtraAgentData(), true);
}
 
Example #6
Source File: DoctorKafkaMain.java    From doctorkafka with Apache License 2.0 5 votes vote down vote up
private void configureServerRuntime(DoctorKafkaAppConfig configuration, DoctorKafkaConfig config) {
  DefaultServerFactory defaultServerFactory = 
      (DefaultServerFactory) configuration.getServerFactory();

  // Disable gzip compression for HTTP, this is required in-order to make
  // Server-Sent-Events work, else due to GZIP the browser waits for entire chunks
  // to arrive thereby the UI receiving no events
  // We are programmatically disabling it here so it makes it easy to launch
  // Firefly
  GzipHandlerFactory gzipHandlerFactory = new GzipHandlerFactory();
  gzipHandlerFactory.setEnabled(false);
  defaultServerFactory.setGzipFilterFactory(gzipHandlerFactory);
  // Note that if someone explicitly enables gzip in the Dropwizard config YAML
  // then
  // this setting will be over-ruled causing the UI to stop working

  // Disable HTTP request logging
  LogbackAccessRequestLogFactory accessRequestLogFactory = new LogbackAccessRequestLogFactory();
  accessRequestLogFactory.setAppenders(ImmutableList.of());
  defaultServerFactory.setRequestLogFactory(accessRequestLogFactory);

  // Disable admin connector
  defaultServerFactory.setAdminConnectors(ImmutableList.of());

  // Configure bind host and port number
  HttpConnectorFactory application = (HttpConnectorFactory) HttpConnectorFactory.application();
  application.setPort(config.getWebserverPort());
  application.setBindHost(config.getWebserverBindHost());
  defaultServerFactory.setApplicationConnectors(Collections.singletonList(application));
}
 
Example #7
Source File: ScanUploadTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
private static void updatePortsToAvoidCollision(Collection<ConnectorFactory> connectorFactoryCollection) {
    for (ConnectorFactory connectorFactory : connectorFactoryCollection) {
        if (connectorFactory instanceof HttpConnectorFactory) {
            HttpConnectorFactory httpConnectorFactory = (HttpConnectorFactory)connectorFactory;
            httpConnectorFactory.setPort(httpConnectorFactory.getPort() + 100);
        }
    }
}
 
Example #8
Source File: BaseRoleConnectHelper.java    From emodb with Apache License 2.0 5 votes vote down vote up
protected String getAdminBaseURI () {
    int httpPort = 0;
    for(ConnectorFactory connector: ((DefaultServerFactory)_config.getServerFactory()).getAdminConnectors()) {
        if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) {
            httpPort = ((HttpConnectorFactory) connector).getPort();
            break;
        }
    }

    return format("http://localhost:%d", httpPort);
}
 
Example #9
Source File: BaseRoleConnectHelper.java    From emodb with Apache License 2.0 5 votes vote down vote up
protected String getServiceBaseURI () {
    int port = 0;
    for(ConnectorFactory connector: ((DefaultServerFactory)_config.getServerFactory()).getApplicationConnectors()) {
        if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) {
            port = ((HttpConnectorFactory) connector).getPort();
            break;
        }
    }

    return format("http://localhost:%d", port);
}
 
Example #10
Source File: BaseRoleConnectHelper.java    From emodb with Apache License 2.0 5 votes vote down vote up
protected int getServiceBasePort () {
    int port = 0;
    for(ConnectorFactory connector: ((DefaultServerFactory)_config.getServerFactory()).getApplicationConnectors()) {
        if (connector.getClass().isAssignableFrom(HttpConnectorFactory.class)) {
            port = ((HttpConnectorFactory) connector).getPort();
            break;
        }
    }

    return port;
}
 
Example #11
Source File: SoaBundle.java    From soabase with Apache License 2.0 5 votes vote down vote up
private static HostAndPort portFromConnectorFactory(ConnectorFactory connectorFactory)
{
    if ( (connectorFactory != null) && HttpConnectorFactory.class.isAssignableFrom(connectorFactory.getClass()) )
    {
        HttpConnectorFactory factory = (HttpConnectorFactory)connectorFactory;
        String bindHost = MoreObjects.firstNonNull(factory.getBindHost(), "localhost");
        return HostAndPort.fromParts(bindHost, factory.getPort());
    }
    return defaultHostAndPort;
}
 
Example #12
Source File: RandomPortsListener.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@Override
public void onRun(final Configuration configuration,
                  final Environment environment,
                  final DropwizardTestSupport<Configuration> rule) throws Exception {
    final ServerFactory server = configuration.getServerFactory();
    if (server instanceof SimpleServerFactory) {
        ((HttpConnectorFactory) ((SimpleServerFactory) server).getConnector()).setPort(0);
    } else {
        final DefaultServerFactory dserv = (DefaultServerFactory) server;
        ((HttpConnectorFactory) dserv.getApplicationConnectors().get(0)).setPort(0);
        ((HttpConnectorFactory) dserv.getAdminConnectors().get(0)).setPort(0);
    }
}
 
Example #13
Source File: MetadataResourceApp.java    From eagle with Apache License 2.0 4 votes vote down vote up
@Override
public void run(Configuration configuration, Environment environment) throws Exception {
    ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()).getApplicationConnectors().get(0)).setPort(0);
    ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()).getAdminConnectors().get(0)).setPort(0);
    environment.jersey().register(MetadataResource.class);
}
 
Example #14
Source File: EagleMetricResourceApp.java    From eagle with Apache License 2.0 4 votes vote down vote up
@Override
public void run(Configuration configuration, Environment environment) throws Exception {
    ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()).getApplicationConnectors().get(0)).setPort(0);
    ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()).getAdminConnectors().get(0)).setPort(0);
    environment.jersey().register(EagleMetricResource.class);
}
 
Example #15
Source File: StreamlineApplication.java    From streamline with Apache License 2.0 4 votes vote down vote up
private void registerResources(StreamlineConfiguration configuration, Environment environment, Subject subject) throws ConfigException,
        ClassNotFoundException, IllegalAccessException, InstantiationException {
    StorageManager storageManager = getDao(configuration);
    TransactionManager transactionManager;
    if (storageManager instanceof TransactionManager) {
        transactionManager = (TransactionManager) storageManager;
    } else {
        transactionManager = new NOOPTransactionManager();
    }
    environment.jersey().register(new TransactionEventListener(transactionManager, true));
    Collection<Class<? extends Storable>> streamlineEntities = getStorableEntities();
    storageManager.registerStorables(streamlineEntities);
    LOG.info("Registered streamline entities {}", streamlineEntities);
    FileStorage fileStorage = this.getJarStorage(configuration, storageManager);
    int appPort = ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()).getApplicationConnectors().get(0)).getPort();
    String catalogRootUrl = configuration.getCatalogRootUrl().replaceFirst("8080", appPort + "");
    List<ModuleConfiguration> modules = configuration.getModules();
    List<Object> resourcesToRegister = new ArrayList<>();

    // add StreamlineConfigResource
    resourcesToRegister.add(new StreamlineConfigurationResource(configuration));

    // authorizer
    StreamlineAuthorizer authorizer;
    AuthorizerConfiguration authorizerConf = configuration.getAuthorizerConfiguration();
    SecurityCatalogService securityCatalogService = new SecurityCatalogService(storageManager);
    if (authorizerConf != null) {
        authorizer = ((Class<StreamlineAuthorizer>) Class.forName(authorizerConf.getClassName())).newInstance();
        Map<String, Object> authorizerConfig = new HashMap<>();
        authorizerConfig.put(DefaultStreamlineAuthorizer.CONF_CATALOG_SERVICE, securityCatalogService);
        authorizerConfig.put(DefaultStreamlineAuthorizer.CONF_ADMIN_PRINCIPALS, authorizerConf.getAdminPrincipals());
        authorizer.init(authorizerConfig);
        String filterClazzName = authorizerConf.getContainerRequestFilter();
        ContainerRequestFilter filter;
        if (StringUtils.isEmpty(filterClazzName)) {
            filter = new StreamlineKerberosRequestFilter(); // default
        } else {
            filter = ((Class<ContainerRequestFilter>) Class.forName(filterClazzName)).newInstance();
        }
        LOG.info("Registering ContainerRequestFilter: {}", filter.getClass().getCanonicalName());
        environment.jersey().register(filter);
    } else {
        LOG.info("Authorizer config not set, setting noop authorizer");
        String noopAuthorizerClassName = "com.hortonworks.streamline.streams.security.impl.NoopAuthorizer";
        authorizer = ((Class<StreamlineAuthorizer>) Class.forName(noopAuthorizerClassName)).newInstance();
    }

    for (ModuleConfiguration moduleConfiguration: modules) {
        String moduleName = moduleConfiguration.getName();
        String moduleClassName = moduleConfiguration.getClassName();
        LOG.info("Registering module [{}] with class [{}]", moduleName, moduleClassName);
        ModuleRegistration moduleRegistration = (ModuleRegistration) Class.forName(moduleClassName).newInstance();
        if (moduleConfiguration.getConfig() == null) {
            moduleConfiguration.setConfig(new HashMap<String, Object>());
        }
        if (moduleName.equals(Constants.CONFIG_STREAMS_MODULE)) {
            moduleConfiguration.getConfig().put(Constants.CONFIG_CATALOG_ROOT_URL, catalogRootUrl);
        }
        Map<String, Object> initConfig = new HashMap<>(moduleConfiguration.getConfig());
        initConfig.put(Constants.CONFIG_AUTHORIZER, authorizer);
        initConfig.put(Constants.CONFIG_SECURITY_CATALOG_SERVICE, securityCatalogService);
        initConfig.put(Constants.CONFIG_SUBJECT, subject);
        if ((initConfig.get("proxyUrl") != null) && (configuration.getHttpProxyUrl() == null || configuration.getHttpProxyUrl().isEmpty())) {
            LOG.warn("Please move proxyUrl, proxyUsername and proxyPassword configuration properties under streams module to httpProxyUrl, " +
                    "httpProxyUsername and httpProxyPassword respectively at top level in your streamline.yaml");
            configuration.setHttpProxyUrl((String) initConfig.get("proxyUrl"));
            configuration.setHttpProxyUsername((String) initConfig.get("proxyUsername"));
            configuration.setHttpProxyPassword((String) initConfig.get("proxyPassword"));
        }
        // pass http proxy information from top level config to each module. Up to them how they want to use it. Currently used in StreamsModule
        initConfig.put(Constants.CONFIG_HTTP_PROXY_URL, configuration.getHttpProxyUrl());
        initConfig.put(Constants.CONFIG_HTTP_PROXY_USERNAME, configuration.getHttpProxyUsername());
        initConfig.put(Constants.CONFIG_HTTP_PROXY_PASSWORD, configuration.getHttpProxyPassword());
        moduleRegistration.init(initConfig, fileStorage);
        if (moduleRegistration instanceof StorageManagerAware) {
            LOG.info("Module [{}] is StorageManagerAware and setting StorageManager.", moduleName);
            StorageManagerAware storageManagerAware = (StorageManagerAware) moduleRegistration;
            storageManagerAware.setStorageManager(storageManager);
        }
        if (moduleRegistration instanceof TransactionManagerAware) {
            LOG.info("Module [{}] is TransactionManagerAware and setting TransactionManager.", moduleName);
            TransactionManagerAware transactionManagerAware = (TransactionManagerAware) moduleRegistration;
            transactionManagerAware.setTransactionManager(transactionManager);
        }
        resourcesToRegister.addAll(moduleRegistration.getResources());

    }

    LOG.info("Registering resources to Jersey environment: [{}]", resourcesToRegister);
    for(Object resource : resourcesToRegister) {
        environment.jersey().register(resource);
    }
    environment.jersey().register(MultiPartFeature.class);

    final ErrorPageErrorHandler errorPageErrorHandler = new ErrorPageErrorHandler();
    errorPageErrorHandler.addErrorPage(Response.Status.UNAUTHORIZED.getStatusCode(), "/401.html");
    environment.getApplicationContext().setErrorHandler(errorPageErrorHandler);
}
 
Example #16
Source File: ServerUtils.java    From foxtrot with Apache License 2.0 4 votes vote down vote up
private static int getPortFromConnector(ConnectorFactory connectorFactory) {
    if(connectorFactory instanceof HttpConnectorFactory) {
        return ((HttpConnectorFactory)connectorFactory).getPort();
    }
    throw new PortExtractionException("Cannot extract port from connector");
}