Skip to content

Commit

Permalink
XWIKI-22681: Provide a relative ResourceReferenceEntityReferenceResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
surli committed Nov 22, 2024
1 parent 82aa670 commit bb9b32c
Show file tree
Hide file tree
Showing 12 changed files with 1,101 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.jupiter.api.Test;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.WikiReference;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
Expand Down Expand Up @@ -70,5 +71,19 @@ public void resolveDocumentReferenceWithBaseReference()
assertNull(reference.extractReference(EntityType.WIKI));
assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
assertNull(reference.extractReference(EntityType.DOCUMENT));

reference =
this.resolver.resolve("", EntityType.DOCUMENT, new EntityReference("wikiFoo", EntityType.WIKI));

assertEquals("wikiFoo", reference.extractReference(EntityType.WIKI).getName());
assertNull(reference.extractReference(EntityType.SPACE));
assertNull(reference.extractReference(EntityType.DOCUMENT));

reference =
this.resolver.resolve("", EntityType.DOCUMENT, new WikiReference("wikiFoo"));

assertEquals("wikiFoo", reference.extractReference(EntityType.WIKI).getName());
assertNull(reference.extractReference(EntityType.SPACE));
assertNull(reference.extractReference(EntityType.DOCUMENT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Unit tests for {@link WikiReference}.
Expand All @@ -49,4 +50,11 @@ public void testInvalidParent()
() -> new WikiReference(new EntityReference("wiki", EntityType.WIKI, badParent)));
assertEquals("Unexpected parent [" + badParent + "] in a wiki reference", expected.getMessage());
}

