📄 .#contentdirectoryservice.java.1.3
字号:
}
} catch (NoSuchElementException nse) {
LOG.fatal(nse);
}
}
/**
* Retrieve metadata for an object.
*
* For example Windows Media Connect (WMC) does not include metadata in
* the response to a Browse request with the BrowseDirectChildren flag.
*
* @param objectId Requested object
*
* @return MediaContent for the specific object. Notice that the callee
* should use the instanceof operator to test what kind of
* object was returned (one of the classes derived from
* MediaContent).
*
* @throws IOException
*/
public MediaContent browseMetadata(String objectId) throws IOException {
MediaContent mc = null;
try {
// XXX MetadataResultHandler mrh = new MetadataResultHandler();
// XXX invokeBrowse(objectID, BROWSEFLAG_METADATA, 0, 1, resultParser);
// Do something with mrh?
} catch (NoSuchElementException nse) {
LOG.fatal(nse);
}
return mc;
}
/**
* Invokes the browse action at the server side.
*
* Used by the public methods browseDirectChildren and browseMetadata.
*/
private void invokeBrowse(
String objectID,
int browseFlag,
int startIndex,
int requestedCount,
DefaultHandler resultParser) throws IOException
{
SoapInvoke si = new SoapInvoke(
"\"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"",
getControlURL());
StringBuffer buffer = new StringBuffer();
// XXX: Not all devices like to get the <?xml..?> part. Should perhaps
// try first to send a request with the <?xml..?> and if an error
// is returned then try to send a new request without.
//
buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
buffer.append(
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"");
buffer.append(
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n");
buffer.append("<s:Body>\r\n");
buffer.append
("<u:Browse xmlns:u=\"urn:schemas-upnp-org:service:");
buffer.append("contentDirectory:1\">\r\n");
buffer.append("<ObjectID>" + objectID + "</ObjectID>\r\n");
buffer.append("<BrowseFlag>");
buffer.append(
(browseFlag == BROWSEFLAG_DIRECT_CHILDREN) ?
"BrowseDirectChildren" : "BrowseMetadata");
buffer.append("</BrowseFlag>\r\n");
buffer.append("<Filter>*</Filter>\r\n");
buffer.append(
"<StartingIndex>" + startIndex + "</StartingIndex>\r\n");
buffer.append(
"<RequestedCount>" + requestedCount + "</RequestedCount>\r\n");
buffer.append("<SortCriteria></SortCriteria>\r\n");
buffer.append("</u:Browse>\r\n");
buffer.append("</s:Body>\r\n");
buffer.append("</s:Envelope>\r\n");
si.setBody(buffer.toString());
String resp = si.invoke(
"\"urn:schemas-upnp-org:service:ContentDirectory:1#Browse\"");
GenericSoapResultParser gparser = new GenericSoapResultParser(
new ByteArrayInputStream(resp.getBytes()),
resultParser);
gparser.parseSoapResult();
try {
if (resultParser instanceof ContentContainerHandler) {
BrowseResultParser brp = (BrowseResultParser)resultParser;
brp.setNumberReturned(gparser.getNumberReturned());
brp.setTotalMatches(gparser.getTotalMatches());
} else {
System.out.println(
"Nope, result parser does not implement BrowseResultParser.");
}
/*
} catch (ClassNotFoundException cnfe) {
System.out.println("Did not find the BrowseResultParser class...");
*/
} finally {
}
}
private class GenericSoapResultParser {
private InputStream is;
private DefaultHandler resultParserHandler;
private SAXParser parser;
private GenericSoapResultHandler gh;
public GenericSoapResultParser(InputStream is, DefaultHandler dh) {
try {
SAXParserFactory pfactory = SAXParserFactory.newInstance();
parser = pfactory.newSAXParser();
this.is = is;
resultParserHandler = dh;
} catch (SAXException se) {
LOG.debug("SAXException caught in GSRP::ctor");
LOG.debug(se);
} catch (ParserConfigurationException pce) {
LOG.debug("PCE in GSRP::ctor");
LOG.debug(pce);
}
}
public void parseSoapResult() {
try {
gh = new GenericSoapResultHandler();
parser.parse(is, gh);
parser.parse(
new ByteArrayInputStream(
gh.resultElement.getBytes()),
resultParserHandler);
} catch (SAXException se) {
LOG.debug("Error in generic soap result parser");
LOG.debug(se);
} catch (IOException ioe) {
LOG.debug("IOException in GSRP");
LOG.debug(ioe);
}
}
public int getNumberReturned() {
return gh.numberReturned;
}
public int getTotalMatches() {
return gh.totalMatches;
}
private class GenericSoapResultHandler extends DefaultHandler {
public String resultElement;
public int totalMatches = 0;
public int numberReturned = 0;
private int currentElement;
private final int ELEMENT_IS_UNKNOWN = -1;
private final int ELEMENT_IS_RESULT = 0;
private final int ELEMENT_IS_NUMBER_RETURNED = 1;
private final int ELEMENT_IS_TOTAL_MATCHES = 2;
public GenericSoapResultHandler() {
currentElement = ELEMENT_IS_UNKNOWN;
}
public void characters(
char[] ch,
int start,
int length) throws SAXException
{
String txt = new String(ch, start, length);
switch(currentElement) {
case ELEMENT_IS_RESULT:
resultElement = txt;
break;
case ELEMENT_IS_NUMBER_RETURNED:
numberReturned = Integer.parseInt(txt);
break;
case ELEMENT_IS_TOTAL_MATCHES:
totalMatches = Integer.parseInt(txt);
break;
}
currentElement = ELEMENT_IS_UNKNOWN;
}
public void startElement(
String uri,
String localName,
String qName,
Attributes attr) throws SAXException
{
if (qName.equals("Result")) {
currentElement = ELEMENT_IS_RESULT;
} else if (qName.equals("NumberReturned")) {
currentElement = ELEMENT_IS_NUMBER_RETURNED;
} else if (qName.equals("TotalMatches")) {
currentElement = ELEMENT_IS_TOTAL_MATCHES;
} else {
currentElement = ELEMENT_IS_UNKNOWN;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -