Search in sources :

Example 16 with XWikiRestException

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

the class PageTagsResourceImpl method setTags.

@Override
public Response setTags(String wikiName, String spaceName, String pageName, Boolean minorRevision, Tags tags) 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);
        }
        List<String> tagNames = new ArrayList<String>();
        for (Tag tag : tags.getTags()) {
            tagNames.add(tag.getName());
        }
        XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
        BaseObject xwikiObject = xwikiDocument.getObject("XWiki.TagClass", 0);
        if (xwikiObject == null) {
            int objectNumber = xwikiDocument.createNewObject("XWiki.TagClass", Utils.getXWikiContext(componentManager));
            xwikiObject = xwikiDocument.getObject("XWiki.TagClass", objectNumber);
            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 (Object propertyNameObject : xwikiClass.getPropertyNames()) {
                String propertyName = (String) propertyNameObject;
                xwikiObject.set(propertyName, "", Utils.getXWikiContext(componentManager));
            }
        }
        xwikiObject.set("tags", tagNames, Utils.getXWikiContext(componentManager));
        doc.save("", Boolean.TRUE.equals(minorRevision));
        return Response.status(Status.ACCEPTED).entity(tags).build();
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) ArrayList(java.util.ArrayList) 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) BaseObject(com.xpn.xwiki.objects.BaseObject) Tag(org.xwiki.rest.model.jaxb.Tag) XWikiException(com.xpn.xwiki.XWikiException)

Example 17 with XWikiRestException

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

the class PageTagsResourceImpl method getPageTags.

@Override
public Tags getPageTags(String wikiName, String spaceName, String pageName) throws XWikiRestException {
    try {
        String pageId = Utils.getPageId(wikiName, parseSpaceSegments(spaceName), pageName);
        List<String> tagNames = getTagsFromDocument(pageId);
        Tags tags = objectFactory.createTags();
        for (String tagName : tagNames) {
            Tag tag = objectFactory.createTag();
            tag.setName(tagName);
            String tagUri = Utils.createURI(uriInfo.getBaseUri(), PagesForTagsResource.class, wikiName, tagName).toString();
            Link tagLink = objectFactory.createLink();
            tagLink.setHref(tagUri);
            tagLink.setRel(Relations.TAG);
            tag.getLinks().add(tagLink);
            tags.getTags().add(tag);
        }
        return tags;
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : XWikiRestException(org.xwiki.rest.XWikiRestException) PagesForTagsResource(org.xwiki.rest.resources.tags.PagesForTagsResource) Tag(org.xwiki.rest.model.jaxb.Tag) Tags(org.xwiki.rest.model.jaxb.Tags) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Example 18 with XWikiRestException

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

the class JobsResourceImpl method executeJob.

@Override
public JobStatus executeJob(String jobType, boolean async, JobRequest restJobRequest) throws XWikiRestException {
    // TODO: provide extension point to decide of the access depending on the job
    if (!this.authorization.hasAccess(Right.PROGRAM, null)) {
        throw new WebApplicationException(Status.UNAUTHORIZED);
    }
    // Parse JobRequest
    DefaultRequest request = this.factory.toJobRequest(restJobRequest);
    if (request == null) {
        request = new DefaultRequest();
    }
    // Start job
    Job job;
    try {
        // Give a few context related values to the job
        if (request.getProperty(JobRequestContext.KEY) == null) {
            JobRequestContext.set(request, this.xcontextProvider.get());
        }
        job = this.jobExecutor.execute(jobType, request);
    } catch (JobException e) {
        throw new XWikiRestException("Failed to start job", e);
    }
    // Wait for the job end if asked
    if (!async) {
        try {
            job.join();
        } catch (InterruptedException e) {
            throw new XWikiRestException("The job as been interrupted", e);
        }
        // Fail the HTTP request if the job failed
        if (job.getStatus().getError() != null) {
            throw new XWikiRestException("The job failed (" + ExceptionUtils.getRootCauseMessage(job.getStatus().getError()) + ")", job.getStatus().getError());
        }
    }
    // Get job status
    org.xwiki.job.event.status.JobStatus status = job.getStatus();
    // Convert Job status
    return this.factory.toRestJobStatus(status, null, true, false, false, null);
}
Also used : JobException(org.xwiki.job.JobException) WebApplicationException(javax.ws.rs.WebApplicationException) DefaultRequest(org.xwiki.job.DefaultRequest) XWikiRestException(org.xwiki.rest.XWikiRestException) Job(org.xwiki.job.Job)

Example 19 with XWikiRestException

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

the class AllObjectsForClassNameResourceImpl method getObjects.

@Override
public Objects getObjects(String wikiName, String className, Integer start, Integer number, String order, Boolean withPrettyNames) throws XWikiRestException {
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    try {
        Objects objects = new Objects();
        Utils.getXWikiContext(componentManager).setWikiId(wikiName);
        String query = "select doc, obj from BaseObject as obj, XWikiDocument as doc where obj.name=doc.fullName and obj.className=:className";
        if ("date".equals(order)) {
            query += " order by doc.date desc";
        }
        List<Object> queryResult = null;
        queryResult = queryManager.createQuery(query, Query.XWQL).bindValue("className", className).setLimit(number).setOffset(start).execute();
        for (Object object : queryResult) {
            Object[] fields = (Object[]) object;
            XWikiDocument xwikiDocument = (XWikiDocument) fields[0];
            xwikiDocument.setDatabase(wikiName);
            Document doc = new Document(xwikiDocument, Utils.getXWikiContext(componentManager));
            BaseObject xwikiObject = (BaseObject) fields[1];
            ObjectSummary objectSummary = DomainObjectFactory.createObjectSummary(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, xwikiObject, false, Utils.getXWikiApi(componentManager), withPrettyNames);
            objects.getObjectSummaries().add(objectSummary);
        }
        return objects;
    } catch (Exception e) {
        throw new XWikiRestException(e);
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiRestException(org.xwiki.rest.XWikiRestException) Objects(org.xwiki.rest.model.jaxb.Objects) BaseObject(com.xpn.xwiki.objects.BaseObject) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) XWikiRestException(org.xwiki.rest.XWikiRestException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 20 with XWikiRestException

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

the class ObjectPropertiesResourceImpl method getObjectProperties.

@Override
public Properties getObjectProperties(String wikiName, String spaceName, String pageName, String className, Integer objectNumber, Boolean withPrettyNames) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        com.xpn.xwiki.objects.BaseObject baseObject = getBaseObject(doc, className, objectNumber);
        if (baseObject == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        Object object = DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, baseObject, false, Utils.getXWikiApi(componentManager), withPrettyNames);
        Properties properties = objectFactory.createProperties();
        properties.getProperties().addAll(object.getProperties());
        String objectUri = Utils.createURI(uriInfo.getBaseUri(), ObjectResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), object.getClassName(), object.getNumber()).toString();
        Link objectLink = objectFactory.createLink();
        objectLink.setHref(objectUri);
        objectLink.setRel(Relations.OBJECT);
        properties.getLinks().add(objectLink);
        return properties;
    } 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) Properties(org.xwiki.rest.model.jaxb.Properties) Object(org.xwiki.rest.model.jaxb.Object) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) Link(org.xwiki.rest.model.jaxb.Link) 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