Skip to content

Commit

Permalink
Adobe-Consulting-Services#3357 - Added debugging and null checking to…
Browse files Browse the repository at this point in the history
… ReferencesModel to preven… (Adobe-Consulting-Services#3372)

* Adobe-Consulting-Services#3357 - Added debugging and nullchecking to ReferencesModel to prevent NPEs
  • Loading branch information
davidjgonzalez authored and YegorKozlov committed Aug 15, 2024
1 parent ed6f4e7 commit 3818636
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 56 deletions.
10 changes: 3 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)

## Unreleased ([details][unreleased changes details])


### Fixed

- #3380 - Remove forced red theme from system notification text body
- #3398 - CreateRedirectConfigurationServlet throws PersistenceException when ancestor node types are different than expected
- #3402 - EnsureOakIndexManagerImpl does not pick up changes in EnsureOakIndex configurations.
- #3357 - Added debugging and null checking to ReferencesModel to prevent NPE
- #3398 - CreateRedirectConfigurationServlet throws PersistenceException when ancestor node types are different than expected
- #3275 - CCVAR: Fixed Same Attribute not updating correctly.

### Changed
Expand All @@ -24,12 +26,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
- #3401 - Move SyslogAppender into separate bundle for onprem only. SyslogAppender does not work in Cloud Service.
- #3390 - Remove usage of commons collections 3


### Fixed

- #3402 - EnsureOakIndexManagerImpl does not pick up changes in EnsureOakIndex configurations.


## 6.6.2 - 2024-06-25

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.adobe.acs.commons.reports.models;

import com.adobe.acs.commons.reports.internal.DelimiterConfiguration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
Expand All @@ -36,59 +37,87 @@
import com.adobe.granite.references.ReferenceAggregator;
import com.adobe.granite.references.ReferenceList;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Model(adaptables = Resource.class)
public class ReferencesModel implements ReportCellCSVExporter {
private static final Logger log = LoggerFactory.getLogger(ReferencesModel.class);

@ValueMapValue
@OSGiService
private ReferenceAggregator aggregator;

@OSGiService
private DelimiterConfiguration delimiterConfiguration;

private ReferenceList referenceList;

private Resource resource;

public ReferencesModel(Resource resource) {
this.resource = resource;
}

/**
* Used only for testing.
*
* @param delimiterConfiguration the delimiter configuration to use for this exporter
*/
ReferencesModel(Resource resource, DelimiterConfiguration delimiterConfiguration) {
this.resource = resource;
this.delimiterConfiguration = delimiterConfiguration;
}

@ValueMapValue
@OSGiService
private ReferenceAggregator aggregator;

@OSGiService
private DelimiterConfiguration delimiterConfiguration;

private ReferenceList referenceList;

private Resource resource;

public ReferencesModel(Resource resource) {
this.resource = resource;
}

/**
* Used only for testing.
* @param delimiterConfiguration the delimiter configuration to use for this exporter
*/
ReferencesModel(Resource resource, DelimiterConfiguration delimiterConfiguration) {
this.resource = resource;
this.delimiterConfiguration = delimiterConfiguration;
}

public List<Reference> getReferences() {
return Optional.ofNullable(referenceList)
.map(Collections::unmodifiableList)
.orElse(Collections.emptyList());
}

@Override
public String getValue(Object result) {
resource = (Resource) result;
init();
List<String> refStrings = new ArrayList<>();
for (Reference reference : referenceList) {
refStrings.add(reference.getType() + " - " + reference.getTarget().getPath());
public List<Reference> getReferences() {
return Optional.ofNullable(referenceList)
.map(Collections::unmodifiableList)
.orElse(Collections.emptyList());
}
return StringUtils.join(refStrings, delimiterConfiguration.getMultiValueDelimiter());
}

@PostConstruct
public void init() {
referenceList = aggregator.createReferenceList(resource);
Iterator<Reference> references = referenceList.iterator();
while (references.hasNext()) {
if (references.next().getTarget().getPath().equals(resource.getPath())) {
references.remove();
}

@Override
public String getValue(Object result) {
resource = (Resource) result;
init();
List<String> refStrings = new ArrayList<>();
for (Reference reference : referenceList) {
refStrings.add(reference.getType() + " - " + reference.getTarget().getPath());
}
return StringUtils.join(refStrings, delimiterConfiguration.getMultiValueDelimiter());
}

@PostConstruct
public void init() {
if (resource == null) {
throw new IllegalStateException("Resource is null, and must must be set before calling init()");
}

referenceList = aggregator.createReferenceList(resource);
Iterator<Reference> references = referenceList.iterator();

while (references.hasNext()) {
Reference reference = references.next();

if (reference == null) {
log.warn("Reference is null for resource: {}", resource.getPath());
continue;
}

Resource target = reference.getTarget();
if (target == null) {
log.warn("Reference target is null for resource: {}", resource.getPath());
continue;
}

String targetPath = target.getPath();
if (StringUtils.isBlank(targetPath)) {
log.warn("Reference target path is blank for resource: {}", resource.getPath());
continue;
}

if (StringUtils.equals(targetPath, resource.getPath())) {
references.remove();
}
}
}
}
}

0 comments on commit 3818636

Please sign in to comment.