-
-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XWIKI-22681: Provide a relative ResourceReferenceEntityReferenceResolver
- Loading branch information
Showing
12 changed files
with
1,101 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...rendering/internal/resolver/AbstractRelativeResourceReferenceEntityReferenceResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
99 changes: 99 additions & 0 deletions
99
...ndering/internal/resolver/RelativeAttachmentResourceReferenceEntityReferenceResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...rendering/internal/resolver/RelativeDocumentResourceReferenceEntityReferenceResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.