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);
}
}
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;
}
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);
}
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;
}
}
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;
}
Aggregations