use of org.xwiki.resource.ResourceReference in project xwiki-platform by xwiki.
the class XWikiAction method handleRedirectObject.
/**
* Redirect the user to an other location if the document holds an XWiki.RedirectClass instance (used when a
* document is moved).
*
* @param context the XWiki context
* @return either or not a redirection have been sent
* @throws XWikiException if error occurs
* @since 8.0RC1
* @since 7.4.2
*/
protected boolean handleRedirectObject(XWikiContext context) throws XWikiException {
WikiReference wikiReference = context.getWikiReference();
// Look if the document has a redirect object
XWikiDocument doc = context.getDoc();
BaseObject redirectObj = doc.getXObject(new DocumentReference("RedirectClass", new SpaceReference("XWiki", wikiReference)));
if (redirectObj == null) {
return false;
}
// Get the location
String location = redirectObj.getStringValue("location");
if (StringUtils.isBlank(location)) {
return false;
}
// Resolve the location to get a reference
DocumentReferenceResolver<String> resolver = Utils.getComponent(DocumentReferenceResolver.TYPE_STRING);
EntityReference locationReference = resolver.resolve(location, wikiReference);
// Get the type of the current target
ResourceReference resourceReference = Utils.getComponent(ResourceReferenceManager.class).getResourceReference();
EntityResourceReference entityResource = (EntityResourceReference) resourceReference;
EntityReference entityReference = entityResource.getEntityReference();
// If the entity is inside a document, compute the new entity with the new document part.
if (entityReference.getType().ordinal() > EntityType.DOCUMENT.ordinal()) {
EntityReference parentDocument = entityReference.extractReference(EntityType.DOCUMENT);
locationReference = entityReference.replaceParent(parentDocument, locationReference);
}
// Get the URL corresponding to the location
// Note: the anchor part is lost in the process, because it is not sent to the server
// (see: http://stackoverflow.com/a/4276491)
String url = context.getWiki().getURL(locationReference, context.getAction(), context.getRequest().getQueryString(), null, context);
// Send the redirection
try {
context.getResponse().sendRedirect(url);
} catch (IOException e) {
throw new XWikiException("Failed to redirect.", e);
}
return true;
}
use of org.xwiki.resource.ResourceReference in project xwiki-platform by xwiki.
the class IntegrationTest method assertURL.
private void assertURL(String url, ResourceType expectedType, ResourceReference expectedReference) throws Exception {
ExtendedURL extendedURL = new ExtendedURL(new URL(url), "xwiki");
ResourceType resourceType = this.resourceTypeResolver.resolve(extendedURL, Collections.<String, Object>emptyMap());
assertEquals(expectedType.getId(), resourceType.getId());
ResourceReference reference = this.resourceReferenceResolver.resolve(extendedURL, resourceType, Collections.<String, Object>emptyMap());
assertEquals(expectedReference, reference);
}
use of org.xwiki.resource.ResourceReference in project xwiki-platform by xwiki.
the class BinEntityResourceReferenceResolverTest method createResourceWhenURLHasParameters.
@Test
public void createResourceWhenURLHasParameters() throws Exception {
EntityReference fullReference = buildEntityReference("wiki", Arrays.asList("space"), "page");
ResourceReference resource = testCreateResource("http://localhost/bin/view/space/page?param1=value1¶m2=value2", "view", fullReference, fullReference, EntityType.DOCUMENT);
// Assert parameters
// Note: the parameters order are the same as the order specified in the URL.
Map<String, List<String>> expectedMap = new LinkedHashMap<String, List<String>>();
expectedMap.put("param1", Arrays.asList("value1"));
expectedMap.put("param2", Arrays.asList("value2"));
assertEquals(expectedMap, resource.getParameters());
// Also verify it works when there's a param with no value.
resource = testCreateResource("http://localhost/bin/view/space/page?param", "view", fullReference, fullReference, EntityType.DOCUMENT);
expectedMap = new LinkedHashMap<>();
expectedMap.put("param", Collections.<String>emptyList());
assertEquals(expectedMap, resource.getParameters());
}
use of org.xwiki.resource.ResourceReference in project xwiki-platform by xwiki.
the class DownloadAction method getFileName.
/**
* @return the filename of the attachment or null if the URL didn't point to an attachment
*/
private String getFileName() {
// Extract the Attachment file name from the parsed request URL that was done before this Action is called
ResourceReference resourceReference = Utils.getComponent(ResourceReferenceManager.class).getResourceReference();
EntityResourceReference entityResource = (EntityResourceReference) resourceReference;
// Try to extract the attachment from the reference but it's possible that the URL didn't point to an
// attachment, in which case we return null.
EntityReference attachmentReference = entityResource.getEntityReference().extractReference(EntityType.ATTACHMENT);
return attachmentReference == null ? null : attachmentReference.getName();
}
use of org.xwiki.resource.ResourceReference in project xwiki-platform by xwiki.
the class DownloadRevAction method getFileName.
/**
* @return the filename of the attachment.
*/
private String getFileName() {
// Extract the Attachment file name from the parsed request URL that was done before this Action is called
ResourceReference resourceReference = Utils.getComponent(ResourceReferenceManager.class).getResourceReference();
EntityResourceReference entityResource = (EntityResourceReference) resourceReference;
return entityResource.getEntityReference().extractReference(EntityType.ATTACHMENT).getName();
}
Aggregations