Skip to content
Open
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
Expand Up @@ -17,6 +17,7 @@
import com.agiletec.aps.system.common.entity.model.attribute.TextAttribute;
import com.agiletec.aps.system.services.group.Group;
import com.agiletec.aps.system.services.lang.Lang;
import com.agiletec.aps.util.ApplicationContextProvider;
import com.agiletec.plugins.jacms.aps.system.services.content.model.CmsAttributeReference;
import com.agiletec.plugins.jacms.aps.system.services.resource.IResourceManager;
import com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInterface;
Expand All @@ -30,8 +31,6 @@
import org.entando.entando.ent.util.EntLogging.EntLogFactory;
import org.entando.entando.ent.util.EntLogging.EntLogger;
import org.jdom2.Element;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
* Classe astratta di appoggio agli attributi di tipo Risorsa.
Expand Down Expand Up @@ -386,7 +385,7 @@
return;
}
try {
ResourceInterface resource = this.getResourceManager().loadResource(resourceId.toString());

Check failure on line 388 in cms-plugin/src/main/java/com/agiletec/plugins/jacms/aps/system/services/content/model/attribute/AbstractResourceAttribute.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Fix this access that will throw a NullPointerException when executed.

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ_MTCB5jmi89DNoNomV&open=AZ_MTCB5jmi89DNoNomV&pullRequest=359
if (null != resource) {
this.setResource(resource, this.getDefaultLangCode());
}
Expand Down Expand Up @@ -419,6 +418,9 @@
}

protected IResourceManager getResourceManager() {
if (this.resourceManager == null) {
this.resourceManager = ApplicationContextProvider.resolveBean(IResourceManager.class);
}
return resourceManager;
}

