private void installBundle(HttpServletRequest request, PluginContext pluginContext) { if (ServletFileUpload.isMultipartContent(request)) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List<?> files = upload.parseRequest(request); byte[] buffer = new byte[8192]; for (Object name : files) { FileItem element = (FileItem) name; if (!element.isFormField()) { String fileName = element.getName(); if (!fileName.endsWith(".jar")) { throw new FileUploadException("Wrong data type. Needs to be a \".jar\"."); } fileName = fileName.replace('\\', '/'); // Windows stub if (fileName.contains("/")) { fileName = fileName.substring('/' + 1); } InputStream is = element.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("bundle", fileName)); int len = 0; while ((len = is.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.flush(); fos.close(); is.close(); context.installBundle("file:bundle/" + fileName); message = "Info: Installed Bundle " + fileName; } } } catch (Exception e) { message = "Error: " + e.getMessage(); } } }
@Override public View getContentView(HttpServletRequest request, PluginContext pluginContext) { if (pluginContext.getLocalPath().equals("/edit")) { Bundle bundle = getBundleById(request.getParameter("id")); BundleConfiguration bundleConfiguration = null; try { bundleConfiguration = new BundleConfiguration(bundle, configAdmin, metaTypeService); } catch (Exception e) { message = "Error: " + e.getMessage(); return new RedirectView(pluginContext.getApplicationPath()); } return new ConfigurationView(bundle, bundleConfiguration, metaTypeService, loader); } else if (pluginContext.getLocalPath().equals("/changebundle")) { String id = request.getParameter("id"); Bundle bundle = getBundleById(id); try { BundleConfiguration bundleConfiguration = new BundleConfiguration(bundle, configAdmin, metaTypeService); Dictionary<String, String> bundleProperties = new Hashtable<String, String>(); String[] keys = request.getParameterValues("keyList"); if (keys != null) { for (String key : keys) { String value = request.getParameter(key + "Property"); if (value != null && !value.isEmpty()) { bundleProperties.put(key, request.getParameter(key + "Property")); } } } String newkey = request.getParameter("newkey"); if (!newkey.equals("")) { bundleProperties.put(newkey, request.getParameter("newkeyProperty")); } bundleConfiguration.setBundleProperties(bundleProperties); return new RedirectView(pluginContext.getApplicationPath() + "/edit?id=" + id); } catch (Exception e) { message = "Error: " + e.getMessage(); return new RedirectView(pluginContext.getApplicationPath()); } } else if (pluginContext.getLocalPath().equals("/install")) { installBundle(request, pluginContext); return new RedirectView(pluginContext.getApplicationPath()); } else if (pluginContext.getLocalPath().equals("/uninstall")) { Bundle bundle = getBundleById(request.getParameter("id")); if (bundle != null) { try { message = "Info: " + bundle.getSymbolicName() + " uninstalled"; bundle.uninstall(); } catch (BundleException e) { message = "Error: " + e.getMessage(); } } return new RedirectView(pluginContext.getApplicationPath()); } else if (pluginContext.getLocalPath().equals("/update")) { Bundle bundle = getBundleById(request.getParameter("id")); if (bundle != null) { try { message = "Info: " + bundle.getSymbolicName() + " updated"; bundle.update(); } catch (BundleException e) { message = "Error: " + e.getMessage(); } } return new RedirectView(pluginContext.getApplicationPath()); } else if (pluginContext.getLocalPath().equals("/stop")) { Bundle bundle = getBundleById(request.getParameter("id")); if (bundle != null) { try { message = "Info: " + bundle.getSymbolicName() + " stoped"; bundle.stop(); } catch (BundleException e) { message = "Error: " + e.getMessage(); } } return new RedirectView(pluginContext.getApplicationPath()); } else if (pluginContext.getLocalPath().equals("/start")) { Bundle bundle = getBundleById(request.getParameter("id")); if (bundle != null) { try { message = "Info: " + bundle.getSymbolicName() + " started"; bundle.start(); } catch (BundleException e) { message = "Error: " + e.getMessage(); } } return new RedirectView(pluginContext.getApplicationPath()); } else { String temp = message; message = null; return new BundleListView(context, loader, temp); } }