Search in sources :

Example 56 with XWikiContext

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

the class ModelFactory method toRestPage.

public Page toRestPage(URI baseUri, URI self, Document doc, boolean useVersion, Boolean withPrettyNames, Boolean withObjects, Boolean withXClass, Boolean withAttachments) throws XWikiException {
    Page page = this.objectFactory.createPage();
    toRestPageSummary(page, baseUri, doc, useVersion, withPrettyNames);
    XWikiContext xwikiContext = this.xcontextProvider.get();
    page.setMajorVersion(doc.getRCSVersion().at(0));
    page.setMinorVersion(doc.getRCSVersion().at(1));
    page.setHidden(doc.isHidden());
    page.setLanguage(doc.getLocale().toString());
    page.setCreator(doc.getCreator());
    if (withPrettyNames) {
        page.setCreatorName(xwikiContext.getWiki().getUserName(doc.getCreator(), null, false, xwikiContext));
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(doc.getCreationDate());
    page.setCreated(calendar);
    page.setModifier(doc.getContentAuthor());
    if (withPrettyNames) {
        page.setModifierName(xwikiContext.getWiki().getUserName(doc.getContentAuthor(), null, false, xwikiContext));
    }
    calendar = Calendar.getInstance();
    calendar.setTime(doc.getContentUpdateDate());
    page.setModified(calendar);
    page.setComment(doc.getComment());
    page.setContent(doc.getContent());
    if (self != null) {
        Link pageLink = this.objectFactory.createLink();
        pageLink.setHref(self.toString());
        pageLink.setRel(Relations.SELF);
        page.getLinks().add(pageLink);
    }
    com.xpn.xwiki.api.Class xwikiClass = doc.getxWikiClass();
    if (xwikiClass != null) {
        String classUri = Utils.createURI(baseUri, ClassResource.class, doc.getWiki(), xwikiClass.getName()).toString();
        Link classLink = this.objectFactory.createLink();
        classLink.setHref(classUri);
        classLink.setRel(Relations.CLASS);
        page.getLinks().add(classLink);
    }
    XWikiContext xcontext = xcontextProvider.get();
    // Add attachments
    if (withAttachments) {
        page.setAttachments(objectFactory.createAttachments());
        for (com.xpn.xwiki.api.Attachment attachment : doc.getAttachmentList()) {
            URL url = xcontext.getURLFactory().createAttachmentURL(attachment.getFilename(), doc.getSpace(), doc.getName(), "download", null, doc.getWiki(), xcontext);
            String attachmentXWikiAbsoluteUrl = url.toString();
            String attachmentXWikiRelativeUrl = xcontext.getURLFactory().getURL(url, xcontext);
            page.getAttachments().getAttachments().add(toRestAttachment(baseUri, attachment, attachmentXWikiRelativeUrl, attachmentXWikiAbsoluteUrl, withPrettyNames, false));
        }
    }
    // Add objects
    if (withObjects) {
        page.setObjects(objectFactory.createObjects());
        XWikiDocument xwikiDocument = xcontext.getWiki().getDocument(doc.getDocumentReference(), xcontext);
        for (List<BaseObject> objects : xwikiDocument.getXObjects().values()) {
            for (BaseObject object : objects) {
                // Deleting an object leads to a null entry in the list of objects.
                if (object != null) {
                    page.getObjects().getObjectSummaries().add(toRestObject(baseUri, doc, object, false, withPrettyNames));
                }
            }
        }
    }
    // Add xclass
    if (withXClass) {
        page.setClazz(toRestClass(baseUri, doc.getxWikiClass()));
    }
    return page;
}
Also used : ClassResource(org.xwiki.rest.resources.classes.ClassResource) Calendar(java.util.Calendar) XWikiContext(com.xpn.xwiki.XWikiContext) Page(org.xwiki.rest.model.jaxb.Page) URL(java.net.URL) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Link(org.xwiki.rest.model.jaxb.Link)

Example 57 with XWikiContext

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

the class ModelFactory method fillObjectSummary.

private void fillObjectSummary(ObjectSummary objectSummary, Document doc, BaseObject xwikiObject, Boolean withPrettyNames) {
    objectSummary.setId(String.format("%s:%s", doc.getPrefixedFullName(), xwikiObject.getGuid()));
    objectSummary.setGuid(xwikiObject.getGuid());
    objectSummary.setPageId(doc.getPrefixedFullName());
    objectSummary.setPageVersion(doc.getVersion());
    objectSummary.setPageAuthor(doc.getAuthor());
    if (withPrettyNames) {
        XWikiContext xwikiContext = this.xcontextProvider.get();
        objectSummary.setPageAuthorName(xwikiContext.getWiki().getUserName(doc.getAuthor(), null, false, xwikiContext));
    }
    objectSummary.setWiki(doc.getWiki());
    objectSummary.setSpace(doc.getSpace());
    objectSummary.setPageName(doc.getName());
    objectSummary.setClassName(xwikiObject.getClassName());
    objectSummary.setNumber(xwikiObject.getNumber());
    String[] propertyNames = xwikiObject.getPropertyNames();
    if (propertyNames.length > 0) {
        try {
            objectSummary.setHeadline(serializePropertyValue(xwikiObject.get(propertyNames[0])));
        } catch (XWikiException e) {
        // Should never happen
        }
    }
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException)

Example 58 with XWikiContext

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

the class Utils method getXWikiContext.

/**
 * Retrieve the XWiki context from the current execution context.
 *
 * @param componentManager The component manager to be used to retrieve the execution context.
 * @return The XWiki context.
 * @throws RuntimeException If there was an error retrieving the context.
 */
public static XWikiContext getXWikiContext(ComponentManager componentManager) {
    Execution execution;
    XWikiContext xwikiContext;
    try {
        execution = componentManager.getInstance(Execution.class);
        xwikiContext = (XWikiContext) execution.getContext().getProperty("xwikicontext");
        return xwikiContext;
    } catch (Exception e) {
        throw new RuntimeException("Unable to get XWiki context", e);
    }
}
Also used : Execution(org.xwiki.context.Execution) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) URIException(org.apache.commons.httpclient.URIException)

