Search in sources :

Example 1 with UnsupportedResourceReferenceException

use of org.xwiki.resource.UnsupportedResourceReferenceException in project xwiki-platform by xwiki.

the class WebJarsScriptService method url.

/**
 * Creates an URL that can be used to load a resource (JavaScript, CSS, etc.) from a WebJar in the passed namespace.
 *
 * @param webjarId the id of the WebJar that contains the resource; the format of the WebJar id is
 *            {@code groupId:artifactId} (e.g. {@code org.xwiki.platform:xwiki-platform-job-webjar}), where the
 *            {@code groupId} can be omitted if it is {@link #DEFAULT_WEBJAR_GROUP_ID} (i.e. {@code angular}
 *            translates to {@code org.webjars:angular})
 * @param namespace the namespace in which the webjars resources will be loaded from (e.g. for a wiki namespace you
 *                  should use the format {@code wiki:<wikiId>}). If null then defaults to the current wiki
 *                  namespace. And if the passed namespace doesn't exist, falls back to the main wiki namespace
 * @param path the path within the WebJar, starting from the version folder (e.g. you should pass just
 *            {@code angular.js} if the actual path is {@code META-INF/resources/webjars/angular/2.1.11/angular.js})
 * @param params additional query string parameters to add to the returned URL; there are two known (reserved)
 *            parameters: {@code version} (the WebJar version) and {@code evaluate} (a boolean parameter that
 *            specifies if the requested resource has Velocity code that needs to be evaluated); besides these you
 *            can pass whatever parameters you like (they will be taken into account or not depending on the
 *            resource)
 * @return the URL to load the WebJar resource (relative to the context path of the web application)
 * @since 8.1M2
 */
public String url(String webjarId, String namespace, String path, Map<String, ?> params) {
    if (StringUtils.isEmpty(webjarId)) {
        return null;
    }
    String groupId = DEFAULT_WEBJAR_GROUP_ID;
    String artifactId = webjarId;
    int groupSeparatorPosition = webjarId.indexOf(':');
    if (groupSeparatorPosition >= 0) {
        // A different group id.
        groupId = webjarId.substring(0, groupSeparatorPosition);
        artifactId = webjarId.substring(groupSeparatorPosition + 1);
    }
    Map<String, Object> urlParams = new LinkedHashMap<>();
    if (params != null) {
        urlParams.putAll(params);
    }
    // For backward-compatibility reasons we still support passing the target wiki in parameters. However we need
    // to remove it from the params if that's the case since we don't want to output a URL with the wiki id in the
    // query string (since the namespace is now part of the URL).
    urlParams.remove(WIKI);
    Object version = urlParams.remove(VERSION);
    if (version == null) {
        // Try to determine the version based on the extensions that are currently installed or provided by default.
        version = getVersion(String.format("%s:%s", groupId, artifactId), namespace);
    }
    // Construct a WebJarsResourceReference so that we can serialize it!
    WebJarsResourceReference resourceReference = getResourceReference(artifactId, version, namespace, path, urlParams);
    ExtendedURL extendedURL;
    try {
        extendedURL = this.defaultResourceReferenceSerializer.serialize(resourceReference);
    } catch (SerializeResourceReferenceException | UnsupportedResourceReferenceException e) {
        this.logger.warn("Error while serializing WebJar URL for id [{}], path = [{}]. Root cause = [{}]", webjarId, path, ExceptionUtils.getRootCauseMessage(e));
        return null;
    }
    return extendedURL.serialize();
}
Also used : SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException) ExtendedURL(org.xwiki.url.ExtendedURL) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with UnsupportedResourceReferenceException

use of org.xwiki.resource.UnsupportedResourceReferenceException in project xwiki-platform by xwiki.

the class FilesystemResourceReferenceSerializer method serialize.

@Override
public ExtendedURL serialize(WebJarsResourceReference reference) throws SerializeResourceReferenceException, UnsupportedResourceReferenceException {
    // Copy the resource from the webjar to the filesystem
    FilesystemExportContext exportContext = this.exportContextProvider.get();
    try {
        FilesystemResourceReferenceCopier copier = new FilesystemResourceReferenceCopier();
        copier.copyResourceFromJAR(WEBJARS_RESOURCE_PREFIX, reference.getResourceName(), WEBJAR_PATH, exportContext);
        // filesystem.
        if (reference.getResourceName().toLowerCase().endsWith("css")) {
            copier.processCSS(WEBJARS_RESOURCE_PREFIX, reference.getResourceName(), WEBJAR_PATH, exportContext);
        }
    } catch (Exception e) {
        throw new SerializeResourceReferenceException(String.format("Failed to extract and copy WebJAR resource [%s]", reference.getResourceName()), e);
    }
    List<String> pathSegments = new ArrayList<>();
    // Adjust path depending on where the current doc is stored
    if (exportContext.getCSSParentLevel() == 0) {
        for (int i = 0; i < exportContext.getDocParentLevel(); i++) {
            pathSegments.add(PARENT);
        }
    } else {
        // Adjust path for links inside CSS files (since they need to be relative to the CSS file they're in).
        for (int i = 0; i < exportContext.getCSSParentLevel(); i++) {
            pathSegments.add(PARENT);
        }
    }
    pathSegments.add(WEBJAR_PATH);
    for (String resourceSegment : StringUtils.split(reference.getResourceName(), '/')) {
        pathSegments.add(resourceSegment);
    }
    return new RelativeExtendedURL(pathSegments);
}
Also used : SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) RelativeExtendedURL(org.xwiki.url.internal.RelativeExtendedURL) ArrayList(java.util.ArrayList) FilesystemExportContext(org.xwiki.url.filesystem.FilesystemExportContext) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException)

