Skip to content

Commit

Permalink
Backup package manager initial commit
Browse files Browse the repository at this point in the history
Add temporary directory field to system settings by updating the Sightly markup,
SystemSettingsService, config servlet, and WCMUse bean.

Add new page for backup manager and add to sidebar links. Create new component
to display packages. Create client-side backup controller and backup service to
communicate with the server. Update gruntfile for client-side build process. Add
servlet for server-side communication.

Add package service to list and create packages. Add VltUtils from Apache Sling
to help with package creation. Update createFilter method. Add NOTICE file to
display such.

Add Vault packaging dependencies.
  • Loading branch information
nateyolles committed Sep 29, 2015
1 parent f6fa378 commit 407fa42
Show file tree
Hide file tree
Showing 17 changed files with 787 additions and 22 deletions.
4 changes: 4 additions & 0 deletions NOTICE
@@ -0,0 +1,4 @@
Public Sling + Sightly blog engine

This product contains code derived from Apache Sling from https://sling.apache.org/.
Copyright The Apache Software Foundation
45 changes: 45 additions & 0 deletions core/pom.xml
Expand Up @@ -254,6 +254,36 @@
<bundleFileName>${project.build.directory}/dependency/httpclient-osgi-4.5.jar</bundleFileName>
</configuration>
</execution>
<execution>
<id>install-distribution</id>
<goals>
<goal>install-file</goal>
</goals>
<phase>install</phase>
<configuration>
<bundleFileName>${project.build.directory}/dependency/org.apache.sling.distribution.core-0.1.6.jar</bundleFileName>
</configuration>
</execution>
<execution>
<id>install-hc</id>
<goals>
<goal>install-file</goal>
</goals>
<phase>install</phase>
<configuration>
<bundleFileName>${project.build.directory}/dependency/org.apache.sling.hc.core-1.2.2.jar</bundleFileName>
</configuration>
</execution>
<execution>
<id>install-vault</id>
<goals>
<goal>install-file</goal>
</goals>
<phase>install</phase>
<configuration>
<bundleFileName>${project.build.directory}/dependency/org.apache.jackrabbit.vault-3.1.18.jar</bundleFileName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down Expand Up @@ -448,6 +478,21 @@
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.distribution.core</artifactId>
<version>0.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.hc.core</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit.vault</groupId>
<artifactId>org.apache.jackrabbit.vault</artifactId>
<version>3.1.18</version>
</dependency>
</dependencies>

