Search in sources :

Example 6 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference 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 EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class XWikiRequestProcessor method processPath.

@Override
protected String processPath(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
    String url = httpServletRequest.getRequestURL().toString();
    try {
        ExtendedURL extendedURL = new ExtendedURL(new URL(url), httpServletRequest.getContextPath());
        ResourceType type = this.typeResolver.resolve(extendedURL, Collections.<String, Object>emptyMap());
        EntityResourceReference entityResourceReference = (EntityResourceReference) this.resolver.resolve(extendedURL, type, Collections.<String, Object>emptyMap());
        return "/" + entityResourceReference.getAction().getActionName() + "/";
    } catch (Exception e) {
        throw new IOException(String.format("Failed to extract the Entity Action from URL [%s]", url), e);
    }
}
Also used : EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceType(org.xwiki.resource.ResourceType) IOException(java.io.IOException) ExtendedURL(org.xwiki.url.ExtendedURL) ExtendedURL(org.xwiki.url.ExtendedURL) URL(java.net.URL) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 8 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class IntegrationTest method extractResourceReference.

@Test
public void extractResourceReference() throws Exception {
    // Entity Resource References
    assertURL("http://localhost:8080/xwiki/bin/view/space/page", EntityResourceReference.TYPE, new EntityResourceReference(new DocumentReference("xwiki", "space", "page"), EntityResourceAction.VIEW));
    assertURL("http://localhost:8080/xwiki/wiki/mywiki/view/space/page", new ResourceType("wiki"), new EntityResourceReference(new DocumentReference("mywiki", "space", "page"), EntityResourceAction.VIEW));
    // Resources Resource References
    assertURL("http://localhost:8080/xwiki/resources/js/prototype/prototype.js", ResourcesResourceReference.TYPE, new ResourcesResourceReference());
    // Skins Resource References
    assertURL("http://localhost:8080/xwiki/skins/flamingo/logo.png", SkinsResourceReference.TYPE, new SkinsResourceReference());
}
Also used : ResourcesResourceReference(org.xwiki.resource.resources.ResourcesResourceReference) SkinsResourceReference(org.xwiki.resource.skins.SkinsResourceReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ResourceType(org.xwiki.resource.ResourceType) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 9 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class BinEntityResourceReferenceResolverTest 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("bin"), 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 10 with EntityResourceReference

use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.

the class WikiEntityResourceReferenceResolverTest method resolve.

@Test
public void resolve() throws Exception {
    ExtendedURL url = new ExtendedURL(new URL("http://localhost/wiki/somewiki/view/space/page"), null);
    url.getSegments().remove(0);
    when(wikiReferenceExtractor.extract(url)).thenReturn(this.wikiReference);
    EntityReference reference = buildEntityReference("somewiki", "space", "page");
    EntityResourceReference result = (EntityResourceReference) testCreateResource("http://localhost/wiki/somewiki/view/space/page", "view", reference, reference, EntityType.DOCUMENT);
    assertEquals(new DocumentReference("somewiki", "space", "page"), result.getEntityReference());
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ExtendedURL(org.xwiki.url.ExtendedURL) ExtendedURL(org.xwiki.url.ExtendedURL) URL(java.net.URL) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

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