Search in sources :

Example 61 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class WikiTemplateMigration method hibernateMigrate.

@Override
protected void hibernateMigrate() throws DataMigrationException, XWikiException {
    // XWiki objects
    XWikiContext context = getXWikiContext();
    XWiki xwiki = context.getWiki();
    // WikiManager.WikiTemplateClass reference
    DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, WikiTemplateClassDocumentInitializer.DOCUMENT_NAME);
    // XWiki.XWikiServerClass reference
    DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), XWiki.SYSTEM_SPACE, "XWikiServerClass");
    // Superadmin reference
    DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, "superadmin");
    try {
        // Get all the descriptor documents
        String statement = "select distinct doc.fullName " + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj";
        Query query = queryManager.createQuery(statement, Query.XWQL);
        List<String> results = query.execute();
        for (String wikiPage : results) {
            XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context);
            // Get the "iswikitemplate" value
            BaseObject descriptorObject = document.getXObject(descriptorClassReference);
            int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0);
            // We remove the deprecated property from the descriptor
            descriptorObject.removeField(OLD_TEMPLATE_PROPERTY);
            // Add the new WikiManager.WikiTemplateClass object
            BaseObject object = document.getXObject(templateClassReference, true, context);
            // The new object might already exists and have a template property already set
            isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
            // Set the (new) value
            object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
            // The document must have an author
            document.setAuthorReference(superAdmin);
            // Save the document
            xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context);
        }
    } catch (QueryException e) {
        throw new DataMigrationException("Failed to get the list of all existing descriptors.", e);
    } catch (XWikiException e) {
        throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DataMigrationException(com.xpn.xwiki.store.migration.DataMigrationException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 62 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class WysiwygEditorScriptService method render.

/**
 * Produces the input for the editor by rendering the specified content template as a full HTML page, making sure
 * the skin extension hooks are resolved. The template is rendered in the context of the current document and the
 * Velocity context is not isolated so you can put the data needed by the template in the Velocity context before
 * calling this method. The advantage of using this method to obtain the editor input is that the editor doesn't
 * have to make an additional HTTP request for the content template.
 *
 * @param templateReference specifies the document that serves as the template for the editor content
 * @return the result of rendering the specified content template
 */
public String render(DocumentReference templateReference) {
    if (!this.authorization.hasAccess(Right.VIEW, templateReference)) {
        return null;
    }
    XWikiContext xcontext = this.xcontextProvider.get();
    try {
        XWikiDocument template = xcontext.getWiki().getDocument(templateReference, xcontext);
        String templateSyntax = template.getSyntax().toIdString();
        String output = xcontext.getDoc().getRenderedContent(template.getContent(), templateSyntax, xcontext);
        // Make sure the skin extension hooks are properly replaced with style sheets includes.
        return xcontext.getWiki().getPluginManager().endParsing(output, xcontext);
    } catch (XWikiException e) {
        this.logger.debug("Failed to render [{}].", templateReference, e);
        return null;
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException)

Example 63 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class DefaultWikiUserConfigurationHelper method saveConfiguration.

@Override
public void saveConfiguration(WikiUserConfiguration configuration, String wikiId) throws WikiUserManagerException {
    XWikiContext context = xcontextProvider.get();
    // Get the document
    XWikiDocument document = getDocument(wikiId);
    // Fill the object
    BaseObject object = document.getXObject(WikiUserClassDocumentInitializer.CONFIGURATION_CLASS, true, context);
    object.setStringValue(WikiUserClassDocumentInitializer.FIELD_USERSCOPE, configuration.getUserScope().name().toLowerCase());
    if (configuration.getMembershipType() != null) {
        object.setStringValue(WikiUserClassDocumentInitializer.FIELD_MEMBERSHIPTYPE, configuration.getMembershipType().name().toLowerCase());
    }
    // Save the document
    try {
        XWiki xwiki = context.getWiki();
        document.setHidden(true);
        // The document must have a creator
        if (document.getCreatorReference() == null) {
            document.setCreatorReference(context.getUserReference());
        }
        // The document must have an author
        if (document.getAuthorReference() == null) {
            document.setAuthorReference(context.getUserReference());
        }
        xwiki.saveDocument(document, "Changed configuration.", context);
    } catch (XWikiException e) {
        throw new WikiUserManagerException(String.format("Fail to save the confguration document for wiki [%s].", wikiId), e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 64 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class DefaultWikiUserConfigurationHelper method getDocument.

private XWikiDocument getDocument(String wikiId) throws WikiUserManagerException {
    try {
        XWikiContext context = xcontextProvider.get();
        XWiki xwiki = context.getWiki();
        DocumentReference reference = new DocumentReference(wikiId, CONFIGURATION_SPACE_NAME, CONFIGURATION_PAGE_NAME);
        return xwiki.getDocument(reference, context);
    } catch (XWikiException e) {
        throw new WikiUserManagerException(String.format("Fail to get the configuration document for wiki [%s].", wikiId));
    }
}
Also used : WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 65 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class XWikiContextInitializationFilter method initializeXWikiContext.

/**
 * Initializes the XWiki context.
 *
 * @param request the request being processed
 * @param response the response
 * @throws ServletException if the initialization fails
 */
protected void initializeXWikiContext(ServletRequest request, ServletResponse response) throws ServletException {
    try {
        // Not all request types specify an action (e.g. GWT-RPC) so we default to the empty string.
        String action = "";
        XWikiServletContext xwikiEngine = new XWikiServletContext(this.filterConfig.getServletContext());
        XWikiServletRequest xwikiRequest = new XWikiServletRequest((HttpServletRequest) request);
        XWikiServletResponse xwikiResponse = new XWikiServletResponse((HttpServletResponse) response);
        // Create the XWiki context.
        XWikiContext context = Utils.prepareContext(action, xwikiRequest, xwikiResponse, xwikiEngine);
        // parameter is specified.
        if (this.mode >= 0) {
            context.setMode(this.mode);
        }
        // Initialize the Container component which is the new way of transporting the Context in the new component
        // architecture. Further initialization might require the Container component.
        initializeContainerComponent(context);
        // Initialize the XWiki database. XWiki#getXWiki(XWikiContext) calls XWikiContext.setWiki(XWiki).
        XWiki xwiki = XWiki.getXWiki(context);
        // Initialize the URL factory.
        context.setURLFactory(xwiki.getURLFactoryService().createURLFactory(context.getMode(), context));
        // Prepare the localized resources, according to the selected language.
        xwiki.prepareResources(context);
        // Initialize the current user.
        XWikiUser user = context.getWiki().checkAuth(context);
        if (user != null) {
            DocumentReferenceResolver<String> documentReferenceResolver = Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "explicit");
            SpaceReference defaultUserSpace = new SpaceReference(XWiki.SYSTEM_SPACE, new WikiReference(context.getWikiId()));
            DocumentReference userReference = documentReferenceResolver.resolve(user.getUser(), defaultUserSpace);
            context.setUserReference(XWikiRightService.GUEST_USER.equals(userReference.getName()) ? null : userReference);
        }
    } catch (XWikiException e) {
        throw new ServletException("Failed to initialize the XWiki context.", e);
    }
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiServletResponse(com.xpn.xwiki.web.XWikiServletResponse) SpaceReference(org.xwiki.model.reference.SpaceReference) XWikiServletContext(com.xpn.xwiki.web.XWikiServletContext) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ServletException(javax.servlet.ServletException) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)442 XWikiContext (com.xpn.xwiki.XWikiContext)156 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)147 DocumentReference (org.xwiki.model.reference.DocumentReference)98 BaseObject (com.xpn.xwiki.objects.BaseObject)88 IOException (java.io.IOException)57 QueryException (org.xwiki.query.QueryException)57 ArrayList (java.util.ArrayList)56 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)51 XWiki (com.xpn.xwiki.XWiki)48 XWikiRestException (org.xwiki.rest.XWikiRestException)44 Session (org.hibernate.Session)42 Document (com.xpn.xwiki.api.Document)38 InitializationException (org.xwiki.component.phase.InitializationException)36 WebApplicationException (javax.ws.rs.WebApplicationException)32 SQLException (java.sql.SQLException)31 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)30 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)29 UnexpectedException (org.xwiki.store.UnexpectedException)29 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)25