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