Example 59 with XWikiContext

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

the class XWikiAuthentication method authenticate.

@Override
public boolean authenticate(Request request, Response response) {
    /*
         * Browser authentication resource is a special resource that allows to trigger the authentication dialog box in
         * web browsers
         */
    if (request.getResourceRef().getPath().endsWith(BrowserAuthenticationResource.URI_PATTERN)) {
        return super.authenticate(request, response);
    }
    ComponentManager componentManager = (ComponentManager) getContext().getAttributes().get(Constants.XWIKI_COMPONENT_MANAGER);
    XWikiContext xwikiContext = Utils.getXWikiContext(componentManager);
    XWiki xwiki = Utils.getXWiki(componentManager);
    DocumentReferenceResolver<String> resolver;
    EntityReferenceSerializer<String> serializer;
    try {
        resolver = componentManager.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
        serializer = componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING);
    } catch (ComponentLookupException e1) {
        return false;
    }
    /* By default set XWiki.Guest as the user that is sending the request. */
    xwikiContext.setUserReference(null);
    /*
         * After performing the authentication we should add headers to the response to allow applications to verify if
         * the authentication is still valid We are also adding the XWiki version at the same moment.
         */
    Series<Header> responseHeaders = (Series<Header>) response.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
    if (responseHeaders == null) {
        responseHeaders = new Series<>(Header.class);
        response.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders);
    }
    responseHeaders.add("XWiki-User", serializer.serialize(xwikiContext.getUserReference()));
    responseHeaders.add("XWiki-Version", xwikiContext.getWiki().getVersion());
    // Try with standard XWiki auth
    try {
        XWikiUser xwikiUser = xwiki.checkAuth(xwikiContext);
        if (xwikiUser != null) {
            // Make sure the user is in the context
            xwikiContext.setUserReference(resolver.resolve(xwikiUser.getUser()));
            getLogger().fine(String.format("Authenticated as '%s'.", xwikiUser.getUser()));
            // the user has changed so we need to reset the header
            responseHeaders.set("XWiki-User", serializer.serialize(xwikiContext.getUserReference()));
            return true;
        }
    } catch (XWikiException e) {
        getLogger().log(Level.WARNING, "Exception occurred while authenticating.", e);
    }
    // Falback on restlet auth
    return super.authenticate(request, response);
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) Series(org.restlet.util.Series) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) Header(org.restlet.data.Header) ComponentManager(org.xwiki.component.manager.ComponentManager) XWikiException(com.xpn.xwiki.XWikiException)

Example 60 with XWikiContext

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

the class AbstractExtensionRESTResource method createExtensionVersionFromSolrDocument.

