use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class WikiSkinUtils method getSkinResourceFromDocumentSkin.
private Resource<?> getSkinResourceFromDocumentSkin(String resource, XWikiDocument skinDocument, Skin skin) {
if (skinDocument != null) {
// Try to find a XWikiSkinFileOverrideClass object
BaseObject obj = skinDocument.getXObject(XWikiSkinFileOverrideClassDocumentInitializer.DOCUMENT_REFERENCE, XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_PATH, resource, false);
if (obj != null) {
ObjectPropertyReference reference = new ObjectPropertyReference(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT, obj.getReference());
return new ObjectPropertyWikiResource(getPath(reference), skin, reference, skinDocument.getAuthorReference(), this.xcontextProvider, obj.getLargeStringValue(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT));
}
// Try parsing the object property
BaseProperty<ObjectPropertyReference> property = getSkinResourceProperty(resource, skinDocument);
if (property != null) {
ObjectPropertyReference reference = property.getReference();
return new ObjectPropertyWikiResource(getPath(reference), skin, reference, skinDocument.getAuthorReference(), this.xcontextProvider, (String) property.getValue());
}
// Try parsing a document attachment
// Convert "/" into "." in order to support wiki skin attachments to override some resources located in
// subdirectories.
String normalizedResourceName = StringUtils.replaceChars(resource, '/', '.');
XWikiAttachment attachment = skinDocument.getAttachment(normalizedResourceName);
if (attachment != null) {
AttachmentReference reference = attachment.getReference();
return new AttachmentWikiResource(getPath(reference), skin, reference, attachment.getAuthorReference(), this.xcontextProvider);
}
}
return null;
}
use of org.xwiki.model.reference.AttachmentReference 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 org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class AbstractXWIKI14697DataMigration method setStore.
private void setStore(List<Object[]> attachments, Session session) {
WikiReference wikiReference = getXWikiContext().getWikiReference();
List<Long> fileAttachments = new ArrayList<>(attachments.size());
List<Long> configuredAttachments = new ArrayList<>(attachments.size());
for (Object[] attachment : attachments) {
Long id = (Long) attachment[0];
String filename = (String) attachment[1];
String fullName = (String) attachment[2];
DocumentReference documentReference = this.resolver.resolve(fullName, wikiReference);
AttachmentReference attachmentReference = new AttachmentReference(filename, documentReference);
if (isFile(attachmentReference)) {
fileAttachments.add(id);
} else {
configuredAttachments.add(id);
}
}
// Set file store
setStore(session, fileAttachments, FileSystemStoreUtils.HINT);
// Set configured store
if (!configuredAttachments.isEmpty()) {
String configuredStore = this.configuration.getProperty("xwiki.store.attachment.hint");
if (configuredStore != null) {
setStore(session, configuredAttachments, configuredStore);
} else {
this.logger.warn("The following attachment with the following ids have unknown store: ", configuredAttachments);
}
}
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class OfficeAttachmentImporter method convertAttachmentContent.
/**
* Converts the content of the specified office file to wiki syntax.
*
* @param attachmentReference specifies the office file whose content should be converted
* @param filterStyles controls whether styles are filtered when converting the HTML produced by the office server
* to wiki syntax
* @return the annotated XHTML text obtained from the specified office document
* @throws Exception if converting the content of the specified attachment fails
*/
private String convertAttachmentContent(AttachmentReference attachmentReference, boolean filterStyles) throws Exception {
InputStream officeFileStream = documentAccessBridge.getAttachmentContent(attachmentReference);
String officeFileName = attachmentReference.getName();
DocumentReference targetDocRef = attachmentReference.getDocumentReference();
XDOMOfficeDocument xdomOfficeDocument;
if (isPresentation(attachmentReference.getName())) {
xdomOfficeDocument = presentationBuilder.build(officeFileStream, officeFileName, targetDocRef);
} else {
xdomOfficeDocument = documentBuilder.build(officeFileStream, officeFileName, targetDocRef, filterStyles);
}
// Attach the images extracted from the imported office document to the target wiki document.
for (Map.Entry<String, byte[]> artifact : xdomOfficeDocument.getArtifacts().entrySet()) {
AttachmentReference artifactReference = new AttachmentReference(artifact.getKey(), targetDocRef);
documentAccessBridge.setAttachmentContent(artifactReference, artifact.getValue());
}
return xdomOfficeDocument.getContentAsString("annotatedxhtml/1.0");
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class URIClassLoaderTest method testFindResource.
/**
* Verify that resource located in a URI with an attachmentjar protocol can be found.
*/
@Test
public void testFindResource() throws Exception {
URLStreamHandlerFactory urlStreamHandlerFactory = getComponentManager().getInstance(URLStreamHandlerFactory.class);
URIClassLoader cl = new URIClassLoader(new URI[] { new URI("attachmentjar://page%40filename1"), new URI("http://some/url"), new URI("attachmentjar://filename2") }, urlStreamHandlerFactory);
Assert.assertEquals(3, cl.getURLs().length);
Assert.assertEquals("attachmentjar://page%40filename1", cl.getURLs()[0].toString());
Assert.assertEquals("http://some/url", cl.getURLs()[1].toString());
Assert.assertEquals("attachmentjar://filename2", cl.getURLs()[2].toString());
final AttachmentReference attachmentName1 = new AttachmentReference("filename1", new DocumentReference("wiki", "space", "page"));
final AttachmentReference attachmentName2 = new AttachmentReference("filename2", new DocumentReference("wiki", "space", "page"));
getMockery().checking(new Expectations() {
{
allowing(URIClassLoaderTest.this.arf).resolve("page@filename1");
will(returnValue(attachmentName1));
oneOf(URIClassLoaderTest.this.dab).getAttachmentContent(attachmentName1);
will(returnValue(new ByteArrayInputStream(createJarFile("/nomatch"))));
allowing(URIClassLoaderTest.this.arf).resolve("filename2");
will(returnValue(attachmentName2));
oneOf(URIClassLoaderTest.this.dab).getAttachmentContent(attachmentName2);
will(returnValue(new ByteArrayInputStream(createJarFile("/something"))));
}
});
Assert.assertEquals("jar:attachmentjar://filename2!/something", cl.findResource("/something").toString());
}
Aggregations