Search in sources :

Example 1 with ConfigurationService

use of org.eclipse.kura.configuration.ConfigurationService in project kura by eclipse.

the class GwtComponentServiceImpl method findComponentConfiguration.

@Override
public List<GwtConfigComponent> findComponentConfiguration(GwtXSRFToken xsrfToken) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    ConfigurationService cs = ServiceLocator.getInstance().getService(ConfigurationService.class);
    List<GwtConfigComponent> gwtConfigs = new ArrayList<GwtConfigComponent>();
    try {
        List<ComponentConfiguration> configs = cs.getComponentConfigurations();
        // sort the list alphabetically by service name
        Collections.sort(configs, new Comparator<ComponentConfiguration>() {

            @Override
            public int compare(ComponentConfiguration arg0, ComponentConfiguration arg1) {
                String name0;
                int start = arg0.getPid().lastIndexOf('.');
                int substringIndex = start + 1;
                if (start != -1 && substringIndex < arg0.getPid().length()) {
                    name0 = arg0.getPid().substring(substringIndex);
                } else {
                    name0 = arg0.getPid();
                }
                String name1;
                start = arg1.getPid().lastIndexOf('.');
                substringIndex = start + 1;
                if (start != -1 && substringIndex < arg1.getPid().length()) {
                    name1 = arg1.getPid().substring(substringIndex);
                } else {
                    name1 = arg1.getPid();
                }
                return name0.compareTo(name1);
            }
        });
        for (ComponentConfiguration config : configs) {
            // ignore items we want to hide
            if (!config.getPid().endsWith("CommandCloudApp")) {
                continue;
            }
            OCD ocd = config.getDefinition();
            if (ocd != null) {
                GwtConfigComponent gwtConfig = new GwtConfigComponent();
                gwtConfig.setComponentId(ocd.getId());
                gwtConfig.setComponentName(ocd.getName());
                gwtConfig.setComponentDescription(ocd.getDescription());
                if (ocd.getIcon() != null && !ocd.getIcon().isEmpty()) {
                    Icon icon = ocd.getIcon().get(0);
                    gwtConfig.setComponentIcon(icon.getResource());
                }
                List<GwtConfigParameter> gwtParams = new ArrayList<GwtConfigParameter>();
                gwtConfig.setParameters(gwtParams);
                for (AD ad : ocd.getAD()) {
                    GwtConfigParameter gwtParam = new GwtConfigParameter();
                    gwtParam.setId(ad.getId());
                    gwtParam.setName(ad.getName());
                    gwtParam.setDescription(ad.getDescription());
                    gwtParam.setType(GwtConfigParameterType.valueOf(ad.getType().name()));
                    gwtParam.setRequired(ad.isRequired());
                    gwtParam.setCardinality(ad.getCardinality());
                    if (ad.getOption() != null && !ad.getOption().isEmpty()) {
                        Map<String, String> options = new HashMap<String, String>();
                        for (Option option : ad.getOption()) {
                            options.put(option.getLabel(), option.getValue());
                        }
                        gwtParam.setOptions(options);
                    }
                    gwtParam.setMin(ad.getMin());
                    gwtParam.setMax(ad.getMax());
                    if (config.getConfigurationProperties() != null) {
                        // handle the value based on the cardinality of the
                        // attribute
                        int cardinality = ad.getCardinality();
                        Object value = config.getConfigurationProperties().get(ad.getId());
                        if (value != null) {
                            if (cardinality == 0 || cardinality == 1 || cardinality == -1) {
                                gwtParam.setValue(value.toString());
                            } else {
                                // this could be an array value
                                if (value instanceof Object[]) {
                                    Object[] objValues = (Object[]) value;
                                    List<String> strValues = new ArrayList<String>();
                                    for (Object v : objValues) {
                                        if (v != null) {
                                            strValues.add(v.toString());
                                        }
                                    }
                                    gwtParam.setValues(strValues.toArray(new String[] {}));
                                }
                            }
                        }
                        gwtParams.add(gwtParam);
                    }
                }
                gwtConfigs.add(gwtConfig);
            }
        }
    } catch (Throwable t) {
        KuraExceptionHandler.handle(t);
    }
    return gwtConfigs;
}
Also used : AD(org.eclipse.kura.configuration.metatype.AD) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) OCD(org.eclipse.kura.configuration.metatype.OCD) GwtConfigParameter(org.eclipse.kura.web.shared.model.GwtConfigParameter) Option(org.eclipse.kura.configuration.metatype.Option) ConfigurationService(org.eclipse.kura.configuration.ConfigurationService) Icon(org.eclipse.kura.configuration.metatype.Icon) GwtConfigComponent(org.eclipse.kura.web.shared.model.GwtConfigComponent)