Expand All @@ -430,11 +432,9 @@
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
if (ctx == null) {
this.resourceManager = ApplicationContextProvider.resolveBean(IResourceManager.class);
if (this.resourceManager == null) {
logger.warn("Null WebApplicationContext during deserialization");
return;
}
this.resourceManager = ctx.getBean(IResourceManager.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.agiletec.aps.system.common.entity.model.attribute.HypertextAttribute;
import com.agiletec.aps.system.services.lang.ILangManager;
import com.agiletec.aps.system.services.lang.Lang;
import com.agiletec.aps.util.ApplicationContextProvider;
import com.agiletec.aps.system.services.page.IPageManager;
import com.agiletec.plugins.jacms.aps.system.services.content.IContentManager;
import com.agiletec.plugins.jacms.aps.system.services.content.model.CmsAttributeReference;
Expand All @@ -32,8 +33,6 @@
import org.entando.entando.ent.util.EntLogging.EntLogFactory;
import org.entando.entando.ent.util.EntLogging.EntLogger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
* Rappresenta una informazione di tipo "ipertesto" specifico per il cms.
Expand Down Expand Up @@ -205,6 +204,9 @@ private SymbolicLinkValidator getSymbolicLinkValidator(BeanFactory beanFactory)

@Deprecated
protected IContentManager getContentManager() {
if (this.contentManager == null) {
this.contentManager = ApplicationContextProvider.resolveBean(IContentManager.class);
}
return contentManager;
}

Expand All @@ -215,6 +217,9 @@ public void setContentManager(IContentManager contentManager) {

@Deprecated
protected IPageManager getPageManager() {
if (this.pageManager == null) {
this.pageManager = ApplicationContextProvider.resolveBean(IPageManager.class);
}
return pageManager;
}

Expand All @@ -225,6 +230,9 @@ public void setPageManager(IPageManager pageManager) {

@Deprecated
public IResourceManager getResourceManager() {
if (this.resourceManager == null) {
this.resourceManager = ApplicationContextProvider.resolveBean(IResourceManager.class);
}
return resourceManager;
}

Expand All @@ -245,14 +253,15 @@ public boolean isSearchableOptionSupported() {
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
if (ctx == null) {
this.contentManager = ApplicationContextProvider.resolveBean(IContentManager.class);
this.pageManager = ApplicationContextProvider.resolveBean(IPageManager.class);
this.resourceManager = ApplicationContextProvider.resolveBean(IResourceManager.class);
ILangManager langManager = ApplicationContextProvider.resolveBean(ILangManager.class);
if (langManager != null) {
this.setLangManager(langManager);
}
if (this.resourceManager == null) {
logger.warn("Null WebApplicationContext during deserialization");
return;
}
this.contentManager = ctx.getBean(IContentManager.class);
this.pageManager = ctx.getBean(IPageManager.class);
this.resourceManager = ctx.getBean(IResourceManager.class);
this.setLangManager(ctx.getBean(ILangManager.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.agiletec.aps.system.common.entity.model.attribute.AbstractJAXBAttribute;
import com.agiletec.aps.system.common.entity.model.attribute.TextAttribute;
import com.agiletec.aps.system.services.lang.ILangManager;
import com.agiletec.aps.util.ApplicationContextProvider;
import com.agiletec.aps.system.services.lang.Lang;
import com.agiletec.aps.system.services.page.IPageManager;
import com.agiletec.plugins.jacms.aps.system.services.content.IContentManager;
Expand All @@ -40,8 +41,6 @@
import org.entando.entando.ent.util.EntLogging.EntLogger;
import org.jdom2.Element;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
* Rappresenta una informazione di tipo "link". La destinazione del link è la
Expand Down Expand Up @@ -331,6 +330,9 @@ private String getProperty(String key, String langCode) {

@Deprecated
protected IContentManager getContentManager() {
if (this.contentManager == null) {
this.contentManager = ApplicationContextProvider.resolveBean(IContentManager.class);
}
return contentManager;
}

Expand All @@ -341,6 +343,9 @@ public void setContentManager(IContentManager contentManager) {

@Deprecated
protected IPageManager getPageManager() {
if (this.pageManager == null) {
this.pageManager = ApplicationContextProvider.resolveBean(IPageManager.class);
}
return pageManager;
}

Expand All @@ -351,6 +356,9 @@ public void setPageManager(IPageManager pageManager) {

@Deprecated
protected ILinkResolverManager getLinkResolverManager() {
if (this.linkResolverManager == null) {
this.linkResolverManager = ApplicationContextProvider.resolveBean(ILinkResolverManager.class);
}
return linkResolverManager;
}

Expand All @@ -361,6 +369,9 @@ public void setLinkResolverManager(ILinkResolverManager linkResolverManager) {

@Deprecated
public IResourceManager getResourceManager() {
if (this.resourceManager == null) {
this.resourceManager = ApplicationContextProvider.resolveBean(IResourceManager.class);
}
return resourceManager;
}

Expand All @@ -381,16 +392,17 @@ public void setResourceManager(IResourceManager resourceManager) {
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
if (ctx == null) {
this.contentManager = ApplicationContextProvider.resolveBean(IContentManager.class);
this.pageManager = ApplicationContextProvider.resolveBean(IPageManager.class);
this.linkResolverManager = ApplicationContextProvider.resolveBean(ILinkResolverManager.class);
this.resourceManager = ApplicationContextProvider.resolveBean(IResourceManager.class);
ILangManager langManager = ApplicationContextProvider.resolveBean(ILangManager.class);
if (langManager != null) {
this.setLangManager(langManager);
}
if (this.resourceManager == null) {
logger.warn("Null WebApplicationContext during deserialization");
return;
}
this.contentManager = ctx.getBean(IContentManager.class);
this.pageManager = ctx.getBean(IPageManager.class);
this.linkResolverManager = ctx.getBean(ILinkResolverManager.class);
this.resourceManager = ctx.getBean(IResourceManager.class);
this.setLangManager(ctx.getBean(ILangManager.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
package com.agiletec.plugins.jacms.aps.system.services.content.parse.attribute;

import com.agiletec.aps.system.common.entity.parse.attribute.TextAttributeHandler;
import com.agiletec.aps.util.ApplicationContextProvider;
import com.agiletec.plugins.jacms.aps.system.services.content.model.attribute.ResourceAttributeInterface;
import com.agiletec.plugins.jacms.aps.system.services.resource.IResourceManager;
import com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInterface;
import java.io.IOException;
import org.entando.entando.ent.util.EntLogging.EntLogFactory;
import org.entando.entando.ent.util.EntLogging.EntLogger;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

Expand Down Expand Up @@ -126,6 +125,9 @@ private void endResource() {
*/
@Deprecated
protected IResourceManager getResourceManager() {
if (this.resourceManager == null) {
this.resourceManager = ApplicationContextProvider.resolveBean(IResourceManager.class);
}
return this.resourceManager;
}

Expand Down Expand Up @@ -164,11 +166,9 @@ protected void setMetadataKey(String metadataKey) {
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
if (ctx == null) {
this.resourceManager = ApplicationContextProvider.resolveBean(IResourceManager.class);
if (this.resourceManager == null) {
_logger.warn("Null WebApplicationContext during deserialization");
return;
}
this.resourceManager = ctx.getBean(IResourceManager.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.agiletec.aps.system.services.baseconfig.ConfigInterface;
import com.agiletec.aps.system.services.lang.ILangManager;
import com.agiletec.aps.system.services.page.IPageManager;
import com.agiletec.aps.util.ApplicationContextProvider;
import com.agiletec.plugins.jacms.aps.system.JacmsSystemConstants;
import com.agiletec.plugins.jacms.aps.system.services.content.IContentManager;
import com.agiletec.plugins.jacms.aps.system.services.content.parse.attribute.ResourceAttributeHandler;
Expand Down Expand Up @@ -46,10 +47,16 @@
AttachAttribute attribute = new AttachAttribute();
attribute.setName("testAttach");
attribute.setResourceManager(resourceManager);
// current web application context available
attribute = testSerializeAndDeserialize(attribute);
Assertions.assertNotNull(attribute.getResourceManager());
// no current web application context -> re-wired through the ApplicationContextProvider fallback
attribute = testSerializeAndDeserializeNullApplicationContext(attribute);
Assertions.assertNull(attribute.getResourceManager());
Assertions.assertNotNull(attribute.getResourceManager());
// neither the current context nor the provider are available -> stays null (warn path).
// The raw field is checked (not the getter) so the self-healing getter does not re-resolve.
attribute = testSerializeAndDeserializeNoContextAvailable(attribute);
Assertions.assertNull(ReflectionTestUtils.getField(attribute, "resourceManager"));
}

@Test
Expand All @@ -60,15 +67,23 @@
attribute.setPageManager(pageManager);
attribute.setResourceManager(resourceManager);
attribute.setLangManager(langManager);
// current web application context available
attribute = testSerializeAndDeserialize(attribute);
Assertions.assertNotNull(attribute.getContentManager());
Assertions.assertNotNull(attribute.getPageManager());
Assertions.assertNotNull(attribute.getResourceManager());
Assertions.assertNotNull(ReflectionTestUtils.invokeGetterMethod(attribute, "langManager"));
// no current web application context -> re-wired through the ApplicationContextProvider fallback
attribute = testSerializeAndDeserializeNullApplicationContext(attribute);
Assertions.assertNull(attribute.getContentManager());
Assertions.assertNull(attribute.getPageManager());
Assertions.assertNull(attribute.getResourceManager());
Assertions.assertNotNull(attribute.getContentManager());
Assertions.assertNotNull(attribute.getPageManager());
Assertions.assertNotNull(attribute.getResourceManager());
// neither the current context nor the provider are available -> stays null (warn path).
// Raw fields are checked (not the getters) so the self-healing getters do not re-resolve.
attribute = testSerializeAndDeserializeNoContextAvailable(attribute);
Assertions.assertNull(ReflectionTestUtils.getField(attribute, "contentManager"));
Assertions.assertNull(ReflectionTestUtils.getField(attribute, "pageManager"));
Assertions.assertNull(ReflectionTestUtils.getField(attribute, "resourceManager"));
}

@Test
Expand All @@ -80,28 +95,42 @@
attribute.setResourceManager(resourceManager);
attribute.setLinkResolverManager(linkResolverManager);
attribute.setLangManager(langManager);
// current web application context available
attribute = testSerializeAndDeserialize(attribute);
Assertions.assertNotNull(attribute.getContentManager());
Assertions.assertNotNull(attribute.getPageManager());
Assertions.assertNotNull(attribute.getResourceManager());
Assertions.assertNotNull(attribute.getLinkResolverManager());
Assertions.assertNotNull(ReflectionTestUtils.invokeGetterMethod(attribute, "langManager"));
// no current web application context -> re-wired through the ApplicationContextProvider fallback
attribute = testSerializeAndDeserializeNullApplicationContext(attribute);
Assertions.assertNull(attribute.getContentManager());
Assertions.assertNull(attribute.getPageManager());
Assertions.assertNull(attribute.getResourceManager());
Assertions.assertNull(attribute.getLinkResolverManager());
Assertions.assertNull(ReflectionTestUtils.invokeGetterMethod(attribute, "langManager"));
Assertions.assertNotNull(attribute.getContentManager());
Assertions.assertNotNull(attribute.getPageManager());
Assertions.assertNotNull(attribute.getResourceManager());
Assertions.assertNotNull(attribute.getLinkResolverManager());
// neither the current context nor the provider are available -> stays null (warn path).
// Raw fields are checked (not the getters) so the self-healing getters do not re-resolve.
attribute = testSerializeAndDeserializeNoContextAvailable(attribute);
Assertions.assertNull(ReflectionTestUtils.getField(attribute, "contentManager"));
Assertions.assertNull(ReflectionTestUtils.getField(attribute, "pageManager"));
Assertions.assertNull(ReflectionTestUtils.getField(attribute, "resourceManager"));
Assertions.assertNull(ReflectionTestUtils.getField(attribute, "linkResolverManager"));
Assertions.assertNull(ReflectionTestUtils.getField(attribute, "_langManager"));
}

@Test
void testSerializeResourceAttributeHandler() throws Exception {
ResourceAttributeHandler attributeHandler = new ResourceAttributeHandler();
attributeHandler.setResourceManager(resourceManager);
// current web application context available
attributeHandler = testSerializeAndDeserialize(attributeHandler);
Assertions.assertNotNull(ReflectionTestUtils.invokeGetterMethod(attributeHandler, "resourceManager"));
// no current web application context -> re-wired through the ApplicationContextProvider fallback
attributeHandler = testSerializeAndDeserializeNullApplicationContext(attributeHandler);
Assertions.assertNull(ReflectionTestUtils.invokeGetterMethod(attributeHandler, "resourceManager"));
Assertions.assertNotNull(ReflectionTestUtils.invokeGetterMethod(attributeHandler, "resourceManager"));
// neither the current context nor the provider are available -> stays null (warn path).
attributeHandler = testSerializeAndDeserializeNoContextAvailable(attributeHandler);
Assertions.assertNull(ReflectionTestUtils.getField(attributeHandler, "resourceManager"));
}

private <T> T testSerializeAndDeserializeNullApplicationContext(T attribute) throws Exception {
Expand All @@ -111,6 +140,17 @@
}
}

private <T> T testSerializeAndDeserializeNoContextAvailable(T attribute) throws Exception {
try (MockedStatic<ContextLoader> contextLoader = Mockito.mockStatic(ContextLoader.class);
MockedStatic<ApplicationContextProvider> provider =
Mockito.mockStatic(ApplicationContextProvider.class)) {
contextLoader.when(() -> ContextLoader.getCurrentWebApplicationContext()).thenReturn(null);

Check warning on line 147 in cms-plugin/src/test/java/com/agiletec/plugins/jacms/aps/system/services/content/model/attribute/AttributeSerializationIntegrationTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this lambda with method reference 'ContextLoader::getCurrentWebApplicationContext'.

See more on https://sonarcloud.io/project/issues?id=entando_app-engine&issues=AZ_MTCDIjmi89DNoNomW&open=AZ_MTCDIjmi89DNoNomW&pullRequest=359
provider.when(() -> ApplicationContextProvider.resolveBean(Mockito.any())).thenReturn(null);
provider.when(ApplicationContextProvider::getBeanFactory).thenReturn(null);
return testSerializeAndDeserialize(attribute);
}
}

private <T> T testSerializeAndDeserialize(T object) throws Exception {

byte[] data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.agiletec.aps.system.common.entity.parse.attribute.AttributeHandlerInterface;
import com.agiletec.aps.system.common.searchengine.IndexableAttributeInterface;
import com.agiletec.aps.system.services.lang.ILangManager;
import com.agiletec.aps.util.ApplicationContextProvider;
import com.agiletec.aps.util.ApsProperties;
import com.agiletec.aps.util.ApsPropertiesDOM;
import java.io.IOException;
Expand All @@ -38,8 +39,6 @@
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
* This abstract class must be used when implementing Entity Attributes.
Expand Down Expand Up @@ -606,6 +605,9 @@ public void setAttributeManagerClassName(String attributeManagerClassName) {

@Deprecated
protected ILangManager getLangManager() {
if (this._langManager == null) {
this._langManager = ApplicationContextProvider.resolveBean(ILangManager.class);
}
return _langManager;
}

Expand All @@ -617,9 +619,9 @@ public void setLangManager(ILangManager langManager) {
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
if (ctx != null) {
this.setLangManager(ctx.getBean(ILangManager.class));
ILangManager langManager = ApplicationContextProvider.resolveBean(ILangManager.class);
if (langManager != null) {
this.setLangManager(langManager);
} else {
_logger.warn("Null WebApplicationContext during deserialization of Attribute '{}'", this.getName());
}
Expand Down
Loading
Loading