Search in sources :

Example 11 with ComponentLookupException

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

the class ConfiguredReputationAlgorithmProvider method get.

/**
 * Retrieve an instance of the desired ReputationAlorithm (default/simple/custom).
 *
 * @param documentRef documentRef the document to which the ratings are associated to
 * @return the reputation algorithm selected by looking at the current configuration settings
 */
@Override
public ReputationAlgorithm get(DocumentReference documentRef) {
    String defaultAlgorithmHint = "default";
    String reputationAlgorithmHint = getXWiki().Param(RatingsManager.RATINGS_CONFIG_PARAM_PREFIX + RatingsManager.RATINGS_CONFIG_FIELDNAME_REPUTATIONALGORITHM_HINT, defaultAlgorithmHint);
    try {
        XWikiDocument configurationDocument = ratingsConfiguration.getConfigurationDocument(documentRef);
        if (configurationDocument != null && !configurationDocument.isNew() && configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE) != null) {
            BaseProperty prop = (BaseProperty) configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE).get(RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_REPUTATION_ALGORITHM_HINT);
            String hint = (prop == null) ? null : (String) prop.getValue();
            if (hint == "custom") {
                prop = (BaseProperty) configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE).get(RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_REPUTATION_CUSTOM_ALGORITHM);
                hint = (prop == null) ? null : (String) prop.getValue();
            }
            reputationAlgorithmHint = (hint == null) ? reputationAlgorithmHint : hint;
        }
    } catch (Exception e) {
        logger.error("Cannot read reputation algorithm config", e);
    }
    // if the reputation algorithm hint is a page let's try to get the instance from groovy
    if (reputationAlgorithmHint.contains(".")) {
        try {
            ReputationAlgorithmGroovy reputationInstance = (ReputationAlgorithmGroovy) getXWiki().parseGroovyFromPage(reputationAlgorithmHint, getXWikiContext());
            if (reputationInstance != null) {
                reputationInstance.setComponentManager(componentManager);
                reputationInstance.setExecution(execution);
                reputationInstance.setXWikiContext(getXWikiContext());
                reputationInstance.setRatingsManager(ratingsManagerProvider.get(documentRef));
                return reputationInstance;
            }
        } catch (Throwable e) {
            logger.error("Cannot instanciate Reputation algorithm from page " + reputationAlgorithmHint, e);
        }
    }
    try {
        return componentManager.getInstance(ReputationAlgorithm.class, reputationAlgorithmHint);
    } catch (ComponentLookupException e) {
        // TODO Auto-generated catch block
        logger.error("Error loading ratings manager component for hint " + reputationAlgorithmHint, e);
        try {
            return componentManager.getInstance(ReputationAlgorithm.class, defaultAlgorithmHint);
        } catch (ComponentLookupException e1) {
            return null;
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) BaseProperty(com.xpn.xwiki.objects.BaseProperty) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 12 with ComponentLookupException

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

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

the class ResourceReferenceHandlerServlet method cleanupComponents.

private void cleanupComponents() throws ServletException {
    Container container;
    try {
        container = this.rootComponentManager.getInstance(Container.class);
    } catch (ComponentLookupException e) {
        throw new ServletException("Failed to locate a Container component", e);
    }
    Execution execution;
    try {
        execution = this.rootComponentManager.getInstance(Execution.class);
    } catch (ComponentLookupException e) {
        throw new ServletException("Failed to locate a Execution component", e);
    }
    // We must ensure we clean the ThreadLocal variables located in the Container and Execution components as
    // otherwise we will have a potential memory leak.
    container.removeRequest();
    container.removeResponse();
    container.removeSession();
    execution.removeContext();
}
Also used : ServletException(javax.servlet.ServletException) Container(org.xwiki.container.Container) Execution(org.xwiki.context.Execution) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 14 with ComponentLookupException

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

the class ResourceReferenceHandlerServlet method handleResourceReference.

private void handleResourceReference(ResourceReference resourceReference) throws ServletException {
    ResourceReferenceHandlerManager<?> resourceReferenceHandlerManager;
    try {
        Type role = new DefaultParameterizedType(null, ResourceReferenceHandlerManager.class, ResourceType.class);
        resourceReferenceHandlerManager = this.rootComponentManager.getInstance(role);
    } catch (ComponentLookupException e) {
        // Should not happen since a Resource Reference Handler should always exist on the system.
        throw new ServletException("Failed to locate a Resource Reference Handler Manager component", e);
    }
    try {
        resourceReferenceHandlerManager.handle(resourceReference);
    } catch (ResourceReferenceHandlerException e) {
        throw new ServletException(String.format("Failed to handle Resource Reference [%s]", resourceReference), e);
    }
}
Also used : ServletException(javax.servlet.ServletException) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Type(java.lang.reflect.Type) ResourceType(org.xwiki.resource.ResourceType) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 15 with ComponentLookupException

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

the class ResourceReferenceHandlerServlet method getResourceReferenceResolver.

private ResourceReferenceResolver<ExtendedURL> getResourceReferenceResolver() throws ServletException {
    ResourceReferenceResolver<ExtendedURL> urlResolver;
    try {
        Type role = new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class);
        urlResolver = this.rootComponentManager.getInstance(role);
    } catch (ComponentLookupException e) {
        // Should not happen since a URL provider should exist on the system.
        throw new ServletException("Failed to locate an ExtendedURL Resource Reference Resolver component", e);
    }
    return urlResolver;
}
Also used : ServletException(javax.servlet.ServletException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Type(java.lang.reflect.Type) ResourceType(org.xwiki.resource.ResourceType) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Aggregations

ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)104 ComponentManager (org.xwiki.component.manager.ComponentManager)24 Test (org.junit.Test)15 DocumentReference (org.xwiki.model.reference.DocumentReference)12 XWikiContext (com.xpn.xwiki.XWikiContext)10 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)10 InitializationException (org.xwiki.component.phase.InitializationException)8 ArrayList (java.util.ArrayList)7 XWikiException (com.xpn.xwiki.XWikiException)5 HashMap (java.util.HashMap)5 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)5 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)5 FilterException (org.xwiki.filter.FilterException)5 ExtendedURL (org.xwiki.url.ExtendedURL)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 Type (java.lang.reflect.Type)4 HashSet (java.util.HashSet)4 WikiComponentException (org.xwiki.component.wiki.WikiComponentException)4 ExecutionContext (org.xwiki.context.ExecutionContext)4 WikiReference (org.xwiki.model.reference.WikiReference)4