Example 2 with ConfigurationService

use of org.eclipse.kura.configuration.ConfigurationService in project kura by eclipse.

the class GwtComponentServiceImpl method updateComponentConfiguration.

@Override
public void updateComponentConfiguration(GwtXSRFToken xsrfToken, GwtConfigComponent gwtCompConfig) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    ConfigurationService cs = ServiceLocator.getInstance().getService(ConfigurationService.class);
    try {
        // Build the new properties
        Map<String, Object> properties = new HashMap<String, Object>();
        ComponentConfiguration backupCC = cs.getComponentConfiguration(gwtCompConfig.getComponentId());
        Map<String, Object> backupConfigProp = backupCC.getConfigurationProperties();
        for (GwtConfigParameter gwtConfigParam : gwtCompConfig.getParameters()) {
            Object objValue;
            ComponentConfiguration currentCC = cs.getComponentConfiguration(gwtCompConfig.getComponentId());
            Map<String, Object> currentConfigProp = currentCC.getConfigurationProperties();
            Object currentObjValue = currentConfigProp.get(gwtConfigParam.getId());
            int cardinality = gwtConfigParam.getCardinality();
            if (cardinality == 0 || cardinality == 1 || cardinality == -1) {
                String strValue = gwtConfigParam.getValue();
                if (currentObjValue instanceof Password && PLACEHOLDER.equals(strValue)) {
                    objValue = currentConfigProp.get(gwtConfigParam.getId());
                } else {
                    objValue = getObjectValue(gwtConfigParam, strValue);
                }
            } else {
                String[] strValues = gwtConfigParam.getValues();
                if (currentObjValue instanceof Password[]) {
                    Password[] currentPasswordValue = (Password[]) currentObjValue;
                    for (int i = 0; i < strValues.length; i++) {
                        if (PLACEHOLDER.equals(strValues[i])) {
                            strValues[i] = new String(currentPasswordValue[i].getPassword());
                        }
                    }
                }
                objValue = getObjectValue(gwtConfigParam, strValues);
            }
            properties.put(gwtConfigParam.getId(), objValue);
        }
        // Force kura.service.pid into properties, if originally present
        if (backupConfigProp.get(KURA_SERVICE_PID) != null) {
            properties.put(KURA_SERVICE_PID, backupConfigProp.get(KURA_SERVICE_PID));
        }
        // 
        // apply them
        cs.updateConfiguration(gwtCompConfig.getComponentId(), properties);
    } catch (Throwable t) {
        KuraExceptionHandler.handle(t);
    }
}
Also used : ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) HashMap(java.util.HashMap) GwtConfigParameter(org.eclipse.kura.web.shared.model.GwtConfigParameter) ConfigurationService(org.eclipse.kura.configuration.ConfigurationService) Password(org.eclipse.kura.configuration.Password)

Example 3 with ConfigurationService

use of org.eclipse.kura.configuration.ConfigurationService in project kura by eclipse.

the class GwtSslServiceImpl method updateSslConfiguration.

