Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(AChecker) fixes #28156: Expose the AChecker DWR class as a REST Endpoint #29879

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.dotcms.enterprise.achecker;

import com.dotcms.enterprise.achecker.model.GuideLineBean;
import com.dotmarketing.business.DotValidationException;
import com.dotmarketing.exception.DotDataException;

import java.util.List;
import java.util.Map;

/**
* This API provides the methods to interact with the Accessibility Checker service in dotCMS. Users
* of this API can access the different Accessibility Guidelines available in the system, and
* validate content against them.
*
* @author Jose Castro
* @since Sep 5th, 2024
*/
public interface ACheckerAPI {

String LANG = "lang";
String CONTENT = "content";
String GUIDELINES = "guidelines";
String FRAGMENT = "fragment";

/**
* Returns the list of Accessibility Guidelines available in the system.
*
* @return The list of Accessibility Guidelines available in the system.
*
* @throws DotDataException If the guidelines can't be retrieved.
*/
List<GuideLineBean> getAccessibilityGuidelineList() throws DotDataException;

/**
* Validates the given content against the specified Accessibility Guidelines.
*
* @param validationData Map containing the parameters to validate:
* <ul>
* <li>{@code lang}: the language of the content to validate (ISO
* 639-1, 2 letters)</li>
* <li>{@code content}: the content to validate</li>
* <li>{@code guidelines}: the guidelines to validate against
* (comma-separated list of guideline abbreviations)</li>
* <li>{@code fragment}: whether the content is a fragment (does
* not contain the required HTML tags)</li>
* </ul>
*
* @return The result of the validation, as a JSON object.
*
* @throws DotValidationException If the validation fails.
*/
ACheckerResponse validate(final Map<String, String> validationData) throws DotValidationException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.dotcms.enterprise.achecker.impl;

import com.dotcms.enterprise.achecker.ACheckerAPI;
import com.dotcms.enterprise.achecker.ACheckerRequest;
import com.dotcms.enterprise.achecker.ACheckerResponse;
import com.dotcms.enterprise.achecker.dao.GuidelinesDAO;
import com.dotcms.enterprise.achecker.model.GuideLineBean;
import com.dotcms.enterprise.achecker.tinymce.DaoLocator;
import com.dotcms.exception.ExceptionUtil;
import com.dotcms.util.EnterpriseFeature;
import com.dotmarketing.business.DotValidationException;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.util.UtilMethods;

import java.util.List;
import java.util.Map;

/**
* Implements the {@link ACheckerAPI} interface.
*
* @author Jose Castro
* @since Sep 5th, 2024
*/
public class ACheckerAPIImpl implements ACheckerAPI {

private List<GuideLineBean> accessibilityGuidelineList = null;

/**
* Returns the list of Accessibility Guidelines available in the system.
*
* @return The list of Accessibility Guidelines available in the system.
*
* @throws Exception If the guidelines can't be retrieved.
*/
@EnterpriseFeature
public List<GuideLineBean> getAccessibilityGuidelineList() throws DotDataException {
try {
if (UtilMethods.isNotSet(this.accessibilityGuidelineList)) {
GuidelinesDAO gLines = DaoLocator.getGuidelinesDAO();
this.accessibilityGuidelineList = gLines.getOpenGuidelines();
}
return this.accessibilityGuidelineList;
} catch (final Exception e) {
throw new DotDataException(ExceptionUtil.getErrorMessage(e), e);
}
}

/**
* Validates the given content against the specified Accessibility Guidelines.
*
* @param validationData Map containing the parameters to validate:
* <ul>
* <li>{@code lang}: the language of the content to validate (ISO
* 639-1, 2 letters)</li>
* <li>{@code content}: the content to validate</li>
* <li>{@code guidelines}: the guidelines to validate against
* (comma-separated list of guideline abbreviations)</li>
* <li>{@code fragment}: whether the content is a fragment (does
* not contain the required HTML tags)</li>
* </ul>
*
* @return The result of the validation, as a JSON object.
*
* @throws DotValidationException If the validation fails.
*/
@EnterpriseFeature
public ACheckerResponse validate(final Map<String, String> validationData) throws DotValidationException {
final String lang = validationData.get(LANG);
final String content = validationData.get(CONTENT);
final String guidelines = validationData.get(GUIDELINES);
final String fragment = validationData.get(FRAGMENT);
try {
if (UtilMethods.isSet(lang) && lang.trim().length() == 2) {
DaoLocator.getLangCodesDAO().getLangCodeBy3LetterCode(lang);
}
final ACheckerRequest request = new ACheckerRequest(lang, content, guidelines, Boolean.parseBoolean(fragment));
return new ACheckerImpl().validate(request);
} catch (final Exception e) {
throw new DotValidationException(ExceptionUtil.getErrorMessage(e), e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,21 @@

package com.dotcms.enterprise.achecker.model;

import com.dotcms.enterprise.achecker.utility.Constants;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import com.dotcms.enterprise.achecker.utility.Constants;
import com.dotcms.enterprise.achecker.model.ReflectionBean;

/**
* This class represents a Validation Guideline use by the Accessibility Checker to determine
* whether a specified content meets the expected requirements. Content Authors can select the
* guideline they want to validate against a given content.
*
* @author root
* @since N/A
*/
public class GuideLineBean extends ReflectionBean {


private String preamble;
private String earlid;
Expand Down Expand Up @@ -167,5 +172,22 @@ public boolean isDefaultGuideLine() {
return Constants.DEFAULT_GUIDELINE.equalsIgnoreCase(this.abbr);
}


@Override
public String toString() {
return "GuideLineBean{" +
"preamble='" + preamble + '\'' +
", earlid='" + earlid + '\'' +
", long_name='" + long_name + '\'' +
", abbr='" + abbr + '\'' +
", title='" + title + '\'' +
", guideline_id=" + guideline_id +
", user_id=" + user_id +
", status=" + status +
", open_to_public=" + open_to_public +
", seal_icon_name='" + seal_icon_name + '\'' +
", subset='" + subset + '\'' +
", defaultGuideLine=" + defaultGuideLine +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,75 +45,53 @@

package com.dotcms.enterprise.achecker.tinymce;

import java.util.List;
import java.util.Map;

import com.dotcms.enterprise.LicenseUtil;
import com.dotcms.enterprise.achecker.ACheckerRequest;
import com.dotcms.enterprise.achecker.ACheckerResponse;
import com.dotcms.enterprise.achecker.dao.GuidelinesDAO;
import com.dotcms.enterprise.achecker.dao.LangCodesDAO;
import com.dotcms.enterprise.achecker.impl.ACheckerImpl;
import com.dotcms.enterprise.achecker.model.GuideLineBean;
import com.dotcms.enterprise.achecker.tinymce.DaoLocator;
import com.dotcms.enterprise.license.LicenseLevel;
import com.dotcms.exception.ExceptionUtil;
import com.dotcms.rest.api.v1.accessibility.ACheckerResource;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.util.Logger;

import java.util.List;
import java.util.Map;

/**
* @deprecated This one and all DWR-related classes will be deprecated in the near future. Please
* use the REST {@link ACheckerResource} Endpoint instead.
*/
@Deprecated(forRemoval = true)
public class ACheckerDWR {

private List<GuideLineBean> listaGuidelines = null;

private List<GuideLineBean> getListaGudelines() throws Exception {
if(LicenseUtil.getLevel()< LicenseLevel.STANDARD.level)
throw new RuntimeException("need enterprise license");
try{
if( listaGuidelines == null ){
GuidelinesDAO gLines = DaoLocator.getGuidelinesDAO();
listaGuidelines = gLines.getOpenGuidelines();
}
return listaGuidelines;
}catch (Exception e) {
throw e;
}
return APILocator.getACheckerAPI().getAccessibilityGuidelineList();
}


public List<GuideLineBean> getSupportedGudelines(){
if(LicenseUtil.getLevel()<LicenseLevel.STANDARD.level)
throw new RuntimeException("need enterprise license");
try{
if (LicenseUtil.getLevel()<LicenseLevel.STANDARD.level) {
throw new RuntimeException("need enterprise license");
}
try {
return getListaGudelines();
}catch (Exception e) {
e.printStackTrace();
} catch (final Exception e) {
Logger.error(this, String.format("Failed to retrieve Accessibility Guidelines: %s",
ExceptionUtil.getErrorMessage(e)), e);
}
return null;
}

public ACheckerResponse validate(Map<String, String> params) {
if(LicenseUtil.getLevel()<LicenseLevel.STANDARD.level)
throw new RuntimeException("need enterprise license");
public ACheckerResponse validate(final Map<String, String> params) {
if (LicenseUtil.getLevel()<LicenseLevel.STANDARD.level) {
throw new RuntimeException("need enterprise license");
}
try {
String lang = params.get("lang");
String content = params.get("content");
String guidelines = params.get("guidelines");
String fragment = params.get("fragment");
if( lang != null && lang.trim().length() == 2 ){
LangCodesDAO langCodeDao = DaoLocator.getLangCodesDAO();
langCodeDao .getLangCodeBy3LetterCode( lang );
}
String toValidate = content ;
ACheckerRequest request = new ACheckerRequest(lang, toValidate, guidelines, Boolean.parseBoolean(fragment) );
ACheckerImpl achecker = new ACheckerImpl();
return achecker.validate(request);
} catch (Exception e) {
e.printStackTrace();
return APILocator.getACheckerAPI().validate(params);
} catch (final Exception e) {
Logger.error(this, String.format("Failed to validate Accessibility Guidelines in content with params: " +
"[ %s ] : %s", params, ExceptionUtil.getErrorMessage(e)), e);
}
return null;

}




}
Loading
Loading