【问题标题】:How to enable https for jetty using XmlConfiguration?如何使用 XmlConfiguration 为码头启用 https?
【发布时间】:2016-05-02 15:05:26
【问题描述】:

我们有一个基于 OSGi 的服务器,我们使用嵌入式码头来处理网络流量。
我正在使用XmlConfiguration 创建一个码头服务器实例,请参见下面的代码。
configStream 来自 jetty-http.xml,默认情况下从我们的插件或自定义位置读取。

现在我正在尝试为服务器启用 https。我想加载jetty-ssl.xml和jetty-https.xml和jetty-http.xml一样。

我该怎么做?我无法将另一个流加载到 XmlConfiguration。
是否有另一种方法,可能没有XmlConfiguration?

XmlConfiguration xmlConfig = new XmlConfiguration(configStream);
Object root = xmlConfig.configure();
if (!(root instanceof Server)) {
    throw new IllegalArgumentException("expected a Server object as a root for server configuration"); //$NON-NLS-1$
}
server = (Server) root;

【问题讨论】:

    标签: java https jetty jetty-9


    【解决方案1】:

    我找到了解决方案,请参见下面的代码。
    XmlConfiguration 可用于所有 xml 文件,如果其中两个创建一个 Server 实例(例如 jetty.xml 和 jetty-ssl.xml),则只有一个Server 已创建并将配置/bean 添加到同一实例中。

    List<String> configurations = new ArrayList<String>();
    configurations.add("jetty.xml"); //$NON-NLS-1$
    
    // use pre-configured jetty xml files to construct a server instance
    if (System.getProperty("jetty.sslContext.keyStorePath") != null) { //$NON-NLS-1$
        configurations.add("jetty-ssl.xml"); //$NON-NLS-1$
        configurations.add("jetty-ssl-context.xml"); //$NON-NLS-1$
        configurations.add("jetty-https.xml"); //$NON-NLS-1$
    } else {
        configurations.add("jetty-http.xml"); //$NON-NLS-1$
    }
    
    XmlConfiguration last = null;
    List<Object> objects = new ArrayList<Object>();
    
    for (String configFile : configurations) {
        InputStream configStream = null;
    
        File xmlConfiguration = new File(webserverHome, CONFIG_LOCATION + configFile);
        if (xmlConfiguration.exists()) {
            configStream = new FileInputStream(xmlConfiguration);
            logger.info("Using custom XML configuration {}", xmlConfiguration); //$NON-NLS-1$
        } else {
            // configStream = ... // read from bundle
            logger.info("Using default XML configuration {}/{}", Activator.PLUGIN_ID, CONFIG_LOCATION + configFile); //$NON-NLS-1$
        }
    
        XmlConfiguration configuration = new XmlConfiguration(configStream);
        if (last != null) {
            configuration.getIdMap().putAll(last.getIdMap());
        }
        objects.add(configuration.configure());
        last = configuration;
    }
    
    // first object is a Server instance because of the jetty.xml
    server = (Server) objects.get(0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 2021-01-30
      • 2011-04-23
      • 2014-02-02
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      相关资源
      最近更新 更多