Search in sources :

Example 1 with DeploymentAgentService

use of org.eclipse.kura.deployment.agent.DeploymentAgentService 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 2 with DeploymentAgentService

use of org.eclipse.kura.deployment.agent.DeploymentAgentService in project kura by eclipse.

the class GwtPackageServiceImpl method uninstallDeploymentPackage.

@Override
public void uninstallDeploymentPackage(GwtXSRFToken xsrfToken, String packageName) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    DeploymentAgentService deploymentAgentService = ServiceLocator.getInstance().getService(DeploymentAgentService.class);
    try {
        deploymentAgentService.uninstallDeploymentPackageAsync(GwtSafeHtmlUtils.htmlEscape(packageName));
    } catch (Exception e) {
        throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : DeploymentAgentService(org.eclipse.kura.deployment.agent.DeploymentAgentService) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException)

Example 3 with DeploymentAgentService

use of org.eclipse.kura.deployment.agent.DeploymentAgentService in project kura by eclipse.

the class UploadRequest method doPostDeployUpload.

private void doPostDeployUpload(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);
    }
    // Check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(req);
    if (!isMultipart) {
        s_logger.error("Not a file upload request");
        throw new ServletException("Not a file upload request");
    }
    UploadRequest upload = new UploadRequest(this.m_diskFileItemFactory);
    try {
        upload.parse(req);
    } catch (FileUploadException e) {
        s_logger.error("Error parsing the file upload request", e);
        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 = null;
    InputStream is = null;
    File localFile = null;
    OutputStream os = null;
    boolean successful = false;
    try {
        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 item = fileItems.get(0);
        String filename = item.getName();
        is = item.getInputStream();
        String filePath = System.getProperty("java.io.tmpdir") + File.separator + filename;
        localFile = new File(filePath);
        if (localFile.exists()) {
            if (localFile.delete()) {
                s_logger.error("Cannot delete file: {}", filePath);
                throw new ServletException("Cannot delete file: " + filePath);
            }
        }
        try {
            localFile.createNewFile();
            localFile.deleteOnExit();
        } catch (IOException e) {
            s_logger.error("Cannot create file: {}", filePath, e);
            throw new ServletException("Cannot create file: " + filePath);
        }
        try {
            os = new FileOutputStream(localFile);
        } catch (FileNotFoundException e) {
            s_logger.error("Cannot find file: {}", filePath, e);
            throw new ServletException("Cannot find file: " + filePath, e);
        }
        s_logger.info("Copying uploaded package file to file: {}", filePath);
        try {
            IOUtils.copy(is, os);
        } catch (IOException e) {
            s_logger.error("Failed to copy deployment package file: {}", filename, e);
            throw new ServletException("Failed to copy deployment package file: " + filename, e);
        }
        try {
            os.close();
        } catch (IOException e) {
            s_logger.warn("Cannot close output stream", e);
        }
        URL url = localFile.toURI().toURL();
        String sUrl = url.toString();
        s_logger.info("Installing package...");
        try {
            deploymentAgentService.installDeploymentPackageAsync(sUrl);
            successful = true;
        } catch (Exception e) {
            s_logger.error("Package installation failed", e);
            throw new ServletException("Package installation failed", e);
        }
    } catch (IOException e) {
        throw e;
    } catch (ServletException e) {
        throw e;
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                s_logger.warn("Cannot close output stream", e);
            }
        }
        if (localFile != null && !successful) {
            try {
                localFile.delete();
            } catch (Exception e) {
                s_logger.warn("Cannot delete file");
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                s_logger.warn("Cannot close input stream", e);
            }
        }
        if (fileItems != null) {
            for (FileItem fileItem : fileItems) {
                fileItem.delete();
            }
        }
    }
}
Also used : DeploymentAgentService(org.eclipse.kura.deployment.agent.DeploymentAgentService) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) 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) URL(java.net.URL) ServiceLocator(org.eclipse.kura.web.server.util.ServiceLocator) ServletException(javax.servlet.ServletException) FileItem(org.apache.commons.fileupload.FileItem) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

IOException (java.io.IOException)3 DeploymentAgentService (org.eclipse.kura.deployment.agent.DeploymentAgentService)3 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)3 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ServletException (javax.servlet.ServletException)2 FileUploadException (org.apache.commons.fileupload.FileUploadException)2 ServiceLocator (org.eclipse.kura.web.server.util.ServiceLocator)2 GwtXSRFToken (org.eclipse.kura.web.shared.model.GwtXSRFToken)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ZipInputStream (java.util.zip.ZipInputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 FileItem (org.apache.commons.fileupload.FileItem)1 SAXException (org.xml.sax.SAXException)1