use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.
the class DownloadAction method getFileName.
/**
* @return the filename of the attachment or null if the URL didn't point to an attachment
*/
private String getFileName() {
// Extract the Attachment file name from the parsed request URL that was done before this Action is called
ResourceReference resourceReference = Utils.getComponent(ResourceReferenceManager.class).getResourceReference();
EntityResourceReference entityResource = (EntityResourceReference) resourceReference;
// Try to extract the attachment from the reference but it's possible that the URL didn't point to an
// attachment, in which case we return null.
EntityReference attachmentReference = entityResource.getEntityReference().extractReference(EntityType.ATTACHMENT);
return attachmentReference == null ? null : attachmentReference.getName();
}
use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.
the class DownloadRevAction method getFileName.
/**
* @return the filename of the attachment.
*/
private String getFileName() {
// Extract the Attachment file name from the parsed request URL that was done before this Action is called
ResourceReference resourceReference = Utils.getComponent(ResourceReferenceManager.class).getResourceReference();
EntityResourceReference entityResource = (EntityResourceReference) resourceReference;
return entityResource.getEntityReference().extractReference(EntityType.ATTACHMENT).getName();
}
use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.
the class WikiEntityResourceReferenceResolverTest method testCreateResource.
private ResourceReference testCreateResource(String testURL, String expectedActionName, EntityReference expectedReference, EntityReference returnedReference, EntityType expectedEntityType) throws Exception {
when(this.entityReferenceResolver.resolve(expectedReference, expectedEntityType)).thenReturn(returnedReference);
ExtendedURL extendedURL = new ExtendedURL(new URL(testURL), null);
// Remove the resource type segment since this is what gets passed to specific Reference Resolvers.
extendedURL.getSegments().remove(0);
EntityResourceReference entityResource = this.resolver.resolve(extendedURL, new ResourceType("wiki"), Collections.<String, Object>emptyMap());
assertEquals(expectedActionName, entityResource.getAction().getActionName());
assertEquals(returnedReference, entityResource.getEntityReference());
return entityResource;
}
use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.
the class EntityResourceReferenceResolver method resolve.
@Override
public EntityResourceReference resolve(ExtendedURL extendedURL, ResourceType resourceType, Map<String, Object> parameters) throws CreateResourceReferenceException, UnsupportedResourceReferenceException {
EntityResourceReference entityURL;
// UC1: <action>/<entity reference type>/<entity reference>
// UC2: <action>/<entity reference> ==> type = page
// UC3: <entity reference> ==> type = page, action = view
List<String> pathSegments = extendedURL.getSegments();
String action = ACTION_VIEW;
EntityType entityType;
String entityReferenceAsString;
if (pathSegments.size() == 3) {
action = pathSegments.get(0);
entityType = computeEntityType(pathSegments.get(1));
entityReferenceAsString = pathSegments.get(2);
} else if (pathSegments.size() == 2) {
action = pathSegments.get(0);
entityType = computeDefaultEntityType(action);
entityReferenceAsString = pathSegments.get(1);
} else if (pathSegments.size() == 1) {
entityType = computeDefaultEntityType(action);
entityReferenceAsString = pathSegments.get(0);
} else {
throw new CreateResourceReferenceException(String.format("Invalid Entity URL [%s]", extendedURL.serialize()));
}
// Convert the string representation of the Entity reference into a proper EntityResourceReference.
entityURL = new EntityResourceReference(this.defaultEntityReferenceResolver.resolve(entityReferenceAsString, entityType), EntityResourceAction.fromString(action));
return entityURL;
}
use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.
the class AbstractEntityResourceReferenceResolver method resolve.
@Override
public EntityResourceReference resolve(ExtendedURL extendedURL, ResourceType type, Map<String, Object> parameters) throws CreateResourceReferenceException, UnsupportedResourceReferenceException {
EntityResourceReference reference = null;
// Extract the wiki reference from the URL
WikiReference wikiReference = extractWikiReference(extendedURL);
// See BinEntityResourceReferenceResolverTest to check the various cases supported.
List<String> pathSegments = extendedURL.getSegments();
List<String> spaceNames = null;
String pageName = null;
String attachmentName = null;
String action = VIEW_ACTION;
if (pathSegments.size() != 0) {
String firstSegment = pathSegments.get(0);
action = firstSegment;
// Handle actions specifying an attachment.
if (FILE_ACTION_LIST.contains(firstSegment) && pathSegments.size() >= 4) {
// Last segment is the attachment
attachmentName = pathSegments.get(pathSegments.size() - 1);
// Last but one segment is the page name
pageName = pathSegments.get(pathSegments.size() - 2);
// All segments in between are the space names
spaceNames = extractSpaceNames(pathSegments, 1, pathSegments.size() - 3);
} else {
// Handle actions not specifying any attachment.
Pair<String, Integer> actionAndStartPosition = computeActionAndStartPosition(firstSegment, pathSegments);
action = actionAndStartPosition.getLeft();
int startPosition = actionAndStartPosition.getRight();
// need to use /view/something/ which is not natural in the Nested Document mode.
if (pathSegments.size() - startPosition == 1) {
spaceNames = Arrays.asList(pathSegments.get(startPosition));
} else {
// Last segment is the page name
pageName = pathSegments.get(pathSegments.size() - 1);
// All segments in between are the space names
spaceNames = extractSpaceNames(pathSegments, startPosition, pathSegments.size() - 2);
}
}
}
if (reference == null) {
reference = new EntityResourceReference(buildEntityReference(wikiReference, spaceNames, pageName, attachmentName), EntityResourceAction.fromString(action));
}
copyParameters(extendedURL, reference);
return reference;
}
Aggregations