Search in sources :

Example 6 with ResourceReference

use of org.xwiki.resource.ResourceReference in project xwiki-platform by xwiki.

the class XWikiAction method handleRedirectObject.

/**
 * Redirect the user to an other location if the document holds an XWiki.RedirectClass instance (used when a
 * document is moved).
 *
 * @param context the XWiki context
 * @return either or not a redirection have been sent
 * @throws XWikiException if error occurs
 * @since 8.0RC1
 * @since 7.4.2
 */
protected boolean handleRedirectObject(XWikiContext context) throws XWikiException {
    WikiReference wikiReference = context.getWikiReference();
    // Look if the document has a redirect object
    XWikiDocument doc = context.getDoc();
    BaseObject redirectObj = doc.getXObject(new DocumentReference("RedirectClass", new SpaceReference("XWiki", wikiReference)));
    if (redirectObj == null) {
        return false;
    }
    // Get the location
    String location = redirectObj.getStringValue("location");
    if (StringUtils.isBlank(location)) {
        return false;
    }
    // Resolve the location to get a reference
    DocumentReferenceResolver<String> resolver = Utils.getComponent(DocumentReferenceResolver.TYPE_STRING);
    EntityReference locationReference = resolver.resolve(location, wikiReference);
    // Get the type of the current target
    ResourceReference resourceReference = Utils.getComponent(ResourceReferenceManager.class).getResourceReference();
    EntityResourceReference entityResource = (EntityResourceReference) resourceReference;
    EntityReference entityReference = entityResource.getEntityReference();
    // If the entity is inside a document, compute the new entity with the new document part.
    if (entityReference.getType().ordinal() > EntityType.DOCUMENT.ordinal()) {
        EntityReference parentDocument = entityReference.extractReference(EntityType.DOCUMENT);
        locationReference = entityReference.replaceParent(parentDocument, locationReference);
    }
    // Get the URL corresponding to the location
    // Note: the anchor part is lost in the process, because it is not sent to the server
    // (see: http://stackoverflow.com/a/4276491)
    String url = context.getWiki().getURL(locationReference, context.getAction(), context.getRequest().getQueryString(), null, context);
    // Send the redirection
    try {
        context.getResponse().sendRedirect(url);
    } catch (IOException e) {
        throw new XWikiException("Failed to redirect.", e);
    }
    return true;
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) IOException(java.io.IOException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) ResourceReference(org.xwiki.resource.ResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 7 with ResourceReference

use of org.xwiki.resource.ResourceReference in project xwiki-platform by xwiki.

the class IntegrationTest method assertURL.

private void assertURL(String url, ResourceType expectedType, ResourceReference expectedReference) throws Exception {
    ExtendedURL extendedURL = new ExtendedURL(new URL(url), "xwiki");
    ResourceType resourceType = this.resourceTypeResolver.resolve(extendedURL, Collections.<String, Object>emptyMap());
    assertEquals(expectedType.getId(), resourceType.getId());
    ResourceReference reference = this.resourceReferenceResolver.resolve(extendedURL, resourceType, Collections.<String, Object>emptyMap());
    assertEquals(expectedReference, reference);
}
Also used : ResourceType(org.xwiki.resource.ResourceType) ResourcesResourceReference(org.xwiki.resource.resources.ResourcesResourceReference) SkinsResourceReference(org.xwiki.resource.skins.SkinsResourceReference) ResourceReference(org.xwiki.resource.ResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ExtendedURL(org.xwiki.url.ExtendedURL) ExtendedURL(org.xwiki.url.ExtendedURL) URL(java.net.URL)

Example 8 with ResourceReference

use of org.xwiki.resource.ResourceReference in project xwiki-platform by xwiki.

the class BinEntityResourceReferenceResolverTest method createResourceWhenURLHasParameters.

@Test
public void createResourceWhenURLHasParameters() throws Exception {
    EntityReference fullReference = buildEntityReference("wiki", Arrays.asList("space"), "page");
    ResourceReference resource = testCreateResource("http://localhost/bin/view/space/page?param1=value1&param2=value2", "view", fullReference, fullReference, EntityType.DOCUMENT);
    // Assert parameters
    // Note: the parameters order are the same as the order specified in the URL.
    Map<String, List<String>> expectedMap = new LinkedHashMap<String, List<String>>();
    expectedMap.put("param1", Arrays.asList("value1"));
    expectedMap.put("param2", Arrays.asList("value2"));
    assertEquals(expectedMap, resource.getParameters());
    // Also verify it works when there's a param with no value.
    resource = testCreateResource("http://localhost/bin/view/space/page?param", "view", fullReference, fullReference, EntityType.DOCUMENT);
    expectedMap = new LinkedHashMap<>();
    expectedMap.put("param", Collections.<String>emptyList());
    assertEquals(expectedMap, resource.getParameters());
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) List(java.util.List) ResourceReference(org.xwiki.resource.ResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 9 with ResourceReference

use of org.xwiki.resource.ResourceReference 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 10 with ResourceReference

use of org.xwiki.resource.ResourceReference 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)

Aggregations

ResourceReference (org.xwiki.resource.ResourceReference)14 EntityResourceReference (org.xwiki.resource.entity.EntityResourceReference)11 ResourceReferenceManager (org.xwiki.resource.ResourceReferenceManager)6 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)4 ResourceType (org.xwiki.resource.ResourceType)4 XWikiException (com.xpn.xwiki.XWikiException)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 EntityReference (org.xwiki.model.reference.EntityReference)3 ExtendedURL (org.xwiki.url.ExtendedURL)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 URL (java.net.URL)2 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 ExecutionContext (org.xwiki.context.ExecutionContext)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWiki (com.xpn.xwiki.XWiki)1 MonitorPlugin (com.xpn.xwiki.monitor.api.MonitorPlugin)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 FileUploadPlugin (com.xpn.xwiki.plugin.fileupload.FileUploadPlugin)1 FileNotFoundException (java.io.FileNotFoundException)1