Search in sources :

Example 36 with WikiManagerException

use of org.xwiki.wiki.manager.WikiManagerException in project xwiki-platform by xwiki.

the class DefaultWikiTemplateManager method isTemplate.

@Override
public boolean isTemplate(String wikiId) throws WikiTemplateManagerException {
    try {
        // Get the descriptor
        WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
        // Get the property group
        WikiTemplatePropertyGroup group = (WikiTemplatePropertyGroup) descriptor.getPropertyGroup(WikiTemplatePropertyGroupProvider.GROUP_NAME);
        // Return the value
        return group.isTemplate();
    } catch (WikiManagerException e) {
        throw new WikiTemplateManagerException(String.format(errorMessageNoDescriptor, wikiId), e);
    }
}
Also used : WikiTemplatePropertyGroup(org.xwiki.wiki.template.WikiTemplatePropertyGroup) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) WikiTemplateManagerException(org.xwiki.wiki.template.WikiTemplateManagerException)

Example 37 with WikiManagerException

use of org.xwiki.wiki.manager.WikiManagerException in project xwiki-platform by xwiki.

the class WikiTemplatePropertyGroupProvider method get.

@Override
public WikiPropertyGroup get(String wikiId) throws WikiPropertyGroupException {
    WikiTemplatePropertyGroup group = new WikiTemplatePropertyGroup(GROUP_NAME);
    try {
        XWikiDocument descriptorDocument = wikiDescriptorDocumentHelper.getDocumentFromWikiId(wikiId);
        // Get the object
        BaseObject object = descriptorDocument.getXObject(WikiTemplateClassDocumentInitializer.SERVER_CLASS);
        if (object != null) {
            group.setTemplate(object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, 0) != 0);
        }
    } catch (WikiManagerException e) {
        throw new WikiPropertyGroupException(String.format(ERROR_MESSAGE_NO_DESCRIPTOR_DOCUMENT, wikiId), e);
    }
    return group;
}
Also used : WikiTemplatePropertyGroup(org.xwiki.wiki.template.WikiTemplatePropertyGroup) WikiPropertyGroupException(org.xwiki.wiki.properties.WikiPropertyGroupException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 38 with WikiManagerException

use of org.xwiki.wiki.manager.WikiManagerException in project xwiki-platform by xwiki.

the class WikiTemplatePropertyGroupProviderTest method getWhenException.

@Test
public void getWhenException() throws Exception {
    WikiManagerException exception = new WikiManagerException("error in WikiManager");
    when(wikiDescriptorDocumentHelper.getDocumentFromWikiId("wikiId")).thenThrow(exception);
    // Test
    boolean exceptionCaught = false;
    try {
        mocker.getComponentUnderTest().get("wikiId");
    } catch (WikiPropertyGroupException e) {
        exceptionCaught = true;
        assertEquals("Unable to load descriptor document for wiki [wikiId].", e.getMessage());
        assertEquals(exception, e.getCause());
    }
    assertTrue(exceptionCaught);
}
Also used : WikiPropertyGroupException(org.xwiki.wiki.properties.WikiPropertyGroupException) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) Test(org.junit.Test)

Example 39 with WikiManagerException

use of org.xwiki.wiki.manager.WikiManagerException in project xwiki-platform by xwiki.

the class WikiTemplateManagerScript method setTemplate.

/**
 * Set if the specified wiki is a template or not.
 *
 * @param wikiId the ID of the wiki to specify
 * @param value whether or not the wiki is a template
 * @return true if the action succeed
 */
public boolean setTemplate(String wikiId, boolean value) {
    XWikiContext context = xcontextProvider.get();
    try {
        // Check if the current script has the programing rights
        authorizationManager.checkAccess(Right.PROGRAM, context.getDoc().getAuthorReference(), context.getDoc().getDocumentReference());
        // Get the descriptor
        WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
        // Get the wiki owner
        String owner = descriptor.getOwnerId();
        // Check right access
        WikiReference wikiReference = new WikiReference(descriptor.getId());
        String currentUser = entityReferenceSerializer.serialize(context.getUserReference());
        if (!currentUser.equals(owner)) {
            authorizationManager.checkAccess(Right.ADMIN, context.getUserReference(), wikiReference);
        }
        // Do the job
        wikiTemplateManager.setTemplate(wikiId, value);
        // Return success
        return true;
    } catch (WikiTemplateManagerException e) {
        error(String.format("Failed to set the template value [%s] for the wiki [%s].", value, wikiId), e);
        return false;
    } catch (AccessDeniedException e) {
        error(String.format("Access denied for [%s] to change the template value of the wiki [%s]. The user has" + " not the right to perform this operation or the script has not the programming right.", context.getUserReference(), wikiId), e);
        return false;
    } catch (WikiManagerException e) {
        error(String.format("Failed to get the descriptor of the wiki [%s].", wikiId), e);
        return false;
    }
}
Also used : AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) WikiReference(org.xwiki.model.reference.WikiReference) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) WikiTemplateManagerException(org.xwiki.wiki.template.WikiTemplateManagerException)

