Search in sources :

Example 81 with Document

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);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiException(com.xpn.xwiki.XWikiException)

Example 82 with Document

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);
}
Also used : Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiException(com.xpn.xwiki.XWikiException)

Example 83 with Document

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()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) HashMap(java.util.HashMap) BaseObject(com.xpn.xwiki.objects.BaseObject) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 84 with Document

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();
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiReference(org.xwiki.model.reference.WikiReference) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 85 with Document

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());
}
Also used : Locale(java.util.Locale) WebApplicationException(javax.ws.rs.WebApplicationException) Document(com.xpn.xwiki.api.Document) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) WebApplicationException(javax.ws.rs.WebApplicationException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

Document (com.xpn.xwiki.api.Document)97 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)60 XWikiException (com.xpn.xwiki.XWikiException)41 XWikiRestException (org.xwiki.rest.XWikiRestException)40 DocumentReference (org.xwiki.model.reference.DocumentReference)37 BaseObject (com.xpn.xwiki.objects.BaseObject)24 Test (org.junit.Test)23 WebApplicationException (javax.ws.rs.WebApplicationException)22 ArrayList (java.util.ArrayList)16 Attachment (com.xpn.xwiki.api.Attachment)14 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 Vector (java.util.Vector)12 Link (org.xwiki.rest.model.jaxb.Link)10 XWikiContext (com.xpn.xwiki.XWikiContext)9 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)7 Date (java.util.Date)7 XWiki (com.xpn.xwiki.api.XWiki)6 RangeIterable (org.xwiki.rest.internal.RangeIterable)6 Object (org.xwiki.rest.model.jaxb.Object)6 XWiki (com.xpn.xwiki.XWiki)5