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");
}
}
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);
}
}
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();
}
}
}
}
Aggregations