在我们的项目中,我们使用Apache CXF框架实现了SOAP Web服务。客户端用于请求服务器执行某些命令。请求由主机,端口和用于连接的协议组成。如果客户端使用HTTPS配置的端口号并将协议指定为HTTP,那么我们会得到拒绝的连接 - 预期的套接字异常。但是,我需要抛出一个正确的错误消息,如“无法连接到主机”XYZ“与端口”ABC“使用http协议”。为此,我需要在运行时从tomcat server.xml文件中获取配置的http和https端口号,然后将其与我的请求参数进行比较。
任何人,请帮我解决一下这个问题?
答案 0 :(得分:3)
您始终可以解析tomcat的server.xml文件并获取端口值:
public static Integer getTomcatPortFromConfigXml(File serverXml) {
Integer port;
try {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(serverXml);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile
("/Server/Service[@name='Catalina']/Connector[count(@scheme)=0]/@port[1]");
String result = (String) expr.evaluate(doc, XPathConstants.STRING);
port = result != null && result.length() > 0 ? Integer.valueOf(result) : null;
} catch (Exception e) {
port = null;
}
return port;
}
上面的代码应该从server.xml获取HTTP端口。对于HTTPS端口,必须将XPathExpression修改为
XPathExpression expr = xpath.compile
("/Server/Service[@name='Catalina']/Connector[@scheme='https']/@port[1]");
请注意,上述代码段基于这样的假设:server.xml是标准的tomcat服务器文件,其中服务名称定义为" Catalina"。以下是标准的server.xml文件:
<Server>
<Service name="Catalina">
<Connector port="8080">
#...
</Connector>
</Service>
</Server>
参考:Code link
答案 1 :(得分:0)
可以通过这种方式在运行时访问Tomcat核心类(对于Tomcat 7,我没有使用Tomcat 8进行测试):
import java.lang.management.ManagementFactory;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardServer;
import org.apache.log4j.Logger;
public class TomcatConnectors {
public static final String CATALINA_SERVICE_NAME = "Catalina";
public static final String CONNECTOR_HTTP_PROTOCOL_NAME = "HTTP/1.1";
private Logger logger = Logger.getLogger(this.getClass());
private Collection<Connector> connectors;
/**
*
*/
public TomcatConnectors() {
super();
this.connectors = new HashSet<Connector>();
this.loadConnectors();
this.getConnectorPorts();
}
/**
*
* @return
*/
protected StandardServer getServerInstance(){
org.apache.catalina.core.StandardServer server = null;
try{
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
server = (StandardServer)mbeanServer.getAttribute(
new ObjectName("Catalina:type=Server"),
"managedResource"
);
if(logger.isDebugEnabled()){
logger.debug("Server found. Info: ");
logger.debug(" - address : " + server.getAddress());
logger.debug(" - domain : " + server.getDomain());
logger.debug(" - info : " + server.getInfo());
logger.debug(" - shutdown port : " + server.getPort());
logger.debug(" - shutdown command : " + server.getShutdown());
logger.debug(" - serverInfo : " + server.getServerInfo());
logger.debug(" - status : " + server.getStateName());
}
}catch(Throwable t){
logger.fatal("Fatal Error Recovering StandardServer from MBeanServer : " + t.getClass().getName() + ": " + t.getMessage(), t);
}
return server;
}
/*
*
*/
protected Service getCatalinaService(){
org.apache.catalina.core.StandardServer server = this.getServerInstance();
Service[] services = server.findServices();
for(Service aService : services){
if(logger.isDebugEnabled()){
logger.debug("Service: " + aService.getName() +
", info: " + aService.getInfo() +
", state: " + aService.getStateName());
}
if(aService.getName().equalsIgnoreCase(CATALINA_SERVICE_NAME)){
return aService;
}
}
return null;
}
protected void loadConnectors() {
Service catalinaService = this.getCatalinaService();
if(catalinaService == null){
throw new IllegalStateException("Service Catalina cannot be null");
}
if(catalinaService.findConnectors() != null && catalinaService.findConnectors().length > 0){
logger.debug("List of connectors: ");
for(Connector aConnector : catalinaService.findConnectors()){
if(logger.isDebugEnabled()){
logger.debug("Connector.getProtocol: " + aConnector.getProtocol());
logger.debug("Connector.getPort: " + aConnector.getPort());
logger.debug("Connector.getInfo: " + aConnector.getInfo());
logger.debug("Connector.getStateName: " + aConnector.getStateName());
logger.debug("Connector.property.bindOnInit: " + aConnector.getProperty("bindOnInit"));
logger.debug("Connector.attribute.bindOnInit: " + aConnector.getAttribute("bindOnInit"));
logger.debug("Connector.getState: " + aConnector.getState());
}
this.connectors.add(aConnector);
}
}
}
/**
* @return the connectors
*/
public Collection<Connector> getConnectors() {
if(this.connectors.isEmpty()){
this.loadConnectors();
}
return connectors;
}
public Map<String, Set<Integer>> getConnectorPorts(){
if(this.connectors.isEmpty()){
this.loadConnectors();
}
Map<String, Set<Integer>> connectorPorts = new HashMap<String, Set<Integer>>();
for(Connector c: this.connectors){
Set<Integer> set;
if(!connectorPorts.containsKey(c.getProtocol())){
set = new HashSet<Integer>();
set.add(c.getLocalPort());
}else{
set = connectorPorts.get(c.getProtocol());
set.add(c.getLocalPort());
}
connectorPorts.put(c.getProtocol(), set);
}
logger.debug("connectorPorts : " + connectorPorts);
return connectorPorts;
}
}
这是我测试过的配置:
<Service name="Catalina">
<Connector port="8787" protocol="HTTP/1.1" />
<Connector port="8009" protocol="AJP/1.3" />
...
这是输出:
TomcatConnectors:137 - connectorPorts : {HTTP/1.1=[8787], AJP/1.3=[8009]}