From a310e13d6ba77198ecbb4510701b2d20bc7f5497 Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Sat, 16 Dec 2023 15:53:48 +0100 Subject: [PATCH] fix: Fix Huge Memory consumption in WebDav Access - EXO-68314 (#308) Prior to this change, when accessing webdav and getting a property or check it, the jcr:data content is parsed as string and added in properties list. This change will make the property value lazily loaded only when really needed and not when building the Property object. (cherry picked from commit 6132294af960c3139085fd0abc047462c98b6d9e) --- .../jcr/webdav/resource/FileResource.java | 27 +------- .../webdav/resource/FileResourceProperty.java | 66 +++++++++++++++++++ 2 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/resource/FileResourceProperty.java diff --git a/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/resource/FileResource.java b/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/resource/FileResource.java index a11efeb1b4..30f7b1cf0d 100644 --- a/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/resource/FileResource.java +++ b/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/resource/FileResource.java @@ -322,30 +322,9 @@ else if (name.equals(OWNER)) /** * Returns node's property wrapped in {@link HierarchicalProperty}. */ - private HierarchicalProperty getProperty(Node node, QName name) throws PathNotFoundException, RepositoryException - { - Property property = node.getProperty(WebDavNamespaceContext.createName(name)); - String propertyValue; - if (property.getDefinition().isMultiple()) - { - if (property.getValues().length >= 1) - { - propertyValue = property.getValues()[0].getString(); - } - else - { - // this means that we return empty value, because according to WebDAV spec: - // this is a property whose semantics and syntax are not enforced by the server - // the server only records the value of a dead property; - // the client is responsible for maintaining the consistency of the syntax and semantics of a dead property. - propertyValue = ""; - } - } - else - { - propertyValue = property.getString(); - } - return new HierarchicalProperty(name, propertyValue); + private HierarchicalProperty getProperty(Node node, QName name) throws RepositoryException { + Property property = node.getProperty(WebDavNamespaceContext.createName(name)); + return new FileResourceProperty(name, property); } diff --git a/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/resource/FileResourceProperty.java b/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/resource/FileResourceProperty.java new file mode 100644 index 0000000000..c892a295d5 --- /dev/null +++ b/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/resource/FileResourceProperty.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2003-2023 eXo Platform SAS. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation; either version 3 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see. + */ +package org.exoplatform.services.jcr.webdav.resource; + +import javax.jcr.Property; +import javax.jcr.RepositoryException; +import javax.xml.namespace.QName; + +import org.exoplatform.common.util.HierarchicalProperty; +import org.exoplatform.services.log.ExoLogger; +import org.exoplatform.services.log.Log; + +public class FileResourceProperty extends HierarchicalProperty { + private static final Log LOG = ExoLogger.getLogger("exo.jcr.component.webdav.FileResourceProperty"); + + private Property property; + + public FileResourceProperty(QName name, Property property) { + super(name); + this.property = property; + } + + @Override + public String getValue() { + if (property != null && value == null) { + try { + if (property.getDefinition().isMultiple()) { + if (property.getValues().length >= 1) { + value = property.getValues()[0].getString(); + } else { + // this means that we return empty value, because according to + // WebDAV spec: this is a property whose semantics and syntax are + // not enforced + // by the server the server only records the value of a dead + // property. + // the client is responsible for maintaining the consistency of the + // syntax and semantics of a dead property. + value = ""; + } + } else { + value = property.getString(); + } + } catch (RepositoryException e) { + LOG.warn("Error getting property {} value. Returning the already retrieved value", name, e); + } finally { + property = null; + } + } + return value; + } + +}