Example 3 with UnsupportedResourceReferenceException

use of org.xwiki.resource.UnsupportedResourceReferenceException in project xwiki-platform by xwiki.

the class StandardExtendedURLResourceReferenceSerializerTest method serializeWhenNoMatchingSerializer.

@Test
public void serializeWhenNoMatchingSerializer() throws Exception {
    TestResourceReference resource = new TestResourceReference();
    ComponentManager componentManager = this.mocker.getInstance(ComponentManager.class, "context");
    when(componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, TestResourceReference.class, ExtendedURL.class), "standard")).thenThrow(new ComponentLookupException("error"));
    when(componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, TestResourceReference.class, ExtendedURL.class))).thenThrow(new ComponentLookupException("error"));
    try {
        this.mocker.getComponentUnderTest().serialize(resource);
        fail("Should have thrown an exception here");
    } catch (UnsupportedResourceReferenceException expected) {
        assertEquals("Failed to find serializer for Resource Reference [type = [test], parameters = []] and " + "URL format [standard]", expected.getMessage());
    }
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ExtendedURL(org.xwiki.url.ExtendedURL) Test(org.junit.Test)

Example 4 with UnsupportedResourceReferenceException

use of org.xwiki.resource.UnsupportedResourceReferenceException in project xwiki-platform by xwiki.

the class AbstractResourceReferenceResolver method findResourceResolver.

/**
 * Find the right Resolver for the passed Resource type and call it.
 */
protected ResourceReferenceResolver<ExtendedURL> findResourceResolver(String hintPrefix, ResourceType type) throws UnsupportedResourceReferenceException {
    ResourceReferenceResolver<ExtendedURL> resolver;
    Type roleType = new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class);
    String roleHint = computeResolverHint(hintPrefix, type.getId());
    // Step 1: Look for a Resolver specific to the scheme and specific to the Resource Type
    if (this.componentManager.hasComponent(roleType, roleHint)) {
        try {
            resolver = this.componentManager.getInstance(roleType, roleHint);
        } catch (ComponentLookupException cle) {
            // There's no Resolver registered for the passed Resource Type
            throw new UnsupportedResourceReferenceException(String.format("Failed to lookup Resource Reference Resolver for hint [%s]", roleHint), cle);
        }
    } else {
        // schemes
        try {
            resolver = this.componentManager.getInstance(roleType, type.getId());
        } catch (ComponentLookupException cle) {
            // There's no Resolver registered for the passed Resource Type
            throw new UnsupportedResourceReferenceException(String.format("Failed to lookup Resource Reference Resolver for type [%s]", type), cle);
        }
    }
    return resolver;
}
Also used : DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Type(java.lang.reflect.Type) ResourceType(org.xwiki.resource.ResourceType) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 5 with UnsupportedResourceReferenceException

use of org.xwiki.resource.UnsupportedResourceReferenceException in project xwiki-platform by xwiki.

the class DefaultResourceReferenceSerializer method serialize.

@Override
public ExtendedURL serialize(ResourceReference resource) throws SerializeResourceReferenceException, UnsupportedResourceReferenceException {
    ResourceReferenceSerializer<ResourceReference, ExtendedURL> serializer;
    ParameterizedType type = new DefaultParameterizedType(null, ResourceReferenceSerializer.class, ResourceReference.class, ExtendedURL.class);
    try {
        serializer = this.componentManager.getInstance(type, this.urlContextManager.getURLFormatId());
    } catch (ComponentLookupException e) {
        throw new UnsupportedResourceReferenceException(String.format("Invalid URL format id [%s]. Cannot serialize Resource Reference [%s].", this.urlContextManager.getURLFormatId(), resource), e);
    }
    return serializer.serialize(resource);
}
Also used : DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ResourceReference(org.xwiki.resource.ResourceReference) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Aggregations

UnsupportedResourceReferenceException (org.xwiki.resource.UnsupportedResourceReferenceException)5 ExtendedURL (org.xwiki.url.ExtendedURL)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)3 SerializeResourceReferenceException (org.xwiki.resource.SerializeResourceReferenceException)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 ResourceReference (org.xwiki.resource.ResourceReference)1 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)1 ResourceType (org.xwiki.resource.ResourceType)1 FilesystemExportContext (org.xwiki.url.filesystem.FilesystemExportContext)1 RelativeExtendedURL (org.xwiki.url.internal.RelativeExtendedURL)1 WebJarsResourceReference (org.xwiki.webjars.internal.WebJarsResourceReference)1