use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.
the class RepositoryManager method updateExtensionVersionDependencies.
private boolean updateExtensionVersionDependencies(XWikiDocument document, Extension extension) throws XWikiException {
boolean needSave = false;
List<ExtensionDependency> dependencies = new ArrayList<>(extension.getDependencies());
int dependencyIndex = 0;
// Clean misplaced or bad existing dependencies associated to this extension version
List<BaseObject> xobjects = document.getXObjects(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE);
if (xobjects != null) {
boolean deleteExistingObjects = false;
// Clone since we are going to modify and parse it at the same time
xobjects = new ArrayList<>(document.getXObjects(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE));
for (int i = 0; i < xobjects.size(); ++i) {
BaseObject dependencyObject = xobjects.get(i);
if (dependencyObject != null) {
String extensionVersion = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION, (String) null);
if (StringUtils.isNotEmpty(extensionVersion) && extension.getId().getVersion().equals(new DefaultVersion(extensionVersion))) {
if (deleteExistingObjects) {
document.removeXObject(dependencyObject);
needSave = true;
} else {
String xobjectId = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_ID);
String xobjectConstraint = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_CONSTRAINT);
List<String> xobjectRepositories = (List<String>) getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_REPOSITORIES);
boolean xobjectOptional = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_OPTIONAL, 0) == 1;
if (dependencies.size() > dependencyIndex) {
ExtensionDependency dependency = dependencies.get(dependencyIndex);
DefaultExtensionDependency xobjectDependency = new DefaultExtensionDependency(xobjectId, new DefaultVersionConstraint(xobjectConstraint), xobjectOptional, dependency.getProperties());
xobjectDependency.setRepositories(XWikiRepositoryModel.toRepositoryDescriptors(xobjectRepositories, this.extensionFactory));
if (dependency.equals(xobjectDependency)) {
++dependencyIndex;
continue;
}
}
deleteExistingObjects = true;
document.removeXObject(dependencyObject);
needSave = true;
}
}
}
}
}
// Add missing dependencies
if (dependencyIndex < dependencies.size()) {
XWikiContext xcontext = this.xcontextProvider.get();
for (; dependencyIndex < dependencies.size(); ++dependencyIndex) {
ExtensionDependency dependency = dependencies.get(dependencyIndex);
BaseObject dependencyObject = document.newXObject(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE, xcontext);
dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION, extension.getId().getVersion().getValue(), xcontext);
dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_ID, dependency.getId(), xcontext);
dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_CONSTRAINT, dependency.getVersionConstraint().getValue(), xcontext);
dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_OPTIONAL, dependency.isOptional() ? 1 : 0, xcontext);
dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_REPOSITORIES, XWikiRepositoryModel.toStringList(dependency.getRepositories()), xcontext);
needSave = true;
}
}
return needSave;
}
use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.
the class RepositoryManager method getExtensionVersionObject.
/**
* @since 9.5RC1
*/
public BaseObject getExtensionVersionObject(XWikiDocument extensionDocument, String version, boolean allowProxying) {
if (version == null) {
List<BaseObject> objects = extensionDocument.getXObjects(XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE);
if (objects == null || objects.isEmpty()) {
return null;
} else {
return objects.get(objects.size() - 1);
}
}
BaseObject extensionVersionObject = extensionDocument.getObject(XWikiRepositoryModel.EXTENSIONVERSION_CLASSNAME, "version", version, false);
if (extensionVersionObject == null && allowProxying && isVersionProxyingEnabled(extensionDocument)) {
// no ExtensionVersionClass object so we need to create such object temporarily and delete it
try {
// FIXME - see XWIKI-14138 - this is nasty hack for obtaining ExtensionVersion XObject, when its
// FIXME information should be proxied and not stored permanently behind extension document
// FIXME To be substitude by some better solution in the future
XWikiDocument extensionDocumentClone = extensionDocument.clone();
addExtensionVersionObjectToDocument(extensionDocumentClone, version);
extensionVersionObject = extensionDocumentClone.getObject(XWikiRepositoryModel.EXTENSIONVERSION_CLASSNAME, "version", version, false);
} catch (XWikiException | ResolveException e) {
throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
return extensionVersionObject;
}
use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.
the class ModelFactory method toRestObject.
public Object toRestObject(URI baseUri, Document doc, BaseObject xwikiObject, boolean useVersion, Boolean withPrettyNames) {
Object object = this.objectFactory.createObject();
fillObjectSummary(object, doc, xwikiObject, withPrettyNames);
XWikiContext xwikiContext = this.xcontextProvider.get();
BaseClass xwikiClass = xwikiObject.getXClass(xwikiContext);
for (java.lang.Object propertyClassObject : xwikiClass.getProperties()) {
com.xpn.xwiki.objects.classes.PropertyClass propertyClass = (com.xpn.xwiki.objects.classes.PropertyClass) propertyClassObject;
Property property = this.objectFactory.createProperty();
for (java.lang.Object o : propertyClass.getProperties()) {
BaseProperty baseProperty = (BaseProperty) o;
Attribute attribute = this.objectFactory.createAttribute();
attribute.setName(baseProperty.getName());
/* Check for null values in order to prevent NPEs */
if (baseProperty.getValue() != null) {
attribute.setValue(baseProperty.getValue().toString());
} else {
attribute.setValue("");
}
property.getAttributes().add(attribute);
}
if (propertyClass instanceof ListClass) {
ListClass listClass = (ListClass) propertyClass;
List allowedValueList = listClass.getList(xwikiContext);
if (!allowedValueList.isEmpty()) {
Formatter f = new Formatter();
for (int i = 0; i < allowedValueList.size(); i++) {
if (i != allowedValueList.size() - 1) {
f.format("%s,", allowedValueList.get(i).toString());
} else {
f.format("%s", allowedValueList.get(i).toString());
}
}
Attribute attribute = this.objectFactory.createAttribute();
attribute.setName(Constants.ALLOWED_VALUES_ATTRIBUTE_NAME);
attribute.setValue(f.toString());
property.getAttributes().add(attribute);
}
}
property.setName(propertyClass.getName());
property.setType(propertyClass.getClassType());
try {
property.setValue(serializePropertyValue(xwikiObject.get(propertyClass.getName())));
} catch (XWikiException e) {
// Should never happen
}
String propertyUri;
if (useVersion) {
propertyUri = Utils.createURI(baseUri, ObjectPropertyAtPageVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), doc.getVersion(), xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
} else {
propertyUri = Utils.createURI(baseUri, ObjectPropertyResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
}
Link propertyLink = this.objectFactory.createLink();
propertyLink.setHref(propertyUri);
propertyLink.setRel(Relations.SELF);
property.getLinks().add(propertyLink);
object.getProperties().add(property);
}
Link objectLink = getObjectLink(this.objectFactory, baseUri, doc, xwikiObject, useVersion, Relations.SELF);
object.getLinks().add(objectLink);
return object;
}
use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.
the class ModelFactory method toRestPage.
public Page toRestPage(URI baseUri, URI self, Document doc, boolean useVersion, Boolean withPrettyNames, Boolean withObjects, Boolean withXClass, Boolean withAttachments) throws XWikiException {
Page page = this.objectFactory.createPage();
toRestPageSummary(page, baseUri, doc, useVersion, withPrettyNames);
XWikiContext xwikiContext = this.xcontextProvider.get();
page.setMajorVersion(doc.getRCSVersion().at(0));
page.setMinorVersion(doc.getRCSVersion().at(1));
page.setHidden(doc.isHidden());
page.setLanguage(doc.getLocale().toString());
page.setCreator(doc.getCreator());
if (withPrettyNames) {
page.setCreatorName(xwikiContext.getWiki().getUserName(doc.getCreator(), null, false, xwikiContext));
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(doc.getCreationDate());
page.setCreated(calendar);
page.setModifier(doc.getContentAuthor());
if (withPrettyNames) {
page.setModifierName(xwikiContext.getWiki().getUserName(doc.getContentAuthor(), null, false, xwikiContext));
}
calendar = Calendar.getInstance();
calendar.setTime(doc.getContentUpdateDate());
page.setModified(calendar);
page.setComment(doc.getComment());
page.setContent(doc.getContent());
if (self != null) {
Link pageLink = this.objectFactory.createLink();
pageLink.setHref(self.toString());
pageLink.setRel(Relations.SELF);
page.getLinks().add(pageLink);
}
com.xpn.xwiki.api.Class xwikiClass = doc.getxWikiClass();
if (xwikiClass != null) {
String classUri = Utils.createURI(baseUri, ClassResource.class, doc.getWiki(), xwikiClass.getName()).toString();
Link classLink = this.objectFactory.createLink();
classLink.setHref(classUri);
classLink.setRel(Relations.CLASS);
page.getLinks().add(classLink);
}
XWikiContext xcontext = xcontextProvider.get();
// Add attachments
if (withAttachments) {
page.setAttachments(objectFactory.createAttachments());
for (com.xpn.xwiki.api.Attachment attachment : doc.getAttachmentList()) {
URL url = xcontext.getURLFactory().createAttachmentURL(attachment.getFilename(), doc.getSpace(), doc.getName(), "download", null, doc.getWiki(), xcontext);
String attachmentXWikiAbsoluteUrl = url.toString();
String attachmentXWikiRelativeUrl = xcontext.getURLFactory().getURL(url, xcontext);
page.getAttachments().getAttachments().add(toRestAttachment(baseUri, attachment, attachmentXWikiRelativeUrl, attachmentXWikiAbsoluteUrl, withPrettyNames, false));
}
}
// Add objects
if (withObjects) {
page.setObjects(objectFactory.createObjects());
XWikiDocument xwikiDocument = xcontext.getWiki().getDocument(doc.getDocumentReference(), xcontext);
for (List<BaseObject> objects : xwikiDocument.getXObjects().values()) {
for (BaseObject object : objects) {
// Deleting an object leads to a null entry in the list of objects.
if (object != null) {
page.getObjects().getObjectSummaries().add(toRestObject(baseUri, doc, object, false, withPrettyNames));
}
}
}
}
// Add xclass
if (withXClass) {
page.setClazz(toRestClass(baseUri, doc.getxWikiClass()));
}
return page;
}
use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.
the class ExtensionVersionFileRESTResource method downloadLocalExtension.
private ResponseBuilder downloadLocalExtension(String extensionId, String extensionVersion) throws ResolveException, IOException, QueryException, XWikiException {
XWikiDocument extensionDocument = getExistingExtensionDocumentById(extensionId);
checkRights(extensionDocument);
ResourceReference resourceReference = repositoryManager.getDownloadReference(extensionDocument, extensionVersion);
ResponseBuilder response = null;
if (ResourceType.ATTACHMENT.equals(resourceReference.getType())) {
// It's an attachment
AttachmentReference attachmentReference = this.attachmentResolver.resolve(resourceReference.getReference(), extensionDocument.getDocumentReference());
XWikiContext xcontext = getXWikiContext();
XWikiDocument document = xcontext.getWiki().getDocument(attachmentReference.getDocumentReference(), xcontext);
checkRights(document);
XWikiAttachment xwikiAttachment = document.getAttachment(attachmentReference.getName());
response = getAttachmentResponse(xwikiAttachment);
} else if (ResourceType.URL.equals(resourceReference.getType())) {
// It's an URL
URL url = new URL(resourceReference.getReference());
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "XWikiExtensionRepository");
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
httpClient.setRoutePlanner(routePlanner);
HttpGet getMethod = new HttpGet(url.toString());
HttpResponse subResponse;
try {
subResponse = httpClient.execute(getMethod);
} catch (Exception e) {
throw new IOException("Failed to request [" + getMethod.getURI() + "]", e);
}
response = Response.status(subResponse.getStatusLine().getStatusCode());
// TODO: find a proper way to do a perfect proxy of the URL without directly using Restlet classes.
// Should probably use javax.ws.rs.ext.MessageBodyWriter
HttpEntity entity = subResponse.getEntity();
InputRepresentation content = new InputRepresentation(entity.getContent(), entity.getContentType() != null ? new MediaType(entity.getContentType().getValue()) : MediaType.APPLICATION_OCTET_STREAM, entity.getContentLength());
BaseObject extensionObject = getExtensionObject(extensionDocument);
String type = getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_TYPE);
Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
disposition.setFilename(extensionId + '-' + extensionVersion + '.' + type);
content.setDisposition(disposition);
response.entity(content);
} else if (ExtensionResourceReference.TYPE.equals(resourceReference.getType())) {
ExtensionResourceReference extensionResource;
if (resourceReference instanceof ExtensionResourceReference) {
extensionResource = (ExtensionResourceReference) resourceReference;
} else {
extensionResource = new ExtensionResourceReference(resourceReference.getReference());
}
response = downloadRemoteExtension(extensionResource);
} else {
throw new WebApplicationException(Status.NOT_FOUND);
}
return response;
}
Aggregations