/** * Create package in the JCR under /etc/packages/group_name. * {@link org.apache.sling.distribution.serialization.impl.vlt.JcrVaultDistributionPackageBuilder#uploadPackage} * * @param session The current session * @param pack the Vault Package to upload * @return the JCR Package from the uploaded file * @throws IOException * @throws RepositoryException */ private JcrPackage uploadPackage(Session session, VaultPackage pack) throws IOException, RepositoryException { JcrPackageManager packageManager = packaging.getPackageManager(session); InputStream in = FileUtils.openInputStream(pack.getFile()); try { JcrPackage jcrPackage = packageManager.upload(in, true); return jcrPackage; } finally { IOUtils.closeQuietly(in); } } }
/** * Resolves path to Jcr Package. * * @param path the path to resolve to Jcr Package * @return the Jcr Package that corresponds to the provided path or {@code null} */ private JcrPackage getJcrPackage(final ResourceResolver resourceResolver, final String path) { final Resource eventResource = resourceResolver.getResource(path); if (eventResource == null) { log.warn("Could not find resource at path [ {} ] with the mapped service user. Either the resource has been removed meanwhile or the service user does not have the necessary rights.", path); return null; } JcrPackage jcrPackage = null; try { jcrPackage = packaging.open(eventResource.adaptTo(Node.class), false); } catch (RepositoryException e) { log.warn("Error checking if the path [ {} ] is a JCR Package.", path); } return jcrPackage; }
/** * Get the list of all packages in order of newest to oldest. * * @param request The Sling HTTP servlet request * @return List of all JCR Packages */ public List<JcrPackage> getPackageList(final SlingHttpServletRequest request) { Session session = request.getResourceResolver().adaptTo(Session.class); JcrPackageManager packageManager = packaging.getPackageManager(session); List<JcrPackage> packages = null; try { packages = packageManager.listPackages(); } catch (RepositoryException e) { LOGGER.error("Could not get package list", e); } return packages; }
@Override public void exportToStream(ResourceResolver resourceResolver, DistributionExportOptions exportOptions, OutputStream outputStream) throws DistributionException { Session session = null; try { DistributionRequest request = VltUtils.sanitizeRequest(exportOptions.getRequest()); session = getSession(resourceResolver); String packageGroup = PACKAGE_GROUP; String packageName = TYPE + "_" + System.currentTimeMillis() + "_" + UUID.randomUUID(); WorkspaceFilter filter = VltUtils.createFilter(request, nodeFilters, propertyFilters); ExportOptions opts = VltUtils.getExportOptions(filter, packageRoots, packageGroup, packageName, VERSION, useBinaryReferences, exportPathMapping); log.debug("assembling package {} user {}", packageGroup + '/' + packageName + "-" + VERSION, resourceResolver.getUserID()); packaging.getPackageManager().assemble(session, opts, outputStream); } catch (Exception e) { throw new DistributionException(e); } finally { ungetSession(session); } }
/** * Delete package from the JCR. * * @param request The current request. * @param groupName The name of the package group to install * @param packageName The name of the package to install * @param version The version of the package to install * @return true if package was installed successfully */ public boolean deletePackage(final SlingHttpServletRequest request, final String groupName, final String packageName, final String version) { Session session = request.getResourceResolver().adaptTo(Session.class); boolean result; final JcrPackageManager packageManager = packaging.getPackageManager(session); final PackageId packageId = new PackageId(groupName, packageName, version); try { final JcrPackage jcrPackage = packageManager.open(packageId); packageManager.remove(jcrPackage); result = true; } catch (RepositoryException e) { LOGGER.error("Could not remove package", e); result = false; } return result; }
/** * Upload a package. * * @param request The current Sling HTTP servlet request * @return The uploaded JCR Package */ public JcrPackage uploadBackupPackage(final SlingHttpServletRequest request) { final RequestParameterMap params = request.getRequestParameterMap(); Session session = null; JcrPackage jcrPackage = null; for (final Map.Entry<String, RequestParameter[]> pairs : params.entrySet()) { final RequestParameter[] pArr = pairs.getValue(); final RequestParameter param = pArr[0]; if (!param.isFormField()) { try { session = request.getResourceResolver().adaptTo(Session.class); final JcrPackageManager packageManager = packaging.getPackageManager(session); final InputStream stream = param.getInputStream(); jcrPackage = packageManager.upload(stream, true); } catch (java.io.IOException e) { LOGGER.error("Could not get image input stream", e); } catch (RepositoryException e) { LOGGER.error("Could not upload package to repository", e); } } } return jcrPackage; }
/** * {@inheritDoc} */ public JcrPackage createPackageFromPathFilterSets(final Collection<PathFilterSet> pathFilterSets, final Session session, final String groupName, final String name, String version, final ConflictResolution conflictResolution, final Map<String, String> packageDefinitionProperties) throws IOException, RepositoryException { final JcrPackageManager jcrPackageManager = packaging.getPackageManager(session); if (ConflictResolution.Replace.equals(conflictResolution)) { this.removePackage(jcrPackageManager, groupName, name, version); } else if (ConflictResolution.IncrementVersion.equals(conflictResolution)) { version = this.getNextVersion(jcrPackageManager, groupName, name, version).toString(); } final JcrPackage jcrPackage = jcrPackageManager.create(groupName, name, version); final JcrPackageDefinition jcrPackageDefinition = jcrPackage.getDefinition(); final DefaultWorkspaceFilter workspaceFilter = new DefaultWorkspaceFilter(); for (final PathFilterSet pathFilterSet : pathFilterSets) { workspaceFilter.add(pathFilterSet); } jcrPackageDefinition.setFilter(workspaceFilter, true); for (final Map.Entry<String, String> entry : packageDefinitionProperties.entrySet()) { jcrPackageDefinition.set(entry.getKey(), entry.getValue(), false); } session.save(); return jcrPackage; }
boolean result; final JcrPackageManager packageManager = packaging.getPackageManager(session); final PackageId packageId = new PackageId(groupName, packageName, version);
.getPackageManager(resourceResolver.adaptTo(Session.class));
manager = packagingService.getPackageManager(admin); } else { manager = new DefaultPackagingService().getPackageManager(admin);
/** * Create a JCR package and store it under /etc/packages/group_name/package_name-version.zip. * {@link org.apache.sling.distribution.serialization.impl.vlt.JcrVaultDistributionPackageBuilder#createPackageForAdd} * * @param request The Sling HTTP servlet request * @param groupName The name of the package group * @param packageName The name of the package * @param version The version of the package * @param paths The JCR paths to include in the package * @return true the saved JCR Package */ public JcrPackage createPackage(final SlingHttpServletRequest request, final String groupName, final String packageName, final String version, final String[] paths) { Session session = null; VaultPackage vaultPackage = null; JcrPackage savedPackage = null; File tempDirectory = VltUtils.getTempFolder(settingsService.getTemporaryDirectory()); try { session = request.getResourceResolver().adaptTo(Session.class); WorkspaceFilter filter = VltUtils.createFilter(paths, true); ExportOptions opts = VltUtils.getExportOptions(filter, paths, groupName, packageName, version); vaultPackage = VltUtils.createPackage(packaging.getPackageManager(), session, opts, tempDirectory); savedPackage = uploadPackage(session, vaultPackage); session.save(); } catch (Exception e) { VltUtils.deletePackage(vaultPackage); } return savedPackage; }