Search in sources :

Example 61 with WikiReference

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

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

the class XWikiAttachment method loadAttachmentContent.

public void loadAttachmentContent(XWikiContext xcontext) throws XWikiException {
    if (this.content == null) {
        WikiReference currentWiki = xcontext.getWikiReference();
        try {
            // Make sure we work on the attachment's wiki
            WikiReference attachmentWiki = getReference().getDocumentReference().getWikiReference();
            if (attachmentWiki != null) {
                xcontext.setWikiReference(attachmentWiki);
            }
            try {
                XWikiAttachmentStoreInterface store = getAttachmentContentStore(xcontext);
                store.loadAttachmentContent(this, xcontext, true);
            } catch (ComponentLookupException e) {
                throw new XWikiException("Failed to find store for attachment [" + getReference() + "]", e);
            }
        } finally {
            if (currentWiki != null) {
                xcontext.setWikiReference(currentWiki);
            }
        }
    }
}
Also used : XWikiAttachmentStoreInterface(com.xpn.xwiki.store.XWikiAttachmentStoreInterface) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiReference(org.xwiki.model.reference.WikiReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 63 with WikiReference

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

the class AbstractMandatoryDocumentInitializer method initialize.

@Override
public void initialize() throws InitializationException {
    // If a local reference was specified but isMainWikiOnly() is true, then convert to a main wiki reference.
    if (this.reference != null && this.reference.extractReference(EntityType.WIKI) == null && isMainWikiOnly()) {
        synchronized (this) {
            if (this.reference.extractReference(EntityType.WIKI) == null) {
                // Convert to main wiki reference
                EntityReference mainWikiEntityReference = this.resolver.resolve(this.reference, new WikiReference(this.wikiDescriptorManager.getMainWikiId()));
                this.reference = mainWikiEntityReference;
            }
        }
    }
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) WikiReference(org.xwiki.model.reference.WikiReference)

Example 64 with WikiReference

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

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

the class XWikiTest method testGetDocumentWithEntityReference.

public void testGetDocumentWithEntityReference() throws Exception {
    Mock mockStore = registerMockComponent(XWikiStoreInterface.class);
    this.xwiki.setStore((XWikiStoreInterface) mockStore.proxy());
    mockStore.expects(atLeastOnce()).method("loadXWikiDoc").with(NOT_NULL, same(getContext())).will(new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            return invocation.parameterValues.get(0);
        }
    });
    DocumentReference documentReference = new DocumentReference("wiki", "Main", "WebHome");
    WikiDescriptor mockWikiDescriptor = new WikiDescriptor("wiki", "wiki");
    mockWikiDescriptor.setMainPageReference(documentReference);
    this.mockWikiDescriptorManager.stubs().method("getById").with(same("wiki")).will(returnValue(mockWikiDescriptor));
    assertEquals(documentReference, this.xwiki.getDocument(new WikiReference("wiki"), getContext()).getDocumentReference());
    assertEquals(documentReference, this.xwiki.getDocument(new ObjectReference("object", documentReference), getContext()).getDocumentReference());
}
Also used : Invocation(org.jmock.core.Invocation) CustomStub(org.jmock.core.stub.CustomStub) ObjectReference(org.xwiki.model.reference.ObjectReference) BaseObject(com.xpn.xwiki.objects.BaseObject) WikiReference(org.xwiki.model.reference.WikiReference) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor)

Aggregations

WikiReference (org.xwiki.model.reference.WikiReference)220 DocumentReference (org.xwiki.model.reference.DocumentReference)127 Test (org.junit.Test)106 SpaceReference (org.xwiki.model.reference.SpaceReference)58 XWikiContext (com.xpn.xwiki.XWikiContext)33 XWikiException (com.xpn.xwiki.XWikiException)24 EntityReference (org.xwiki.model.reference.EntityReference)24 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)23 ArrayList (java.util.ArrayList)19 AccessDeniedException (org.xwiki.security.authorization.AccessDeniedException)18 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)15 WikiDescriptor (org.xwiki.wiki.descriptor.WikiDescriptor)11 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)10 WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)10 XWiki (com.xpn.xwiki.XWiki)9 BaseObject (com.xpn.xwiki.objects.BaseObject)9 ComponentManager (org.xwiki.component.manager.ComponentManager)9 Expectations (org.jmock.Expectations)8 Before (org.junit.Before)8 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)8