Search in sources :

Example 1 with SimpleXMLParserDocument

use of com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument in project BiglyBT by BiglySoftware.

the class TOTorrentXMLDeserialiser method deserialise.

public TOTorrent deserialise(File file) throws TOTorrentException {
    try {
        SimpleXMLParserDocument doc = SimpleXMLParserDocumentFactory.create(file);
        TOTorrent res = decodeRoot(doc);
        return (res);
    } catch (SimpleXMLParserDocumentException e) {
        throw (new TOTorrentException("XML Parse Fails: " + e.getMessage(), TOTorrentException.RT_DECODE_FAILS));
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) SimpleXMLParserDocumentException(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException) SimpleXMLParserDocument(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 2 with SimpleXMLParserDocument

use of com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument in project BiglyBT by BiglySoftware.

the class UPnPServiceImpl method loadDescription.

protected void loadDescription() throws UPnPException {
    SimpleXMLParserDocument doc = device.getUPnP().downloadXML(device, getDescriptionURL());
    parseActions(doc.getChild("ActionList"));
    parseStateVars(doc.getChild("ServiceStateTable"));
}
Also used : SimpleXMLParserDocument(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument)

Example 3 with SimpleXMLParserDocument

use of com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument in project BiglyBT by BiglySoftware.

the class UPnPActionInvocationImpl method invoke.

@Override
public UPnPActionArgument[] invoke() throws UPnPException {
    UPnPService service = action.getService();
    String soap_action = service.getServiceType() + "#" + action.getName();
    SimpleXMLParserDocument resp_doc = null;
    try {
        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" + "  <s:Body>\n";
        request += "    <u:" + action.getName() + " xmlns:u=\"" + service.getServiceType() + "\">\n";
        for (int i = 0; i < arg_names.size(); i++) {
            String name = (String) arg_names.get(i);
            String value = (String) arg_values.get(i);
            request += "      <" + name + ">" + value + "</" + name + ">\n";
        }
        request += "    </u:" + action.getName() + ">\n";
        request += "  </s:Body>\n" + "</s:Envelope>";
        // try standard POST
        resp_doc = ((UPnPDeviceImpl) action.getService().getDevice()).getUPnP().performSOAPRequest(service, soap_action, request);
        SimpleXMLParserDocumentNode body = resp_doc.getChild("Body");
        SimpleXMLParserDocumentNode faultSection = body.getChild("Fault");
        if (faultSection != null) {
            String faultValue = faultSection.getValue();
            if (faultValue != null && faultValue.length() > 0) {
                throw (new UPnPException("Invoke of '" + soap_action + "' failed - fault reported: " + faultValue, soap_action, action, resp_doc, faultValue, -1));
            }
            SimpleXMLParserDocumentNode faultDetail = faultSection.getChild("detail");
            if (faultDetail != null) {
                SimpleXMLParserDocumentNode error = faultDetail.getChild("UPnPError");
                if (error != null) {
                    int errCodeNumber = -1;
                    String errDescValue = null;
                    SimpleXMLParserDocumentNode errCode = error.getChild("errorCode");
                    if (errCode != null) {
                        String errCodeValue = errCode.getValue();
                        try {
                            errCodeNumber = Integer.parseInt(errCodeValue);
                        } catch (Throwable t) {
                        }
                    }
                    SimpleXMLParserDocumentNode errDesc = error.getChild("errorDescription");
                    if (errDesc != null) {
                        errDescValue = errDesc.getValue();
                        if (errDescValue != null && errDescValue.length() == 0) {
                            errDescValue = null;
                        }
                    }
                    throw (new UPnPException("Invoke of '" + soap_action + "' failed - fault reported: " + errDescValue, soap_action, action, resp_doc, errDescValue, errCodeNumber));
                }
            }
        }
        SimpleXMLParserDocumentNode resp_node = body.getChild(action.getName() + "Response");
        if (resp_node == null) {
            throw (new UPnPException("Invoke of '" + soap_action + "' failed - response missing: " + body.getValue(), soap_action, action, resp_doc, null, -1));
        }
        SimpleXMLParserDocumentNode[] out_nodes = resp_node.getChildren();
        UPnPActionArgument[] resp = new UPnPActionArgument[out_nodes.length];
        for (int i = 0; i < out_nodes.length; i++) {
            resp[i] = new UPnPActionArgumentImpl(out_nodes[i].getName(), out_nodes[i].getValue());
        }
        return (resp);
    } catch (Throwable e) {
        if (e instanceof UPnPException) {
            throw ((UPnPException) e);
        }
        throw new UPnPException("Invoke of '" + soap_action + "' on '" + action.getService().getControlURLs() + "' failed: " + e.getMessage(), e, soap_action, action, resp_doc);
    }
}
Also used : SimpleXMLParserDocument(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument) UPnPActionArgument(com.biglybt.net.upnp.UPnPActionArgument) UPnPService(com.biglybt.net.upnp.UPnPService) UPnPDeviceImpl(com.biglybt.net.upnp.impl.device.UPnPDeviceImpl) SimpleXMLParserDocumentNode(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentNode) UPnPException(com.biglybt.net.upnp.UPnPException)

Example 4 with SimpleXMLParserDocument

use of com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument in project BiglyBT by BiglySoftware.

the class UPnPImpl method downloadXMLSupport.

protected SimpleXMLParserDocument downloadXMLSupport(String friendly_name, URL url) throws UPnPException {
    String url_str = url.toExternalForm();
    boolean record_failure = true;
    try {
        TorrentUtils.setTLSDescription("UPnP Device" + (friendly_name == null ? "" : (": " + friendly_name)));
        ResourceDownloaderFactory rdf = adapter.getResourceDownloaderFactory();
        int retries;
        synchronized (failed_urls) {
            long[] fails = failed_urls.get(url_str);
            if (fails == null) {
                retries = 3;
            } else {
                long consec_fails = fails[0];
                long last_fail = fails[1];
                long max_period = 10 * 60 * 1000;
                long period = 60 * 1000;
                for (int i = 0; i < consec_fails; i++) {
                    period <<= 1;
                    if (period >= max_period) {
                        period = max_period;
                        break;
                    }
                }
                if (SystemTime.getMonotonousTime() - last_fail < period) {
                    record_failure = false;
                    throw (new UPnPException("Download failed too recently, ignoring"));
                }
                retries = 1;
            }
        }
        ResourceDownloader rd = rdf.getRetryDownloader(rdf.create(url, true), retries);
        rd.addListener(this);
        InputStream data = rd.download();
        try {
            SimpleXMLParserDocument res = parseXML(data);
            synchronized (failed_urls) {
                failed_urls.remove(url_str);
            }
            return (res);
        } finally {
            data.close();
        }
    } catch (Throwable e) {
        if (record_failure) {
            synchronized (failed_urls) {
                if (failed_urls.size() >= 64) {
                    failed_urls.clear();
                }
                long[] fails = failed_urls.get(url_str);
                if (fails == null) {
                    fails = new long[2];
                    failed_urls.put(url_str, fails);
                }
                fails[0]++;
                fails[1] = SystemTime.getMonotonousTime();
            }
            adapter.log("Failed to parse XML from :" + url_str + ": " + Debug.getNestedExceptionMessageAndStack(e));
        }
        if (e instanceof UPnPException) {
            throw ((UPnPException) e);
        }
        throw (new UPnPException("Root device location '" + url + "' - data read failed", e));
    } finally {
        TorrentUtils.setTLSDescription(null);
    }
}
Also used : SimpleXMLParserDocument(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)

Example 5 with SimpleXMLParserDocument

use of com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument in project BiglyBT by BiglySoftware.

the class UPnPImpl method parseXML.

public SimpleXMLParserDocument parseXML(InputStream _is) throws SimpleXMLParserDocumentException, IOException {
    // ASSUME UTF-8
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream(1024);
        byte[] buffer = new byte[8192];
        while (true) {
            int len = _is.read(buffer);
            if (len <= 0) {
                break;
            }
            baos.write(buffer, 0, len);
        }
    } finally {
        baos.close();
    }
    byte[] bytes_in = baos.toByteArray();
    InputStream is = new ByteArrayInputStream(bytes_in);
    try {
        StringBuilder data = new StringBuilder(1024);
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is, "UTF-8"));
        Set ignore_map = null;
        while (true) {
            String line = lnr.readLine();
            if (line == null) {
                break;
            }
            for (int i = 0; i < line.length(); i++) {
                char c = line.charAt(i);
                if (c < 0x20 && c != '\r' && c != '\t') {
                    data.append(' ');
                    if (ignore_map == null) {
                        ignore_map = new HashSet();
                    }
                    Character cha = new Character(c);
                    if (!ignore_map.contains(cha)) {
                        ignore_map.add(cha);
                        adapter.trace("    ignoring character(s) " + (int) c + " in xml response");
                    }
                } else {
                    data.append(c);
                }
            }
            data.append("\n");
        }
        String data_str = data.toString();
        adapter.trace("UPnP:Response:" + data_str);
        try {
            SimpleXMLParserDocument doc = adapter.parseXML(data_str);
            return (doc);
        } catch (Throwable e) {
            if (data_str.contains("<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">")) {
                data_str = data_str.replace("<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">", "<scpd>");
                return (adapter.parseXML(data_str));
            }
            throw (e);
        }
    } catch (Throwable e) {
        try {
            FileOutputStream trace = FileUtil.newFileOutputStream(getTraceFile());
            try {
                trace.write(bytes_in);
            } finally {
                trace.close();
            }
        } catch (Throwable f) {
            adapter.log(f);
        }
        if (e instanceof SimpleXMLParserDocumentException) {
            throw ((SimpleXMLParserDocumentException) e);
        }
        throw (new SimpleXMLParserDocumentException(e));
    }
}
Also used : SimpleXMLParserDocumentException(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException) SimpleXMLParserDocument(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument)

Aggregations

SimpleXMLParserDocument (com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument)9 SimpleXMLParserDocumentNode (com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentNode)3 UPnPException (com.biglybt.net.upnp.UPnPException)2 UPnPMSException (com.biglybt.net.upnpms.UPnPMSException)2 ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)2 SimpleXMLParserDocumentException (com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException)2 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 UPnPActionArgument (com.biglybt.net.upnp.UPnPActionArgument)1 UPnPService (com.biglybt.net.upnp.UPnPService)1 UPnPDeviceImpl (com.biglybt.net.upnp.impl.device.UPnPDeviceImpl)1 ResourceDownloaderFactory (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory)1 ResourceDownloaderFactoryImpl (com.biglybt.pifimpl.local.utils.resourcedownloader.ResourceDownloaderFactoryImpl)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1