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();
}
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);
}
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());
}
}
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());
}
}
Aggregations