use of org.xwiki.resource.entity.EntityResourceReference 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.entity.EntityResourceReference 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.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.
the class IntegrationTest method extractResourceReference.
@Test
public void extractResourceReference() throws Exception {
// Entity Resource References
assertURL("http://localhost:8080/xwiki/bin/view/space/page", EntityResourceReference.TYPE, new EntityResourceReference(new DocumentReference("xwiki", "space", "page"), EntityResourceAction.VIEW));
assertURL("http://localhost:8080/xwiki/wiki/mywiki/view/space/page", new ResourceType("wiki"), new EntityResourceReference(new DocumentReference("mywiki", "space", "page"), EntityResourceAction.VIEW));
// Resources Resource References
assertURL("http://localhost:8080/xwiki/resources/js/prototype/prototype.js", ResourcesResourceReference.TYPE, new ResourcesResourceReference());
// Skins Resource References
assertURL("http://localhost:8080/xwiki/skins/flamingo/logo.png", SkinsResourceReference.TYPE, new SkinsResourceReference());
}
use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.
the class BinEntityResourceReferenceResolverTest method testCreateResource.
private ResourceReference testCreateResource(String testURL, String expectedActionName, EntityReference expectedReference, EntityReference returnedReference, EntityType expectedEntityType) throws Exception {
when(this.entityReferenceResolver.resolve(expectedReference, expectedEntityType)).thenReturn(returnedReference);
ExtendedURL extendedURL = new ExtendedURL(new URL(testURL), null);
// Remove the resource type segment since this is what gets passed to specific Reference Resolvers.
extendedURL.getSegments().remove(0);
EntityResourceReference entityResource = this.resolver.resolve(extendedURL, new ResourceType("bin"), Collections.<String, Object>emptyMap());
assertEquals(expectedActionName, entityResource.getAction().getActionName());
assertEquals(returnedReference, entityResource.getEntityReference());
return entityResource;
}
use of org.xwiki.resource.entity.EntityResourceReference in project xwiki-platform by xwiki.
the class WikiEntityResourceReferenceResolverTest method resolve.
@Test
public void resolve() throws Exception {
ExtendedURL url = new ExtendedURL(new URL("http://localhost/wiki/somewiki/view/space/page"), null);
url.getSegments().remove(0);
when(wikiReferenceExtractor.extract(url)).thenReturn(this.wikiReference);
EntityReference reference = buildEntityReference("somewiki", "space", "page");
EntityResourceReference result = (EntityResourceReference) testCreateResource("http://localhost/wiki/somewiki/view/space/page", "view", reference, reference, EntityType.DOCUMENT);
assertEquals(new DocumentReference("somewiki", "space", "page"), result.getEntityReference());
}
Aggregations