Search in sources :

Example 1 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class DefaultRecordableEventDescriptorManager method getDescriptorsFromWiki.

private List<RecordableEventDescriptor> getDescriptorsFromWiki(String wikiId) throws ComponentLookupException {
    Namespace namespace = new WikiNamespace(wikiId);
    ComponentManager wikiComponentManager = componentManagerManager.getComponentManager(namespace.serialize(), false);
    if (wikiComponentManager == null) {
        return Collections.emptyList();
    }
    List<RecordableEventDescriptor> descriptors = new ArrayList<>();
    descriptors.addAll(wikiComponentManager.getInstanceList(RecordableEventDescriptor.class));
    descriptors.addAll(wikiComponentManager.getInstanceList(UntypedRecordableEventDescriptor.class));
    return descriptors;
}
Also used : RecordableEventDescriptor(org.xwiki.eventstream.RecordableEventDescriptor) UntypedRecordableEventDescriptor(org.xwiki.eventstream.UntypedRecordableEventDescriptor) UntypedRecordableEventDescriptor(org.xwiki.eventstream.UntypedRecordableEventDescriptor) WikiNamespace(org.xwiki.model.namespace.WikiNamespace) ComponentManager(org.xwiki.component.manager.ComponentManager) ArrayList(java.util.ArrayList) WikiNamespace(org.xwiki.model.namespace.WikiNamespace) Namespace(org.xwiki.component.namespace.Namespace)

Example 2 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class DefaultPresentationBuilder method buildPresentationXDOM.

/**
 * Parses the given HTML text into an XDOM tree.
 *
 * @param html the HTML text to parse
 * @param targetDocumentReference specifies the document where the presentation will be imported; we use the target
 *            document reference to get the syntax of the target document and to set the {@code BASE} meta data on
 *            the created XDOM
 * @return a XDOM tree
 * @throws OfficeImporterException if parsing the given HTML fails
 */