@Override
public void updateSslConfiguration(GwtXSRFToken xsrfToken, GwtSslConfig sslConfig) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    try {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(PROP_PROTOCOL, sslConfig.getProtocol());
        properties.put(PROP_HN_VERIFY, sslConfig.isHostnameVerification());
        properties.put(PROP_TRUST_STORE, sslConfig.getKeyStore());
        if (PLACEHOLDER.equals(sslConfig.getKeystorePassword())) {
            CryptoService cryptoService = ServiceLocator.getInstance().getService(CryptoService.class);
            SslManagerServiceOptions options = getSslConfiguration();
            properties.put(PROP_TRUST_PASSWORD, new Password(cryptoService.decryptAes(options.getSslKeystorePassword().toCharArray())));
        } else {
            properties.put(PROP_TRUST_PASSWORD, new Password(sslConfig.getKeystorePassword()));
        }
        properties.put(PROP_CIPHERS, sslConfig.getCiphers());
        ConfigurationService configService = ServiceLocator.getInstance().getService(ConfigurationService.class);
        configService.updateConfiguration(SSL_PID, properties);
    } catch (KuraException e) {
        throw new GwtKuraException(e.getMessage());
    }
}
Also used : SslManagerServiceOptions(org.eclipse.kura.ssl.SslManagerServiceOptions) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) HashMap(java.util.HashMap) CryptoService(org.eclipse.kura.crypto.CryptoService) KuraException(org.eclipse.kura.KuraException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) ConfigurationService(org.eclipse.kura.configuration.ConfigurationService) Password(org.eclipse.kura.configuration.Password)

Example 4 with ConfigurationService

use of org.eclipse.kura.configuration.ConfigurationService in project kura by eclipse.

the class DeviceSnapshotsServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        GwtXSRFToken token = new GwtXSRFToken(request.getParameter("xsrfToken"));
        KuraRemoteServiceServlet.checkXSRFToken(request, token);
    } catch (Exception e) {
        throw new ServletException("Security error: please retry this operation correctly.", e);
    }
    // END XSRF security check
    String snapshotId = request.getParameter("snapshotId");
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/xml");
    response.setHeader("Content-Disposition", "attachment; filename=snapshot_" + snapshotId + ".xml");
    response.setHeader("Cache-Control", "no-transform, max-age=0");
    PrintWriter writer = response.getWriter();
    try {
        ServiceLocator locator = ServiceLocator.getInstance();
        ConfigurationService cs = locator.getService(ConfigurationService.class);
        if (snapshotId != null) {
            long sid = Long.parseLong(snapshotId);
            List<ComponentConfiguration> configs = cs.getSnapshot(sid);
            // build a list of configuration which can be marshalled in XML
            List<ComponentConfigurationImpl> configImpls = new ArrayList<ComponentConfigurationImpl>();
            for (ComponentConfiguration config : configs) {
                configImpls.add((ComponentConfigurationImpl) config);
            }
            XmlComponentConfigurations xmlConfigs = new XmlComponentConfigurations();
            xmlConfigs.setConfigurations(configImpls);
            // 
            // marshall the response and write it
            XmlUtil.marshal(xmlConfigs, writer);
        }
    } catch (Exception e) {
        s_logger.error("Error creating Excel export", e);
        throw new ServletException(e);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : ComponentConfigurationImpl(org.eclipse.kura.core.configuration.ComponentConfigurationImpl) XmlComponentConfigurations(org.eclipse.kura.core.configuration.XmlComponentConfigurations) ArrayList(java.util.ArrayList) GwtXSRFToken(org.eclipse.kura.web.shared.model.GwtXSRFToken) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) ServiceLocator(org.eclipse.kura.web.server.util.ServiceLocator) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) ConfigurationService(org.eclipse.kura.configuration.ConfigurationService) PrintWriter(java.io.PrintWriter)

Example 5 with ConfigurationService

use of org.eclipse.kura.configuration.ConfigurationService in project kura by eclipse.

the class UploadRequest method doPostConfigurationSnapshot.

