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);
}
}
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);
}
}
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);
}
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);
}
}
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);
}
}
Aggregations