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