Search in sources :

Example 1 with SerializeResourceReferenceException

use of org.xwiki.resource.SerializeResourceReferenceException 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 SerializeResourceReferenceException

use of org.xwiki.resource.SerializeResourceReferenceException 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 SerializeResourceReferenceException

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

the class PathConverterTest method convertWhenError.

@Test
public void convertWhenError() throws Exception {
    ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), "truevfs");
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:Sandbox.WebHome@my.zip"), "a/b/c");
    when(serializer.serialize(reference)).thenThrow(new SerializeResourceReferenceException("error"));
    try {
        this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), "attach:Sandbox.WebHome@my.zip/a/b/c");
        fail("Should have thrown an exception here");
    } catch (ConversionException expected) {
        assertEquals("Failed to convert [attach:Sandbox.WebHome@my.zip/a/b/c] to a Path object", expected.getMessage());
    }
}
Also used : TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ConversionException(org.xwiki.properties.converter.ConversionException) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) URI(java.net.URI) Test(org.junit.Test)

Example 4 with SerializeResourceReferenceException

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

the class DefaultVfsManagerTest method getURLerror.

@Test
public void getURLerror() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:xwiki:space.page@attachment"), "path1/path2/test.txt");
    ResourceReferenceSerializer<VfsResourceReference, ExtendedURL> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, ExtendedURL.class));
    when(serializer.serialize(reference)).thenThrow(new SerializeResourceReferenceException("error"));
    try {
        this.mocker.getComponentUnderTest().getURL(reference);
        fail("Should have thrown an exception");
    } catch (VfsException expected) {
        assertEquals("Failed to compute URL for [uri = [attach:xwiki:space.page@attachment], " + "path = [path1/path2/test.txt], parameters = []]", expected.getMessage());
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Aggregations

SerializeResourceReferenceException (org.xwiki.resource.SerializeResourceReferenceException)4 Test (org.junit.Test)2 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)2 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)2 UnsupportedResourceReferenceException (org.xwiki.resource.UnsupportedResourceReferenceException)2 ExtendedURL (org.xwiki.url.ExtendedURL)2 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)2 URI (java.net.URI)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 TPath (net.java.truevfs.access.TPath)1 ConversionException (org.xwiki.properties.converter.ConversionException)1 FilesystemExportContext (org.xwiki.url.filesystem.FilesystemExportContext)1 RelativeExtendedURL (org.xwiki.url.internal.RelativeExtendedURL)1 VfsException (org.xwiki.vfs.VfsException)1 WebJarsResourceReference (org.xwiki.webjars.internal.WebJarsResourceReference)1