Skip to content

Commit

Permalink
XWIKI-22681: Provide a relative ResourceReferenceEntityReferenceResolver
Browse files Browse the repository at this point in the history
  * Fix remaining tests
  • Loading branch information
surli committed Dec 6, 2024
1 parent 08683de commit f8e553d
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*
* @version $Id$
* @since 17.0.0RC1
* @since 16.10.1
*/
public abstract class AbstractRelativeResourceReferenceEntityReferenceResolver
extends AbstractResourceReferenceEntityReferenceResolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*
* @version $Id$
* @since 17.0.0RC1
* @since 16.10.1
*/
@Component
@Named("relative/attach")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package org.xwiki.rendering.internal.resolver;

import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
Expand All @@ -37,6 +39,7 @@
*
* @version $Id$
* @since 17.0.0RC1
* @since 16.10.1
*/
@Component
@Named("relative/doc")
Expand All @@ -47,6 +50,9 @@ public class RelativeDocumentResourceReferenceEntityReferenceResolver
@Inject
private DocumentReferenceResolver<String> defaultStringDocumentReferenceResolver;

@Inject
private DocumentReferenceResolver<EntityReference> documentReferenceResolver;

/**
* Default constructor.
*/
Expand Down Expand Up @@ -74,17 +80,40 @@ protected EntityReference resolveUntyped(ResourceReference resourceReference, En
this.relativeReferenceResolver.resolve(resourceReference.getReference(), EntityType.DOCUMENT,
baseReference);

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

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

private EntityReference setDefaultDocument(EntityReference relativeReference)
{
String defaultDocumentName =
this.defaultReferenceProvider.getDefaultReference(EntityType.DOCUMENT).getName();

EntityReference result;
if (!defaultDocumentName.equals(relativeReference.getName())) {
EntityReference parentSpace = new EntityReference(relativeReference.getName(), EntityType.SPACE,
relativeReference.getParent());
result = new EntityReference(defaultDocumentName, EntityType.DOCUMENT, parentSpace);
} else {
result = relativeReference;
}

EntityReference oldSpaceReference = result.extractFirstReference(EntityType.SPACE);
if (oldSpaceReference != null) {
EntityReference newSpaceReference = new EntityReference(oldSpaceReference,
Map.of(EntityReference.PARENT_TYPE_PARAMETER, EntityType.SPACE));
result = result.replaceParent(oldSpaceReference, newSpaceReference);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
* Convert attachment resource reference into entity reference.
*
* @version $Id$
* @since 13.10.5
* @since 17.0.0RC1
* @since 16.10.1
*/
@Component
@Named("relative/pageAttach")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @version $Id$
* @since 17.0.0RC1
* @since 16.10.1
*/
@Component
@Named("relative/page")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
*
* @version $Id$
* @since 17.0.0RC1
* @since 16.10.1
*/
@Component
@Named("relative")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @version $Id$
* @since 17.0.0RC1
* @since 16.10.1
*/
@Component
@Named("relative/space")
Expand Down
Loading

0 comments on commit f8e553d

Please sign in to comment.