protected XDOM buildPresentationXDOM(String html, DocumentReference targetDocumentReference) throws OfficeImporterException {
    try {
        ComponentManager contextComponentManager = this.contextComponentManagerProvider.get();
        String syntaxId = this.documentAccessBridge.getTranslatedDocumentInstance(targetDocumentReference).getSyntax().toIdString();
        BlockRenderer renderer = contextComponentManager.getInstance(BlockRenderer.class, syntaxId);
        Map<String, String> galleryParameters = Collections.emptyMap();
        ExpandedMacroBlock gallery = new ExpandedMacroBlock("gallery", galleryParameters, renderer, false, contextComponentManager);
        gallery.addChild(this.xhtmlParser.parse(new StringReader(html)));
        XDOM xdom = new XDOM(Collections.singletonList((Block) gallery));
        // Make sure (image) references are resolved relative to the target document reference.
        xdom.getMetaData().addMetaData(MetaData.BASE, entityReferenceSerializer.serialize(targetDocumentReference));
        return xdom;
    } catch (Exception e) {
        throw new OfficeImporterException("Failed to build presentation XDOM.", e);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ComponentManager(org.xwiki.component.manager.ComponentManager) StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) OfficeConverterException(org.xwiki.officeimporter.converter.OfficeConverterException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 3 with ComponentManager

use of org.xwiki.component.manager.ComponentManager 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 4 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class XWikiRestletJaxRsApplication method createInboundRoot.

@Override
public Restlet createInboundRoot() {
    // Create the JAX-RS application and add it to the main Restlet JAX-RS application
    add(this.jaxrsApplication);
    // Create the root restlet. This basically sets up a chain: setup/cleanup filter -> authentication filter ->
    // router
    XWikiSetupCleanupFilter setupCleanupFilter = new XWikiSetupCleanupFilter();
    XWikiAuthentication xwikiAuthentication = new XWikiAuthentication(getContext());
    ComponentManager componentManager = (ComponentManager) getContext().getAttributes().get(Constants.XWIKI_COMPONENT_MANAGER);
    xwikiAuthentication.setVerifier(new XWikiSecretVerifier(getContext(), componentManager));
    // Create a router for adding resources
    Router router = new Router();
    router.attach(BrowserAuthenticationResource.URI_PATTERN, BrowserAuthenticationResource.class);
    router.setDefaultMatchingMode(Template.MODE_STARTS_WITH);
    // Add to the router the restlet generated by the JAX-RS application which takes care of dispatching requests to
    // JAX-RS resources
    Restlet jaxRsRoot = super.createInboundRoot();
    // Add support for media query parameter for selecting the media type
    getTunnelService().setEnabled(true);
    getMetadataService().addCommonExtensions();
    getTunnelService().setQueryTunnel(true);
    router.attach(jaxRsRoot);
    // Build the actual chain
    setupCleanupFilter.setNext(xwikiAuthentication);
    xwikiAuthentication.setNext(router);
    // Return the setup/cleanup filter (the entry point for the chain) as the root restlet
    return setupCleanupFilter;
}
Also used : Restlet(org.restlet.Restlet) ComponentManager(org.xwiki.component.manager.ComponentManager) Router(org.restlet.routing.Router)

Example 5 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class ComponentsObjectFactory method getInstance.

@Override
public <T> T getInstance(Class<T> clazz) throws InstantiateException {
    try {
        ComponentManager componentManager = this.componentManagerProvider.get();
        // Use the component manager to lookup the class. This ensure that injections are properly executed.
        XWikiRestComponent component = componentManager.getInstance(XWikiRestComponent.class, clazz.getName());
        // JAX-RS resources and providers must be declared as components whose hint is the FQN of the class
        // implementing it. This is needed because of they are looked up using the FQN as the hint.
        ComponentDescriptor<XWikiRestComponent> componentDescriptor = componentManager.getComponentDescriptor(XWikiRestComponent.class, clazz.getName());
        // Retrieve the list of releasable components from the execution context. This is used to store component
        // instances that need to be released at the end of the request.
        ExecutionContext executionContext = this.execution.getContext();
        List<XWikiRestComponent> releasableComponentReferences = (List<XWikiRestComponent>) executionContext.getProperty(Constants.RELEASABLE_COMPONENT_REFERENCES);
        if (releasableComponentReferences == null) {
            releasableComponentReferences = new ArrayList<>();
            executionContext.setProperty(Constants.RELEASABLE_COMPONENT_REFERENCES, releasableComponentReferences);
        }
        // Only add the components that have a per-lookup instantiation strategy.
        if (componentDescriptor.getInstantiationStrategy() == ComponentInstantiationStrategy.PER_LOOKUP) {
            releasableComponentReferences.add(component);
        }
        // component hint to the actual fully qualified name of the Java class.
        return (T) component;
    } catch (ComponentLookupException e) {
        throw new InstantiateException(e);
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XWikiRestComponent(org.xwiki.rest.XWikiRestComponent) InstantiateException(org.restlet.ext.jaxrs.InstantiateException) ComponentManager(org.xwiki.component.manager.ComponentManager) ArrayList(java.util.ArrayList) List(java.util.List) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Aggregations

ComponentManager (org.xwiki.component.manager.ComponentManager)76 Test (org.junit.Test)34 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)23 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)18 DocumentReference (org.xwiki.model.reference.DocumentReference)17 Provider (javax.inject.Provider)14 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)12 Before (org.junit.Before)11 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)11 XWikiContext (com.xpn.xwiki.XWikiContext)9 Expectations (org.jmock.Expectations)9 WikiReference (org.xwiki.model.reference.WikiReference)9 HashMap (java.util.HashMap)7 Execution (org.xwiki.context.Execution)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 XWiki (com.xpn.xwiki.XWiki)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 ArrayList (java.util.ArrayList)6 ExecutionContext (org.xwiki.context.ExecutionContext)6 MimeMessage (javax.mail.internet.MimeMessage)5