Search in sources :

Example 1 with ServiceLocator

use of org.eclipse.kura.web.server.util.ServiceLocator 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 2 with ServiceLocator

use of org.eclipse.kura.web.server.util.ServiceLocator in project kura by eclipse.

the class UploadRequest method getFileUploadInMemorySizeThreshold.

private static int getFileUploadInMemorySizeThreshold() {
    ServiceLocator locator = ServiceLocator.getInstance();
    int sizeThreshold = DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD;
    try {
        SystemService systemService = locator.getService(SystemService.class);
        sizeThreshold = Integer.parseInt(systemService.getProperties().getProperty("file.upload.in.memory.size.threshold", String.valueOf(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD)));
    } catch (GwtKuraException e) {
        s_logger.error("Error locating SystemService", e);
    }
    return sizeThreshold;
}
Also used : ServiceLocator(org.eclipse.kura.web.server.util.ServiceLocator) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) SystemService(org.eclipse.kura.system.SystemService)

Example 3 with ServiceLocator

use of org.eclipse.kura.web.server.util.ServiceLocator in project kura by eclipse.

the class UploadRequest method doPostDeploy.

private void doPostDeploy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServiceLocator locator = ServiceLocator.getInstance();
    DeploymentAgentService deploymentAgentService;
    try {
        deploymentAgentService = locator.getService(DeploymentAgentService.class);
    } catch (GwtKuraException e) {
        s_logger.error("Error locating DeploymentAgentService", e);
        throw new ServletException("Error locating DeploymentAgentService", e);
    }
    String reqPathInfo = req.getPathInfo();
    if (reqPathInfo.endsWith("url")) {
        String packageDownloadUrl = req.getParameter("packageUrl");
        if (packageDownloadUrl == null) {
            s_logger.error("Deployment package URL parameter missing");
            throw new ServletException("Deployment package URL parameter missing");
        }
        // BEGIN XSRF - Servlet dependent code
        String tokenId = req.getParameter("xsrfToken");
        try {
            GwtXSRFToken token = new GwtXSRFToken(tokenId);
            KuraRemoteServiceServlet.checkXSRFToken(req, token);
        } catch (Exception e) {
            throw new ServletException("Security error: please retry this operation correctly.", e);
        }
        try {
            s_logger.info("Installing package...");
            deploymentAgentService.installDeploymentPackageAsync(packageDownloadUrl);
        } catch (Exception e) {
            s_logger.error("Failed to install package at URL {}", packageDownloadUrl, e);
            throw new ServletException("Error installing deployment package", e);
        }
    } else if (reqPathInfo.endsWith("upload")) {
        doPostDeployUpload(req, resp);
    } else {
        s_logger.error("Unsupported package deployment request");
        throw new ServletException("Unsupported package deployment request");
    }
}
Also used : ServiceLocator(org.eclipse.kura.web.server.util.ServiceLocator) DeploymentAgentService(org.eclipse.kura.deployment.agent.DeploymentAgentService) ServletException(javax.servlet.ServletException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) 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)

Example 4 with ServiceLocator

use of org.eclipse.kura.web.server.util.ServiceLocator in project kura by eclipse.

the class UploadRequest method doGetIcon.

private void doGetIcon(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String queryString = req.getQueryString();
    if (queryString == null) {
        s_logger.error("Error parsing query string.");
        throw new ServletException("Error parsing query string.");
    }
    // Parse the query string
    Map<String, String> pairs;
    try {
        pairs = parseQueryString(queryString);
    } catch (UnsupportedEncodingException e) {
        s_logger.error("Error parsing query string.");
        throw new ServletException("Error parsing query string: " + e.getLocalizedMessage());
    }
    // Check for malformed request
    if (pairs == null || pairs.size() != 1) {
        s_logger.error("Error parsing query string.");
        throw new ServletException("Error parsing query string.");
    }
    String pid = pairs.get("pid");
    if (pid != null && pid.length() > 0) {
        BundleContext ctx = Console.getBundleContext();
        Bundle[] bundles = ctx.getBundles();
        ServiceLocator locator = ServiceLocator.getInstance();
        // Iterate over bundles to find PID
        for (Bundle b : bundles) {
            MetaTypeService mts;
            try {
                mts = locator.getService(MetaTypeService.class);
            } catch (GwtKuraException e1) {
                s_logger.error("Error parsing query string.");
                throw new ServletException("Error parsing query string.");
            }
            MetaTypeInformation mti = mts.getMetaTypeInformation(b);
            String[] pids = mti.getPids();
            for (String p : pids) {
                if (p.equals(pid)) {
                    try {
                        InputStream is = mti.getObjectClassDefinition(pid, null).getIcon(32);
                        if (is == null) {
                            s_logger.error("Error reading icon file.");
                            throw new ServletException("Error reading icon file.");
                        }
                        OutputStream os = resp.getOutputStream();
                        byte[] buffer = new byte[1024];
                        for (int length = 0; (length = is.read(buffer)) > 0; ) {
                            os.write(buffer, 0, length);
                        }
                        is.close();
                        os.close();
                    } catch (IOException e) {
                        s_logger.error("Error reading icon file.");
                        throw new IOException("Error reading icon file.");
                    }
                }
            }
        }
    } else {
        s_logger.error("Error parsing query string.");
        throw new ServletException("Error parsing query string.");
    }
}
Also used : GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) MetaTypeService(org.osgi.service.metatype.MetaTypeService) Bundle(org.osgi.framework.Bundle) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) ServiceLocator(org.eclipse.kura.web.server.util.ServiceLocator) BundleContext(org.osgi.framework.BundleContext)

Example 5 with ServiceLocator

use of org.eclipse.kura.web.server.util.ServiceLocator 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

ServiceLocator (org.eclipse.kura.web.server.util.ServiceLocator)11 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)8 SystemService (org.eclipse.kura.system.SystemService)6 IOException (java.io.IOException)5 ServletException (javax.servlet.ServletException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ConfigurationService (org.eclipse.kura.configuration.ConfigurationService)4 GwtXSRFToken (org.eclipse.kura.web.shared.model.GwtXSRFToken)4 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 FileUploadException (org.apache.commons.fileupload.FileUploadException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ZipInputStream (java.util.zip.ZipInputStream)2 FileItem (org.apache.commons.fileupload.FileItem)2 ComponentConfiguration (org.eclipse.kura.configuration.ComponentConfiguration)2 ComponentConfigurationImpl (org.eclipse.kura.core.configuration.ComponentConfigurationImpl)2 XmlComponentConfigurations (org.eclipse.kura.core.configuration.XmlComponentConfigurations)2