From 8dafd01b22612473778554faa082c8c4cc7000c3 Mon Sep 17 00:00:00 2001 From: Alexandre Oliveira Date: Sun, 29 Sep 2024 19:39:20 -0300 Subject: [PATCH] - Code Smells: WEM and Search --- .../viglet/turing/sn/TurSNSearchProcess.java | 1 - .../java/com/viglet/turing/solr/TurSolr.java | 10 +-- .../commons/sn/field/TurSNFieldName.java | 3 + .../turing/connector/wem/TurWEMCommander.java | 11 +-- .../wem/broker/attribute/TurWEMAttrClass.java | 82 ++++++++++--------- .../wem/broker/indexer/TurWEMIndex.java | 52 ++++++------ .../broker/update/TurWEMUpdateFileWidget.java | 2 +- .../turing/connector/wem/ext/TurCTDName.java | 2 +- .../connector/wem/ext/TurChannelPath.java | 6 +- .../turing/connector/wem/ext/TurDPSUrl.java | 3 +- .../wem/ext/TurSpotlightExtraFields.java | 3 +- .../wem/mappers/MappingDefinitions.java | 18 +--- .../mappers/MappingDefinitionsProcess.java | 4 + 13 files changed, 101 insertions(+), 96 deletions(-) diff --git a/turing-app/src/main/java/com/viglet/turing/sn/TurSNSearchProcess.java b/turing-app/src/main/java/com/viglet/turing/sn/TurSNSearchProcess.java index 6370a78c79..6426953c63 100644 --- a/turing-app/src/main/java/com/viglet/turing/sn/TurSNSearchProcess.java +++ b/turing-app/src/main/java/com/viglet/turing/sn/TurSNSearchProcess.java @@ -34,7 +34,6 @@ import com.viglet.turing.persistence.dto.sn.field.TurSNSiteFieldExtDto; import com.viglet.turing.persistence.dto.sn.field.TurSNSiteFieldExtFacetDto; import com.viglet.turing.persistence.model.sn.TurSNSite; -import com.viglet.turing.persistence.model.sn.field.TurSNSiteFieldExt; import com.viglet.turing.persistence.model.sn.field.TurSNSiteFieldExtFacet; import com.viglet.turing.persistence.model.sn.metric.TurSNSiteMetricAccess; import com.viglet.turing.persistence.repository.sn.TurSNSiteRepository; diff --git a/turing-app/src/main/java/com/viglet/turing/solr/TurSolr.java b/turing-app/src/main/java/com/viglet/turing/solr/TurSolr.java index d38c13738a..99ca9fa2e7 100644 --- a/turing-app/src/main/java/com/viglet/turing/solr/TurSolr.java +++ b/turing-app/src/main/java/com/viglet/turing/solr/TurSolr.java @@ -891,11 +891,11 @@ private static boolean isFacetTypeDefault(TurSNSiteFacetFieldEnum turSNSiteFacet private static TurSNSiteFacetFieldEnum getFacetType(TurSNSiteFieldExt facet, TurSNSite turSNSite, TurSNFilterQueryOperator operator) { - return operatorIsNotEmpty(operator) ? - getFaceTypeFromOperator(operator) : - isFacetTypeDefault(facet.getFacetType()) ? - getFacetTypeFromSite(turSNSite) : - facet.getFacetType(); + if (operatorIsNotEmpty(operator)) + return getFaceTypeFromOperator(operator); + if (isFacetTypeDefault(facet.getFacetType())) + return getFacetTypeFromSite(turSNSite); + return facet.getFacetType(); } diff --git a/turing-commons/src/main/java/com/viglet/turing/commons/sn/field/TurSNFieldName.java b/turing-commons/src/main/java/com/viglet/turing/commons/sn/field/TurSNFieldName.java index 01424ab524..ba1df94bca 100644 --- a/turing-commons/src/main/java/com/viglet/turing/commons/sn/field/TurSNFieldName.java +++ b/turing-commons/src/main/java/com/viglet/turing/commons/sn/field/TurSNFieldName.java @@ -23,6 +23,9 @@ * @since 0.3.9 */ public class TurSNFieldName { + private TurSNFieldName() { + throw new IllegalStateException("Semantic Navigation Field Names class"); + } public static final String ID = "id"; public static final String URL = "url"; public static final String ABSTRACT = "abstract"; diff --git a/turing-wem/src/main/java/com/viglet/turing/connector/wem/TurWEMCommander.java b/turing-wem/src/main/java/com/viglet/turing/connector/wem/TurWEMCommander.java index 8351910c11..97221a3583 100644 --- a/turing-wem/src/main/java/com/viglet/turing/connector/wem/TurWEMCommander.java +++ b/turing-wem/src/main/java/com/viglet/turing/connector/wem/TurWEMCommander.java @@ -174,7 +174,6 @@ private void runByGuidList() private void runAllObjectTypes() throws ApplicationException, ConfigException, ValidationException { IPagingList contentTypeIPagingList = ContentType.findAll(); - @SuppressWarnings("unchecked") List contentTypes = contentTypeIPagingList.asList(); contentTypes.add(StaticFile.getTypeObjectTypeRef().getObjectType()); @@ -203,7 +202,7 @@ private void indexByContentType(String siteName, ObjectType objectType) rp.setTopRelationOnly(false); AsObjectType aot = AsObjectType.getInstance(new ObjectTypeRef((ManagedObject) objectType)); - IValidToIndex instance = mappingDefinitions.validToIndex(objectType, turingConfig); + IValidToIndex instance = mappingDefinitions.validToIndex(objectType); if (aot.isStaticFile()) { results = queryStaticFilesList(rp, instance); } else { @@ -227,8 +226,7 @@ private void indexByContentType(String siteName, ObjectType objectType) } - private IPagingList queryContentInstanceList(ObjectType objectType, RequestParameters rp, IValidToIndex instance) - throws Exception { + private IPagingList queryContentInstanceList(ObjectType objectType, RequestParameters rp, IValidToIndex instance) { IPagingList results; ContentInstanceWhereClause clause = new ContentInstanceWhereClause(); @@ -241,7 +239,7 @@ private IPagingList queryContentInstanceList(ObjectType objectType, RequestParam return results; } - private IPagingList queryStaticFilesList(RequestParameters rp, IValidToIndex instance) throws Exception { + private IPagingList queryStaticFilesList(RequestParameters rp, IValidToIndex instance) { IPagingList results; StaticFileWhereClause clause = new StaticFileWhereClause(); StaticFileDBQuery query = new StaticFileDBQuery(); @@ -338,8 +336,7 @@ private void indexContentInstances(ManagedObjectVCMRef[] refs, HashMap attributeByClass(TurAttrDefContext turAttrDefContext, AttributeData attributeData) - throws Exception { + private TurWEMAttrClass() { + throw new IllegalStateException("TurWEMAttrClass"); + } - TuringTag turingTag = turAttrDefContext.getTuringTag(); - ContentInstance ci = turAttrDefContext.getContentInstance(); - IHandlerConfiguration config = turAttrDefContext.getiHandlerConfiguration(); - List attributesDefs = new ArrayList<>(); + public static List attributeByClass(TurAttrDefContext turAttrDefContext, AttributeData attributeData) { - if (turingTag.getSrcClassName() != null) { - String className = turingTag.getSrcClassName(); - if (log.isDebugEnabled()) - log.debug("ClassName : " + className); + TuringTag turingTag = turAttrDefContext.getTuringTag(); + ContentInstance ci = turAttrDefContext.getContentInstance(); + IHandlerConfiguration config = turAttrDefContext.getiHandlerConfiguration(); + List attributesDefs = new ArrayList<>(); - Object extAttribute = Class.forName(className).getDeclaredConstructor().newInstance(); - TurMultiValue turMultiValue = ((ExtAttributeInterface) extAttribute).consume(turingTag, ci, attributeData, - config); - TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue); - attributesDefs.add(turAttrDef); - } else { - TurMultiValue turMultiValue = new TurMultiValue(); - if (turingTag.getSrcAttributeType() != null && turingTag.getSrcAttributeType().equals(HTML)) { - turMultiValue.add(TurCommonsUtils.html2Text(attributeData.getValue().toString())); - TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue); - attributesDefs.add(turAttrDef); - } else if (attributeData != null && attributeData.getValue() != null) { - turMultiValue.add(attributeData.getValue().toString()); - TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue); - attributesDefs.add(turAttrDef); - } + if (turingTag.getSrcClassName() != null) { + String className = turingTag.getSrcClassName(); + if (log.isDebugEnabled()) + log.debug("ClassName : " + className); + try { + Object extAttribute = Class.forName(className).getDeclaredConstructor().newInstance(); + TurMultiValue turMultiValue = ((ExtAttributeInterface) extAttribute).consume(turingTag, ci, attributeData, + config); + TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue); + attributesDefs.add(turAttrDef); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | + NoSuchMethodException | ClassNotFoundException e) { + log.error(e.getMessage(), e); + } + } else { + TurMultiValue turMultiValue = new TurMultiValue(); + if (turingTag.getSrcAttributeType() != null && turingTag.getSrcAttributeType().equals(HTML)) { + turMultiValue.add(TurCommonsUtils.html2Text(attributeData.getValue().toString())); + TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue); + attributesDefs.add(turAttrDef); + } else if (attributeData != null && attributeData.getValue() != null) { + turMultiValue.add(attributeData.getValue().toString()); + TurAttrDef turAttrDef = new TurAttrDef(turingTag.getTagName(), turMultiValue); + attributesDefs.add(turAttrDef); + } - } - return attributesDefs; - } + } + return attributesDefs; + } } diff --git a/turing-wem/src/main/java/com/viglet/turing/connector/wem/broker/indexer/TurWEMIndex.java b/turing-wem/src/main/java/com/viglet/turing/connector/wem/broker/indexer/TurWEMIndex.java index ad4868a242..81bdbc47e8 100644 --- a/turing-wem/src/main/java/com/viglet/turing/connector/wem/broker/indexer/TurWEMIndex.java +++ b/turing-wem/src/main/java/com/viglet/turing/connector/wem/broker/indexer/TurWEMIndex.java @@ -79,35 +79,13 @@ private static void indexContentInstance(ManagedObject mo, IHandlerConfiguration MappingDefinitions mappingDefinitions) { try { ContentInstance contentInstance = (ContentInstance) mo; - // When there is related content but no associated site. if (MGMT_SITE.equals(siteName)) { siteName = TuringUtils.getSiteName(contentInstance, config); } - - AsLocaleData asLocaleData = null; - if (hasLocale(contentInstance)) - asLocaleData = contentInstance.getLocale().getAsLocale().getData(); - - TurSNSiteConfig turSNSiteConfig = config.getSNSiteConfig(siteName, asLocaleData); String contentTypeName = contentInstance.getObjectType().getData().getName(); if (isCTDIntoMapping(contentTypeName, config)) { - if (mappingDefinitions.isClassValidToIndex(contentInstance, config)) { - log.info("Viglet Turing indexer Processing Content Type: {}, WEM Site: {}, SNSite: {}, Locale: {}", - contentTypeName, siteName, turSNSiteConfig.getName(), turSNSiteConfig.getLocale()); - String xmlToIndex = generateXMLToIndex(contentInstance, config); - if (xmlToIndex.contains(FILE_PROTOCOL)) { - generateZipImport(xmlToIndex, turSNSiteConfig, config); - } else { - postIndex(xmlToIndex, turSNSiteConfig, config); - } - } else { - if (mappingDefinitions.hasClassValidToIndex(mo.getObjectType().getData().getName()) - && mo.getContentManagementId() != null) { - TurWEMDeindex.indexDelete(mo.getContentManagementId(), config, siteName); - } - - } + indexFromMapping(mo, config, siteName, mappingDefinitions, contentInstance, contentTypeName); } else { if (log.isDebugEnabled()) log.debug("Mapping definition is not found in the mappingXML for the CTD and ignoring: {}", contentTypeName); @@ -117,6 +95,34 @@ private static void indexContentInstance(ManagedObject mo, IHandlerConfiguration } } + private static void indexFromMapping(ManagedObject mo, IHandlerConfiguration config, String siteName, + MappingDefinitions mappingDefinitions, ContentInstance contentInstance, + String contentTypeName) throws Exception { + TurSNSiteConfig turSNSiteConfig = config.getSNSiteConfig(siteName, getAsLocaleData(contentInstance)); + if (mappingDefinitions.isContentValidToIndex(contentInstance, config)) { + log.info("Viglet Turing indexer Processing Content Type: {}, WEM Site: {}, SNSite: {}, Locale: {}", + contentTypeName, siteName, turSNSiteConfig.getName(), turSNSiteConfig.getLocale()); + String xmlToIndex = generateXMLToIndex(contentInstance, config); + if (xmlToIndex.contains(FILE_PROTOCOL)) { + generateZipImport(xmlToIndex, turSNSiteConfig, config); + } else { + postIndex(xmlToIndex, turSNSiteConfig, config); + } + } else { + if (mappingDefinitions.hasClassValidToIndex(mo.getObjectType().getData().getName()) + && mo.getContentManagementId() != null) { + TurWEMDeindex.indexDelete(mo.getContentManagementId(), config, siteName); + } + } + } + + private static AsLocaleData getAsLocaleData(ContentInstance contentInstance) throws ApplicationException { + AsLocaleData asLocaleData = null; + if (hasLocale(contentInstance)) + asLocaleData = contentInstance.getLocale().getAsLocale().getData(); + return asLocaleData; + } + private static boolean hasLocale(ContentInstance contentInstance) throws ApplicationException { return (contentInstance.getLocale() != null) && (contentInstance.getLocale().getAsLocale() != null) && (contentInstance.getLocale().getAsLocale().getData() != null); diff --git a/turing-wem/src/main/java/com/viglet/turing/connector/wem/broker/update/TurWEMUpdateFileWidget.java b/turing-wem/src/main/java/com/viglet/turing/connector/wem/broker/update/TurWEMUpdateFileWidget.java index b6254a6570..314e3da294 100644 --- a/turing-wem/src/main/java/com/viglet/turing/connector/wem/broker/update/TurWEMUpdateFileWidget.java +++ b/turing-wem/src/main/java/com/viglet/turing/connector/wem/broker/update/TurWEMUpdateFileWidget.java @@ -40,7 +40,7 @@ private TurWEMUpdateFileWidget() { } public static List attributeFileWidgetUpdate(TurAttrDefContext turAttrDefContext, - AttributeData attributeData) throws Exception { + AttributeData attributeData) { TuringTag turingTag = turAttrDefContext.getTuringTag(); ContentInstance ci = turAttrDefContext.getContentInstance(); diff --git a/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurCTDName.java b/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurCTDName.java index be3d72f10a..b19f48dcc7 100644 --- a/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurCTDName.java +++ b/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurCTDName.java @@ -39,6 +39,6 @@ public TurMultiValue consume(TuringTag tag, ContentInstance ci, AttributeData at } catch (ApplicationException e) { log.error(e.getMessage(), e); } - return null; + return new TurMultiValue(); } } diff --git a/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurChannelPath.java b/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurChannelPath.java index 9615c3b547..d5628a75dc 100644 --- a/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurChannelPath.java +++ b/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurChannelPath.java @@ -17,8 +17,6 @@ */ package com.viglet.turing.connector.wem.ext; -import java.lang.invoke.MethodHandles; - import com.viglet.turing.connector.wem.beans.TurMultiValue; import com.viglet.turing.connector.wem.beans.TuringTag; import com.viglet.turing.connector.wem.config.IHandlerConfiguration; @@ -31,6 +29,8 @@ import com.vignette.as.client.javabean.ContentInstance; import com.vignette.logging.context.ContextLogger; +import java.lang.invoke.MethodHandles; + public class TurChannelPath implements ExtAttributeInterface { private static final ContextLogger log = ContextLogger.getLogger(MethodHandles.lookup().lookupClass()); @@ -47,7 +47,7 @@ public TurMultiValue consume(TuringTag tag, ContentInstance ci, AttributeData at for (String s : breadcrumb) channelPath.append("/").append(s); } } catch (ApplicationException | AuthorizationException | ValidationException e) { - throw new RuntimeException(e); + log.error(e.getMessage(), e); } return TurMultiValue.singleItem(channelPath.toString()); } diff --git a/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurDPSUrl.java b/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurDPSUrl.java index 9e006ea2a6..52c61ca909 100644 --- a/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurDPSUrl.java +++ b/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurDPSUrl.java @@ -55,7 +55,8 @@ public TurMultiValue consume(TuringTag tag, ContentInstance ci, AttributeData at return turMultiValue; } } catch (ApplicationException | AuthorizationException | ValidationException | RemoteException e) { - throw new RuntimeException(e); + log.error(e.getMessage(), e); } + return new TurMultiValue(); } } diff --git a/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurSpotlightExtraFields.java b/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurSpotlightExtraFields.java index 7dc0e0f21f..804ebc3a04 100644 --- a/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurSpotlightExtraFields.java +++ b/turing-wem/src/main/java/com/viglet/turing/connector/wem/ext/TurSpotlightExtraFields.java @@ -33,6 +33,7 @@ import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; public class TurSpotlightExtraFields implements ExtAttributeInterface { @@ -74,7 +75,7 @@ public TurMultiValue consume(TuringTag tag, ContentInstance ci, AttributeData at } catch (ApplicationException | JsonProcessingException e) { log.error(e.getMessage(), e); } - return null; + return new TurMultiValue(); } static class TurCTDAttributes { diff --git a/turing-wem/src/main/java/com/viglet/turing/connector/wem/mappers/MappingDefinitions.java b/turing-wem/src/main/java/com/viglet/turing/connector/wem/mappers/MappingDefinitions.java index 0e6dc19169..eaaf4704f5 100644 --- a/turing-wem/src/main/java/com/viglet/turing/connector/wem/mappers/MappingDefinitions.java +++ b/turing-wem/src/main/java/com/viglet/turing/connector/wem/mappers/MappingDefinitions.java @@ -71,26 +71,16 @@ public boolean hasClassValidToIndex(String contentTypeName) { return status; } - public IValidToIndex validToIndex(ObjectType ot, IHandlerConfiguration config) { - + public IValidToIndex validToIndex(ObjectType ot) { try { String contentTypeName; contentTypeName = ot.getData().getName(); - if (this.hasClassValidToIndex(contentTypeName)) { CTDMappings ctdMappings = turCTDMappingMap.get(contentTypeName); IValidToIndex instance = null; String className = ctdMappings.getClassValidToIndex(); if (className != null) { - Class clazz = Class.forName(className); - - if (clazz == null) { - if (logger.isDebugEnabled()) - logger.debug(String.format("Valid to Index className is not found in the jar file: %s", - className)); - - } else - instance = (IValidToIndex) clazz.newInstance(); + instance = (IValidToIndex) Class.forName(className).getDeclaredConstructor().newInstance(); } return instance; } @@ -101,9 +91,9 @@ public IValidToIndex validToIndex(ObjectType ot, IHandlerConfiguration config) { } - public boolean isClassValidToIndex(ContentInstance ci, IHandlerConfiguration config) { + public boolean isContentValidToIndex(ContentInstance ci, IHandlerConfiguration config) { try { - IValidToIndex iValidToIndex = validToIndex(ci.getObjectType(), config); + IValidToIndex iValidToIndex = validToIndex(ci.getObjectType()); return !(iValidToIndex != null && !iValidToIndex.isValid(ci, config)); } catch (Exception e) { logger.error(e.getMessage(), e); diff --git a/turing-wem/src/main/java/com/viglet/turing/connector/wem/mappers/MappingDefinitionsProcess.java b/turing-wem/src/main/java/com/viglet/turing/connector/wem/mappers/MappingDefinitionsProcess.java index b1dadfaffd..033f8aa022 100644 --- a/turing-wem/src/main/java/com/viglet/turing/connector/wem/mappers/MappingDefinitionsProcess.java +++ b/turing-wem/src/main/java/com/viglet/turing/connector/wem/mappers/MappingDefinitionsProcess.java @@ -49,6 +49,10 @@ public static MappingDefinitions loadMappings(String resourceXml) { try { DocumentBuilderFactory dlf = DocumentBuilderFactory.newInstance(); + dlf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + dlf.setFeature("http://xml.org/sax/features/external-general-entities", false); + dlf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + DocumentBuilder db = dlf.newDocumentBuilder(); File f = new File(resourceXml);