<properties>
Expand Down
Expand Up @@ -14,39 +14,28 @@
*/
public class SystemConfig extends WCMUse {

/**
* The Sling Script Helper to get services.
*/
/** The Sling Script Helper to get services. */
private SlingScriptHelper scriptHelper;

/**
* The current resource.
*/
/** The current resource. */
private Resource resource;

/**
* The name of the blog.
*/
/** The name of the blog. */
private String blogName;

/**
* Setting for extensionless URLs.
*/
/** The file system temporary directory */
private String temporaryDirectory;

/** Setting for extensionless URLs. */
private boolean extensionlessUrls;

/**
* The separator between blog name and page title.
*/
/** The separator between blog name and page title. */
private static final String TITLE_SEPARATOR = " - ";

/**
* The title property of the resource.
*/
/** The title property of the resource. */
private static final String TITLE_PROPERTY = "title";

/**
* Initialize the Sightly component.
*/
/** Initialize the Sightly component. */
@Override
public void activate() {
scriptHelper = getSlingScriptHelper();
Expand All @@ -57,6 +46,7 @@ public void activate() {
if (systemSettingsService != null) {
blogName = systemSettingsService.getBlogName();
extensionlessUrls = systemSettingsService.getExtensionlessUrls();
temporaryDirectory = systemSettingsService.getTemporaryDirectory();
}
}

Expand All @@ -69,6 +59,15 @@ public String getBlogName() {
return blogName;
}

/**
* Get the temporary directory.
*
* @return The temporary directory.
*/
public String getTemporaryDirectory() {
return temporaryDirectory;
}

/**
* Get the setting for extensionless URLs.
*
Expand Down
@@ -0,0 +1,42 @@
package com.nateyolles.sling.publick.services;

import java.util.List;

import org.apache.jackrabbit.vault.packaging.JcrPackage;
import org.apache.sling.api.SlingHttpServletRequest;

/**
* API's to get, create, upload, install and delete packages.
*/
public interface PackageService {

/**
* 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
*/
List<JcrPackage> getPackageList(final SlingHttpServletRequest request);

/**
* Create a JCR package and store it under /etc/packages/group_name/package_name-version.zip.
*
* @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 if package is created successfully
*/
boolean createPackage(final SlingHttpServletRequest request, final String groupName,
final String packageName, final String version, final String[] paths);

/**
* Create a Publick backup package.
*
* @param request The Sling HTTP servlet request
* @param packageName The name of the package
* @return true if package is created successfully
*/
boolean createBackupPackage(final SlingHttpServletRequest request, final String packageName);
}
Expand Up @@ -14,6 +14,9 @@ public interface SystemSettingsService {
/** OSGi property name for extensionless URLs */
public static final String SYSTEM_EXTENSIONLESS_URLS = "system.extentionlessUrls";

/** OSGi property name for the temporary directory */
public static final String SYSTEM_TEMPORARY_DIRECTORY = "system.temporaryDirectory";

/**
* Set multiple properties for the System Settings service.
*
Expand Down Expand Up @@ -55,4 +58,19 @@ public interface SystemSettingsService {
* @return true if the save was successful.
*/
boolean setExtensionlessUrls(final boolean value);

/**
* Get the setting for temporary directory.
*
* @return The setting for temporary directory.
*/
String getTemporaryDirectory();

/**
* Set the value for temporary directory.
*
* @param directory The setting for the temporary directory.
* @return true if the save was successful.
*/
boolean setTemporaryDirectory(final String directory);
}
@@ -0,0 +1,151 @@
package com.nateyolles.sling.publick.services.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.jcr.Session;
import javax.jcr.RepositoryException;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nateyolles.sling.publick.utils.VltUtils;
import com.nateyolles.sling.publick.services.PackageService;
import com.nateyolles.sling.publick.services.SystemSettingsService;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.vault.fs.api.WorkspaceFilter;
import org.apache.jackrabbit.vault.packaging.ExportOptions;
import org.apache.jackrabbit.vault.packaging.JcrPackage;
import org.apache.jackrabbit.vault.packaging.JcrPackageManager;
import org.apache.jackrabbit.vault.packaging.Packaging;
import org.apache.jackrabbit.vault.packaging.VaultPackage;
import org.apache.sling.api.SlingHttpServletRequest;

/**
* Service to get, create, upload, install and delete packages.
*/
@Service( value = PackageService.class )
@Component( immediate = true )
public class PackageServiceImpl implements PackageService {

/** Service to get the JCR Package Manager */
@Reference
private Packaging packaging;

/** Service to get the system temporary folder for creating packages */
@Reference
private SystemSettingsService settingsService;

/** The logger */
private static final Logger LOGGER = LoggerFactory.getLogger(PackageServiceImpl.class);

/** Default package group name for Publick backups */
private static final String BACKUP_GROUP = "publick_backup";

/** Default package version for Publick backups */
private static final String BACKUP_VERSION = "1.0";

/** Default package paths for Publick backups */
private static final String[] BACKUP_PATHS = {"/content/assets", "/content/blog", "/content/comments"};

/**
* 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;
}

/**
* 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 if package is created successfully
*/
public boolean createPackage(final SlingHttpServletRequest request, final String groupName,
final String packageName, final String version, final String[] paths) {

Session session = null;
VaultPackage vaultPackage = 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);

uploadPackage(session, vaultPackage);
session.save();
} catch (Exception e) {
VltUtils.deletePackage(vaultPackage);
} finally {
if (session != null && session.isLive()) {
session.logout();
session = null;
}
}

return true;
}

/**
* Create a Publick backup package.
*
* @param request The Sling HTTP servlet request
* @param packageName The name of the package
* @return true if package is created successfully
*/
public boolean createBackupPackage(final SlingHttpServletRequest request, final String packageName) {
return createPackage(request, BACKUP_GROUP, packageName, BACKUP_VERSION, BACKUP_PATHS);
}

/**
* 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);
}
}
}
Expand Up @@ -114,4 +114,23 @@ public boolean getExtensionlessUrls() {
public boolean setExtensionlessUrls(final boolean value) {
return osgiService.setProperty(COMPONENT_PID, SYSTEM_EXTENSIONLESS_URLS, value);
}

/**
* Get the setting for temporary directory.
*
* @return The setting for temporary directory.
*/
public String getTemporaryDirectory() {
return osgiService.getStringProperty(COMPONENT_PID, SYSTEM_TEMPORARY_DIRECTORY, null);
}

/**
* Set the value for temporary directory.
*
* @param value The setting for the temporary directory.
* @return true if the save was successful.
*/
public boolean setTemporaryDirectory(final String directory) {
return osgiService.setProperty(COMPONENT_PID, SYSTEM_TEMPORARY_DIRECTORY, directory);
}
}

0 comments on commit 407fa42

Please sign in to comment.