Search in sources :

Example 41 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class CreatePageNestedDocumentsTest method assertCreatedNestedDocument.

private void assertCreatedNestedDocument(DocumentReference pageReference, ViewPage viewPage) {
    SpaceReference spaceReference = pageReference.getLastSpaceReference();
    BreadcrumbElement breadcrumb = viewPage.getBreadcrumb();
    if (breadcrumb.canBeExpanded()) {
        breadcrumb.expand();
    }
    assertEquals("/" + getUtil().getURLFragment(spaceReference), breadcrumb.getPathAsString());
    assertEquals(spaceReference.getName(), viewPage.getDocumentTitle());
    assertEquals(getUtil().serializeReference(pageReference), viewPage.getMetaDataValue("reference"));
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) BreadcrumbElement(org.xwiki.test.ui.po.BreadcrumbElement)

Example 42 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class XWikiDocument method setSpace.

/**
 * Set the full local space reference.
 * <p>
 * Note that this method cannot be removed for now since it's used by Hibernate for loading a XWikiDocument.
 *
 * @see #getSpace()
 * @deprecated since 2.2M1 used {@link #setDocumentReference(DocumentReference)} instead
 */
@Deprecated
public void setSpace(String spaces) {
    if (spaces != null) {
        DocumentReference reference = getDocumentReference();
        EntityReference spaceReference = getRelativeEntityReferenceResolver().resolve(spaces, EntityType.SPACE);
        spaceReference = spaceReference.appendParent(getDocumentReference().getWikiReference());
        setDocumentReferenceInternal(new DocumentReference(reference.getName(), new SpaceReference(spaceReference)));
    }
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

Example 43 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class DefaultXWikiContextInitializer method authenticate.

private void authenticate(XWikiContext xcontext) throws XWikiException {
    // By default set guest as the user that is sending the request.
    xcontext.setUserReference(null);
    XWikiUser xwikiUser = xcontext.getWiki().checkAuth(xcontext);
    if (xwikiUser != null) {
        SpaceReference defaultUserSpace = new SpaceReference(XWiki.SYSTEM_SPACE, new WikiReference(xcontext.getWikiId()));
        DocumentReference userReference = this.explicitResolver.resolve(xwikiUser.getUser(), defaultUserSpace);
        xcontext.setUserReference(XWikiRightService.GUEST_USER.equals(userReference.getName()) ? null : userReference);
        this.logger.debug("Authenticated as [{}].", xwikiUser.getUser());
    }
}
Also used : XWikiUser(com.xpn.xwiki.user.api.XWikiUser) SpaceReference(org.xwiki.model.reference.SpaceReference) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 44 with SpaceReference

use of org.xwiki.model.reference.SpaceReference 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 45 with SpaceReference

use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.

the class XWikiAction method redirectSpaceURLs.

/**
 * In order to let users enter URLs to Spaces we do the following when receiving {@code /A/B} (where A and B are
 * spaces):
 * <ul>
 * <li>check that the action is "view" (we only support this for the view action since otherwise this would break
 * apps written before this concept was introduced in XWiki 7.2M1)</li>
 * <li>if A.B exists then continue</li>
 * <li>if A.B doesn't exist then forward to A.B.WebHome</li>
 * </ul>
 * In order to disable this redirect you should provide the {@code spaceRedirect=false} Query String parameter and
 * value.
 *
 * @since 7.2M1
 */
private boolean redirectSpaceURLs(String action, XWikiURLFactory urlf, XWiki xwiki, XWikiContext context) throws Exception {
    if ("view".equals(action) && !"false".equalsIgnoreCase(context.getRequest().getParameter("spaceRedirect"))) {
        DocumentReference reference = xwiki.getDocumentReference(context.getRequest(), context);
        if (!xwiki.exists(reference, context)) {
            String defaultDocumentName = Utils.getComponent(EntityReferenceProvider.class).getDefaultReference(EntityType.DOCUMENT).getName();
            // Avoid an infinite loop by ensuring we're not on a WebHome already
            if (!reference.getName().equals(defaultDocumentName)) {
                // Consider the reference as a Space Reference and Construct a new reference to the home of that
                // Space. Then generate the URL for it and forward to it
                SpaceReference spaceReference = new SpaceReference(reference.getName(), reference.getParent());
                EntityReferenceSerializer<String> localSerializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "local");
                // Extract the anchor
                String anchor = new URL(context.getRequest().getRequestURL().toString()).getRef();
                URL forwardURL = urlf.createURL(localSerializer.serialize(spaceReference), defaultDocumentName, action, context.getRequest().getQueryString(), anchor, spaceReference.getWikiReference().getName(), context);
                // Since createURL() contain the webapp context and since RequestDispatcher should not contain it,
                // we need to remove it!
                String webappContext = xwiki.getWebAppPath(context);
                String relativeURL = urlf.getURL(forwardURL, context);
                relativeURL = '/' + StringUtils.substringAfter(relativeURL, webappContext);
                context.getRequest().getRequestDispatcher(relativeURL).forward(context.getRequest(), context.getResponse());
                return true;
            }
        }
    }
    return false;
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) URL(java.net.URL)

Aggregations

SpaceReference (org.xwiki.model.reference.SpaceReference)142 DocumentReference (org.xwiki.model.reference.DocumentReference)96 Test (org.junit.Test)83 WikiReference (org.xwiki.model.reference.WikiReference)58 EntityReference (org.xwiki.model.reference.EntityReference)24 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)21 ArrayList (java.util.ArrayList)11 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)11 XWikiException (com.xpn.xwiki.XWikiException)9 QueryRestrictionGroup (com.celements.search.lucene.query.QueryRestrictionGroup)8 Expectations (org.jmock.Expectations)8 XWikiContext (com.xpn.xwiki.XWikiContext)7 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)7 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)7 ComponentManager (org.xwiki.component.manager.ComponentManager)7 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)7 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)7 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)7