serverworker.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 513 行 · 第 1/2 页
JAVA
513 行
} else {
parameters.put(param, null);
}
}
}
if (uri.equals("/favicon.ico")) {
response.setStatusCode(HttpStatus.SC_MOVED_PERMANENTLY);
response.addHeader(LOCATION, "http://ws.apache.org/favicon.ico");
serverHandler.commitResponse(conn, response);
} else if (!uri.startsWith(servicePath)) {
response.setStatusCode(HttpStatus.SC_MOVED_PERMANENTLY);
response.addHeader(LOCATION, servicePath + "/");
serverHandler.commitResponse(conn, response);
} else if (serviceName != null && parameters.containsKey("wsdl")) {
AxisService service = (AxisService) cfgCtx.getAxisConfiguration().
getServices().get(serviceName);
if (service != null) {
try {
response.addHeader(CONTENT_TYPE, TEXT_XML);
serverHandler.commitResponse(conn, response);
service.printWSDL(os, getIpAddress());
} catch (AxisFault e) {
handleException("Axis2 fault writing ?wsdl output", e);
return;
} catch (SocketException e) {
handleException("Error getting ip address for ?wsdl output", e);
return;
}
}
} else if (serviceName != null && parameters.containsKey("wsdl2")) {
AxisService service = (AxisService) cfgCtx.getAxisConfiguration().
getServices().get(serviceName);
if (service != null) {
try {
response.addHeader(CONTENT_TYPE, TEXT_XML);
serverHandler.commitResponse(conn, response);
service.printWSDL2(os, getIpAddress());
} catch (AxisFault e) {
handleException("Axis2 fault writing ?wsdl2 output", e);
return;
} catch (SocketException e) {
handleException("Axis2 fault writing ?wsdl2 output", e);
return;
}
}
} else if (serviceName != null && parameters.containsKey("xsd")) {
if (parameters.get("xsd") == null || "".equals(parameters.get("xsd"))) {
AxisService service = (AxisService) cfgCtx.getAxisConfiguration()
.getServices().get(serviceName);
if (service != null) {
try {
response.addHeader(CONTENT_TYPE, TEXT_XML);
serverHandler.commitResponse(conn, response);
service.printSchema(os);
} catch (AxisFault axisFault) {
handleException("Error writing ?xsd output to client", axisFault);
return;
} catch (IOException e) {
handleException("Error writing ?xsd output to client", e);
return;
}
}
} else {
//cater for named xsds - check for the xsd name
String schemaName = (String) parameters.get("xsd");
AxisService service = (AxisService) cfgCtx.getAxisConfiguration()
.getServices().get(serviceName);
if (service != null) {
//run the population logic just to be sure
service.populateSchemaMappings();
//write out the correct schema
Map schemaTable = service.getSchemaMappingTable();
XmlSchema schema = (XmlSchema)schemaTable.get(schemaName);
if (schema == null) {
int dotIndex = schemaName.indexOf('.');
if (dotIndex > 0) {
String schemaKey = schemaName.substring(0,dotIndex);
schema = (XmlSchema) schemaTable.get(schemaKey);
}
}
//schema found - write it to the stream
if (schema != null) {
response.addHeader(CONTENT_TYPE, TEXT_XML);
serverHandler.commitResponse(conn, response);
schema.write(os);
} else {
// no schema available by that name - send 404
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
}
}
}
} else if (serviceName == null || serviceName.length() == 0) {
try {
response.addHeader(CONTENT_TYPE, TEXT_HTML);
serverHandler.commitResponse(conn, response);
os.write(HTTPTransportReceiver.getServicesHTML(cfgCtx).getBytes());
} catch (IOException e) {
handleException("Error writing ? output to client", e);
}
} else {
if (parameters.isEmpty()) {
AxisService service = (AxisService) cfgCtx.getAxisConfiguration().
getServices().get(serviceName);
if (service != null) {
try {
response.addHeader(CONTENT_TYPE, TEXT_HTML);
serverHandler.commitResponse(conn, response);
os.write(HTTPTransportReceiver.printServiceHTML(serviceName, cfgCtx).getBytes());
} catch (IOException e) {
handleException("Error writing service HTML to client", e);
return;
}
} else {
handleException("Invalid service : " + serviceName, null);
return;
}
} else {
try {
serverHandler.commitResponse(conn, response);
HTTPTransportUtils.processHTTPGetRequest(
msgContext, os,
(request.getFirstHeader(SOAPACTION) != null ?
request.getFirstHeader(SOAPACTION).getValue() : null),
request.getRequestLine().getUri(),
cfgCtx,
parameters);
} catch (AxisFault axisFault) {
handleException("Error processing GET request for: " +
request.getRequestLine().getUri(), axisFault);
}
}
}
// make sure that the output stream is flushed and closed properly
try {
os.flush();
os.close();
} catch (IOException ignore) {}
}
private void handleException(String msg, Exception e) {
if (e == null) {
log.error(msg);
} else {
log.error(msg, e);
}
if (e == null) {
e = new Exception(msg);
}
try {
AxisEngine engine = new AxisEngine(cfgCtx);
MessageContext faultContext = engine.createFaultMessageContext(msgContext, e);
engine.sendFault(faultContext);
} catch (Exception ex) {
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
response.addHeader(CONTENT_TYPE, TEXT_XML);
serverHandler.commitResponse(conn, response);
try {
os.write(msg.getBytes());
if (ex != null) {
os.write(ex.getMessage().getBytes());
}
} catch (IOException ignore) {}
if (conn != null) {
try {
conn.shutdown();
} catch (IOException ignore) {}
}
}
}
public HttpResponse getResponse() {
return response;
}
public OutputStream getOutputStream() {
return os;
}
public InputStream getIs() {
return is;
}
public ServerHandler getServiceHandler() {
return serverHandler;
}
public NHttpServerConnection getConn() {
return conn;
}
/**
* Copied from transport.http of Axis2
*
* Returns the ip address to be used for the replyto epr
* CAUTION:
* This will go through all the available network interfaces and will try to return an ip address.
* First this will try to get the first IP which is not loopback address (127.0.0.1). If none is found
* then this will return this will return 127.0.0.1.
* This will <b>not<b> consider IPv6 addresses.
* <p/>
* TODO:
* - Improve this logic to genaralize it a bit more
* - Obtain the ip to be used here from the Call API
*
* @return Returns String.
* @throws java.net.SocketException
*/
private static String getIpAddress() throws SocketException {
Enumeration e = NetworkInterface.getNetworkInterfaces();
String address = "127.0.0.1";
while (e.hasMoreElements()) {
NetworkInterface netface = (NetworkInterface) e.nextElement();
Enumeration addresses = netface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress ip = (InetAddress) addresses.nextElement();
if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) {
return ip.getHostAddress();
}
}
}
return address;
}
private static boolean isIP(String hostAddress) {
return hostAddress.split("[.]").length == 4;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?