protected ExtensionVersion createExtensionVersionFromSolrDocument(SolrDocument document) {
    XWikiContext xcontext = getXWikiContext();
    ExtensionVersion extension = this.extensionObjectFactory.createExtensionVersion();
    extension.setId(this.<String>getSolrValue(document, Extension.FIELD_ID, true));
    extension.setType(this.<String>getSolrValue(document, Extension.FIELD_TYPE, true));
    extension.setName(this.<String>getSolrValue(document, Extension.FIELD_NAME, false));
    extension.setSummary(this.<String>getSolrValue(document, Extension.FIELD_SUMMARY, false));
    // Recommended
    Boolean recommended = this.<Boolean>getSolrValue(document, RemoteExtension.FIELD_RECOMMENDED, false, false);
    if (recommended == Boolean.TRUE) {
        extension.setRecommended(recommended);
    }
    // SCM
    ExtensionScm scm = new ExtensionScm();
    scm.setUrl(this.<String>getSolrValue(document, Extension.FIELD_SCM, true));
    scm.setConnection(toScmConnection(this.<String>getSolrValue(document, XWikiRepositoryModel.PROP_EXTENSION_SCMCONNECTION, true)));
    scm.setDeveloperConnection(toScmConnection(this.<String>getSolrValue(document, XWikiRepositoryModel.PROP_EXTENSION_SCMDEVCONNECTION, true)));
    if (scm.getUrl() != null || scm.getConnection() != null || scm.getDeveloperConnection() != null) {
        extension.setScm(scm);
    }
    // Issue Management
    ExtensionIssueManagement issueManagement = new ExtensionIssueManagement();
    issueManagement.setSystem(this.<String>getSolrValue(document, XWikiRepositoryModel.PROP_EXTENSION_ISSUEMANAGEMENT_SYSTEM, true));
    issueManagement.setUrl(this.<String>getSolrValue(document, XWikiRepositoryModel.PROP_EXTENSION_ISSUEMANAGEMENT_URL, true));
    if (issueManagement.getSystem() != null || issueManagement.getUrl() != null) {
        extension.setIssueManagement(issueManagement);
    }
    // Rating
    ExtensionRating extensionRating = this.extensionObjectFactory.createExtensionRating();
    extensionRating.setTotalVotes(getSolrValue(document, XWikiRepositoryModel.PROP_RATING_TOTALVOTES, false, 0));
    extensionRating.setAverageVote(getSolrValue(document, XWikiRepositoryModel.PROP_RATING_AVERAGEVOTE, false, 0.0f));
    extension.setRating(extensionRating);
    // Website
    extension.setWebsite(this.<String>getSolrValue(document, Extension.FIELD_WEBSITE, true));
    if (extension.getWebsite() == null) {
        DocumentReference extensionDocumentReference = this.solrDocumentReferenceResolver.resolve(document);
        extension.setWebsite(xcontext.getWiki().getURL(extensionDocumentReference, xcontext));
    }
    // Authors
    Collection<String> authors = this.<String>getSolrValues(document, Extension.FIELD_AUTHORS);
    if (authors != null) {
        for (String authorId : authors) {
            extension.getAuthors().add(resolveExtensionAuthor(authorId));
        }
    }
    // Features
    Collection<String> features = this.<String>getSolrValues(document, Extension.FIELD_FEATURES);
    if (features != null && !features.isEmpty()) {
        for (String feature : features) {
            org.xwiki.extension.ExtensionId extensionId = ExtensionIdConverter.toExtensionId(feature, null);
            ExtensionId extensionFeature = this.extensionObjectFactory.createExtensionId();
            extensionFeature.setId(extensionId.getId());
            if (extensionId.getVersion() != null) {
                extensionFeature.setVersion(extensionId.getVersion().getValue());
            }
            extension.getExtensionFeatures().add(extensionFeature);
            extension.getFeatures().add(extensionFeature.getId());
        }
    }
    // License
    String licenseName = this.<String>getSolrValue(document, Extension.FIELD_LICENSE, true);
    if (licenseName != null) {
        License license = this.extensionObjectFactory.createLicense();
        license.setName(licenseName);
        extension.getLicenses().add(license);
    }
    // Allowed namespaces
    Collection<String> namespaces = this.<String>getSolrValues(document, Extension.FIELD_ALLOWEDNAMESPACES);
    if (namespaces != null && !namespaces.isEmpty()) {
        Namespaces restNamespaces = this.extensionObjectFactory.createNamespaces();
        restNamespaces.withNamespaces(namespaces);
        extension.setAllowedNamespaces(restNamespaces);
    }
    // Version
    extension.setVersion(this.<String>getSolrValue(document, Extension.FIELD_VERSION, true));
    // Properties
    addProperties(extension, this.<String>getSolrValues(document, Extension.FIELD_PROPERTIES));
    return extension;
}
Also used : ExtensionIssueManagement(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionIssueManagement) Namespaces(org.xwiki.extension.repository.xwiki.model.jaxb.Namespaces) License(org.xwiki.extension.repository.xwiki.model.jaxb.License) XWikiContext(com.xpn.xwiki.XWikiContext) ExtensionId(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionId) ExtensionRating(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionRating) ExtensionScm(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionScm) ExtensionVersion(org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersion) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)564 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)203 XWikiException (com.xpn.xwiki.XWikiException)195 DocumentReference (org.xwiki.model.reference.DocumentReference)150 XWiki (com.xpn.xwiki.XWiki)106 BaseObject (com.xpn.xwiki.objects.BaseObject)104 Test (org.junit.Test)64 ArrayList (java.util.ArrayList)55 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)43 ExecutionContext (org.xwiki.context.ExecutionContext)43 Session (org.hibernate.Session)37 InitializationException (org.xwiki.component.phase.InitializationException)36 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)34 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)33 WikiReference (org.xwiki.model.reference.WikiReference)31 Before (org.junit.Before)29 EntityReference (org.xwiki.model.reference.EntityReference)28 QueryException (org.xwiki.query.QueryException)28 Execution (org.xwiki.context.Execution)27 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)24