use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.
the class AttachmentAtPageVersionResourceImpl method getAttachment.
@Override
public Response getAttachment(String wikiName, String spaceName, String pageName, String version, String attachmentName) throws XWikiRestException {
try {
DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, version, true, false);
Document doc = documentInfo.getDocument();
final com.xpn.xwiki.api.Attachment xwikiAttachment = doc.getAttachment(attachmentName);
if (xwikiAttachment == null) {
throw new WebApplicationException(Status.NOT_FOUND);
}
return Response.ok().type(xwikiAttachment.getMimeType()).entity(xwikiAttachment.getContent()).build();
} catch (XWikiException e) {
throw new XWikiRestException(e);
}
}
use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.
the class SyndEntryDocumentSource method source.
@Override
public void source(SyndEntry entry, Object obj, Map<String, Object> params, XWikiContext context) throws XWikiException {
// cast source
Document doc = castDocument(obj, context);
// test access rights
if (!doc.hasAccessLevel("view")) {
throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, "Access denied in view mode!");
}
// prepare parameters (overwrite instance parameters)
Map<String, Object> trueParams = joinParams(params, getParams());
sourceDocument(entry, doc, trueParams, context);
}
use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.
the class XWikiRightServiceImplTest method testProgrammingRightsAfterDropPermissionsForRenderingCycle.
/**
* This test will fail unless:
* SuperAdmin has programming permission before calling Document#dropPermissions().
* SuperAdmin does not have programming permission after calling dropPermissions().
*/
public void testProgrammingRightsAfterDropPermissionsForRenderingCycle() {
final Document doc = new Document(new XWikiDocument(new DocumentReference("XWiki", "Test", "Permissions")), this.getContext());
// doc.setContentAuthor(XWikiRightService.SUPERADMIN_USER_FULLNAME);
// this.getContext().setDoc(doc);
this.getContext().setUser(XWikiRightService.SUPERADMIN_USER_FULLNAME);
assertTrue("User does not have programming right prior to calling " + "doc.dropPermissions()", this.rightService.hasProgrammingRights(this.getContext()));
final Map<String, Object> backup = new HashMap<String, Object>();
XWikiDocument.backupContext(backup, this.getContext());
doc.dropPermissions();
assertFalse("Author retains programming right after calling doc.dropPermissions()", this.rightService.hasProgrammingRights(this.getContext()));
final Map<String, Object> backup2 = new HashMap<String, Object>();
XWikiDocument.backupContext(backup2, this.getContext());
assertTrue("User does not have programming right after switching contexts.", this.rightService.hasProgrammingRights(this.getContext()));
XWikiDocument.restoreContext(backup2, this.getContext());
assertFalse("Author did not lose programming right after switching contexts back.", this.rightService.hasProgrammingRights(this.getContext()));
XWikiDocument.restoreContext(backup, this.getContext());
assertTrue("Author did not regain programming right after switching contexts back.", this.rightService.hasProgrammingRights(this.getContext()));
}
use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.
the class Utils method getPageId.
/**
* Get the page id given its components.
*
* @return The page id.
*/
public static String getPageId(String wikiName, String pageFullName) {
DocumentReferenceResolver<String> defaultDocumentReferenceResolver = com.xpn.xwiki.web.Utils.getComponent(DocumentReferenceResolver.TYPE_STRING);
DocumentReference documentReference = defaultDocumentReferenceResolver.resolve(pageFullName, new WikiReference(wikiName));
XWikiDocument xwikiDocument = new XWikiDocument(documentReference);
Document document = new Document(xwikiDocument, null);
return document.getPrefixedFullName();
}
use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.
the class XWikiResource method getDocumentInfo.
/**
* Retrieve a document. This method never returns null. If something goes wrong with respect to some precondition an
* exception is thrown.
*
* @param wikiName The wiki name. Cannot be null.
* @param spaces The space hierarchy. Cannot be null.
* @param pageName The page name. Cannot be null.
* @param localeString The language. Null for the default language.
* @param version The version. Null for the latest version.
* @param failIfDoesntExist True if an exception should be raised whenever the page doesn't exist.
* @param failIfLocked True if an exception should be raised whenever the page is locked.
* @return A DocumentInfo structure containing the actual document and additional information about it.
* @throws IllegalArgumentException If a parameter has an incorrect value (e.g. null)
* @throws WebApplicationException NOT_FOUND if failIfDoesntExist is true and the page doesn't exist.
* PRECONDITION_FAILED if failIfLocked is true and the document is locked.
*/
public DocumentInfo getDocumentInfo(String wikiName, List<String> spaces, String pageName, String localeString, String version, boolean failIfDoesntExist, boolean failIfLocked) throws XWikiException {
if ((wikiName == null) || (spaces == null || spaces.isEmpty()) || (pageName == null)) {
throw new IllegalArgumentException(String.format("wikiName, spaceName and pageName must all be not null. Current values: (%s:%s.%s)", wikiName, spaces, pageName));
}
// If the language of the translated document is not the one we requested, then the requested translation
// doesn't exist. new translated document by hand.
// TODO: Ideally this method should take a Locale as input and not a String
Locale locale;
if (localeString != null) {
try {
locale = LocaleUtils.toLocale(localeString);
} catch (Exception e) {
// Language is invalid, we consider that the translation has not been found.
throw new WebApplicationException(Status.NOT_FOUND);
}
} else {
locale = null;
}
// Create document reference
DocumentReference reference = new DocumentReference(wikiName, spaces, pageName, locale);
// Get document
Document doc = Utils.getXWikiApi(componentManager).getDocument(reference);
// If doc is null, we don't have the rights to access the document
if (doc == null) {
throw new WebApplicationException(Status.UNAUTHORIZED);
}
if (failIfDoesntExist && doc.isNew()) {
throw new WebApplicationException(Status.NOT_FOUND);
}
// Get a specific version if requested to
if (version != null) {
doc = doc.getDocumentRevision(version);
}
// Check if the doc is locked.
if (failIfLocked && doc.getLocked()) {
throw new WebApplicationException(Status.PRECONDITION_FAILED);
}
return new DocumentInfo(doc, doc.isNew());
}
Aggregations