Search in sources :

Example 1 with UPnPAction

use of org.osgi.service.upnp.UPnPAction in project felix by apache.

the class BuildDevice method addServices.

/**
 * Method used to create a new Service in CyberLink world without creating the XML
 *
 * @param id ServiceId
 * @param devUPnP the CyberLink device that where the new Service will be created
 * @param sr ServiceReference to OSGi Device that used as source of the information
 *              for the creation of the device
 */
private static void addServices(String id, Device devUPnP, ServiceReference sr) {
    UPnPDevice devOSGi = (UPnPDevice) Activator.bc.getService(sr);
    if (devOSGi == null) {
        // added by twa to prevent a null pointer exception
        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " is deregistered from the framework while is exported");
        return;
    }
    UPnPService[] services = devOSGi.getServices();
    if (services == null || services.length == 0)
        return;
    for (int i = 0; i < services.length; i++) {
        Service ser = new Service();
        devUPnP.addService(ser);
        ser.setServiceType(services[i].getType());
        ser.setServiceID(services[i].getId());
        ser.setSCPDURL(id + "/service/" + i + "/gen-desc.xml");
        ser.setDescriptionURL(id + "/service/" + i + "/gen-desc.xml");
        ser.setControlURL(id + "/service/" + i + "/ctrl");
        ser.setEventSubURL(id + "/service/" + i + "/event");
        UPnPAction[] actions = services[i].getActions();
        for (int j = 0; j < actions.length; j++) {
            boolean valid = true;
            Action act = new Action(ser.getServiceNode());
            act.setName(actions[j].getName());
            ArgumentList al = new ArgumentList();
            String[] names = actions[j].getInputArgumentNames();
            if (names != null) {
                for (int k = 0; k < names.length; k++) {
                    UPnPStateVariable variable = actions[j].getStateVariable(names[k]);
                    if (variable == null) {
                        /*
                             * //TODO Create a stict and relaxed behavior of the base driver which 
                             * export as much it can or export only 100% complaint UPnPDevice service 
                             */
                        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " contains the action " + actions[j].getName() + " with the Input argument " + names[k] + " not related to any UPnPStateVariable. Thus this action won't be exported");
                        valid = false;
                        break;
                    }
                    Argument a = new Argument();
                    a.setDirection(Argument.IN);
                    a.setName(names[k]);
                    a.setRelatedStateVariableName(variable.getName());
                    al.add(a);
                }
            }
            names = actions[j].getOutputArgumentNames();
            if (names != null && valid) {
                for (int k = 0; k < names.length; k++) {
                    UPnPStateVariable variable = actions[j].getStateVariable(names[k]);
                    if (variable == null) {
                        /*
                             * //TODO Create a stict and relaxed behavior of the base driver which 
                             * export as much it can or export only 100% complaint UPnPDevice service 
                             */
                        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " contains the action " + actions[j].getName() + " with the Output argument " + names[k] + " not related to any UPnPStateVariable. Thus this action won't be exported");
                    }
                    Argument a = new Argument();
                    a.setDirection(Argument.OUT);
                    a.setName(names[k]);
                    a.setRelatedStateVariableName(variable.getName());
                    al.add(a);
                }
            }
            if (valid) {
                act.setArgumentList(al);
                ser.addAction(act);
            }
        }
        UPnPStateVariable[] vars = services[i].getStateVariables();
        for (int j = 0; j < vars.length; j++) {
            StateVariable var = new StateVariable();
            var.setDataType(vars[j].getUPnPDataType());
            var.setName(vars[j].getName());
            var.setSendEvents(vars[j].sendsEvents());
            String[] values = vars[j].getAllowedValues();
            if (values != null) {
                AllowedValueList avl = new AllowedValueList(values);
                var.setAllowedValueList(avl);
            } else if (vars[j].getMaximum() != null) {
                AllowedValueRange avr = new AllowedValueRange(vars[j].getMaximum(), vars[j].getMinimum(), vars[j].getStep());
                var.setAllowedValueRange(avr);
            }
            if (vars[j].getDefaultValue() != null)
                try {
                    var.setDefaultValue(Converter.toString(vars[j].getDefaultValue(), vars[j].getUPnPDataType()));
                } catch (Exception ignored) {
                }
            ser.addStateVariable(var);
        }
        Activator.bc.ungetService(sr);
    }
}
Also used : UPnPAction(org.osgi.service.upnp.UPnPAction) Action(org.cybergarage.upnp.Action) AllowedValueList(org.cybergarage.upnp.AllowedValueList) AllowedValueRange(org.cybergarage.upnp.AllowedValueRange) Argument(org.cybergarage.upnp.Argument) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) StateVariable(org.cybergarage.upnp.StateVariable) UPnPService(org.osgi.service.upnp.UPnPService) Service(org.cybergarage.upnp.Service) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) ArgumentList(org.cybergarage.upnp.ArgumentList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPDevice(org.osgi.service.upnp.UPnPDevice) UPnPService(org.osgi.service.upnp.UPnPService) UPnPAction(org.osgi.service.upnp.UPnPAction)

Example 2 with UPnPAction

use of org.osgi.service.upnp.UPnPAction in project felix by apache.

the class UPnPServiceImpl method getActions.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.service.upnp.UPnPService#getActions()
	 */
public UPnPAction[] getActions() {
    // TODO check again
    Enumeration e = actions.elements();
    if (e == null) {
        return null;
    }
    UPnPAction[] uPnPacts = new UPnPAction[actions.size()];
    int i = 0;
    while (e.hasMoreElements()) {
        uPnPacts[i] = (UPnPActionImpl) e.nextElement();
        i++;
    }
    return uPnPacts;
}
Also used : Enumeration(java.util.Enumeration) UPnPAction(org.osgi.service.upnp.UPnPAction)

Example 3 with UPnPAction

use of org.osgi.service.upnp.UPnPAction in project felix by apache.

the class ControlServlet method doPost.

/**
 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 */
@Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Map<String, Object> json = new HashMap<String, Object>();
        // $NON-NLS-1$
        String method = request.getParameter("action");
        if (// $NON-NLS-1$
        "listDevices".equals(method)) {
            getSession(request).unsubscribe();
            ServiceReference[] refs = tracker.getServiceReferences();
            // add root devices only
            for (int i = 0; refs != null && i < refs.length; i++) {
                if (refs[i] != null && refs[i].getProperty(UPnPDevice.PARENT_UDN) == null) {
                    Map<String, Object> deviceJSON = deviceTreeToJSON(refs[i]);
                    if (null != deviceJSON) {
                        @SuppressWarnings("unchecked") List<Object> // $NON-NLS-1$
                        list = (List<Object>) json.get("devices");
                        if (list == null) {
                            list = new ArrayList<Object>();
                            // $NON-NLS-1$
                            json.put("devices", list);
                        }
                        list.add(deviceJSON);
                    }
                }
            }
        } else if (// $NON-NLS-1$
        "serviceDetails".equals(method)) {
            UPnPService service = requireService(request);
            SessionObject session = // 
            getSession(request).subscribe(require("udn", request), // $NON-NLS-1$
            service.getId());
            json = Serializer.serviceToJSON(service, session);
        } else if (// $NON-NLS-1$
        "invokeAction".equals(method)) {
            UPnPService service = requireService(request);
            // $NON-NLS-1$
            UPnPAction action = service.getAction(require("actionID", request));
            // $NON-NLS-1$
            String[] names = request.getParameterValues("names");
            if (null == names) {
                // $NON-NLS-1$
                names = request.getParameterValues("names[]");
            }
            // $NON-NLS-1$
            String[] vals = request.getParameterValues("vals");
            if (null == vals) {
                // $NON-NLS-1$
                vals = request.getParameterValues("vals[]");
            }
            json = invoke(action, names, vals);
        } else {
            throw new ServletException("Invalid action: " + method);
        }
        // $NON-NLS-1$
        response.setContentType("application/json");
        // $NON-NLS-1$
        response.setCharacterEncoding("UTF-8");
        final JSONWriter writer = new JSONWriter(response.getWriter());
        writer.value(json);
        writer.flush();
    } catch (ServletException e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        // $NON-NLS-1$
        response.setContentType("text/plain");
        e.printStackTrace(response.getWriter());
        response.flushBuffer();
    } catch (Exception e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        // $NON-NLS-1$
        response.setContentType("text/plain");
        e.printStackTrace(response.getWriter());
        response.flushBuffer();
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) HashMap(java.util.HashMap) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) ServletException(javax.servlet.ServletException) ArrayList(java.util.ArrayList) List(java.util.List) UPnPService(org.osgi.service.upnp.UPnPService) UPnPAction(org.osgi.service.upnp.UPnPAction)

