Search in sources :

Example 11 with EntityResourceReference

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();
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceReference(org.xwiki.resource.ResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference)

Example 12 with EntityResourceReference

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();
}
Also used : ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceReference(org.xwiki.resource.ResourceReference)

Example 13 with EntityResourceReference

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;
}
Also used : EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceType(org.xwiki.resource.ResourceType) ExtendedURL(org.xwiki.url.ExtendedURL) ExtendedURL(org.xwiki.url.ExtendedURL) URL(java.net.URL)

Example 14 with EntityResourceReference

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;
}
Also used : EntityType(org.xwiki.model.EntityType) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) CreateResourceReferenceException(org.xwiki.resource.CreateResourceReferenceException)

Example 15 with EntityResourceReference

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;
}
Also used : EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) WikiReference(org.xwiki.model.reference.WikiReference)

Aggregations

EntityResourceReference (org.xwiki.resource.entity.EntityResourceReference)18 ResourceReference (org.xwiki.resource.ResourceReference)8 ResourceReferenceManager (org.xwiki.resource.ResourceReferenceManager)7 URL (java.net.URL)5 Test (org.junit.Test)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 ResourceType (org.xwiki.resource.ResourceType)5 ExtendedURL (org.xwiki.url.ExtendedURL)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 IOException (java.io.IOException)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 EntityReference (org.xwiki.model.reference.EntityReference)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 XWikiPluginManager (com.xpn.xwiki.plugin.XWikiPluginManager)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Date (java.util.Date)2 WikiReference (org.xwiki.model.reference.WikiReference)2 XWiki (com.xpn.xwiki.XWiki)1