use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class AbstractExtensionRESTResource method resolveExtensionAuthor.
protected ExtensionAuthor resolveExtensionAuthor(String authorId) {
ExtensionAuthor author = new ExtensionAuthor();
XWikiContext xcontext = getXWikiContext();
XWikiDocument document;
try {
document = xcontext.getWiki().getDocument(authorId, xcontext);
} catch (XWikiException e) {
document = null;
}
if (document != null && !document.isNew()) {
author.setName(xcontext.getWiki().getPlainUserName(document.getDocumentReference(), xcontext));
author.setUrl(document.getExternalURL("view", xcontext));
} else {
author.setName(authorId);
}
return author;
}
use of com.xpn.xwiki.doc.XWikiDocument 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;
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class ExtensionVersionRESTResource method getExtensionVersion.
@GET
public ExtensionVersion getExtensionVersion(@PathParam("extensionId") String extensionId, @PathParam("extensionVersion") String extensionVersion) throws XWikiException, QueryException {
XWikiDocument extensionDocument = getExistingExtensionDocumentById(extensionId);
checkRights(extensionDocument);
return createExtension(extensionDocument, extensionVersion);
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class FilesystemAttachmentStoreTest method documentUpdateOnDeleteTest.
@Test
public void documentUpdateOnDeleteTest() throws Exception {
final List<XWikiAttachment> attachList = new ArrayList<XWikiAttachment>();
attachList.add(this.mockAttach);
this.doc.setAttachmentList(attachList);
getMockery().checking(new Expectations() {
{
oneOf(mockAttachVersionStore).deleteArchive(mockAttach, mockContext, false);
exactly(2).of(mockHibernateSession).delete(with(any(Object.class)));
oneOf(mockHibernate).saveXWikiDoc(doc, mockContext, false);
will(new CustomAction("Make sure the attachment has been removed from the list.") {
public Object invoke(final Invocation invoc) {
final XWikiDocument document = (XWikiDocument) invoc.getParameter(0);
Assert.assertTrue("Attachment was not removed from the list.", document.getAttachmentList().size() == 0);
return null;
}
});
}
});
this.createFile();
this.attachStore.deleteXWikiAttachment(this.mockAttach, true, this.mockContext, false);
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class FilesystemAttachmentVersioningStoreTest method setUp.
@Before
@Override
public void setUp() throws Exception {
super.setUp();
Utils.setComponentManager(this.getComponentManager());
final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
this.storageLocation = new File(tmpDir, "test-storage-location");
this.fileTools = new FilesystemStoreTools(new FileStringEntityReferenceSerializer(), storageLocation, new DummyLockProvider());
final AttachmentListMetadataSerializer serializer = new AttachmentListMetadataSerializer(new AttachmentMetadataSerializer());
this.versionStore = new FilesystemAttachmentVersioningStore();
FieldUtils.writeDeclaredField(this.versionStore, "fileTools", this.fileTools, true);
FieldUtils.writeDeclaredField(this.versionStore, "metaSerializer", serializer, true);
final XWikiDocument doc = new XWikiDocument(new DocumentReference("xwiki", "Main", "WebHome"));
final XWikiAttachment version1 = new XWikiAttachment();
version1.setVersion("1.1");
version1.setFilename("attachment.txt");
version1.setDoc(doc);
version1.setAttachment_content(new StringAttachmentContent("I am version 1.1"));
final XWikiAttachment version2 = new XWikiAttachment();
version2.setVersion("1.2");
version2.setFilename("attachment.txt");
version2.setDoc(doc);
version2.setAttachment_content(new StringAttachmentContent("I am version 1.2"));
final XWikiAttachment version3 = new XWikiAttachment();
version3.setVersion("1.3");
version3.setFilename("attachment.txt");
version3.setDoc(doc);
version3.setAttachment_content(new StringAttachmentContent("I am version 1.3"));
this.provider = this.fileTools.getAttachmentFileProvider(version1.getReference());
this.archive = new ListAttachmentArchive(new ArrayList<XWikiAttachment>() {
{
add(version1);
add(version2);
add(version3);
}
});
}
Aggregations