Skip to content

Commit

Permalink
Xref validation (#604)
Browse files Browse the repository at this point in the history
* make helper singlton to improve memory

* Bug fix: NPE issue due to no initialization at test run

* Refactor static variable to non static

* clear helper variable
  • Loading branch information
rednitish authored Jun 4, 2021
1 parent abf60f1 commit 56ad1a7
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String preprocess(String content) {
content = preprocessWithPattern(content, XREF_PATTERN, urlList);
content = preprocessWithPattern(content, TRIANGLE_PATTERN, urlList);
}
new XrefValidationHelper().setObjectsToValidate(urlList);
XrefValidationHelper.getInstance().setObjectsToValidate(urlList);
return content;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
// FIXME - need to rework document preview servlets to support latest suffix (variant preview servlet already works)
String forwardString = firstResource.get().getPath() + ".preview/" + mode;
new XrefValidationHelper().initList();
request.getRequestDispatcher(forwardString).forward(request, response);
} catch (RepositoryException e) {
throw new ServletException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.redhat.pantheon.asciidoctor.AsciidoctorService;
import com.redhat.pantheon.helper.PantheonConstants;
import com.redhat.pantheon.model.document.DocumentVariant;
import com.redhat.pantheon.validation.helper.XrefValidationHelper;
import org.apache.commons.lang3.LocaleUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
Expand Down Expand Up @@ -93,6 +94,7 @@ protected void doGet(SlingHttpServletRequest request,

// only allow forced rerendering if this is a draft version. Released and historical revs are written in stone.
boolean draft = latest && variant.hasDraft();
XrefValidationHelper.getInstance().initList();
String html = asciidoctorService.getDocumentHtml(
variant.getParentLocale().getParent(),
LocaleUtils.toLocale(variant.getParentLocale().getName()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.redhat.pantheon.model.document.DocumentVersion;
import com.redhat.pantheon.servlet.util.ServletHelper;
import com.redhat.pantheon.sling.ServiceResourceResolverProvider;
import com.redhat.pantheon.validation.helper.XrefValidationHelper;
import org.apache.http.HttpStatus;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.PersistenceException;
Expand Down Expand Up @@ -100,6 +101,7 @@ public void run(SlingHttpServletRequest request, PostResponse response, SlingPos
.released().get();

// Regenerate the document once more
XrefValidationHelper.getInstance().initList();
asciidoctorService.getDocumentHtml(document, locale, variant, false, new HashMap(),true);
events.fireEvent(new DocumentVersionPublishedEvent(documentVersion), 15);
ServletUtils.getCustomerPortalUrl(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected void versionUpload(SlingHttpServletRequest request,
resolver.commit();

Map<String, Object> context = asciidoctorService.buildContextFromRequest(request);
new XrefValidationHelper().initList();
XrefValidationHelper.getInstance().initList();
asciidoctorService.getDocumentHtml(document, localeObj, document.getWorkspace().getCanonicalVariantName(),
true, context, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ public class XrefValidationHelper {

private List<String> xRefs;

public XrefValidationHelper() {
initList();
private XrefValidationHelper() {
}

private static class SingletonHelper{
private static final XrefValidationHelper INSTANCE = new XrefValidationHelper();
}

public static XrefValidationHelper getInstance(){
return SingletonHelper.INSTANCE;
}
public List<String> getObjectsToValidate() {
return xRefs;
}
Expand All @@ -23,8 +29,9 @@ public void initList() {
}

public void setObjectsToValidate(List<String> objectsToValidate) {
if(objectsToValidate.size()>0){
xRefs.addAll(objectsToValidate);
if(null == xRefs || objectsToValidate.isEmpty()){
return;
}
xRefs.addAll(objectsToValidate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ private Violations checkIfXrefValid(Violations violations) {
private ErrorDetails checkXref() {
ErrorDetails errorDetails = new ErrorDetails();
try {
XrefValidationHelper xrefValidationHelper = new XrefValidationHelper();
List<String> xrefTargets = xrefValidationHelper.getObjectsToValidate();
List<String> xrefTargets = XrefValidationHelper.getInstance().getObjectsToValidate();
if(null == xrefTargets || xrefTargets.size()==0){
return errorDetails;
}
Expand Down

0 comments on commit 56ad1a7

Please sign in to comment.