Skip to content

Commit

Permalink
Merge pull request #105 from exadel-inc/release/3.2.0
Browse files Browse the repository at this point in the history
Release/3.2.0
  • Loading branch information
AlKaliada authored Apr 18, 2023
2 parents c21fe98 + f21e8fb commit c093edd
Show file tree
Hide file tree
Showing 24 changed files with 390 additions and 102 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.exadel.etoolbox</groupId>
<artifactId>etoolbox-backpack</artifactId>
<version>3.1.1</version>
<version>3.2.0</version>
</parent>

<artifactId>etoolbox-backpack.core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.exadel.etoolbox.backpack.core.services;

import org.apache.sling.api.resource.ResourceResolver;

import java.util.List;

public interface LiveCopyService {

List<String> getPaths(ResourceResolver resourceResolver, String path, boolean includeLiveCopies);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.exadel.etoolbox.backpack.core.services.impl;

import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveCopy;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.exadel.etoolbox.backpack.core.services.LiveCopyService;
import com.exadel.etoolbox.backpack.core.services.pckg.BasePackageService;
import com.exadel.etoolbox.backpack.core.servlets.model.PackageModel;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RangeIterator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component(service = LiveCopyService.class)
public class LiveCopyServiceImpl implements LiveCopyService {

private static final Logger LOGGER = LoggerFactory.getLogger(LiveCopyServiceImpl.class);

@Reference
private LiveRelationshipManager liveRelationshipManager;

/**
* Called by {@link BasePackageService#getPackageInfo(ResourceResolver, PackageModel)} to adjust paths to resources
* intended for the package
*
* @param path Resource path
* @param includeLiveCopies Flag indicating if this resource's live copies must be included
* @param resourceResolver Current {@code ResourceResolver} object
* @return List of paths
*/
@Override
public List<String> getPaths(ResourceResolver resourceResolver, String path, boolean includeLiveCopies) {
List<String> paths = new ArrayList<>(Collections.singletonList(path));
if (!includeLiveCopies) {
return paths;
}
paths.addAll(getLiveCopies(resourceResolver, path));
return paths;
}

private List<String> getLiveCopies(ResourceResolver resourceResolver, String path) {
List<String> paths = new ArrayList<>();
Resource resource = resourceResolver.getResource(path);
if (resource == null) {
return paths;
}
try {
RangeIterator relationships = liveRelationshipManager.getLiveRelationships(resource, null, null);
while (relationships.hasNext()) {
LiveRelationship relationship = (LiveRelationship) relationships.next();
LiveCopy liveCopy = relationship.getLiveCopy();
if (liveCopy == null) {
continue;
}
String liveCopyPath = liveCopy.getPath();
String syncPath = liveCopyPath + relationship.getSyncPath();
if (resourceResolver.getResource(syncPath) != null) {
paths.add(syncPath);
}
paths.addAll(getLiveCopies(resourceResolver, liveCopyPath));
}
} catch (WCMException e) {
LOGGER.error("Can't get relationships of the resource {}", resource.getPath(), e);
}
return paths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.day.cq.dam.api.Asset;
import com.exadel.etoolbox.backpack.core.dto.repository.ReferencedItem;
import com.exadel.etoolbox.backpack.core.dto.response.PackageInfo;
import com.exadel.etoolbox.backpack.core.services.LiveCopyService;
import com.exadel.etoolbox.backpack.core.services.QueryService;
import com.exadel.etoolbox.backpack.core.services.ReferenceService;
import com.exadel.etoolbox.backpack.core.services.pckg.BasePackageService;
Expand Down Expand Up @@ -56,6 +57,7 @@
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Implements {@link BasePackageService} to provide base operation with package
Expand Down Expand Up @@ -86,18 +88,21 @@ public class BasePackageServiceImpl implements BasePackageService {

@Reference
@SuppressWarnings("UnusedDeclaration") // value injected by Sling
protected ResourceResolverFactory resourceResolverFactory;
private ResourceResolverFactory resourceResolverFactory;

@Reference
@SuppressWarnings("UnusedDeclaration") // value injected by Sling
protected SlingRepository slingRepository;
private SlingRepository slingRepository;

@Reference
@SuppressWarnings("UnusedDeclaration") // value injected by Sling
protected ReferenceService referenceService;
private ReferenceService referenceService;

@Reference
protected QueryService queryService;
private QueryService queryService;

@Reference
private LiveCopyService liveCopyService;

@SuppressWarnings("UnstableApiUsage") // sticking to Guava Cache version bundled in uber-jar; still safe to use
protected Cache<String, PackageInfo> packageInfos;
Expand Down Expand Up @@ -155,7 +160,7 @@ public PackageInfo getPackageInfo(final ResourceResolver resourceResolver, final
} else {
actualPaths = packageModel.getPaths().stream()
.filter(s -> resourceResolver.getResource(s.getPath()) != null)
.map(path -> getActualPath(path.getPath(), path.isExcludeChildren(), resourceResolver))
.flatMap(pathModel -> getActualPaths(resourceResolver, pathModel))
.collect(Collectors.toList());
}
packageInfo.setPackageName(packageModel.getPackageName());
Expand All @@ -181,14 +186,14 @@ public PackageInfo getPackageInfo(final ResourceResolver resourceResolver, final
* to the underlying {@code jcr:content} node
*
* @param path Resource path to inspect
* @param excludeChildren Flag indicating if this resource's children must be excluded
* @param includeChildren Flag indicating if this resource's children must be included
* @param resourceResolver Current {@code ResourceResolver} object
* @return Source path, or the adjusted resource path
*/
private String getActualPath(final String path, final boolean excludeChildren, final ResourceResolver resourceResolver) {
private String getActualPath(final String path, final boolean includeChildren, final ResourceResolver resourceResolver) {
Resource res = resourceResolver.getResource(path);

if (!excludeChildren) {
if (includeChildren) {
return path;
}
if (res != null && res.getChild(JcrConstants.JCR_CONTENT) != null) {
Expand All @@ -197,6 +202,11 @@ private String getActualPath(final String path, final boolean excludeChildren, f
return path;
}

private Stream<String> getActualPaths(ResourceResolver resourceResolver, PathModel pathModel) {
return liveCopyService.getPaths(resourceResolver, pathModel.getPath(), pathModel.includeLiveCopies())
.stream().map(path -> getActualPath(path, pathModel.includeChildren(), resourceResolver));
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private PackageModel getPackageModel(final JcrPackage jcrPackage) throws Reposit
packageModel.setPaths(BasePackageServiceImpl.GSON.fromJson(definition.get(BasePackageServiceImpl.INITIAL_FILTERS), listType));
} else {
List<PathFilterSet> filterSets = filter.getFilterSets();
packageModel.setPaths(filterSets.stream().map(pathFilterSet -> new PathModel(pathFilterSet.getRoot(), false)).collect(Collectors.toList()));
packageModel.setPaths(filterSets.stream().map(pathFilterSet -> new PathModel(pathFilterSet.getRoot(), false, false)).collect(Collectors.toList()));
}
return packageModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ public class PathModel {
private String path;

@RequestParam
private boolean excludeChildren;
private boolean includeChildren;

@RequestParam
private boolean includeLiveCopies;

public PathModel() {
}

public PathModel(final String path, final boolean excludeChildren) {
public PathModel(final String path, final boolean includeChildren, final boolean includeLiveCopies) {
this.path = path;
this.excludeChildren = excludeChildren;
this.includeChildren = includeChildren;
this.includeLiveCopies = includeLiveCopies;
}

/**
Expand All @@ -51,10 +55,18 @@ public String getPath() {
}

/**
* Gets the whether or not child pages excluded from a package build
* @return String value
* Gets the whether or not child pages are included to a package build
* @return boolean value
*/
public boolean includeChildren() {
return includeChildren;
}

/**
* Gets the whether or not live copies of the page included to the package build
* @return boolean value
*/
public boolean isExcludeChildren() {
return excludeChildren;
public boolean includeLiveCopies() {
return includeLiveCopies;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
*/
package com.exadel.etoolbox.backpack.core.services.pckg.impl;

import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.exadel.etoolbox.backpack.core.dto.repository.AssetReferencedItem;
import com.exadel.etoolbox.backpack.core.dto.repository.ReferencedItem;
import com.exadel.etoolbox.backpack.core.dto.response.PackageInfo;
import com.exadel.etoolbox.backpack.core.services.ReferenceService;
import com.exadel.etoolbox.backpack.core.services.impl.LiveCopyServiceImpl;
import com.exadel.etoolbox.backpack.core.services.impl.QueryServiceImpl;
import com.exadel.etoolbox.backpack.core.services.pckg.BasePackageService;
import com.exadel.etoolbox.backpack.core.services.pckg.PackageInfoService;
Expand All @@ -31,12 +34,15 @@
import org.apache.jackrabbit.vault.packaging.JcrPackageDefinition;
import org.apache.jackrabbit.vault.packaging.JcrPackageManager;
import org.apache.jackrabbit.vault.packaging.PackagingService;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Before;
import org.junit.Rule;
import org.mockito.Mock;

import javax.jcr.Node;
import javax.jcr.RangeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import java.io.IOException;
Expand Down Expand Up @@ -86,8 +92,11 @@ public class Base {
protected Map<String, List<String>> referencedResources;
protected BasePackageService basePackageService;

@Mock
private LiveRelationshipManager liveRelationshipManager;

@Before
public void beforeTest() throws IOException, RepositoryException {
public void beforeTest() throws IOException, RepositoryException, WCMException {
referencedResources = new HashMap<>();
referencedResources.put(IMAGE_JPEG, Collections.singletonList(PICTURE_1));
referencedResources.put(IMAGE_PNG, Collections.singletonList(PICTURE_2));
Expand All @@ -104,6 +113,12 @@ public void beforeTest() throws IOException, RepositoryException {
Map<String, Object> properties = new HashMap<>();
properties.put("buildInfoTTL", 1);
context.registerInjectActivateService(new QueryServiceImpl());
liveRelationshipManager = mock(LiveRelationshipManager.class);
context.registerService(LiveRelationshipManager.class, liveRelationshipManager);
RangeIterator relationships = mock(RangeIterator.class);
when(relationships.hasNext()).thenReturn(false);
when(liveRelationshipManager.getLiveRelationships(any(Resource.class), any(), any())).thenReturn(relationships);
context.registerInjectActivateService(new LiveCopyServiceImpl());
basePackageService = context.registerInjectActivateService(new BasePackageServiceImpl(), properties);
packageInfoService = context.registerInjectActivateService(new PackageInfoServiceImpl());

Expand Down Expand Up @@ -185,7 +200,7 @@ protected void verifyPackageFilters(final Node packageNode,

for (int i = 0; i < initialFiltersModels.size(); i++) {
assertEquals("Initial path must be as in expected list", expectedInitialFiltersModels.get(i).getPath(), initialFiltersModels.get(i).getPath());
assertEquals("Initial excludeChildren flag must be as in expected list", expectedInitialFiltersModels.get(i).isExcludeChildren(), initialFiltersModels.get(i).isExcludeChildren());
assertEquals("Initial includeChildren flag must be as in expected list", expectedInitialFiltersModels.get(i).includeChildren(), initialFiltersModels.get(i).includeChildren());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package com.exadel.etoolbox.backpack.core.services.pckg.impl;

import com.day.cq.wcm.api.WCMException;
import com.exadel.etoolbox.backpack.core.dto.response.PackageInfo;
import com.exadel.etoolbox.backpack.core.services.SessionService;
import com.exadel.etoolbox.backpack.core.services.pckg.BasePackageService;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class BuildPackageImplTest extends Base {
private SessionService sessionService;

@Override
public void beforeTest() throws IOException, RepositoryException {
public void beforeTest() throws IOException, RepositoryException, WCMException {
super.beforeTest();
buildPackage = context.registerInjectActivateService(new BuildPackageImpl());
}
Expand Down Expand Up @@ -128,7 +129,7 @@ public void createBasePackage() throws IOException, RepositoryException {
packageInfo.setVersion(PACKAGE_VERSION);
packageInfo.setReferencedResources(referencedResources);
packageInfo.setPaths(Collections.singletonList(PAGE_1));
createPackage(packageInfo, Collections.singletonList(new PathModel(PAGE_1, false)), new DefaultWorkspaceFilter());
createPackage(packageInfo, Collections.singletonList(new PathModel(PAGE_1, true, false)), new DefaultWorkspaceFilter());
}


Expand All @@ -141,7 +142,7 @@ public void before() throws IOException, RepositoryException {
packageInfo.setReferencedResources(referencedResources);
packageInfo.setPaths(Collections.singletonList(PAGE_1));
packageInfo.setPackagePath(PACKAGE_PATH);
aPackage = spy(createPackage(packageInfo, Collections.singletonList(new PathModel(PAGE_1, false)), new DefaultWorkspaceFilter()));
aPackage = spy(createPackage(packageInfo, Collections.singletonList(new PathModel(PAGE_1, true, false)), new DefaultWorkspaceFilter()));


buildPackageServiceSpy = (BuildPackageImpl) spy(buildPackage);
Expand Down
Loading

0 comments on commit c093edd

Please sign in to comment.