Search in sources :

Example 26 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class CommentVersionResourceImpl method getCommentVersion.

@Override
public Comment getCommentVersion(String wikiName, String spaceName, String pageName, String version, Integer id, Integer start, Integer number, Boolean withPrettyNames) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, version, true, false);
        Document doc = documentInfo.getDocument();
        Vector<com.xpn.xwiki.api.Object> xwikiComments = doc.getComments();
        for (com.xpn.xwiki.api.Object xwikiComment : xwikiComments) {
            if (id.equals(xwikiComment.getNumber())) {
                return DomainObjectFactory.createComment(objectFactory, uriInfo.getBaseUri(), doc, xwikiComment, Utils.getXWikiApi(componentManager), withPrettyNames);
            }
        }
        throw new WebApplicationException(Status.NOT_FOUND);
    } 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 27 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class PageTranslationsResourceImpl method getTranslations.

@Override
public Translations getTranslations(String wikiName, String spaceName, String pageName) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        return DomainObjectFactory.createTranslations(objectFactory, uriInfo.getBaseUri(), doc);
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiException(com.xpn.xwiki.XWikiException)

Example 28 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class AbstractDatabaseSearchSource method search.

@Override
public List<SearchResult> search(String partialQueryString, String wikiName, String wikis, boolean hasProgrammingRights, String orderField, String order, boolean distinct, int number, int start, Boolean withPrettyNames, String className, UriInfo uriInfo) throws Exception {
    XWikiContext xwikiContext = this.xcontextProvider.get();
    XWiki xwikiApi = new XWiki(xwikiContext.getWiki(), xwikiContext);
    if (partialQueryString == null || StringUtils.startsWithIgnoreCase(partialQueryString, "select")) {
        return Collections.emptyList();
    }
    String queryString = resolveQuery(distinct, partialQueryString);
    Query query = this.queryManager.createQuery(queryString, this.queryLanguage);
    query.setLimit(number).setOffset(start);
    query.setWiki(wikiName);
    List<Object> queryResult = query.execute();
    WikiReference wikiReference = new WikiReference(wikiName);
    /* Build the result. */
    List<SearchResult> result = new ArrayList<>();
    for (Object object : queryResult) {
        Object[] fields = (Object[]) object;
        String fullName = (String) fields[0];
        String language = (String) fields[3];
        DocumentReference documentReference = this.resolver.resolve(fullName, wikiReference);
        /* Check if the user has the right to see the found document */
        if (this.authorization.hasAccess(Right.VIEW, documentReference)) {
            Document doc = xwikiApi.getDocument(documentReference);
            String title = doc.getDisplayTitle();
            SearchResult searchResult = this.objectFactory.createSearchResult();
            searchResult.setType("page");
            searchResult.setId(doc.getPrefixedFullName());
            searchResult.setPageFullName(doc.getFullName());
            searchResult.setTitle(title);
            searchResult.setWiki(wikiName);
            searchResult.setSpace(doc.getSpace());
            searchResult.setPageName(doc.getName());
            searchResult.setVersion(doc.getVersion());
            searchResult.setAuthor(doc.getAuthor());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(doc.getDate());
            searchResult.setModified(calendar);
            if (withPrettyNames) {
                searchResult.setAuthorName(xwikiApi.getUserName(doc.getAuthor(), false));
            }
            /*
                 * Avoid to return object information if the user is not authenticated. This will prevent crawlers to
                 * retrieve information such as email addresses and passwords from user's profiles.
                 */
            if (StringUtils.isNotEmpty(className) && xwikiContext.getUserReference() != null) {
                XWikiDocument xdocument = xwikiContext.getWiki().getDocument(doc.getDocumentReference(), xwikiContext);
                BaseObject baseObject = xdocument.getObject(className);
                if (baseObject != null) {
                    searchResult.setObject(this.modelFactory.toRestObject(uriInfo.getBaseUri(), doc, baseObject, false, false));
                }
            }
            String pageUri;
            if (StringUtils.isBlank(language)) {
                pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, wikiName, Utils.getSpacesHierarchy(documentReference.getLastSpaceReference()), documentReference.getName()).toString();
            } else {
                searchResult.setLanguage(language);
                pageUri = Utils.createURI(uriInfo.getBaseUri(), PageTranslationResource.class, wikiName, Utils.getSpacesHierarchy(documentReference.getLastSpaceReference()), documentReference.getName(), language).toString();
            }
            Link pageLink = new Link();
            pageLink.setHref(pageUri);
            pageLink.setRel(Relations.PAGE);
            searchResult.getLinks().add(pageLink);
            result.add(searchResult);
        }
    }
    return result;
}
Also used : Query(org.xwiki.query.Query) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.api.XWiki) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Link(org.xwiki.rest.model.jaxb.Link)

Example 29 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class ZipExplorerTest method testGetFileLink.

public void testGetFileLink() throws Exception {
    Mock mockDocument = mock(XWikiDocument.class);
    mockDocument.expects(once()).method("getAttachmentURL").will(returnValue("http://server/xwiki/bin/download/Main/Document/zipfile.zip"));
    Document document = new Document((XWikiDocument) mockDocument.proxy(), null);
    String link = this.plugin.getFileLink(document, "zipfile.zip", "filename", null);
    Assert.assertEquals("http://server/xwiki/bin/download/Main/Document/zipfile.zip/filename", link);
}
Also used : Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Mock(org.jmock.Mock)

Example 30 with Document

use of com.xpn.xwiki.api.Document in project xwiki-platform by xwiki.

the class ZipExplorerTest method testGetFileList.

public void testGetFileList() throws Exception {
    XWikiDocument document = createXWikiDocumentWithZipFileAttachment();
    List<String> entries = this.plugin.getFileList(new Document(document, null), "zipfile.zip", null);
    Assert.assertEquals(2, entries.size());
    Assert.assertEquals("Directory/File.txt", entries.get(0));
    Assert.assertEquals("File2.txt", entries.get(1));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument)

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