private void doPostConfigurationSnapshot(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    UploadRequest upload = new UploadRequest(this.m_diskFileItemFactory);
    try {
        upload.parse(req);
    } catch (FileUploadException e) {
        s_logger.error("Error parsing the file upload request");
        throw new ServletException("Error parsing the file upload request", e);
    }
    // BEGIN XSRF - Servlet dependent code
    Map<String, String> formFields = upload.getFormFields();
    try {
        GwtXSRFToken token = new GwtXSRFToken(formFields.get("xsrfToken"));
        KuraRemoteServiceServlet.checkXSRFToken(req, token);
    } catch (Exception e) {
        throw new ServletException("Security error: please retry this operation correctly.", e);
    }
    // END XSRF security check
    List<FileItem> fileItems = upload.getFileItems();
    if (fileItems.size() != 1) {
        s_logger.error("expected 1 file item but found {}", fileItems.size());
        throw new ServletException("Wrong number of file items");
    }
    FileItem fileItem = fileItems.get(0);
    byte[] data = fileItem.get();
    String xmlString = new String(data, "UTF-8");
    XmlComponentConfigurations xmlConfigs;
    try {
        xmlConfigs = XmlUtil.unmarshal(xmlString, XmlComponentConfigurations.class);
    } catch (Exception e) {
        s_logger.error("Error unmarshaling device configuration", e);
        throw new ServletException("Error unmarshaling device configuration", e);
    }
    ServiceLocator locator = ServiceLocator.getInstance();
    try {
        ConfigurationService cs = locator.getService(ConfigurationService.class);
        List<ComponentConfigurationImpl> configImpls = xmlConfigs.getConfigurations();
        List<ComponentConfiguration> configs = new ArrayList<ComponentConfiguration>();
        configs.addAll(configImpls);
        cs.updateConfigurations(configs);
        // 
        // Add an additional delay after the configuration update
        // to give the time to the device to apply the received
        // configuration
        SystemService ss = locator.getService(SystemService.class);
        long delay = Long.parseLong(ss.getProperties().getProperty("console.updateConfigDelay", "5000"));
        if (delay > 0) {
            Thread.sleep(delay);
        }
    } catch (Exception e) {
        s_logger.error("Error updating device configuration: {}", e);
        throw new ServletException("Error updating device configuration", e);
    }
}
Also used : ComponentConfigurationImpl(org.eclipse.kura.core.configuration.ComponentConfigurationImpl) XmlComponentConfigurations(org.eclipse.kura.core.configuration.XmlComponentConfigurations) ArrayList(java.util.ArrayList) GwtXSRFToken(org.eclipse.kura.web.shared.model.GwtXSRFToken) ServletException(javax.servlet.ServletException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileUploadException(org.apache.commons.fileupload.FileUploadException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServletException(javax.servlet.ServletException) ServiceLocator(org.eclipse.kura.web.server.util.ServiceLocator) ComponentConfiguration(org.eclipse.kura.configuration.ComponentConfiguration) FileItem(org.apache.commons.fileupload.FileItem) SystemService(org.eclipse.kura.system.SystemService) ConfigurationService(org.eclipse.kura.configuration.ConfigurationService) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

ConfigurationService (org.eclipse.kura.configuration.ConfigurationService)12 ArrayList (java.util.ArrayList)9 ComponentConfiguration (org.eclipse.kura.configuration.ComponentConfiguration)9 GwtConfigComponent (org.eclipse.kura.web.shared.model.GwtConfigComponent)6 HashMap (java.util.HashMap)4 ServiceLocator (org.eclipse.kura.web.server.util.ServiceLocator)4 GwtConfigParameter (org.eclipse.kura.web.shared.model.GwtConfigParameter)3 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 Password (org.eclipse.kura.configuration.Password)2 AD (org.eclipse.kura.configuration.metatype.AD)2 Icon (org.eclipse.kura.configuration.metatype.Icon)2 OCD (org.eclipse.kura.configuration.metatype.OCD)2 Option (org.eclipse.kura.configuration.metatype.Option)2 ComponentConfigurationImpl (org.eclipse.kura.core.configuration.ComponentConfigurationImpl)2 XmlComponentConfigurations (org.eclipse.kura.core.configuration.XmlComponentConfigurations)2 SystemService (org.eclipse.kura.system.SystemService)2 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)2 GwtXSRFToken (org.eclipse.kura.web.shared.model.GwtXSRFToken)2 FileNotFoundException (java.io.FileNotFoundException)1