Example 4 with UPnPAction

use of org.osgi.service.upnp.UPnPAction in project felix by apache.

the class GeneralActionListener method actionControlReceived.

/**
 * @see org.cybergarage.upnp.control.ActionListener#actionControlReceived(org.cybergarage.upnp.Action)
 */
public synchronized boolean actionControlReceived(Action upnpAct) {
    if (!open)
        return false;
    UPnPService osgiServ = null;
    try {
        osgiServ = ((UPnPDevice) Activator.bc.getService(dev)).getService(id);
    } catch (Exception ignored) {
    }
    if (osgiServ == null)
        return exiting(false);
    UPnPAction osgiAct = osgiServ.getAction(upnpAct.getName());
    Properties inArgs = null;
    ArgumentList alIn = upnpAct.getInputArgumentList();
    ArgumentList alOut = upnpAct.getOutputArgumentList();
    String[] inArg = osgiAct.getInputArgumentNames();
    boolean invalidAction = false;
    if (inArg != null) {
        inArgs = new Properties();
        Argument arg;
        for (int j = 0; j < inArg.length; j++) {
            arg = alIn.getArgument(inArg[j]);
            try {
                inArgs.put(inArg[j], Converter.parseString(arg.getValue(), arg.getRelatedStateVariable().getDataType()));
            } catch (Exception e) {
                invalidAction = true;
                break;
            }
        }
    }
    Dictionary outArgs = null;
    try {
        outArgs = osgiAct.invoke(inArgs);
    } catch (UPnPException e) {
        // TODO Activator.logger.log()
        upnpAct.setStatus(e.getUPnPError_Code(), e.getMessage());
        invalidAction = true;
    } catch (Exception e) {
        // TODO Activator.logger.log()
        upnpAct.setStatus(UPnPStatus.ACTION_FAILED);
        invalidAction = true;
    }
    if (invalidAction)
        return exiting(false);
    String[] outArg = osgiAct.getOutputArgumentNames();
    if (outArg != null) {
        Argument arg;
        for (int j = 0; j < outArg.length; j++) {
            arg = alOut.getArgument(outArg[j]);
            try {
                arg.setValue(Converter.toString(outArgs.get(outArg[j]), arg.getRelatedStateVariable().getDataType()));
            } catch (Exception e) {
                e.printStackTrace();
                return exiting(false);
            }
        }
    }
    return exiting(true);
}
Also used : Dictionary(java.util.Dictionary) Argument(org.cybergarage.upnp.Argument) UPnPService(org.osgi.service.upnp.UPnPService) Properties(java.util.Properties) ArgumentList(org.cybergarage.upnp.ArgumentList) UPnPException(org.osgi.service.upnp.UPnPException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPException(org.osgi.service.upnp.UPnPException) UPnPAction(org.osgi.service.upnp.UPnPAction)

Example 5 with UPnPAction

use of org.osgi.service.upnp.UPnPAction in project felix by apache.

the class TreePopup method doNodeAction.

private void doNodeAction(UPnPDeviceTreeNode node) {
    if (node == null) {
        clearPropertiesViewer();
        return;
    }
    if (node.category.equals(UPnPDeviceTreeNode.ACTION))
        Mediator.getPropertiesViewer().showActionPanel(true);
    else
        Mediator.getPropertiesViewer().showActionPanel(false);
    if (node.category.equals(UPnPDeviceTreeNode.SERVICE))
        Mediator.getPropertiesViewer().showSubscriptionPanel(true);
    else
        Mediator.getPropertiesViewer().showSubscriptionPanel(false);
    if (node.category.equals(UPnPDeviceTreeNode.DEVICE) || node.category.equals(UPnPDeviceTreeNode.ROOT_DEVICE)) {
        DeviceNode device = (DeviceNode) node.getUserObject();
        UPnPDevice upnpDevice = device.getDevice(Activator.context);
        makeProperties(upnpDevice);
    } else if (node.category.equals(UPnPDeviceTreeNode.SERVICE)) {
        UPnPService service = (UPnPService) node.getUserObject();
        makeProperties(service);
    } else if (node.category.equals(UPnPDeviceTreeNode.ACTION)) {
        UPnPAction action = (UPnPAction) node.getUserObject();
        makeProperties(action);
        Mediator.getPropertiesViewer().setAction(action);
    } else if (node.category.equals(UPnPDeviceTreeNode.STATE) || node.category.equals(UPnPDeviceTreeNode.EVENTED_STATE) || node.category.equals(UPnPDeviceTreeNode.SUBSCRIBED_STATE)) {
        UPnPStateVariable state = (UPnPStateVariable) node.getUserObject();
        makeProperties(state);
    }
}
Also used : DeviceNode(org.apache.felix.upnp.tester.discovery.DeviceNode) UPnPDevice(org.osgi.service.upnp.UPnPDevice) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) UPnPService(org.osgi.service.upnp.UPnPService) UPnPAction(org.osgi.service.upnp.UPnPAction)

Aggregations

UPnPAction (org.osgi.service.upnp.UPnPAction)5 UPnPService (org.osgi.service.upnp.UPnPService)4 Argument (org.cybergarage.upnp.Argument)2 ArgumentList (org.cybergarage.upnp.ArgumentList)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 UPnPDevice (org.osgi.service.upnp.UPnPDevice)2 UPnPStateVariable (org.osgi.service.upnp.UPnPStateVariable)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Properties (java.util.Properties)1 ServletException (javax.servlet.ServletException)1 DeviceNode (org.apache.felix.upnp.tester.discovery.DeviceNode)1 JSONWriter (org.apache.felix.utils.json.JSONWriter)1 Action (org.cybergarage.upnp.Action)1 AllowedValueList (org.cybergarage.upnp.AllowedValueList)1 AllowedValueRange (org.cybergarage.upnp.AllowedValueRange)1