use of org.xwiki.url.ExtendedURL in project xwiki-platform by xwiki.
the class AbstractExtendedURLResourceTypeResolver method resolve.
protected ResourceType resolve(String hintPrefix, ExtendedURL extendedURL, Map<String, Object> parameters) throws CreateResourceTypeException {
ResourceType resourceType;
// Find the Resource Type, which is the first segment in the ExtendedURL.
//
// Note that we need to remove the type from the ExtendedURL instance since it's passed to the specific
// resolvers and they shouldn't be aware of where it was located since they need to be able to resolve the
// rest of the URL independently of the URL scheme, in case they wish to have a single URL syntax for all URL
// schemes.
//
// Examples:
// - scheme 1: /<type>/something
// - scheme 2: /something?type=<type>
//
// The specific resolver for type <type> needs to be passed an ExtendedURL independent of the type, in this
// case, "/something" for both examples.
//
// However since we also want this code to work when short URLs are enabled, we only remove the segment part
// if a Resource type has been identified (see below) and if not, we assume the URL is pointing to an Entity
// Resource.
List<String> segments = extendedURL.getSegments();
resourceType = this.defaultStringResourceTypeResolver.resolve(segments.get(0), Collections.<String, Object>emptyMap());
// Second, if not found, try to locate a URL Resolver registered for all URL schemes
if (this.componentManager.hasComponent(new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class), computeHint(hintPrefix, resourceType.getId()))) {
extendedURL.getSegments().remove(0);
} else if (this.componentManager.hasComponent(new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class), resourceType.getId())) {
extendedURL.getSegments().remove(0);
} else {
// No specific Resource Type Resolver has been found. In order to support short URLs (ie. without the
// "bin" or "wiki" part specified), we assume the URL is pointing to an Entity Resource Reference.
// Since the "wiki" type was not selected, we're assuming that the we'll use the "bin" entity resolver.
resourceType = EntityResourceReference.TYPE;
}
return resourceType;
}
use of org.xwiki.url.ExtendedURL in project xwiki-platform by xwiki.
the class XWikiRequestProcessor method processPath.
@Override
protected String processPath(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
String url = httpServletRequest.getRequestURL().toString();
try {
ExtendedURL extendedURL = new ExtendedURL(new URL(url), httpServletRequest.getContextPath());
ResourceType type = this.typeResolver.resolve(extendedURL, Collections.<String, Object>emptyMap());
EntityResourceReference entityResourceReference = (EntityResourceReference) this.resolver.resolve(extendedURL, type, Collections.<String, Object>emptyMap());
return "/" + entityResourceReference.getAction().getActionName() + "/";
} catch (Exception e) {
throw new IOException(String.format("Failed to extract the Entity Action from URL [%s]", url), e);
}
}
use of org.xwiki.url.ExtendedURL 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.url.ExtendedURL in project xwiki-platform by xwiki.
the class WebJarsScriptServiceTest method computeURLWithVersion.
@Test
public void computeURLWithVersion() throws Exception {
WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:math", Arrays.asList("ang:ular", "2.1.11", "angular.css"));
// Test that colon is not interpreted as groupId/artifactId separator (for backwards compatibility).
when(this.serializer.serialize(resourceReference)).thenReturn(new ExtendedURL(Arrays.asList("xwiki", "ang:ular", "2.1.11", "angular.css")));
assertEquals("/xwiki/ang%3Aular/2.1.11/angular.css", this.mocker.getComponentUnderTest().url("ang:ular/2.1.11/angular.css"));
}
use of org.xwiki.url.ExtendedURL in project xwiki-platform by xwiki.
the class WebJarsScriptServiceTest method computeURLWithoutVersionAndNoExtensionMatchingWebJarId.
@Test
public void computeURLWithoutVersionAndNoExtensionMatchingWebJarId() throws Exception {
WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("math");
WebJarsResourceReference resourceReference = new WebJarsResourceReference("wiki:math", Arrays.asList("angular", "angular.css"));
when(this.serializer.serialize(resourceReference)).thenReturn(new ExtendedURL(Arrays.asList("xwiki", "angular", "angular.css")));
assertEquals("/xwiki/angular/angular.css", this.mocker.getComponentUnderTest().url("angular", "angular.css"));
}
Aggregations