@Test
void instanceOf()
{
assertTrue(new WikiReference("foo") instanceof WikiReference);
assertTrue(new WikiReference("foo") instanceof EntityReference);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.resolver;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
* Abstract class for all relative resource reference resolvers.
*
* @version $Id$
* @since 17.0.0RC1
*/
public abstract class AbstractRelativeResourceReferenceEntityReferenceResolver
extends AbstractResourceReferenceEntityReferenceResolver
{
@Inject
@Named("relative")
protected EntityReferenceResolver<String> relativeReferenceResolver;

/**
* @param type the resource type that this resolver will support
*/
protected AbstractRelativeResourceReferenceEntityReferenceResolver(ResourceType type)
{
super(type);
}

@Override
public EntityReference resolve(ResourceReference resourceReference, EntityType entityType, Object... parameters)
{
if (resourceReference == null) {
return null;
}

if (this.resourceType != null && !resourceReference.getType().equals(this.resourceType)) {
throw new IllegalArgumentException(
String.format("You must pass a resource reference of type [%s]. [%s] was passed", this.resourceType,
resourceReference));
}

EntityReference entityReference;
EntityReference baseReference = getBaseReference(resourceReference, parameters);

if (resourceReference.isTyped()) {
entityReference = resolveTyped(resourceReference, baseReference);
} else {
entityReference = resolveUntyped(resourceReference, baseReference);
}

return entityReference;
}

@Override
protected EntityReference getBaseReference(ResourceReference resourceReference, Object... parameters)
{
EntityReference baseReference =
(parameters.length > 0 && parameters[0] instanceof EntityReference entityReference)
? entityReference : null;

if (!resourceReference.getBaseReferences().isEmpty()) {
// If the passed reference has a base reference, resolve it first with a relative resolver (it should
// normally be absolute but who knows what the API caller has specified...)
baseReference = resolveBaseReference(resourceReference.getBaseReferences(), baseReference);
}

return baseReference;
}

@Override
protected EntityReference resolveBaseReference(List<String> baseReferences, EntityReference defaultBaseReference)
{
EntityReference resolvedBaseReference = defaultBaseReference;
for (String baseReference : baseReferences) {
resolvedBaseReference =
this.relativeReferenceResolver.resolve(baseReference, EntityType.DOCUMENT, resolvedBaseReference);
}

return resolvedBaseReference;
}

@Override
protected EntityReference resolveUntyped(ResourceReference resourceReference, EntityReference baseReference)
{
return resolveTyped(resourceReference, baseReference);
}

@Override
protected EntityReference resolveTyped(ResourceReference resourceReference, EntityReference baseReference)
{
return this.relativeReferenceResolver.resolve(resourceReference.getReference(), getEntityType(), baseReference);
}

/**
*
* @return the entity type of the {@link EntityReference} this resolver produces.
*/
protected abstract EntityType getEntityType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.resolver;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.AttachmentReference;
import org.xwiki.model.reference.AttachmentReferenceResolver;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
* Convert attachment resource reference into entity reference.
*
* @version $Id$
* @since 17.0.0RC1
*/
@Component
@Named("relative/attach")
@Singleton
public class RelativeAttachmentResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
@Inject
private AttachmentReferenceResolver<EntityReference> defaultReferenceAttachmentReferenceResolver;

/**
* Default constructor.
*/
public RelativeAttachmentResourceReferenceEntityReferenceResolver()
{
super(ResourceType.ATTACHMENT);
}

@Override
protected EntityType getEntityType()
{
return EntityType.ATTACHMENT;
}

@Override
protected EntityReference resolveTyped(ResourceReference resourceReference, EntityReference baseReference)
{
if (StringUtils.isEmpty(resourceReference.getReference())) {
return null;
}

// Get relative reference
EntityReference relativeReference =
this.relativeReferenceResolver
.resolve(resourceReference.getReference(), EntityType.ATTACHMENT, baseReference);

EntityReference result = relativeReference;
if (relativeReference.extractReference(EntityType.WIKI) != null) {
// Resolve full reference
AttachmentReference attachmentReference =
this.defaultReferenceAttachmentReferenceResolver.resolve(relativeReference, baseReference);

// See if the resolved (terminal or WebHome) document exists and, if so, use it.
DocumentReference documentReference = attachmentReference.getDocumentReference();

// Take care of fallback if needed
DocumentReference finalDocumentReference =
resolveDocumentReference(relativeReference.getParent(), documentReference, baseReference);
// Also use that resolution if the relative reference doesn't contain any document, even if it doesn't
// exists, to not produce incorrect references.
if (finalDocumentReference != documentReference
|| relativeReference.extractReference(EntityType.DOCUMENT) == null) {
result = new AttachmentReference(attachmentReference.getName(), finalDocumentReference);
}
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.resolver;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
* Convert document resource reference into entity reference.
*
* @version $Id$
* @since 17.0.0RC1
*/
@Component
@Named("relative/doc")
@Singleton
public class RelativeDocumentResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
@Inject
private DocumentReferenceResolver<String> defaultStringDocumentReferenceResolver;

/**
* Default constructor.
*/
public RelativeDocumentResourceReferenceEntityReferenceResolver()
{
super(ResourceType.DOCUMENT);
}

@Override
protected EntityType getEntityType()
{
return EntityType.DOCUMENT;
}

@Override
protected EntityReference resolveUntyped(ResourceReference resourceReference, EntityReference baseReference)
{
// If the reference is empty fallback on typed logic
if (StringUtils.isEmpty(resourceReference.getReference())) {
return resolveTyped(resourceReference, baseReference);
}

// Get relative reference
EntityReference relativeReference =
this.relativeReferenceResolver.resolve(resourceReference.getReference(), EntityType.DOCUMENT,
baseReference);

EntityReference result = relativeReference;
if (relativeReference.extractReference(EntityType.WIKI) != null) {
// Resolve the full document reference
// We don't start from the previously parsed relative reference to not loose "." prefixed reference meaning
DocumentReference reference =
this.defaultStringDocumentReferenceResolver.resolve(resourceReference.getReference(), baseReference);

// Take care of fallback if needed
result = resolveDocumentReference(relativeReference, reference, baseReference);
}

return result;
}
}
Loading

0 comments on commit bb9b32c

Please sign in to comment.