📄 upnphosting.java
字号:
srvDesc.append(" </allowedValueRange>\r\n");
}
}
srvDesc.append(" </stateVariable>\r\n");
}
srvDesc.append(" </serviceStateTable>\r\n</scpd>");
} finally {
}
return srvDesc.toString();
}
/**
* SSDP HTTP Handler.
*
* Handles requests for the /ssdp namespace. This is all requests
* related to the SSDP protocol.
*
* Different cases to handle:
* POST => SOAP => control
* GET => SSDP => description
* SUBSCRIBE => GENA => eventing
* UNSUBSCRIBE => GENA => eventing
*/
private final class SsdpHttpHandler extends HttpRequestHandler {
public final static String NAMESPACE_NAME = "ssdp";
public String allowedMethods() {
return "GET, POST"; //, SUBSCRIBE, UNSUBSCRIBE";
}
/**
* Requests for downloading device and service descriptions.
*/
public byte[] handleGetRequest(HttpRequest req) {
String response = null;
//
// Parse the Request-URI to find the Device's UDN
//
try {
String reqUri = req.getRequestUri();
//
// Request URI will look like:
// /NAMESPACE_NAME/uuid:12345678-90ab-cdef-1234-567890abcde/
//
String deviceUdn = reqUri.substring(
NAMESPACE_NAME.length() + 2);
int i = deviceUdn.indexOf('/');
if (i == -1) {
return STANDARD_RESPONSE_404.getBytes();
}
deviceUdn = deviceUdn.substring(0, i);
LOG.debug("DeviceUDN = '" + deviceUdn + "'");
if(!entities.containsKey(deviceUdn.replace(':', '_'))) {
LOG.warn("Unknown device UDN");
LOG.debug("Entities registered: " + entities.toString());
return STANDARD_RESPONSE_404.getBytes();
}
HostingCacheContainer device =
(HostingCacheContainer)entities.get(deviceUdn.replace(':', '_'));
StringBuffer responseBuf = new StringBuffer(512);
//
// Is the request for the device description or a service
// description?
//
if(reqUri.endsWith("devicedescription.xml")) {
byte[] devDesc = descriptionDocumentCache.getRecord(
device.recordId);
// Write this to the response
//
responseBuf.append("HTTP/1.1 200 OK\r\n");
responseBuf.append("Content-Language: en\r\n");
responseBuf.append(
"Content-Type: text/xml; charset=\"utf-8\"\r\n");
responseBuf.append(
"Content-Length: " + devDesc.length + "\r\n");
responseBuf.append("Connection: close\r\n");
responseBuf.append("\r\n");
response = (responseBuf.toString() + new String(devDesc));
} else if (reqUri.indexOf("/scpd/") != -1) {
//
// Find the service ID
//
// <URLBase>http://10.0.0.1:2580/ssdp/uuid:this-is-a-test/</URLBase>
// <SCPDURL>scpd/urn:this-is-a-test-service</SCPDURL>
//
// Gives: http://address:port/ssdp/uuid:uuid/scpd/urn:serviceid
//
String serviceId = reqUri.substring(
reqUri.indexOf("/scpd/") + 6);
byte[] serviceDesc = descriptionDocumentCache.getRecord(
((HostingCacheContainer)entities.get(serviceId.replace(':', '_'))).recordId); // XXX Is replacement necessary here?
// Write this to the response
//
responseBuf.append("HTTP/1.1 200 OK\r\n");
responseBuf.append("Content-Language: en\r\n");
responseBuf.append("Content-Type: text/xml\r\n");
responseBuf.append("Content-Length: " + serviceDesc.length + "\r\n");
responseBuf.append("\r\n");
response = (responseBuf.toString() + new String(serviceDesc));
} else {
LOG.debug("Unknown request URI (" + reqUri + ")");
return STANDARD_RESPONSE_404.getBytes();
}
} catch(RecordStoreException rsnoe) {
LOG.fatal(rsnoe);
rsnoe.printStackTrace();
StringBuffer responseBuf = new StringBuffer(128);
responseBuf.append("HTTP/1.1 500 Internal Server Error");
responseBuf.append("Content-Length: 0");
responseBuf.append("\r\n");
response = responseBuf.toString();
}
return response.getBytes();
}
public byte[] handlePostRequest(HttpRequest req) {
LOG.debug("handlePostRequest for SSDP");
String reqUri = req.getRequestUri();
//
// Now parse the Request-URI
//
// Skip the leading '/ssdp/'
//
String deviceUdn = reqUri.substring(6);
deviceUdn = deviceUdn.substring(0, deviceUdn.indexOf('/'));
if(!entities.containsKey(deviceUdn)) { // XXX Could be unneccessary...
System.out.println("Unknown device UDN");
// XXX Generate error message here!
System.out.println(entities.toString());
return null;
}
String response = null;
try {
UPnPHostingDevice device =
(UPnPHostingDevice)((HostingCacheContainer)entities.get(
deviceUdn)).entity;
StringBuffer responseBuf = new StringBuffer(512);
//
// Find the ServiceId
String serviceId = (reqUri.substring(
reqUri.indexOf("/control/") + 9)).replace('_', ':');
//
// Does the service exist?
Hashtable serviceTable = device.serviceTable();
if(!serviceTable.containsKey(serviceId)) {
System.out.println("Unknown service ID");
System.out.println(serviceTable.toString());
return null;
}
UPnPHostingService service = (UPnPHostingService)serviceTable.get(serviceId);
if(service == null) {
System.out.println("Service is null. Strange!");
System.out.println(serviceTable.toString());
return null;
}
//
// Check SOAPAction header
String soapAction = null;
if(req.hasHeader("SOAPAction")) {
soapAction = (String)req.getHeader("SOAPAction");
} else if (req.hasHeader("SOAPACTION")) {
soapAction = (String)req.getHeader("SOAPACTION");
} else {
System.out.println("No SOAPAction header found");
return null;
}
if(soapAction.indexOf(service.serviceType()) == -1) {
System.out.println("Wrong service type in SOAPAction header");
return null;
}
String methodName = soapAction.substring(
soapAction.indexOf("#") + 1,
soapAction.length() - 1);
//
// Check if Content-Type is text/xml
String contentType = null;
if(req.hasHeader("Content-Type")) {
contentType = (String)req.getHeader("Content-Type");
} else if(req.hasHeader("CONTENT-TYPE")) {
contentType = (String)req.getHeader("CONTENT-TYPE");
} else {
System.out.println("No Content-Type found. Precondition failed?");
return null;
}
if(!contentType.startsWith("text/xml")) {
System.out.println(
"Wrong content type(" + contentType +
". 415 Unsupported Media Type");
return null;
}
//
// XXX Use Content-Type charset property for String.
// It SHOULD be utf-8
//
//
// Process the body of the HTTP request (SOAP message)
//
UPnPAction method = (UPnPAction)service.actionTable().get(methodName);
UPnPActionArgument[] args = method.getArgumentList();
boolean hasInArguments = false;
boolean hasOutArguments = false;
for(int i = 0; i < args.length; i++) {
switch(args[i].getDirection()) {
case UPnPActionArgument.ARGUMENT_DIRECTION_IN:
hasInArguments = true;
break;
case UPnPActionArgument.ARGUMENT_DIRECTION_OUT:
hasOutArguments = true;
break;
}
}
SoapRequestHandler handler = new SoapRequestHandler(service, args);
try {
SAXParser parser = pfactory.newSAXParser();
parser.parse(
new ByteArrayInputStream(req.getBody().getBytes()),
handler);
} catch (SAXException se) {
System.out.println("SAXException caught");
se.printStackTrace();
}
UPnPActionInvocationException methodInvokeException = null;
if (!handler.hasFaulted()) {
try {
method.invoke(args);
} catch(UPnPActionInvocationException e) {
methodInvokeException = e;
}
}
StringBuffer responseBody = new StringBuffer(128);
responseBody.append("<?xml version=\"1.0\"?>\r\n");
responseBody.append("<s:Envelope ");
responseBody.append(
"xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
responseBody.append(
"s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>\r\n");
responseBody.append(" <s:Body>\r\n");
if (handler.hasFaulted()) {
UPnPActionInvocationException exception =
handler.getException();
responseBody.append(" <s:Fault>\r\n");
responseBody.append(" <faultcode>");
SoapFault fault = exception.getFault();
if (fault.getCodeUri().equals(
"http://schemas.xmlsoap.org/soap/envelope/"))
{
responseBody.append("s:");
} else {
responseBody.append(fault.getCodeUri());
}
responseBody.append(fault.getCode()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -