Search in sources :

Example 11 with XWikiRestException

use of org.xwiki.rest.XWikiRestException in project xwiki-platform by xwiki.

the class WikiPagesResourceImpl method getPages.

@Override
public Pages getPages(String wikiName, Integer start, String name, String space, String author, Integer number) throws XWikiRestException {
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    Pages pages = objectFactory.createPages();
    /* This try is just needed for executing the finally clause. */
    try {
        Map<String, String> filters = new HashMap<String, String>();
        if (!name.equals("")) {
            filters.put("name", name);
        }
        if (!space.equals("")) {
            filters.put("space", Utils.getLocalSpaceId(parseSpaceSegments(space)));
        }
        if (!author.equals("")) {
            filters.put("author", author);
        }
        /* Build the query */
        Formatter f = new Formatter();
        f.format("select doc from XWikiDocument as doc");
        if (filters.keySet().size() > 0) {
            f.format(" where (");
            int i = 0;
            for (String param : filters.keySet()) {
                if (param.equals("name")) {
                    f.format(" upper(doc.fullName) like :name ");
                }
                if (param.equals("space")) {
                    f.format(" upper(doc.space) like :space ");
                }
                if (param.equals("author")) {
                    f.format(" upper(doc.contentAuthor) like :author ");
                }
                i++;
                if (i < filters.keySet().size()) {
                    f.format(" and ");
                }
            }
            f.format(")");
        }
        String queryString = f.toString();
        /* Execute the query by filling the parameters */
        List<Object> queryResult = null;
        try {
            Query query = queryManager.createQuery(queryString, Query.XWQL).setLimit(number).setOffset(start);
            for (String param : filters.keySet()) {
                query.bindValue(param, String.format("%%%s%%", filters.get(param).toUpperCase()));
            }
            queryResult = query.execute();
        } catch (QueryException e) {
            throw new XWikiRestException(e);
        }
        /* Get the results and populate the returned representation */
        for (Object object : queryResult) {
            XWikiDocument xwikiDocument = (XWikiDocument) object;
            xwikiDocument.setDatabase(wikiName);
            Document doc = new Document(xwikiDocument, Utils.getXWikiContext(componentManager));
            /*
                 * We manufacture page summaries in place because we don't have all the data for calling the
                 * DomainObjectFactory method (doing so would require to retrieve an actual Document)
                 */
            PageSummary pageSummary = objectFactory.createPageSummary();
            pageSummary.setId(doc.getPrefixedFullName());
            pageSummary.setFullName(doc.getFullName());
            pageSummary.setWiki(wikiName);
            pageSummary.setSpace(doc.getSpace());
            pageSummary.setName(doc.getName());
            pageSummary.setTitle(doc.getTitle());
            pageSummary.setParent(doc.getParent());
            URL absoluteUrl = Utils.getXWikiContext(componentManager).getURLFactory().createExternalURL(doc.getSpace(), doc.getName(), "view", null, null, Utils.getXWikiContext(componentManager));
            pageSummary.setXwikiAbsoluteUrl(absoluteUrl.toString());
            pageSummary.setXwikiRelativeUrl(Utils.getXWikiContext(componentManager).getURLFactory().getURL(absoluteUrl, Utils.getXWikiContext(componentManager)));
            String pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName()).toString();
            Link pageLink = objectFactory.createLink();
            pageLink.setHref(pageUri);
            pageLink.setRel(Relations.PAGE);
            pageSummary.getLinks().add(pageLink);
            pages.getPageSummaries().add(pageSummary);
        }
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
    return pages;
}
Also used : PageResource(org.xwiki.rest.resources.pages.PageResource) Query(org.xwiki.query.Query) HashMap(java.util.HashMap) Formatter(java.util.Formatter) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) URL(java.net.URL) Pages(org.xwiki.rest.model.jaxb.Pages) QueryException(org.xwiki.query.QueryException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PageSummary(org.xwiki.rest.model.jaxb.PageSummary) Link(org.xwiki.rest.model.jaxb.Link)

Example 12 with XWikiRestException

use of org.xwiki.rest.XWikiRestException in project xwiki-platform by xwiki.

the class WikiSearchQueryResourceImpl method search.

@Override
public SearchResults search(String wikiName, String query, String queryTypeString, Integer number, Integer start, Boolean distinct, String orderField, String order, Boolean withPrettyNames, String className) throws XWikiRestException {
    try {
        SearchResults searchResults = objectFactory.createSearchResults();
        searchResults.setTemplate(String.format("%s?%s", Utils.createURI(uriInfo.getBaseUri(), WikiSearchQueryResource.class, wikiName).toString(), QUERY_TEMPLATE_INFO));
        searchResults.getSearchResults().addAll(searchQuery(query, queryTypeString, wikiName, null, Utils.getXWiki(componentManager).getRightService().hasProgrammingRights(Utils.getXWikiContext(componentManager)), orderField, order, distinct, number, start, withPrettyNames, className));
        return searchResults;
    } catch (Exception e) {
        throw new XWikiRestException(e);
    }
}
Also used : XWikiRestException(org.xwiki.rest.XWikiRestException) WikiSearchQueryResource(org.xwiki.rest.resources.wikis.WikiSearchQueryResource) SearchResults(org.xwiki.rest.model.jaxb.SearchResults) XWikiRestException(org.xwiki.rest.XWikiRestException)

Example 13 with XWikiRestException

use of org.xwiki.rest.XWikiRestException in project xwiki-platform by xwiki.

the class ObjectResourceImpl method deleteObject.

@Override
public void deleteObject(String wikiName, String spaceName, String pageName, String className, Integer objectNumber) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        com.xpn.xwiki.api.Object object = doc.getObject(className, objectNumber);
        if (object == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        doc.removeObject(object);
        doc.save();
    } 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 14 with XWikiRestException

use of org.xwiki.rest.XWikiRestException in project xwiki-platform by xwiki.

the class ObjectsForClassNameResourceImpl method getObjects.

@Override
public Objects getObjects(String wikiName, String spaceName, String pageName, String className, Integer start, Integer number, Boolean withPrettyNames) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        Objects objects = objectFactory.createObjects();
        List<com.xpn.xwiki.objects.BaseObject> objectList = getBaseObjects(doc, className);
        RangeIterable<com.xpn.xwiki.objects.BaseObject> ri = new RangeIterable<com.xpn.xwiki.objects.BaseObject>(objectList, start, number);
        for (com.xpn.xwiki.objects.BaseObject object : ri) {
            // By deleting objects, some of them might become null, so we must check for this
            if (object != null) {
                objects.getObjectSummaries().add(DomainObjectFactory.createObjectSummary(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, object, false, Utils.getXWikiApi(componentManager), withPrettyNames));
            }
        }
        return objects;
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : RangeIterable(org.xwiki.rest.internal.RangeIterable) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) Objects(org.xwiki.rest.model.jaxb.Objects) XWikiException(com.xpn.xwiki.XWikiException)