Example 40 with WikiManagerException

use of org.xwiki.wiki.manager.WikiManagerException in project xwiki-platform by xwiki.

the class XWiki method getXWiki.

/**
 * Return the XWiki object (as in "the Wiki API") corresponding to the requested wiki.
 * <p>
 * Unless <code>wait</code> is false the method return right away null if XWiki is not yet initialized.
 *
 * @param wait wait until XWiki is initialized
 * @param xcontext see {@link XWikiContext}
 * @return an XWiki object configured for the wiki corresponding to the current request
 * @throws XWikiException if the requested URL does not correspond to a real wiki, or if there's an error in the
 *             storage
 */
public static XWiki getXWiki(boolean wait, XWikiContext xcontext) throws XWikiException {
    XWiki xwiki = getMainXWiki(wait, xcontext);
    if (xwiki == null) {
        return null;
    }
    // Extract Entity Resource from URL and put it in the Execution Context
    EntityResourceReference entityResourceReference = initializeResourceFromURL(xcontext);
    // If not an entity resource reference assume main wiki
    if (entityResourceReference == null) {
        return xwiki;
    }
    // Get the wiki id
    String wikiId = entityResourceReference.getEntityReference().extractReference(EntityType.WIKI).getName();
    if (wikiId.equals(xcontext.getMainXWiki())) {
        // The main wiki was requested.
        return xwiki;
    }
    // Check if the wiki exists by checking if a descriptor exists for the wiki id.
    WikiDescriptorManager wikiDescriptorManager = Utils.getComponent(WikiDescriptorManager.class);
    WikiDescriptor descriptor;
    try {
        descriptor = wikiDescriptorManager.getById(wikiId);
    } catch (WikiManagerException e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI, XWikiException.ERROR_XWIKI_STORE_MISC, String.format("Failed find wiki descriptor for wiki id [%s]", wikiId), e);
    }
    if (descriptor == null) {
        throw new XWikiException(XWikiException.MODULE_XWIKI, XWikiException.ERROR_XWIKI_DOES_NOT_EXIST, String.format("The wiki [%s] does not exist", wikiId));
    }
    // Initialize wiki
    xcontext.setWikiId(wikiId);
    xcontext.setOriginalWikiId(wikiId);
    if (!xwiki.initializeWiki(wikiId, wait, xcontext)) {
        // The wiki is still initializing
        return null;
    }
    return xwiki;
}
Also used : WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor)

Aggregations

WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)40 WikiDescriptor (org.xwiki.wiki.descriptor.WikiDescriptor)17 Test (org.junit.Test)13 XWikiContext (com.xpn.xwiki.XWikiContext)12 AccessDeniedException (org.xwiki.security.authorization.AccessDeniedException)11 XWiki (com.xpn.xwiki.XWiki)7 XWikiException (com.xpn.xwiki.XWikiException)7 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)6 WikiTemplateManagerException (org.xwiki.wiki.template.WikiTemplateManagerException)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 WikiPropertyGroupException (org.xwiki.wiki.properties.WikiPropertyGroupException)5 BaseObject (com.xpn.xwiki.objects.BaseObject)4 WikiCreationException (org.xwiki.platform.wiki.creationjob.WikiCreationException)4 WikiTemplatePropertyGroup (org.xwiki.wiki.template.WikiTemplatePropertyGroup)4 ExtensionId (org.xwiki.extension.ExtensionId)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 WikiReference (org.xwiki.model.reference.WikiReference)3 Query (org.xwiki.query.Query)3 QueryException (org.xwiki.query.QueryException)3 DefaultWikiDescriptor (org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor)3