use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.
the class CreatePageNestedDocumentsTest method assertCreatedNestedDocument.
private void assertCreatedNestedDocument(DocumentReference pageReference, ViewPage viewPage) {
SpaceReference spaceReference = pageReference.getLastSpaceReference();
BreadcrumbElement breadcrumb = viewPage.getBreadcrumb();
if (breadcrumb.canBeExpanded()) {
breadcrumb.expand();
}
assertEquals("/" + getUtil().getURLFragment(spaceReference), breadcrumb.getPathAsString());
assertEquals(spaceReference.getName(), viewPage.getDocumentTitle());
assertEquals(getUtil().serializeReference(pageReference), viewPage.getMetaDataValue("reference"));
}
use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.
the class XWikiDocument method setSpace.
/**
* Set the full local space reference.
* <p>
* Note that this method cannot be removed for now since it's used by Hibernate for loading a XWikiDocument.
*
* @see #getSpace()
* @deprecated since 2.2M1 used {@link #setDocumentReference(DocumentReference)} instead
*/
@Deprecated
public void setSpace(String spaces) {
if (spaces != null) {
DocumentReference reference = getDocumentReference();
EntityReference spaceReference = getRelativeEntityReferenceResolver().resolve(spaces, EntityType.SPACE);
spaceReference = spaceReference.appendParent(getDocumentReference().getWikiReference());
setDocumentReferenceInternal(new DocumentReference(reference.getName(), new SpaceReference(spaceReference)));
}
}
use of org.xwiki.model.reference.SpaceReference in project xwiki-platform by xwiki.
the class DefaultXWikiContextInitializer method authenticate.
private void authenticate(XWikiContext xcontext) throws XWikiException {
// By default set guest as the user that is sending the request.
xcontext.setUserReference(null);
XWikiUser xwikiUser = xcontext.getWiki().checkAuth(xcontext);
if (xwikiUser != null) {
SpaceReference defaultUserSpace = new SpaceReference(XWiki.SYSTEM_SPACE, new WikiReference(xcontext.getWikiId()));
DocumentReference userReference = this.explicitResolver.resolve(xwikiUser.getUser(), defaultUserSpace);
xcontext.setUserReference(XWikiRightService.GUEST_USER.equals(userReference.getName()) ? null : userReference);
this.logger.debug("Authenticated as [{}].", xwikiUser.getUser());
}
}
use of org.xwiki.model.reference.SpaceReference 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.model.reference.SpaceReference in project xwiki-platform by xwiki.
the class XWikiAction method redirectSpaceURLs.
/**
* In order to let users enter URLs to Spaces we do the following when receiving {@code /A/B} (where A and B are
* spaces):
* <ul>
* <li>check that the action is "view" (we only support this for the view action since otherwise this would break
* apps written before this concept was introduced in XWiki 7.2M1)</li>
* <li>if A.B exists then continue</li>
* <li>if A.B doesn't exist then forward to A.B.WebHome</li>
* </ul>
* In order to disable this redirect you should provide the {@code spaceRedirect=false} Query String parameter and
* value.
*
* @since 7.2M1
*/
private boolean redirectSpaceURLs(String action, XWikiURLFactory urlf, XWiki xwiki, XWikiContext context) throws Exception {
if ("view".equals(action) && !"false".equalsIgnoreCase(context.getRequest().getParameter("spaceRedirect"))) {
DocumentReference reference = xwiki.getDocumentReference(context.getRequest(), context);
if (!xwiki.exists(reference, context)) {
String defaultDocumentName = Utils.getComponent(EntityReferenceProvider.class).getDefaultReference(EntityType.DOCUMENT).getName();
// Avoid an infinite loop by ensuring we're not on a WebHome already
if (!reference.getName().equals(defaultDocumentName)) {
// Consider the reference as a Space Reference and Construct a new reference to the home of that
// Space. Then generate the URL for it and forward to it
SpaceReference spaceReference = new SpaceReference(reference.getName(), reference.getParent());
EntityReferenceSerializer<String> localSerializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "local");
// Extract the anchor
String anchor = new URL(context.getRequest().getRequestURL().toString()).getRef();
URL forwardURL = urlf.createURL(localSerializer.serialize(spaceReference), defaultDocumentName, action, context.getRequest().getQueryString(), anchor, spaceReference.getWikiReference().getName(), context);
// Since createURL() contain the webapp context and since RequestDispatcher should not contain it,
// we need to remove it!
String webappContext = xwiki.getWebAppPath(context);
String relativeURL = urlf.getURL(forwardURL, context);
relativeURL = '/' + StringUtils.substringAfter(relativeURL, webappContext);
context.getRequest().getRequestDispatcher(relativeURL).forward(context.getRequest(), context.getResponse());
return true;
}
}
}
return false;
}
Aggregations