Example 15 with XWikiRestException

use of org.xwiki.rest.XWikiRestException in project xwiki-platform by xwiki.

the class ObjectsResourceImpl method addObject.

@Override
public Response addObject(String wikiName, String spaceName, String pageName, Boolean minorRevision, Object object) throws XWikiRestException {
    if (object.getClassName() == null) {
        throw new WebApplicationException(Status.BAD_REQUEST);
    }
    try {
        List<String> spaces = parseSpaceSegments(spaceName);
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
        BaseObject xwikiObject = xwikiDocument.newObject(object.getClassName(), Utils.getXWikiContext(componentManager));
        if (xwikiObject == null) {
            throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
        }
        // We must initialize all the fields to an empty value in order to correctly create the object
        BaseClass xwikiClass = Utils.getXWiki(componentManager).getClass(xwikiObject.getClassName(), Utils.getXWikiContext(componentManager));
        for (java.lang.Object propertyNameObject : xwikiClass.getPropertyNames()) {
            String propertyName = (String) propertyNameObject;
            xwikiObject.set(propertyName, "", Utils.getXWikiContext(componentManager));
        }
        for (Property property : object.getProperties()) {
            xwikiObject.set(property.getName(), property.getValue(), Utils.getXWikiContext(componentManager));
        }
        doc.save("", Boolean.TRUE.equals(minorRevision));
        return Response.created(Utils.createURI(uriInfo.getBaseUri(), ObjectResource.class, wikiName, spaces, pageName, object.getClassName(), xwikiObject.getNumber())).entity(DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, xwikiObject, false, Utils.getXWikiApi(componentManager), false)).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) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) Property(org.xwiki.rest.model.jaxb.Property) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiRestException (org.xwiki.rest.XWikiRestException)71 XWikiException (com.xpn.xwiki.XWikiException)46 Document (com.xpn.xwiki.api.Document)40 WebApplicationException (javax.ws.rs.WebApplicationException)26 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)15 Link (org.xwiki.rest.model.jaxb.Link)12 QueryException (org.xwiki.query.QueryException)8 BaseObject (com.xpn.xwiki.objects.BaseObject)7 RangeIterable (org.xwiki.rest.internal.RangeIterable)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 Object (org.xwiki.rest.model.jaxb.Object)5 Property (org.xwiki.rest.model.jaxb.Property)5 Date (java.util.Date)4 Test (org.junit.Test)4 ClassPropertyReference (org.xwiki.model.reference.ClassPropertyReference)4 Objects (org.xwiki.rest.model.jaxb.Objects)4 XWikiRCSNodeId (com.xpn.xwiki.doc.rcs.XWikiRCSNodeId)3 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)3 URI (java.net.URI)3 URL (java.net.URL)3