Search in sources :

Example 1 with UPnPException

use of com.biglybt.net.upnp.UPnPException 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 2 with UPnPException

use of com.biglybt.net.upnp.UPnPException in project BiglyBT by BiglySoftware.

the class NatPMPImpl method addPortMapping.

/**
 * Client framework methods
 *
 * @see: ...service/UPnPSSWANConnectionImpl.java
 */
public void addPortMapping(// false -> UDP
boolean tcp, int port, String description) throws UPnPException {
    try {
        /* use public port for internal port */
        natDevice.addPortMapping(tcp, port, port);
    } catch (Exception e) {
        throw (new UPnPException("addPortMapping failed", e));
    }
    synchronized (this) {
        /* Find and remove old mapping, if any */
        Iterator it = mappings.iterator();
        while (it.hasNext()) {
            portMapping m = (portMapping) it.next();
            if (m.getExternalPort() == port && m.isTCP() == tcp)
                it.remove();
        }
        /* add new port to list */
        mappings.add(new portMapping(port, tcp, natDevice.getLocalAddress().getHostAddress(), description));
    }
}
Also used : Iterator(java.util.Iterator) UPnPException(com.biglybt.net.upnp.UPnPException) UPnPException(com.biglybt.net.upnp.UPnPException)

Example 3 with UPnPException

use of com.biglybt.net.upnp.UPnPException in project BiglyBT by BiglySoftware.

the class UPnPStateVariableImpl method getValue.

@Override
public String getValue() throws UPnPException {
    try {
        String soap_action = "urn:schemas-upnp-org:control-1-0#QueryStateVariable";
        String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + "<s:Body>";
        request += "<u:QueryStateVariable xmlns:u=\"urn:schemas-upnp-org:control-1-0\">" + "<u:varName>" + name + "</u:varName>" + "</u:QueryStateVariable>";
        request += "</s:Body>" + "</s:Envelope>";
        SimpleXMLParserDocument resp_doc = ((UPnPDeviceImpl) service.getDevice()).getUPnP().performSOAPRequest(service, soap_action, request);
        SimpleXMLParserDocumentNode body = resp_doc.getChild("Body");
        SimpleXMLParserDocumentNode fault = body.getChild("Fault");
        if (fault != null) {
            throw (new UPnPException("Invoke fails - fault reported: " + fault.getValue()));
        }
        SimpleXMLParserDocumentNode resp_node = body.getChild("QueryStateVariableResponse");
        if (resp_node == null) {
            throw (new UPnPException("Invoke fails - response missing: " + body.getValue()));
        }
        SimpleXMLParserDocumentNode value_node = resp_node.getChild("return");
        return (value_node.getValue());
    } catch (Throwable e) {
        if (e instanceof UPnPException) {
            throw ((UPnPException) e);
        }
        throw (new UPnPException("Invoke fails", e));
    }
}
Also used : SimpleXMLParserDocument(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument) SimpleXMLParserDocumentNode(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentNode) UPnPException(com.biglybt.net.upnp.UPnPException)

Aggregations

UPnPException (com.biglybt.net.upnp.UPnPException)3 SimpleXMLParserDocument (com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument)2 SimpleXMLParserDocumentNode (com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentNode)2 UPnPActionArgument (com.biglybt.net.upnp.UPnPActionArgument)1 UPnPService (com.biglybt.net.upnp.UPnPService)1 UPnPDeviceImpl (com.biglybt.net.upnp.impl.device.UPnPDeviceImpl)1 Iterator (java.util.Iterator)1